From cbdc30695e9994db750f3f1dd306350692c1a4ae Mon Sep 17 00:00:00 2001 From: npmstudy Date: Mon, 4 Dec 2023 20:48:30 +0800 Subject: [PATCH] chore: refact --- packages/app/src/mw/jwt.ts | 6 +++--- packages/client/package.json | 8 +++++--- packages/client/src/__tests__/index.test.ts | 21 +++++++++++++++++++-- packages/client/src/client.ts | 14 ++++++++++---- packages/client/src/proxy.ts | 2 +- packages/client/test.ts | 6 +++--- 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/app/src/mw/jwt.ts b/packages/app/src/mw/jwt.ts index f546867..feb6c93 100644 --- a/packages/app/src/mw/jwt.ts +++ b/packages/app/src/mw/jwt.ts @@ -8,7 +8,7 @@ export class Jwt extends Proxy { this.config.proxy.inject = 'before'; this.config.proxy.before = ['fn']; } - proxy() { - return koajwt(this.config).unless({ path: this.config.unless }); - } + // proxy() { + // // return koajwt(this.config).unless({ path: this.config.unless }); + // } } diff --git a/packages/client/package.json b/packages/client/package.json index 17b5c40..821afd4 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -26,12 +26,14 @@ "awesome-keywords" ], "devDependencies": { - "vitest": "^0.34.6", + "@tomrpc/core": "workspace:*", "@vitest/coverage-v8": "^0.34.6", - "@tomrpc/core": "workspace:*" + "vitest": "^0.34.6" }, "dependencies": { "debug": "^4.3.4", - "isomorphic-unfetch": "^4.0.2" + "isomorphic-unfetch": "^4.0.2", + "node-fetch": "^3.3.2", + "ofetch": "^1.3.3" } } \ No newline at end of file diff --git a/packages/client/src/__tests__/index.test.ts b/packages/client/src/__tests__/index.test.ts index 1455e9a..b09868e 100644 --- a/packages/client/src/__tests__/index.test.ts +++ b/packages/client/src/__tests__/index.test.ts @@ -1,4 +1,7 @@ import { createServer } from '@tomrpc/core'; +// import fetch1 from 'isomorphic-unfetch'; +import fetch1 from 'node-fetch'; +import { ofetch } from 'ofetch'; import { describe, expect, it } from 'vitest'; import { createClient } from '..'; @@ -59,12 +62,26 @@ describe('lib', () => { // }, }); + console.dir(client); // const res1 = await client.a('hello'); + const res = await ofetch('http://127.0.0.1:3000/a.json'); + console.dir(res); // console.dir(res1); // const res = await client.postUsers('hello postUsers'); // console.dir(res); }); - it('should render lib', () => { - // expect(lib()).toBe('lib'); + // it.only('should render lib', async () => { + // const res = await ofetch('https://jsonplaceholder.typicode.com/todos/1'); + // console.dir(res); + // expect('lib').toBe('lib'); + // }); + + it.only('should render lib', async () => { + const res = ofetch('http://127.0.0.1:3000/a.json', { + method: 'GET', + parseResponse: JSON.parse, + }); + console.dir(res); + expect('lib').toBe('lib'); }); }); diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 5b1afed..cf4d379 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -1,7 +1,9 @@ import debug from 'debug'; -import fetch from 'isomorphic-unfetch'; +// import fetch1 from 'isomorphic-unfetch'; +import fetch1 from 'node-fetch'; +import { ofetch } from 'ofetch'; -const log = debug('@tomrpc/client'); +const log = console.dir; //debug('@tomrpc/client'); /** * @@ -19,12 +21,14 @@ export class TomClient { private config; private host = '127.0.0.1'; private port = '3000'; + private prefix = '/api'; constructor(config) { log(config); // ... this.config = config; if (config.host) this.host = config.host; if (config.port) this.port = config.port; + if (config.prefix) this.prefix = config.prefix; } async get(key: string, ...r: unknown[]) { @@ -32,10 +36,12 @@ export class TomClient { // console.dir(key); // ... const path = key.split('.').join('/'); - const url = `http://${this.host}:${this.port}/${path}?$p=${JSON.stringify(r)}`; + const url = `http://${this.host}:${this.port}${this.prefix}/${path}?$p=${JSON.stringify(r)}`; log(url); - const response = await fetch(url); + console.dir(ofetch + ' - - - '); + const response = await ofetch(url); + log(response); const data = await response.text(); log(data); return data; diff --git a/packages/client/src/proxy.ts b/packages/client/src/proxy.ts index ff30e23..9a83661 100644 --- a/packages/client/src/proxy.ts +++ b/packages/client/src/proxy.ts @@ -1,6 +1,6 @@ import debug from 'debug'; -const log = debug('@tomrpc/client'); +const log = console.dir; //debug('@tomrpc/client'); import { TomClient } from '.'; import { getHttpMethods } from './utils'; diff --git a/packages/client/test.ts b/packages/client/test.ts index d9044f9..843669e 100644 --- a/packages/client/test.ts +++ b/packages/client/test.ts @@ -4,7 +4,7 @@ const main = async () => { const client = createClient({ host: '127.0.0.1', port: 3000, - namespace: 'a', + // namespace: 'a', methodFilter: function (lastKey: string) { if (lastKey === 'a') { return 'post'; @@ -34,9 +34,9 @@ const post = async () => { // }, }); - const res1 = await client.getUsers('hello getUsers'); + const res1 = await client.a('hello getUsers'); console.dir(res1); - const res = await client.postUsers('hello postUsers'); + const res = await client.a('hello postUsers'); console.dir(res); // const b = await client.a('hello');