Skip to content

Commit

Permalink
support Hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016 committed Apr 27, 2019
1 parent 09b09da commit 626965c
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 37 deletions.
6 changes: 6 additions & 0 deletions @types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ declare namespace FibApp {
viewPathPrefix?: string
graphQLPathPrefix?: string
batchPathPrefix?: string

hooks?: Hooks
}

interface Hooks {
beforeSetupRoute?: FxOrmHook.HookActionCallback
}

interface GetTestRoutingOptions {
Expand Down
1 change: 1 addition & 0 deletions demo/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('fib-app', function () {
require('../defs/hooks/spec')

require('./integration/orm-pool-reload')
require('./integration/hooks')
}

if (!process.env.FIBAPP_NO_MODEL_SPEC) {
Expand Down
94 changes: 94 additions & 0 deletions demo/test/integration/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const test = require('test');
test.setup();

describe('Hooks', () => {
var tappInfo
function setup(opts) {
tappInfo = require('../support/spec_helper').getRandomSqliteBasedApp({
hooks: opts.hooks
}, {});
}

var triggered = {
beforeSetupRoute: false
};

afterEach(() => {
Object.keys(triggered).forEach(k => triggered[k] = false)
})

const apis = [
'get',
'post',
'put',
'del',
'eget',
'epost',
'eput',
'edel',
'functionHandler'
]

function assert_apis_not_exist(app) {
apis.forEach(k => {
assert.notExist(app.api[k])
})
}

function assert_apis_exist(app) {
apis.forEach(k => {
assert.isFunction(app.api[k])
})

assert.isFunction(app.filterRequest)
}

it('[beforeSetupRoute] triggered after app instanced', () => {
setup({
hooks: {
beforeSetupRoute () {
assert.isFalse(triggered.beforeSetupRoute)
triggered.beforeSetupRoute = true;

assert.exist(this)
assert.isObject(this)

assert.isObject(this.api)
assert_apis_exist(this)
}
}
})

assert.isTrue(triggered.beforeSetupRoute);
assert_apis_exist(tappInfo.app);
});

it('[beforeSetupRoute] triggered after app instanced - next', () => {
setup({
hooks: {
beforeSetupRoute (next) {
setTimeout(() => {
assert.isFalse(triggered.beforeSetupRoute)
triggered.beforeSetupRoute = true;

assert.exist(this)
assert.isObject(this)

assert.isObject(this.api)
assert_apis_exist(this)

next()
}, 100)
}
}
})

assert.isTrue(triggered.beforeSetupRoute);
assert_apis_exist(tappInfo.app);
});
})

if (require.main === module) {
test.run(console.DEBUG);
process.exit();
}
2 changes: 1 addition & 1 deletion lib.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import util = require('util')

if ((util.buildInfo() as any).fibjs !== '0.25.0') {
if ((util.buildInfo()).fibjs !== '0.25.0') {
module.exports = require('./src')
} else {
module.exports = require('./lib/index.js')
Expand Down
9 changes: 7 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as mq from 'mq';
import * as util from 'util';

import setupApis = require('./http');

import setupTest = require('./testkits')
Expand Down Expand Up @@ -31,8 +33,9 @@ class App extends mq.Routing implements FibApp.FibAppClass {
constructor(connStr: string) {
super();

const dbSetupOpts: FibApp.FibAppDbSetupOpts = arguments[arguments.length - 1]
const appOpts: FibApp.FibAppOpts = (arguments[1] === dbSetupOpts ? null : arguments[1]) || {}
const args = Array.prototype.slice.apply(arguments);
const dbSetupOpts: FibApp.FibAppDbSetupOpts = util.last(args)
const appOpts: FibApp.FibAppOpts = (args[1] === dbSetupOpts ? null : args[1]) || {}

Object.defineProperty(this, '__opts', {
value: filterFibAppOptions(appOpts),
Expand All @@ -43,6 +46,8 @@ class App extends mq.Routing implements FibApp.FibAppClass {
this.__opts.graphqlTypeMap = this.__opts.graphqlTypeMap || (dbSetupOpts as any).graphqlTypeMap || {}
this.ormPool = setupDb(this, connStr, dbSetupOpts);

appOpts.hooks = appOpts.hooks || {};

setupApis.bind(this);

setupTest.bind(this);
Expand Down
3 changes: 3 additions & 0 deletions src/http/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { trigger, wait } = require('@fxjs/orm/lib/orm/entry/Hook.js')

export { trigger, wait }
74 changes: 40 additions & 34 deletions src/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import _view = require('./view');
import { parse_req_resource_and_hdlr_type, filterRequest } from '../utils/filter_request'
import { run_graphql, is_graphql_request } from '../utils/graphql';
import { run_batch } from '../utils/batch-request';

import * as Hook from './hook';

export function bind (app: FibApp.FibAppClass) {
// bind it firstly
app.filterRequest = filterRequest

const api = app.api = {} as FibApp.FibAppInternalApis;
const viewApi = app.viewApi = {} as FibApp.FibAppInternalViewApis;

_base.setup(app);
_extend.setup(app);
_function.setup(app);
Expand All @@ -40,11 +41,11 @@ export function bind (app: FibApp.FibAppClass) {

/* api extend :start */
app.put(`${apiPathPrefix}/:classname/:id/:extend`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType) => app.filterRequest(req, classname, id, extend, api.elink));
app.put(`${apiPathPrefix}/:classname/:id/:extend/:rid`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid) => app.filterRequest(req, classname, id, extend, rid, api.eput));
app.put(`${apiPathPrefix}/:classname/:id/:extend/:rid`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid: FibApp.AppIdType) => app.filterRequest(req, classname, id, extend, rid, api.eput));
app.post(`${apiPathPrefix}/:classname/:id/:extend`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType) => app.filterRequest(req, classname, id, extend, api.epost));
app.get(`${apiPathPrefix}/:classname/:id/:extend`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType) => app.filterRequest(req, classname, id, extend, filterApiCollection(req, app).efind));
app.get(`${apiPathPrefix}/:classname/:id/:extend/:rid`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid) => app.filterRequest(req, classname, id, extend, rid, filterApiCollection(req, app).eget));
app.del(`${apiPathPrefix}/:classname/:id/:extend/:rid`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid) => app.filterRequest(req, classname, id, extend, rid, api.edel));
app.get(`${apiPathPrefix}/:classname/:id/:extend/:rid`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid: FibApp.AppIdType) => app.filterRequest(req, classname, id, extend, rid, filterApiCollection(req, app).eget));
app.del(`${apiPathPrefix}/:classname/:id/:extend/:rid`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid: FibApp.AppIdType) => app.filterRequest(req, classname, id, extend, rid, api.edel));
/* api extend :end */

app.post(`${apiPathPrefix}/:classname/:func`, (req: FibApp.FibAppHttpRequest, classname: string, func: string) => {
Expand All @@ -61,43 +62,48 @@ export function bind (app: FibApp.FibAppClass) {
/* view base :end */
/* view extend :start */
app.get(`${viewPathPrefix}/:classname/:id/:extend`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType) => app.filterRequest(req, classname, id, extend, viewApi.efind));
app.get(`${viewPathPrefix}/:classname/:id/:extend/:rid`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid) => app.filterRequest(req, classname, id, extend, rid, viewApi.eget));
app.get(`${viewPathPrefix}/:classname/:id/:extend/:rid`, (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid: FibApp.AppIdType) => app.filterRequest(req, classname, id, extend, rid, viewApi.eget));
/* view extend :end */
}
}

if (!apiPathPrefix && viewPathPrefix) {
setupViewRoute()
setupApiRoute()
} else {
setupApiRoute()
setupViewRoute()
}
Hook.wait(app, app.__opts.hooks.beforeSetupRoute, function (err: FxOrmError.ExtendedError) {
if (err)
throw err;

/* setup graphql :start */
const mergeRootAndGraphqlRoot = !graphQLPathPrefix || graphQLPathPrefix === '/'
if (!mergeRootAndGraphqlRoot)
app.post(graphQLPathPrefix, (req: FibApp.FibAppHttpRequest) => {
if (!is_graphql_request(req))
return fill_error(req, err_info(4000005))

run_graphql(app, req)
})
/* setup graphql :end */
if (!apiPathPrefix && viewPathPrefix) {
setupViewRoute()
setupApiRoute()
} else {
setupApiRoute()
setupViewRoute()
}

/* setup batch task :end */
const mergeRootAndBatchRoot = !batchPathPrefix || batchPathPrefix === '/'
if (!mergeRootAndBatchRoot)
app.post(batchPathPrefix, (req: FibApp.FibAppHttpRequest) => run_batch(app, req))
/* setup batch task :end */
/* setup graphql :start */
const mergeRootAndGraphqlRoot = !graphQLPathPrefix || graphQLPathPrefix === '/'
if (!mergeRootAndGraphqlRoot)
app.post(graphQLPathPrefix, (req: FibApp.FibAppHttpRequest) => {
if (!is_graphql_request(req))
return fill_error(req, err_info(4000005))

run_graphql(app, req)
})
/* setup graphql :end */


/* finally, root setup */
app.post('/', (req: FibApp.FibAppHttpRequest) => {
if (is_graphql_request(req))
return mergeRootAndGraphqlRoot && run_graphql(app, req);

mergeRootAndBatchRoot && run_batch(app, req)
/* setup batch task :end */
const mergeRootAndBatchRoot = !batchPathPrefix || batchPathPrefix === '/'
if (!mergeRootAndBatchRoot)
app.post(batchPathPrefix, (req: FibApp.FibAppHttpRequest) => run_batch(app, req))
/* setup batch task :end */


/* finally, root setup */
app.post('/', (req: FibApp.FibAppHttpRequest) => {
if (is_graphql_request(req))
return mergeRootAndGraphqlRoot && run_graphql(app, req);

mergeRootAndBatchRoot && run_batch(app, req)
});
});
};

Expand Down

0 comments on commit 626965c

Please sign in to comment.