feat: pkg

This commit is contained in:
DIYgod
2024-02-24 22:23:14 +08:00
parent 0461a95be7
commit a6888c4653
4 changed files with 30 additions and 48 deletions
+1 -1
View File
@@ -55,7 +55,7 @@
"unicorn/new-for-builtins": 0,
"unicorn/no-array-callback-reference": 0,
"unicorn/no-array-reduce": 1,
"unicorn/no-await-expression-member": 1,
"unicorn/no-await-expression-member": 0,
"unicorn/no-empty-file": 1,
"unicorn/no-hex-escape": 1,
"unicorn/no-null": 0,
-39
View File
@@ -1,39 +0,0 @@
const config = require('./config');
let app;
module.exports = {
init: (conf) => {
config.set(
Object.assign(
{
IS_PACKAGE: true,
},
conf
)
);
app = require('./app');
},
request: (path) =>
new Promise((resolve, reject) => {
app.callback()(
{
url: path,
method: 'GET',
headers: {},
socket: {},
},
{
setHeader: () => {},
removeHeader: () => {},
end: (data) => {
data = JSON.parse(data);
if (data.error) {
reject(data.error.message);
} else {
resolve(data);
}
},
}
);
}),
};
+8 -8
View File
@@ -1,19 +1,19 @@
jest.mock('request-promise-native');
const RSSHub = require('../lib/pkg');
import { describe, expect, it } from '@jest/globals';
import { init, request } from './pkg';
describe('pkg', () => {
it('config', () => {
RSSHub.init({
it('config', async () => {
await init({
UA: 'mock',
});
const config = require('../lib/config').value;
const { config } = await import('./config');
expect(config.ua).toBe('mock');
});
it('request', (done) => {
RSSHub.request('/test/1').then((data) => {
request('/test/1').then((data) => {
expect(data).toMatchObject({
atomlink: 'http:///test/1',
atomlink: 'http://localhost/test/1',
title: 'Test 1',
itunes_author: null,
link: 'https://github.com/DIYgod/RSSHub',
@@ -61,7 +61,7 @@ describe('pkg', () => {
});
it('error', (done) => {
RSSHub.request('/test/error')
request('/test/error')
.then(() => {
done();
})
+21
View File
@@ -0,0 +1,21 @@
import { setConfig } from '@/config';
import { Hono } from 'hono';
let app: Hono;
export const init = async (conf) => {
setConfig(
Object.assign(
{
IS_PACKAGE: true,
},
conf
)
);
app = (await import('@/app')).default;
};
export const request = async (path) => {
const res = await app.request(path);
return res.json();
};