Skip to content

Commit

Permalink
Merge pull request #6 from flash-oss/call-meta-within-arg
Browse files Browse the repository at this point in the history
Implement call meta within the arg._ property
  • Loading branch information
koresar authored May 18, 2024
2 parents b7e55a8 + 0d7f8a9 commit 169dda0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ const client = AllserverClient({
ctx.http.headers.authorization = "Basic my-token";
},
async after(ctx) {
if (ctx.error) console.error(ctx.error) else console.log(ctx.result);
if (ctx.error) console.error(ctx.error); else console.log(ctx.result);
},
});
```
Expand Down
4 changes: 4 additions & 0 deletions src/client/AllserverClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ module.exports = require("stampit")({
},

async call(procedureName, arg) {
if (!arg) arg = {};
if (!arg._) arg._ = {};
arg._.procedureName = procedureName;

const transport = this[p].transport;
const defaultCtx = { procedureName, arg, client: this };
const ctx = transport.createCallContext(defaultCtx);
Expand Down
12 changes: 3 additions & 9 deletions src/client/LambdaClientTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ module.exports = require("./ClientTransport").compose({
return result;
},

async call({ procedureName, lambda }) {
async call(ctx) {
let promise = this.awsSdkLambdaClient.invoke({
FunctionName: this.uri.substring("lambda://".length),
Payload: JSON.stringify({
callContext: { ...lambda.callContext, procedureName },
callArg: lambda.callArg,
}),
Payload: JSON.stringify(ctx.arg),
});
if (typeof promise.promise === "function") promise = promise.promise(); // AWS SDK v2 adoption
const invocationResponse = await promise;
Expand All @@ -44,10 +41,7 @@ module.exports = require("./ClientTransport").compose({
createCallContext(defaultCtx) {
return {
...defaultCtx,
lambda: {
callContext: {},
callArg: defaultCtx.arg,
},
lambda: {},
};
},
},
Expand Down
4 changes: 4 additions & 0 deletions src/server/Allserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ module.exports = require("stampit")({
ctx.isIntrospection = this.transport.isIntrospection(ctx);
if (!ctx.isIntrospection && ctx.procedureName) ctx.procedure = this.procedures[ctx.procedureName];

if (!ctx.arg) ctx.arg = {};
if (!ctx.arg._) ctx.arg._ = {};
if (!ctx.arg._.procedureName) ctx.arg._.procedureName = ctx.procedureName;

await this._callMiddlewares(ctx, "before");

if (!ctx.result) {
Expand Down
14 changes: 11 additions & 3 deletions src/server/LambdaTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ module.exports = require("./Transport").compose({
return false;
}
} else {
ctx.arg = ctx.lambda.invoke.callArg || {};
// TODO: change to this during the next major release:
// ctx.arg = ctx.lambda.invoke || {};
ctx.arg = ctx.lambda.invoke.callArg || ctx.lambda.invoke || {};
return true;
}
},
Expand Down Expand Up @@ -54,7 +56,11 @@ module.exports = require("./Transport").compose({
headers: event?.headers,
};
} else {
lambda.invoke = { callContext: event?.callContext, callArg: event?.callArg };
// TODO: change to this during the next major release:
// lambda.invoke = event || {};
lambda.invoke = event?.callArg
? { callContext: event?.callContext, callArg: event?.callArg }
: event || {};
}

this._handleRequest({ ...defaultCtx, lambda });
Expand All @@ -64,7 +70,9 @@ module.exports = require("./Transport").compose({

getProcedureName(ctx) {
const { isHttp, http, invoke } = ctx.lambda;
return isHttp ? http.path.substr(1) : invoke.callContext?.procedureName;
// TODO: change to this during the next major release:
// return (isHttp ? http.path.substr(1) : invoke._?.procedureName) || ""; // if no procedureName then it's an introspection
return isHttp ? http.path.substr(1) : invoke.callContext?.procedureName || invoke._?.procedureName || ""; // if no procedureName then it's an introspection
},

isIntrospection(ctx) {
Expand Down
14 changes: 7 additions & 7 deletions test/client/AllserverClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe("AllserverClient", () => {
},
async call({ procedureName, arg }) {
assert.strictEqual(procedureName, "getRates");
assert.deepStrictEqual(arg, { a: 1 });
assert.deepStrictEqual(arg, { a: 1, _: { procedureName: "getRates" } });
return { success: true, code: "CALLED", message: "A is good", b: 42 };
},
});
Expand Down Expand Up @@ -244,7 +244,7 @@ describe("AllserverClient", () => {
},
async call({ procedureName, arg }) {
assert.strictEqual(procedureName, "getRates");
assert.deepStrictEqual(arg, { a: 1 });
assert.deepStrictEqual(arg, { a: 1, _: { procedureName: "getRates" } });
return { success: true, code: "CALLED", message: "A is good", b: 42 };
},
});
Expand All @@ -256,7 +256,7 @@ describe("AllserverClient", () => {
assert(!ctx.procedureName);
} else {
assert.strictEqual(ctx.procedureName, "getRates");
assert.deepStrictEqual(ctx.arg, { a: 1 });
assert.deepStrictEqual(ctx.arg, { a: 1, _: { procedureName: "getRates" } });
}
beforeCalled += 1;
}
Expand Down Expand Up @@ -340,7 +340,7 @@ describe("AllserverClient", () => {
},
async call({ procedureName, arg }) {
assert.strictEqual(procedureName, "getRates");
assert.deepStrictEqual(arg, { a: 1 });
assert.deepStrictEqual(arg, { a: 1, _: { procedureName: "getRates" } });
return { success: true, code: "CALLED", message: "A is good", b: 42 };
},
});
Expand All @@ -352,7 +352,7 @@ describe("AllserverClient", () => {
assert(!ctx.procedureName);
} else {
assert.strictEqual(ctx.procedureName, "getRates");
assert.deepStrictEqual(ctx.arg, { a: 1 });
assert.deepStrictEqual(ctx.arg, { a: 1, _: { procedureName: "getRates" } });
assert.deepStrictEqual(ctx.result, {
success: true,
code: "CALLED",
Expand Down Expand Up @@ -479,7 +479,7 @@ describe("AllserverClient", () => {
},
async call({ procedureName, arg }) {
assert.strictEqual(procedureName, "foo");
assert.deepStrictEqual(arg, { a: 1 });
assert.deepStrictEqual(arg, { a: 1, _: { procedureName: "foo" } });
return { success: true, code: "CALLED_A", message: "A is good", b: 42 };
},
});
Expand All @@ -498,7 +498,7 @@ describe("AllserverClient", () => {
},
call({ procedureName, arg }) {
assert.strictEqual(procedureName, "foo");
assert.deepStrictEqual(arg, { a: 1 });
assert.deepStrictEqual(arg, { a: 1, _: { procedureName: "foo" } });
return { success: true, code: "CALLED_A", message: "A is good", b: 42 };
},
});
Expand Down
4 changes: 3 additions & 1 deletion test/server/Allserver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ describe("Allserver", () => {
before(ctx) {
assert.strictEqual(this, server, "The `this` context must be the server itself");
assert.deepStrictEqual(ctx, {
arg: { _: { procedureName: "testMethod" } },
callNumber: 0,
procedure: server.procedures.testMethod,
procedureName: "testMethod",
Expand Down Expand Up @@ -377,6 +378,7 @@ describe("Allserver", () => {
after(ctx) {
assert.strictEqual(this, server, "The `this` context must be the server itself");
assert.deepStrictEqual(ctx, {
arg: { _: { procedureName: "testMethod" } },
callNumber: 0,
procedure: server.procedures.testMethod,
procedureName: "testMethod",
Expand Down Expand Up @@ -404,7 +406,7 @@ describe("Allserver", () => {
let logged = false;
const server = Allserver({
logger: {
error(err,code) {
error(err, code) {
assert.strictEqual(code, "ALLSERVER_MIDDLEWARE_ERROR");
assert.strictEqual(err.message, "Handle me please");
logged = true;
Expand Down

0 comments on commit 169dda0

Please sign in to comment.