Skip to content

Commit c002fa2

Browse files
Expose urlSegmentNameCharSet option
1 parent 7c5940b commit c002fa2

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export type Options = {
4545
* Defaults to '_'.
4646
*/
4747
idDivider?: string;
48+
49+
/**
50+
* The charset used for URL segment values. Defaults to `a-zA-Z0-9-_~ %`.
51+
* If the idDivider is set to a different value, it should be included in the charset.
52+
*/
53+
urlSegmentNameCharset?: string;
4854
};
4955

5056
type RelationshipInfo = {
@@ -202,18 +208,21 @@ class RequestHandler extends APIHandlerBase {
202208

203209
// all known types and their metadata
204210
private typeMap: Record<string, ModelInfo>;
205-
public idDivider;
211+
212+
// divider used to separate compound ID fields
213+
private idDivider;
206214

207215
private urlPatterns;
208216

209217
constructor(private readonly options: Options) {
210218
super();
211219
this.idDivider = options.idDivider ?? '_';
212-
this.urlPatterns = this.buildUrlPatterns(this.idDivider);
220+
const segmentCharset = options.urlSegmentNameCharset ?? 'a-zA-Z0-9-_~ %';
221+
this.urlPatterns = this.buildUrlPatterns(this.idDivider, segmentCharset);
213222
}
214223

215-
buildUrlPatterns(idDivider: string) {
216-
const options = { segmentValueCharset: `a-zA-Z0-9-_~ %${idDivider}` };
224+
buildUrlPatterns(idDivider: string, urlSegmentNameCharset: string) {
225+
const options = { segmentValueCharset: urlSegmentNameCharset };
217226
return {
218227
// collection operations
219228
collection: new UrlPattern('/:type', options),

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2549,7 +2549,12 @@ describe('REST server tests', () => {
25492549
zodSchemas = params.zodSchemas;
25502550
modelMeta = params.modelMeta;
25512551

2552-
const _handler = makeHandler({ endpoint: 'http://localhost/api', pageSize: 5, idDivider });
2552+
const _handler = makeHandler({
2553+
endpoint: 'http://localhost/api',
2554+
pageSize: 5,
2555+
idDivider,
2556+
urlSegmentNameCharset: 'a-zA-Z0-9-_~ %@.',
2557+
});
25532558
handler = (args) =>
25542559
_handler({ ...args, zodSchemas, modelMeta, url: new URL(`http://localhost/${args.path}`) });
25552560
});

0 commit comments

Comments
 (0)