Skip to content

Commit

Permalink
chore: refact
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Nov 30, 2023
1 parent 73a0f55 commit ec91697
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 11 deletions.
13 changes: 13 additions & 0 deletions packages/core/src/__tests__/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ describe('app', async () => {
return `${this.path} , ${a} `;
});

rpc.add({
'/add': function (a: string, b: string) {
return { a: a };
},
});

const request = supertest(rpc.callback());

it('should access /a', async () => {
Expand All @@ -40,6 +46,13 @@ describe('app', async () => {
expect(res.body).toEqual({ a: 'hello' });
});

it('should access /add', async () => {
const res = await request.get('/api/add?$p=["hello"]');
expect(res.type).toEqual('application/json');
expect(res.status).toEqual(200);
expect(res.body).toEqual({ a: 'hello' });
});

it('should access GET /b', async () => {
const res = await request.get('/api/b?$p=["hello1"]');
expect(res.type).toEqual('application/json');
Expand Down
87 changes: 86 additions & 1 deletion packages/core/src/__tests__/fn.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Koa from 'koa';
import supertest from 'supertest';
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';

// import { TestProxy, TestProxy2 } from '../../proxy';
import { Fn } from '../index';
Expand Down Expand Up @@ -42,6 +43,19 @@ describe('app', async () => {
expect(2).to.equal(count);
});

it('use default response', async () => {
const fn = new Fn({ prefix: '/apk' });
const app = new Koa();

app.use(fn.default());
const request = supertest(app.callback());

const res = await request.get('/');
expect(res.type).toEqual('text/plain');
expect(res.status).toEqual(200);
expect(res.text).toEqual('[fn plugin] no fn repsonse, please check your fn if exist');
});

it('use fn add a function', async () => {
const fn = new Fn({ prefix: '/apk' });

Expand All @@ -56,4 +70,75 @@ describe('app', async () => {
expect(res.status).toEqual(200);
expect(res.body).toEqual({ a: 'hello' });
});

// it('use fn add a function', async () => {
// const fn = new Fn({ prefix: '/apk' });

// fn.fn('/postAbc', function (a) {
// return { a: a };
// });

// const request = supertest(fn.app.callback());

// const res = await request.post('/postAbc').send(['hello']).set('Accept', 'application/json');

// console.dir(res);

// expect(res.type).toEqual('application/json');
// expect(res.status).toEqual(200);
// expect(res.body).toEqual({ a: 'hello' });
// });

it('use fn add a lock function', async () => {
const fn = new Fn({ prefix: '/apk' });

fn.fn('/getAbc', function (a) {
return { a: a };
});
fn.fn('/Abclock', function (a) {
return { a: a };
});

const request = supertest(fn.app.callback());

const res = await request.get('/Abclock?$p=["hello"]');

expect(res.type).toEqual('text/plain');
expect(res.status).toEqual(200);
expect(res.text).toEqual('process fn:Abclock , you need send lock request from client');
});

it('use fn add two function with the same path', async () => {
const spy = vi.spyOn(console, 'log');
const fn = new Fn({ prefix: '/apk' });

fn.fn('/a', function (a) {
return { a: a };
});

fn.add({
'/a': function (a: string, b: string) {
return `${this.path} , ${a} c2 ${b}`;
},
});

expect(spy).toHaveBeenCalled();
});

it('use fn add arrow function', async () => {
const spy = vi.spyOn(console, 'log');
const fn = new Fn({ prefix: '/apk' });

fn.fn('/a', (a) => {
return { a: a };
});

fn.add({
'/a': (a: string, b: string, ctx) => {
return `${ctx.path} , ${a} c2 ${b}`;
},
});

expect(spy).toHaveBeenCalled();
});
});
40 changes: 38 additions & 2 deletions packages/core/src/__tests__/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import supertest from 'supertest';
import { describe, expect, it, vi } from 'vitest';

import { TestProxy, TestProxy2 } from '../../proxy';
import { createServer, Proxy } from '../index';
import { createServer, Proxy, combine } from '../index';

describe('proxy', async () => {
it('should enable proxy1', async () => {
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('proxy', async () => {
const tp = new Proxy({});
tp.app.use(tp.proxy());

tp.app.use(async (ctx) => {
tp.use(async (ctx) => {
ctx.body = { a: 'a' };
});

Expand All @@ -73,4 +73,40 @@ describe('proxy', async () => {
expect(res.status).toEqual(200);
expect(res.body).toEqual({ a: 'a' });
});

it('should enable proxy2', async () => {
const tp = new Proxy({});
// tp.app.use(tp.proxy());

tp.addInit(async (ctx) => {
ctx.body = { a: 'a' };
});

tp.app.use(combine(tp.init));

const request = supertest(tp.app.callback());

const res = await request.get('/');
expect(res.type).toEqual('application/json');
expect(res.status).toEqual(200);
expect(res.body).toEqual({ a: 'a' });
});

it('should enable proxy2', async () => {
const tp = new Proxy({});
tp.app.use(tp.proxy());

tp.addLoad(async (ctx) => {
ctx.body = { a: 'a' };
});

tp.app.use(combine(tp.load));

const request = supertest(tp.app.callback());

const res = await request.get('/');
expect(res.type).toEqual('application/json');
expect(res.status).toEqual(200);
expect(res.body).toEqual({ a: 'a' });
});
});
10 changes: 8 additions & 2 deletions packages/core/src/fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export class Fn extends Plugable {

const lastKey = key.split('.').pop();
const httpMethods = getHttpMethods();
console.dir('lastKey');
console.dir(lastKey);

const supportMethods = [];
httpMethods.forEach(function (m) {
Expand All @@ -108,11 +110,15 @@ export class Fn extends Plugable {
}
});

console.dir('supportMethods');
console.dir(ctx.method);
console.dir(supportMethods);

if (supportMethods.length === 0) {
log(ctx.path + ',没有匹配到包含get/post等开头的函数');
await next();
} else if (ctx.method === supportMethods[0]) {
log('匹配到包含get/post等方法的函数');
} else if (ctx.method.toLowerCase() === supportMethods[0]) {
console.log('匹配到包含get/post等方法的函数');
await next();
} else {
log('匹配到包含get/post等方法的函数,但method不对');
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ export class Plugable implements Strategy {
// load can before mount
this.load.push(this.getMiddleware());
}
/**
* ?
*/
getConfig(ctx) {
return ctx[this.name];
}

/**
* move middleware to load stage, use template pattern & compose
*/
Expand Down

0 comments on commit ec91697

Please sign in to comment.