Skip to content

Commit b0de9c3

Browse files
Make idDivider configurable
1 parent 4fc4cf7 commit b0de9c3

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ const urlPatterns = {
3333
relationship: new UrlPattern('/:type/:id/relationships/:relationship'),
3434
};
3535

36-
export const idDivider = '_';
37-
3836
/**
3937
* Request handler options
4038
*/
@@ -52,6 +50,12 @@ export type Options = {
5250
* Defaults to 100. Set to Infinity to disable pagination.
5351
*/
5452
pageSize?: number;
53+
54+
/**
55+
* The divider used to separate compound ID fields in the URL.
56+
* Defaults to '_'.
57+
*/
58+
idDivider?: string;
5559
};
5660

5761
type RelationshipInfo = {
@@ -209,9 +213,11 @@ class RequestHandler extends APIHandlerBase {
209213

210214
// all known types and their metadata
211215
private typeMap: Record<string, ModelInfo>;
216+
public idDivider;
212217

213218
constructor(private readonly options: Options) {
214219
super();
220+
this.idDivider = options.idDivider ?? '_';
215221
}
216222

217223
async handleRequest({
@@ -1110,7 +1116,7 @@ class RequestHandler extends APIHandlerBase {
11101116
if (ids.length === 0) {
11111117
return undefined;
11121118
} else {
1113-
return data[ids.map((id) => id.name).join(idDivider)];
1119+
return data[ids.map((id) => id.name).join(this.idDivider)];
11141120
}
11151121
}
11161122

@@ -1211,10 +1217,10 @@ class RequestHandler extends APIHandlerBase {
12111217
return { [idFields[0].name]: this.coerce(idFields[0].type, resourceId) };
12121218
} else {
12131219
return {
1214-
[idFields.map((idf) => idf.name).join(idDivider)]: idFields.reduce(
1220+
[idFields.map((idf) => idf.name).join(this.idDivider)]: idFields.reduce(
12151221
(acc, curr, idx) => ({
12161222
...acc,
1217-
[curr.name]: this.coerce(curr.type, resourceId.split(idDivider)[idx]),
1223+
[curr.name]: this.coerce(curr.type, resourceId.split(this.idDivider)[idx]),
12181224
}),
12191225
{}
12201226
),
@@ -1230,11 +1236,11 @@ class RequestHandler extends APIHandlerBase {
12301236
}
12311237

12321238
private makeIdKey(idFields: FieldInfo[]) {
1233-
return idFields.map((idf) => idf.name).join(idDivider);
1239+
return idFields.map((idf) => idf.name).join(this.idDivider);
12341240
}
12351241

12361242
private makeCompoundId(idFields: FieldInfo[], item: any) {
1237-
return idFields.map((idf) => item[idf.name]).join(idDivider);
1243+
return idFields.map((idf) => item[idf.name]).join(this.idDivider);
12381244
}
12391245

12401246
private includeRelationshipIds(model: string, args: any, mode: 'select' | 'include') {

packages/server/tests/api/rest.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { CrudFailureReason, type ModelMeta } from '@zenstackhq/runtime';
55
import { loadSchema, run } from '@zenstackhq/testtools';
66
import { Decimal } from 'decimal.js';
77
import SuperJSON from 'superjson';
8-
import makeHandler, { idDivider } from '../../src/api/rest';
8+
import makeHandler from '../../src/api/rest';
9+
10+
const idDivider = '_';
911

1012
describe('REST server tests', () => {
1113
let prisma: any;
@@ -83,7 +85,7 @@ describe('REST server tests', () => {
8385
zodSchemas = params.zodSchemas;
8486
modelMeta = params.modelMeta;
8587

86-
const _handler = makeHandler({ endpoint: 'http://localhost/api', pageSize: 5 });
88+
const _handler = makeHandler({ endpoint: 'http://localhost/api', pageSize: 5, idDivider });
8789
handler = (args) =>
8890
_handler({ ...args, zodSchemas, modelMeta, url: new URL(`http://localhost/${args.path}`) });
8991
});

0 commit comments

Comments
 (0)