Skip to content

Commit ccdb1cc

Browse files
Cleanup
1 parent 9ceaa78 commit ccdb1cc

File tree

1 file changed

+31
-50
lines changed

1 file changed

+31
-50
lines changed

packages/server/src/api/rest/index.ts

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -247,20 +247,13 @@ class RequestHandler extends APIHandlerBase {
247247
let match = urlPatterns.single.match(path);
248248
if (match) {
249249
// single resource read
250-
return await this.processSingleRead(prisma, match.type, match.id, query, modelMeta);
250+
return await this.processSingleRead(prisma, match.type, match.id, query);
251251
}
252252

253253
match = urlPatterns.fetchRelationship.match(path);
254254
if (match) {
255255
// fetch related resource(s)
256-
return await this.processFetchRelated(
257-
prisma,
258-
match.type,
259-
match.id,
260-
match.relationship,
261-
query,
262-
modelMeta
263-
);
256+
return await this.processFetchRelated(prisma, match.type, match.id, match.relationship, query);
264257
}
265258

266259
match = urlPatterns.relationship.match(path);
@@ -271,15 +264,14 @@ class RequestHandler extends APIHandlerBase {
271264
match.type,
272265
match.id,
273266
match.relationship,
274-
query,
275-
modelMeta
267+
query
276268
);
277269
}
278270

279271
match = urlPatterns.collection.match(path);
280272
if (match) {
281273
// collection read
282-
return await this.processCollectionRead(prisma, match.type, query, modelMeta);
274+
return await this.processCollectionRead(prisma, match.type, query);
283275
}
284276

285277
return this.makeError('invalidPath');
@@ -306,8 +298,7 @@ class RequestHandler extends APIHandlerBase {
306298
match.id,
307299
match.relationship,
308300
query,
309-
requestBody,
310-
modelMeta
301+
requestBody
311302
);
312303
}
313304

@@ -345,8 +336,7 @@ class RequestHandler extends APIHandlerBase {
345336
match.id,
346337
match.relationship as string,
347338
query,
348-
requestBody,
349-
modelMeta
339+
requestBody
350340
);
351341
}
352342

@@ -370,8 +360,7 @@ class RequestHandler extends APIHandlerBase {
370360
match.id,
371361
match.relationship as string,
372362
query,
373-
requestBody,
374-
modelMeta
363+
requestBody
375364
);
376365
}
377366

@@ -394,8 +383,7 @@ class RequestHandler extends APIHandlerBase {
394383
prisma: DbClientContract,
395384
type: string,
396385
resourceId: string,
397-
query: Record<string, string | string[]> | undefined,
398-
modelMeta: ModelMeta
386+
query: Record<string, string | string[]> | undefined
399387
): Promise<Response> {
400388
const typeInfo = this.typeMap[type];
401389
if (!typeInfo) {
@@ -420,16 +408,12 @@ class RequestHandler extends APIHandlerBase {
420408
include = allIncludes;
421409
}
422410

423-
let entity = await prisma[type].findUnique(args);
424-
425-
if (typeInfo.idFields.length > 1) {
426-
entity = { ...entity, [this.makeIdKey(typeInfo.idFields)]: resourceId };
427-
}
411+
const entity = await prisma[type].findUnique(args);
428412

429413
if (entity) {
430414
return {
431415
status: 200,
432-
body: await this.serializeItems(type, entity, modelMeta, { include }),
416+
body: await this.serializeItems(type, entity, { include }),
433417
};
434418
} else {
435419
return this.makeError('notFound');
@@ -441,8 +425,7 @@ class RequestHandler extends APIHandlerBase {
441425
type: string,
442426
resourceId: string,
443427
relationship: string,
444-
query: Record<string, string | string[]> | undefined,
445-
modelMeta: ModelMeta
428+
query: Record<string, string | string[]> | undefined
446429
): Promise<Response> {
447430
const typeInfo = this.typeMap[type];
448431
if (!typeInfo) {
@@ -499,7 +482,7 @@ class RequestHandler extends APIHandlerBase {
499482
if (entity?.[relationship]) {
500483
return {
501484
status: 200,
502-
body: await this.serializeItems(relationInfo.type, entity[relationship], modelMeta, {
485+
body: await this.serializeItems(relationInfo.type, entity[relationship], {
503486
linkers: {
504487
document: new Linker(() => this.makeLinkUrl(`/${type}/${resourceId}/${relationship}`)),
505488
paginator,
@@ -517,8 +500,7 @@ class RequestHandler extends APIHandlerBase {
517500
type: string,
518501
resourceId: string,
519502
relationship: string,
520-
query: Record<string, string | string[]> | undefined,
521-
modelMeta: ModelMeta
503+
query: Record<string, string | string[]> | undefined
522504
): Promise<Response> {
523505
const typeInfo = this.typeMap[type];
524506
if (!typeInfo) {
@@ -560,7 +542,7 @@ class RequestHandler extends APIHandlerBase {
560542
}
561543

562544
if (entity?.[relationship]) {
563-
const serialized: any = await this.serializeItems(relationInfo.type, entity[relationship], modelMeta, {
545+
const serialized: any = await this.serializeItems(relationInfo.type, entity[relationship], {
564546
linkers: {
565547
document: new Linker(() =>
566548
this.makeLinkUrl(`/${type}/${resourceId}/relationships/${relationship}`)
@@ -582,8 +564,7 @@ class RequestHandler extends APIHandlerBase {
582564
private async processCollectionRead(
583565
prisma: DbClientContract,
584566
type: string,
585-
query: Record<string, string | string[]> | undefined,
586-
modelMeta: ModelMeta
567+
query: Record<string, string | string[]> | undefined
587568
): Promise<Response> {
588569
const typeInfo = this.typeMap[type];
589570
if (!typeInfo) {
@@ -633,7 +614,7 @@ class RequestHandler extends APIHandlerBase {
633614
if (limit === Infinity) {
634615
const entities = await prisma[type].findMany(args);
635616

636-
const body = await this.serializeItems(type, entities, modelMeta, { include });
617+
const body = await this.serializeItems(type, entities, { include });
637618
const total = entities.length;
638619
body.meta = this.addTotalCountToMeta(body.meta, total);
639620

@@ -656,7 +637,7 @@ class RequestHandler extends APIHandlerBase {
656637
paginator: this.makePaginator(url, offset, limit, total),
657638
},
658639
};
659-
const body = await this.serializeItems(type, entities, modelMeta, options);
640+
const body = await this.serializeItems(type, entities, options);
660641
body.meta = this.addTotalCountToMeta(body.meta, total);
661642

662643
return {
@@ -797,7 +778,7 @@ class RequestHandler extends APIHandlerBase {
797778
const entity = await prisma[type].create(createPayload);
798779
return {
799780
status: 201,
800-
body: await this.serializeItems(type, entity, modelMeta),
781+
body: await this.serializeItems(type, entity),
801782
};
802783
}
803784

@@ -808,8 +789,7 @@ class RequestHandler extends APIHandlerBase {
808789
resourceId: string,
809790
relationship: string,
810791
query: Record<string, string | string[]> | undefined,
811-
requestBody: unknown,
812-
modelMeta: ModelMeta
792+
requestBody: unknown
813793
): Promise<Response> {
814794
const typeInfo = this.typeMap[type];
815795
if (!typeInfo) {
@@ -894,7 +874,7 @@ class RequestHandler extends APIHandlerBase {
894874

895875
const entity: any = await prisma[type].update(updateArgs);
896876

897-
const serialized: any = await this.serializeItems(relationInfo.type, entity[relationship], modelMeta, {
877+
const serialized: any = await this.serializeItems(relationInfo.type, entity[relationship], {
898878
linkers: {
899879
document: new Linker(() => this.makeLinkUrl(`/${type}/${resourceId}/relationships/${relationship}`)),
900880
},
@@ -969,7 +949,7 @@ class RequestHandler extends APIHandlerBase {
969949
const entity = await prisma[type].update(updatePayload);
970950
return {
971951
status: 200,
972-
body: await this.serializeItems(type, entity, modelMeta),
952+
body: await this.serializeItems(type, entity),
973953
};
974954
}
975955

@@ -1134,12 +1114,7 @@ class RequestHandler extends APIHandlerBase {
11341114
}
11351115
}
11361116

1137-
private async serializeItems(
1138-
model: string,
1139-
items: unknown,
1140-
modelMeta: ModelMeta,
1141-
options?: Partial<SerializerOptions<any>>
1142-
) {
1117+
private async serializeItems(model: string, items: unknown, options?: Partial<SerializerOptions<any>>) {
11431118
model = lowerCaseFirst(model);
11441119
const serializer = this.serializers.get(model);
11451120
if (!serializer) {
@@ -1148,17 +1123,23 @@ class RequestHandler extends APIHandlerBase {
11481123

11491124
const typeInfo = this.typeMap[model];
11501125

1126+
let itemsWithId: any = items;
11511127
if (typeInfo.idFields.length > 1 && Array.isArray(items)) {
1152-
items = items.map((item: any) => {
1128+
itemsWithId = items.map((item: any) => {
11531129
return {
11541130
...item,
11551131
[this.makeIdKey(typeInfo.idFields)]: this.makeCompoundId(typeInfo.idFields, item),
11561132
};
11571133
});
1134+
} else if (typeInfo.idFields.length > 1 && typeof items === 'object') {
1135+
itemsWithId = {
1136+
...items,
1137+
[this.makeIdKey(typeInfo.idFields)]: this.makeCompoundId(typeInfo.idFields, items),
1138+
};
11581139
}
11591140

11601141
// serialize to JSON:API structure
1161-
const serialized = await serializer.serialize(items, options);
1142+
const serialized = await serializer.serialize(itemsWithId, options);
11621143

11631144
// convert the serialization result to plain object otherwise SuperJSON won't work
11641145
const plainResult = this.toPlainObject(serialized);
@@ -1219,7 +1200,7 @@ class RequestHandler extends APIHandlerBase {
12191200
return { [idFields[0].name]: this.coerce(idFields[0].type, resourceId) };
12201201
} else {
12211202
return {
1222-
[idFields.map((idf) => idf.name).join('_')]: idFields.reduce(
1203+
[idFields.map((idf) => idf.name).join(idDivider)]: idFields.reduce(
12231204
(acc, curr, idx) => ({
12241205
...acc,
12251206
[curr.name]: this.coerce(curr.type, resourceId.split(idDivider)[idx]),

0 commit comments

Comments
 (0)