@teamkeel/functions-runtime 0.245.2 → 0.246.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/RequestHeaders.js +21 -0
- package/src/handleRequest.js +3 -2
- package/src/handleRequest.test.js +5 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +2 -0
package/package.json
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class RequestHeaders {
|
|
2
|
+
/**
|
|
3
|
+
* @param {{Object.<string, string>}} requestHeaders Map of request headers submitted from the client
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
constructor(requestHeaders) {
|
|
7
|
+
this._headers = new Headers(requestHeaders);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get(key) {
|
|
11
|
+
return this._headers.get(key);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
has(key) {
|
|
15
|
+
return this._headers.has(key);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = {
|
|
20
|
+
RequestHeaders,
|
|
21
|
+
};
|
package/src/handleRequest.js
CHANGED
|
@@ -10,7 +10,7 @@ const { errorToJSONRPCResponse, RuntimeErrors } = require("./errors");
|
|
|
10
10
|
// to execute a custom function based on the contents of a jsonrpc-2.0 payload object.
|
|
11
11
|
// To read more about jsonrpc request and response shapes, please read https://www.jsonrpc.org/specification
|
|
12
12
|
async function handleRequest(request, config) {
|
|
13
|
-
const { createFunctionAPI, functions } = config;
|
|
13
|
+
const { createFunctionAPI, createContextAPI, functions } = config;
|
|
14
14
|
|
|
15
15
|
if (!(request.method in functions)) {
|
|
16
16
|
return createJSONRPCErrorResponse(
|
|
@@ -23,7 +23,8 @@ async function handleRequest(request, config) {
|
|
|
23
23
|
try {
|
|
24
24
|
const result = await functions[request.method](
|
|
25
25
|
request.params,
|
|
26
|
-
createFunctionAPI()
|
|
26
|
+
createFunctionAPI(),
|
|
27
|
+
createContextAPI(request.meta)
|
|
27
28
|
);
|
|
28
29
|
|
|
29
30
|
if (result === undefined) {
|
|
@@ -13,6 +13,7 @@ test("when the custom function returns expected value", async () => {
|
|
|
13
13
|
},
|
|
14
14
|
},
|
|
15
15
|
createFunctionAPI: () => {},
|
|
16
|
+
createContextAPI: () => {},
|
|
16
17
|
};
|
|
17
18
|
|
|
18
19
|
const rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
|
@@ -33,6 +34,7 @@ test("when the custom function doesnt return a value", async () => {
|
|
|
33
34
|
createPost: async () => {},
|
|
34
35
|
},
|
|
35
36
|
createFunctionAPI: () => {},
|
|
37
|
+
createContextAPI: () => {},
|
|
36
38
|
};
|
|
37
39
|
|
|
38
40
|
const rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
|
@@ -53,6 +55,7 @@ test("when there is no matching function for the path", async () => {
|
|
|
53
55
|
createPost: async () => {},
|
|
54
56
|
},
|
|
55
57
|
createFunctionAPI: () => {},
|
|
58
|
+
createContextAPI: () => {},
|
|
56
59
|
};
|
|
57
60
|
|
|
58
61
|
const rpcReq = createJSONRPCRequest("123", "unknown", { title: "a post" });
|
|
@@ -75,6 +78,7 @@ test("when there is an unexpected error in the custom function", async () => {
|
|
|
75
78
|
},
|
|
76
79
|
},
|
|
77
80
|
createFunctionAPI: () => {},
|
|
81
|
+
createContextAPI: () => {},
|
|
78
82
|
};
|
|
79
83
|
|
|
80
84
|
const rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
|
@@ -97,6 +101,7 @@ test("when there is an unexpected object thrown in the custom function", async (
|
|
|
97
101
|
},
|
|
98
102
|
},
|
|
99
103
|
createFunctionAPI: () => {},
|
|
104
|
+
createContextAPI: () => {},
|
|
100
105
|
};
|
|
101
106
|
|
|
102
107
|
const rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
package/src/index.d.ts
CHANGED
|
@@ -52,3 +52,14 @@ export type TimestampQueryInput = {
|
|
|
52
52
|
before: string;
|
|
53
53
|
after: string;
|
|
54
54
|
};
|
|
55
|
+
|
|
56
|
+
// Ctx API
|
|
57
|
+
export type ContextAPI = {
|
|
58
|
+
headers: RequestHeaders;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Request headers query API
|
|
62
|
+
export type RequestHeaders = {
|
|
63
|
+
get(name: string): string;
|
|
64
|
+
has(name: string): boolean;
|
|
65
|
+
};
|
package/src/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
const { ModelAPI } = require("./ModelAPI");
|
|
2
|
+
const { RequestHeaders } = require("./RequestHeaders");
|
|
2
3
|
const { handleRequest } = require("./handleRequest");
|
|
3
4
|
const KSUID = require("ksuid");
|
|
4
5
|
const { getDatabase } = require("./database");
|
|
5
6
|
|
|
6
7
|
module.exports = {
|
|
7
8
|
ModelAPI,
|
|
9
|
+
RequestHeaders,
|
|
8
10
|
handleRequest,
|
|
9
11
|
getDatabase,
|
|
10
12
|
ksuid() {
|