Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
refactor: parse and mount route when routes was called.
Browse files Browse the repository at this point in the history
to avoid plugin not mount finish, but the route was mounted.
  • Loading branch information
amazing-gao committed Feb 5, 2018
1 parent 82f60e9 commit 3639983
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-oai-router",
"version": "2.0.0-alpha.3",
"version": "2.0.0-alpha.4",
"main": "lib",
"scripts": {
"clean": "rm -rf coverage lib",
Expand Down
13 changes: 12 additions & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ class OAIRouter extends Router {
this.api = null;
this.pluginRegister = new PluginRegister(this.options);

spec(apiDoc)
this.apiDoc = apiDoc;
}

/**
* Start to parse apiDoc and mount routes. Must called after all mount action is finished.
* @public
* @returns route dispatcher, koa middleware
*/
routes() {
spec(this.apiDoc)
.then(async (api) => {
this.api = api;

Expand All @@ -43,6 +52,8 @@ class OAIRouter extends Router {

this.emit('error', error);
});

return super.routes();
}

/**
Expand Down
71 changes: 71 additions & 0 deletions test/plugin/plugin.multi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { init, Plugin } from '../helpers';

describe('Plugin multi', () => {
it('multi plugin, should success', async () => {
class PluginX extends Plugin {
constructor() {
super();

this.field = 'parameters';
}
handler() {
const { args } = this;

return (ctx, next) => {
ctx.value = args;

return next();
};
}
}

class PluginY extends Plugin {
constructor() {
super();

this.field = 'parameters';
}
handler() {
const { args } = this;

return (ctx, next) => {
ctx.value += args;

return next();
};
}
}

class PluginZ extends Plugin {
constructor() {
super();

this.field = 'parameters';
}
handler() {
const { args } = this;

return (ctx, next) => {
ctx.value += args;

ctx.response.body = ctx.value;
};
}
}

const { request } = await init({
apiDoc: './test/plugin/api',
plugins: [PluginX, PluginY, PluginZ],
options: {
PluginX: 'x',
PluginY: 'y',
PluginZ: 'z',
},
});

await request
.get('/api/pets')
.expect(200)
.expect('xyz');
});
});

0 comments on commit 3639983

Please sign in to comment.