Skip to content

Commit

Permalink
chore: refact
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Dec 4, 2023
1 parent ebb2976 commit cbdc306
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/app/src/mw/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
// }
}
8 changes: 5 additions & 3 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
21 changes: 19 additions & 2 deletions packages/client/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createServer } from '@tomrpc/core';

Check failure on line 1 in packages/client/src/__tests__/index.test.ts

View workflow job for this annotation

GitHub Actions / build

Unable to resolve path to module '@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 '..';
Expand Down Expand Up @@ -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');
});
});
14 changes: 10 additions & 4 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
@@ -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');

/**
*
Expand All @@ -19,23 +21,27 @@ 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[]) {
// console.dir('client get request');
// 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;
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 3 additions & 3 deletions packages/client/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit cbdc306

Please sign in to comment.