diff --git a/.eslintrc.json b/.eslintrc.json index cc67ae087..7a6853ff6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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, diff --git a/lib/pkg.js b/lib/pkg.js deleted file mode 100644 index f0eed96b4..000000000 --- a/lib/pkg.js +++ /dev/null @@ -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); - } - }, - } - ); - }), -}; diff --git a/test/pkg.js b/lib/pkg.test.ts similarity index 87% rename from test/pkg.js rename to lib/pkg.test.ts index 739a81251..0ccebc900 100644 --- a/test/pkg.js +++ b/lib/pkg.test.ts @@ -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(); }) diff --git a/lib/pkg.ts b/lib/pkg.ts new file mode 100644 index 000000000..9dfafaa1d --- /dev/null +++ b/lib/pkg.ts @@ -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(); +};