@teamkeel/functions-runtime 0.371.1 → 0.373.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/QueryBuilder.js +1 -2
- package/src/errors.js +23 -20
- package/src/handleRequest.js +4 -1
- package/src/handleRequest.test.js +49 -7
- package/src/index.d.ts +3 -5
package/package.json
CHANGED
package/src/QueryBuilder.js
CHANGED
package/src/errors.js
CHANGED
|
@@ -26,25 +26,13 @@ const RuntimeErrors = {
|
|
|
26
26
|
|
|
27
27
|
// errorToJSONRPCResponse transforms a JavaScript Error instance (or derivative) into a valid JSONRPC response object to pass back to the Keel runtime.
|
|
28
28
|
function errorToJSONRPCResponse(request, e) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (e instanceof PermissionError) {
|
|
29
|
+
switch (e.constructor.name) {
|
|
30
|
+
case "PermissionError":
|
|
33
31
|
return createJSONRPCErrorResponse(
|
|
34
32
|
request.id,
|
|
35
33
|
RuntimeErrors.PermissionError,
|
|
36
34
|
e.message
|
|
37
35
|
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return createJSONRPCErrorResponse(
|
|
41
|
-
request.id,
|
|
42
|
-
RuntimeErrors.UnknownError,
|
|
43
|
-
e.message
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
// we want to switch on instanceof but there is no way to do that in js, so best to check the constructor class of the error
|
|
47
|
-
switch (e.error.constructor.name) {
|
|
48
36
|
// Any error thrown in the ModelAPI class is
|
|
49
37
|
// wrapped in a DatabaseError in order to differentiate 'our code' vs the user's own code.
|
|
50
38
|
case "NoResultError":
|
|
@@ -56,23 +44,38 @@ function errorToJSONRPCResponse(request, e) {
|
|
|
56
44
|
e.message
|
|
57
45
|
);
|
|
58
46
|
case "DatabaseError":
|
|
59
|
-
|
|
47
|
+
let err = e;
|
|
48
|
+
|
|
49
|
+
// If wrapped error then unwrap
|
|
50
|
+
if (e instanceof DatabaseError) {
|
|
51
|
+
err = e.error;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (err.constructor.name == "NoResultError") {
|
|
55
|
+
return createJSONRPCErrorResponse(
|
|
56
|
+
request.id,
|
|
57
|
+
|
|
58
|
+
// to be matched to https://github.com/teamkeel/keel/blob/e3115ffe381bfc371d4f45bbf96a15072a994ce5/runtime/actions/update.go#L54-L54
|
|
59
|
+
RuntimeErrors.RecordNotFoundError,
|
|
60
|
+
err.message
|
|
61
|
+
);
|
|
62
|
+
}
|
|
60
63
|
|
|
61
|
-
// if the
|
|
64
|
+
// if the error contains 'code' then assume it has other pg error message keys
|
|
62
65
|
// todo: make this more ironclad.
|
|
63
66
|
// when using lib-pq, should match https://github.com/brianc/node-postgres/blob/master/packages/pg-protocol/src/parser.ts#L371-L386
|
|
64
|
-
if ("code" in
|
|
65
|
-
const { code, detail, table } =
|
|
67
|
+
if ("code" in err) {
|
|
68
|
+
const { code, detail, table } = err;
|
|
66
69
|
|
|
67
70
|
let rpcErrorCode, column, value;
|
|
68
|
-
const [col, val] = parseKeyMessage(
|
|
71
|
+
const [col, val] = parseKeyMessage(err.detail);
|
|
69
72
|
column = col;
|
|
70
73
|
value = val;
|
|
71
74
|
|
|
72
75
|
switch (code) {
|
|
73
76
|
case "23502":
|
|
74
77
|
rpcErrorCode = RuntimeErrors.NotNullConstraintError;
|
|
75
|
-
column =
|
|
78
|
+
column = err.column;
|
|
76
79
|
break;
|
|
77
80
|
case "23503":
|
|
78
81
|
rpcErrorCode = RuntimeErrors.ForeignKeyConstraintError;
|
package/src/handleRequest.js
CHANGED
|
@@ -88,7 +88,10 @@ async function handleRequest(request, config) {
|
|
|
88
88
|
for (const pair of headers.entries()) {
|
|
89
89
|
responseHeaders[pair[0]] = pair[1].split(", ");
|
|
90
90
|
}
|
|
91
|
-
response.meta = {
|
|
91
|
+
response.meta = {
|
|
92
|
+
headers: responseHeaders,
|
|
93
|
+
status: ctx.response.status,
|
|
94
|
+
};
|
|
92
95
|
|
|
93
96
|
return response;
|
|
94
97
|
} catch (e) {
|
|
@@ -23,7 +23,13 @@ test("when the custom function returns expected value", async () => {
|
|
|
23
23
|
actionTypes: {
|
|
24
24
|
createPost: PROTO_ACTION_TYPES.CREATE,
|
|
25
25
|
},
|
|
26
|
-
createContextAPI: () => {
|
|
26
|
+
createContextAPI: () => {
|
|
27
|
+
return {
|
|
28
|
+
response: {
|
|
29
|
+
headers: new Headers(),
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
},
|
|
27
33
|
};
|
|
28
34
|
|
|
29
35
|
const rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
|
@@ -52,7 +58,13 @@ test("when the custom function doesnt return a value", async () => {
|
|
|
52
58
|
actionTypes: {
|
|
53
59
|
createPost: PROTO_ACTION_TYPES.CREATE,
|
|
54
60
|
},
|
|
55
|
-
createContextAPI: () => {
|
|
61
|
+
createContextAPI: () => {
|
|
62
|
+
return {
|
|
63
|
+
response: {
|
|
64
|
+
headers: new Headers(),
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
},
|
|
56
68
|
};
|
|
57
69
|
|
|
58
70
|
const rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
|
@@ -75,7 +87,13 @@ test("when there is no matching function for the path", async () => {
|
|
|
75
87
|
actionTypes: {
|
|
76
88
|
createPost: PROTO_ACTION_TYPES.CREATE,
|
|
77
89
|
},
|
|
78
|
-
createContextAPI: () => {
|
|
90
|
+
createContextAPI: () => {
|
|
91
|
+
return {
|
|
92
|
+
response: {
|
|
93
|
+
headers: new Headers(),
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
79
97
|
};
|
|
80
98
|
|
|
81
99
|
const rpcReq = createJSONRPCRequest("123", "unknown", { title: "a post" });
|
|
@@ -100,7 +118,13 @@ test("when there is an unexpected error in the custom function", async () => {
|
|
|
100
118
|
actionTypes: {
|
|
101
119
|
createPost: PROTO_ACTION_TYPES.CREATE,
|
|
102
120
|
},
|
|
103
|
-
createContextAPI: () => {
|
|
121
|
+
createContextAPI: () => {
|
|
122
|
+
return {
|
|
123
|
+
response: {
|
|
124
|
+
headers: new Headers(),
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
},
|
|
104
128
|
};
|
|
105
129
|
|
|
106
130
|
const rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
|
@@ -128,7 +152,13 @@ test("when a role based permission has already been granted by the main runtime"
|
|
|
128
152
|
createPost: PROTO_ACTION_TYPES.CREATE,
|
|
129
153
|
},
|
|
130
154
|
createModelAPI: () => {},
|
|
131
|
-
createContextAPI: () => {
|
|
155
|
+
createContextAPI: () => {
|
|
156
|
+
return {
|
|
157
|
+
response: {
|
|
158
|
+
headers: new Headers(),
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
},
|
|
132
162
|
};
|
|
133
163
|
|
|
134
164
|
let rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
|
@@ -159,7 +189,13 @@ test("when there is an unexpected object thrown in the custom function", async (
|
|
|
159
189
|
actionTypes: {
|
|
160
190
|
createPost: PROTO_ACTION_TYPES.CREATE,
|
|
161
191
|
},
|
|
162
|
-
createContextAPI: () => {
|
|
192
|
+
createContextAPI: () => {
|
|
193
|
+
return {
|
|
194
|
+
response: {
|
|
195
|
+
headers: new Headers(),
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
},
|
|
163
199
|
};
|
|
164
200
|
|
|
165
201
|
const rpcReq = createJSONRPCRequest("123", "createPost", { title: "a post" });
|
|
@@ -244,7 +280,13 @@ describe("ModelAPI error handling", () => {
|
|
|
244
280
|
return deleted;
|
|
245
281
|
},
|
|
246
282
|
},
|
|
247
|
-
createContextAPI: () =>
|
|
283
|
+
createContextAPI: () => {
|
|
284
|
+
return {
|
|
285
|
+
response: {
|
|
286
|
+
headers: new Headers(),
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
},
|
|
248
290
|
};
|
|
249
291
|
});
|
|
250
292
|
|
package/src/index.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ export type ContextAPI = {
|
|
|
59
59
|
|
|
60
60
|
export type Response = {
|
|
61
61
|
headers: Headers;
|
|
62
|
+
status?: number;
|
|
62
63
|
};
|
|
63
64
|
|
|
64
65
|
export type PageInfo = {
|
|
@@ -69,11 +70,8 @@ export type PageInfo = {
|
|
|
69
70
|
count: number;
|
|
70
71
|
};
|
|
71
72
|
|
|
72
|
-
// Request headers
|
|
73
|
-
export type RequestHeaders =
|
|
74
|
-
get(name: string): string;
|
|
75
|
-
has(name: string): boolean;
|
|
76
|
-
};
|
|
73
|
+
// Request headers cannot be mutated, so remove any methods that mutate
|
|
74
|
+
export type RequestHeaders = Omit<Headers, "append" | "delete" | "set">;
|
|
77
75
|
|
|
78
76
|
export declare class Permissions {
|
|
79
77
|
constructor();
|