Skip to content

Commit

Permalink
fix: args path (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Aug 3, 2020
1 parent fe8c29e commit f0de617
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
32 changes: 19 additions & 13 deletions packages/serverless-spec-builder/registerFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,15 @@ const {
const { join } = require('path');
const { existsSync } = require('fs');

const registerFunctionToIoc = (container, functionName, func) => {
const registerFunctionToIoc = (container, functionName, func, argsPath) => {
class FunctionContainer {
async handler(event) {
const bindCtx = {
ctx: this.ctx,
event,
};

/**
* HTTP Case
*/
const args =
(this.ctx &&
this.ctx.request &&
this.ctx.request.body &&
this.ctx.request.body.args) ||
[];

const args = getValue(bindCtx, argsPath || 'ctx.request.body.args', []);
return func.bind(bindCtx)(...args);
}
}
Expand Down Expand Up @@ -53,7 +44,12 @@ const registerFunctionToIocByConfig = (config, options) => {
const { baseDir = process.cwd() } = options || {};

config.functionList.forEach(functionInfo => {
const { functionName, functionFilePath, functionHandler } = functionInfo;
const {
functionName,
functionFilePath,
functionHandler,
argsPath,
} = functionInfo;

if (!functionFilePath) {
return;
Expand Down Expand Up @@ -84,7 +80,8 @@ const registerFunctionToIocByConfig = (config, options) => {
registerFunctionToIoc(
options.context,
functionHandler || `${functionName || '$default'}.handler`,
fun
fun,
argsPath
);
} catch (error) {
console.error(
Expand All @@ -99,6 +96,15 @@ const registerFunctionToIocByConfig = (config, options) => {
});
};

// copy from https://github.com/developit/dlv/blob/master/index.js
const getValue = (obj, key, def, p, undef) => {
key = key.split ? key.split('.') : key;
for (p = 0; p < key.length; p++) {
obj = obj ? obj[key[p]] : undef;
}
return obj === undef ? def : obj;
};

module.exports = {
registerFunctionToIocByConfig,
registerFunctionToIoc,
Expand Down
1 change: 1 addition & 0 deletions packages/serverless-spec-builder/src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const assignToFunctionMap = (functionMap, handlerConf) => {
functionName: handlerConf.exportFunction,
functionHandler: handlerConf.handler,
functionFilePath: handlerConf.sourceFilePath,
argsPath: handlerConf.argsPath,
});
}
return functionMap;
Expand Down
16 changes: 0 additions & 16 deletions packages/serverless-spec-builder/test/fixtures/wrapper/aggre.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const { FaaSStarter } = require('@midwayjs/faas');
const { asyncWrapper, start } = require('testStarter');

const { registerFunctionToIocByConfig } = require('./registerFunction.js');
const { join } = require('path');

const picomatch = require('picomatch');


Expand All @@ -24,19 +21,6 @@ const initializeMethod = async (initializeContext = {}) => {
] });


registerFunctionToIocByConfig({
"functionList": [
{
"functionName": "test",
"functionHandler": "index.handler",
"functionFilePath": "fun-index.js"
}
]
}, {
baseDir: join(__dirname, 'dist'),
context: starter.loader.getApplicationContext()
});

await starter.start();
inited = true;

Expand Down
1 change: 1 addition & 0 deletions packages/serverless-spec-builder/test/wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('/test/wrapper.test.ts', () => {
{
path: '/api/test',
handler: 'index.handler',
argsPath: 'ctx.request.data.args',
isFunctional: true,
exportFunction: 'test',
sourceFilePath: 'fun-index.js',
Expand Down

0 comments on commit f0de617

Please sign in to comment.