-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathindex.ts
More file actions
443 lines (388 loc) · 16.1 KB
/
index.ts
File metadata and controls
443 lines (388 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import { lowerCaseFirst, safeJSONStringify } from '@zenstackhq/common-helpers';
import { CoreCrudOperations, ORMError, ORMErrorReason, type ClientContract } from '@zenstackhq/orm';
import type { SchemaDef } from '@zenstackhq/orm/schema';
import SuperJSON from 'superjson';
import { match } from 'ts-pattern';
import z from 'zod';
import { fromError } from 'zod-validation-error/v4';
import type { ApiHandler, LogConfig, RequestContext, Response } from '../../types';
import { getProcedureDef, mapProcedureArgs, PROCEDURE_ROUTE_PREFIXES } from '../common/procedures';
import { loggerSchema } from '../common/schemas';
import { processSuperJsonRequestPayload, unmarshalQ } from '../common/utils';
import { log, registerCustomSerializers } from '../utils';
const TRANSACTION_ROUTE_PREFIX = '$transaction' as const;
const VALID_OPS = new Set(CoreCrudOperations as unknown as string[]);
registerCustomSerializers();
/**
* Options for {@link RPCApiHandler}
*/
export type RPCApiHandlerOptions<Schema extends SchemaDef = SchemaDef> = {
/**
* The schema
*/
schema: Schema;
/**
* Logging configuration
*/
log?: LogConfig;
};
/**
* RPC style API request handler that mirrors the ZenStackClient API
*/
export class RPCApiHandler<Schema extends SchemaDef = SchemaDef> implements ApiHandler<Schema> {
constructor(private readonly options: RPCApiHandlerOptions<Schema>) {
this.validateOptions(options);
}
private validateOptions(options: RPCApiHandlerOptions<Schema>) {
const schema = z.strictObject({ schema: z.object(), log: loggerSchema.optional() });
const parseResult = schema.safeParse(options);
if (!parseResult.success) {
throw new Error(`Invalid options: ${fromError(parseResult.error)}`);
}
}
get schema(): Schema {
return this.options.schema;
}
get log(): LogConfig | undefined {
return this.options.log;
}
async handleRequest({ client, method, path, query, requestBody }: RequestContext<Schema>): Promise<Response> {
const parts = path.split('/').filter((p) => !!p);
const op = parts.pop();
let model = parts.pop();
if (parts.length !== 0 || !op || !model) {
return this.makeBadInputErrorResponse('invalid request path');
}
if (model === PROCEDURE_ROUTE_PREFIXES) {
return this.handleProcedureRequest({
client,
method: method.toUpperCase(),
proc: op,
query,
requestBody,
});
}
if (model === TRANSACTION_ROUTE_PREFIX) {
return this.handleTransaction({
client,
method: method.toUpperCase(),
type: op,
requestBody,
});
}
model = lowerCaseFirst(model);
method = method.toUpperCase();
let args: unknown;
let resCode = 200;
switch (op) {
case 'create':
case 'createMany':
case 'createManyAndReturn':
case 'upsert':
if (method !== 'POST') {
return this.makeBadInputErrorResponse('invalid request method, only POST is supported');
}
if (!requestBody) {
return this.makeBadInputErrorResponse('missing request body');
}
args = requestBody;
resCode = 201;
break;
case 'findFirst':
case 'findUnique':
case 'findMany':
case 'aggregate':
case 'groupBy':
case 'count':
case 'exists':
if (method !== 'GET') {
return this.makeBadInputErrorResponse('invalid request method, only GET is supported');
}
try {
args = query?.['q'] ? unmarshalQ(query['q'] as string, query['meta'] as string | undefined) : {};
} catch {
return this.makeBadInputErrorResponse('invalid "q" query parameter');
}
break;
case 'update':
case 'updateMany':
case 'updateManyAndReturn':
if (method !== 'PUT' && method !== 'PATCH') {
return this.makeBadInputErrorResponse('invalid request method, only PUT or PATCH are supported');
}
if (!requestBody) {
return this.makeBadInputErrorResponse('missing request body');
}
args = requestBody;
break;
case 'delete':
case 'deleteMany':
if (method !== 'DELETE') {
return this.makeBadInputErrorResponse('invalid request method, only DELETE is supported');
}
try {
args = query?.['q'] ? unmarshalQ(query['q'] as string, query['meta'] as string | undefined) : {};
} catch (err) {
return this.makeBadInputErrorResponse(
err instanceof Error ? err.message : 'invalid "q" query parameter',
);
}
break;
default:
return this.makeBadInputErrorResponse('invalid operation: ' + op);
}
const { result: processedArgs, error } = await this.processRequestPayload(args);
if (error) {
return this.makeBadInputErrorResponse(error);
}
try {
if (!this.isValidModel(client, model)) {
return this.makeBadInputErrorResponse(`unknown model name: ${model}`);
}
log(
this.options.log,
'debug',
() => `handling "${model}.${op}" request with args: ${safeJSONStringify(processedArgs)}`,
);
const clientResult = await (client as any)[model][op](processedArgs);
let responseBody: any = { data: clientResult };
// superjson serialize response
if (clientResult) {
const { json, meta } = SuperJSON.serialize(clientResult);
responseBody = { data: json };
if (meta) {
responseBody.meta = { serialization: meta };
}
}
const response = { status: resCode, body: responseBody };
log(
this.options.log,
'debug',
() => `sending response for "${model}.${op}" request: ${safeJSONStringify(response)}`,
);
return response;
} catch (err) {
log(this.options.log, 'error', `error occurred when handling "${model}.${op}" request`, err);
if (err instanceof ORMError) {
return this.makeORMErrorResponse(err);
} else {
return this.makeGenericErrorResponse(err);
}
}
}
private async handleTransaction({
client,
method,
type,
requestBody,
}: {
client: ClientContract<Schema>;
method: string;
type: string;
requestBody?: unknown;
}): Promise<Response> {
if (method !== 'POST') {
return this.makeBadInputErrorResponse('invalid request method, only POST is supported');
}
if (type !== 'sequential') {
return this.makeBadInputErrorResponse(`unsupported transaction type: ${type}`);
}
if (!requestBody || !Array.isArray(requestBody) || requestBody.length === 0) {
return this.makeBadInputErrorResponse('request body must be a non-empty array of operations');
}
const processedOps: Array<{ model: string; op: string; args: unknown }> = [];
for (let i = 0; i < requestBody.length; i++) {
const item = requestBody[i];
if (!item || typeof item !== 'object') {
return this.makeBadInputErrorResponse(`operation at index ${i} must be an object`);
}
const { model: itemModel, op: itemOp, args: itemArgs } = item as any;
if (!itemModel || typeof itemModel !== 'string') {
return this.makeBadInputErrorResponse(`operation at index ${i} is missing a valid "model" field`);
}
if (!itemOp || typeof itemOp !== 'string') {
return this.makeBadInputErrorResponse(`operation at index ${i} is missing a valid "op" field`);
}
if (!VALID_OPS.has(itemOp)) {
return this.makeBadInputErrorResponse(`operation at index ${i} has invalid op: ${itemOp}`);
}
if (!this.isValidModel(client, lowerCaseFirst(itemModel))) {
return this.makeBadInputErrorResponse(`operation at index ${i} has unknown model: ${itemModel}`);
}
if (itemArgs !== undefined && itemArgs !== null && (typeof itemArgs !== 'object' || Array.isArray(itemArgs))) {
return this.makeBadInputErrorResponse(`operation at index ${i} has invalid "args" field`);
}
const { result: processedArgs, error: argsError } = await this.processRequestPayload(itemArgs ?? {});
if (argsError) {
return this.makeBadInputErrorResponse(`operation at index ${i}: ${argsError}`);
}
processedOps.push({ model: lowerCaseFirst(itemModel), op: itemOp, args: processedArgs });
}
try {
const promises = processedOps.map(({ model, op, args }) => {
return (client as any)[model][op](args);
});
log(this.options.log, 'debug', () => `handling "$transaction" request with ${promises.length} operations`);
const clientResult = await client.$transaction(promises as any);
const { json, meta } = SuperJSON.serialize(clientResult);
const responseBody: any = { data: json };
if (meta) {
responseBody.meta = { serialization: meta };
}
const response = { status: 200, body: responseBody };
log(
this.options.log,
'debug',
() => `sending response for "$transaction" request: ${safeJSONStringify(response)}`,
);
return response;
} catch (err) {
log(this.options.log, 'error', `error occurred when handling "$transaction" request`, err);
if (err instanceof ORMError) {
return this.makeORMErrorResponse(err);
}
return this.makeGenericErrorResponse(err);
}
}
private async handleProcedureRequest({
client,
method,
proc,
query,
requestBody,
}: {
client: ClientContract<Schema>;
method: string;
proc?: string;
query?: Record<string, string | string[]>;
requestBody?: unknown;
}): Promise<Response> {
if (!proc) {
return this.makeBadInputErrorResponse('missing procedure name');
}
const procDef = getProcedureDef(this.options.schema, proc);
if (!procDef) {
return this.makeBadInputErrorResponse(`unknown procedure: ${proc}`);
}
const isMutation = !!procDef.mutation;
if (isMutation) {
if (method !== 'POST') {
return this.makeBadInputErrorResponse('invalid request method, only POST is supported');
}
} else {
if (method !== 'GET') {
return this.makeBadInputErrorResponse('invalid request method, only GET is supported');
}
}
let argsPayload = method === 'POST' ? requestBody : undefined;
if (method === 'GET') {
try {
argsPayload = query?.['q']
? unmarshalQ(query['q'] as string, query['meta'] as string | undefined)
: undefined;
} catch (err) {
return this.makeBadInputErrorResponse(
err instanceof Error ? err.message : 'invalid "q" query parameter',
);
}
}
const { result: processedArgsPayload, error } = await processSuperJsonRequestPayload(argsPayload);
if (error) {
return this.makeBadInputErrorResponse(error);
}
let procInput: unknown;
try {
procInput = mapProcedureArgs(procDef, processedArgsPayload);
} catch (err) {
return this.makeBadInputErrorResponse(err instanceof Error ? err.message : 'invalid procedure arguments');
}
try {
log(this.options.log, 'debug', () => `handling "$procs.${proc}" request`);
const clientResult = await (client as any).$procs?.[proc](procInput);
const { json, meta } = SuperJSON.serialize(clientResult);
const responseBody: any = { data: json };
if (meta) {
responseBody.meta = { serialization: meta };
}
const response = { status: 200, body: responseBody };
log(
this.options.log,
'debug',
() => `sending response for "$procs.${proc}" request: ${safeJSONStringify(response)}`,
);
return response;
} catch (err) {
log(this.options.log, 'error', `error occurred when handling "$procs.${proc}" request`, err);
if (err instanceof ORMError) {
return this.makeORMErrorResponse(err);
}
return this.makeGenericErrorResponse(err);
}
}
private isValidModel(client: ClientContract<Schema>, model: string) {
return Object.keys(client.$schema.models).some((m) => lowerCaseFirst(m) === lowerCaseFirst(model));
}
private makeBadInputErrorResponse(message: string) {
const resp = {
status: 400,
body: { error: { message } },
};
log(this.options.log, 'debug', () => `sending error response: ${safeJSONStringify(resp)}`);
return resp;
}
private makeGenericErrorResponse(err: unknown) {
const resp = {
status: 500,
body: { error: { message: err instanceof Error ? err.message : 'unknown error' } },
};
log(
this.options.log,
'debug',
() => `sending error response: ${safeJSONStringify(resp)}${err instanceof Error ? '\n' + err.stack : ''}`,
);
return resp;
}
private makeORMErrorResponse(err: ORMError) {
let status = 400;
const error: any = { message: err.message, reason: err.reason };
match(err.reason)
.with(ORMErrorReason.NOT_FOUND, () => {
status = 404;
error.model = err.model;
})
.with(ORMErrorReason.INVALID_INPUT, () => {
status = 422;
error.rejectedByValidation = true;
error.model = err.model;
})
.with(ORMErrorReason.REJECTED_BY_POLICY, () => {
status = 403;
error.rejectedByPolicy = true;
error.model = err.model;
error.rejectReason = err.rejectedByPolicyReason;
})
.with(ORMErrorReason.DB_QUERY_ERROR, () => {
status = 400;
error.dbErrorCode = err.dbErrorCode;
})
.otherwise(() => {});
const resp = { status, body: { error } };
log(this.options.log, 'debug', () => `sending error response: ${safeJSONStringify(resp)}`);
return resp;
}
private async processRequestPayload(args: any) {
const { meta, ...rest } = args ?? {};
if (meta?.serialization) {
try {
// superjson deserialization
args = SuperJSON.deserialize({ json: rest, meta: meta.serialization });
} catch (err) {
return { result: undefined, error: `failed to deserialize request payload: ${(err as Error).message}` };
}
} else {
// drop meta when no serialization info is present
args = rest;
}
return { result: args, error: undefined };
}
}