mirror of
https://github.com/zenstackhq/zenstack
synced 2026-05-24 10:08:55 +00:00
Expose urlSegmentNameCharSet option
This commit is contained in:
parent
7c5940b47a
commit
c002fa247f
2 changed files with 19 additions and 5 deletions
|
|
@ -45,6 +45,12 @@ export type Options = {
|
|||
* Defaults to '_'.
|
||||
*/
|
||||
idDivider?: string;
|
||||
|
||||
/**
|
||||
* The charset used for URL segment values. Defaults to `a-zA-Z0-9-_~ %`.
|
||||
* If the idDivider is set to a different value, it should be included in the charset.
|
||||
*/
|
||||
urlSegmentNameCharset?: string;
|
||||
};
|
||||
|
||||
type RelationshipInfo = {
|
||||
|
|
@ -202,18 +208,21 @@ class RequestHandler extends APIHandlerBase {
|
|||
|
||||
// all known types and their metadata
|
||||
private typeMap: Record<string, ModelInfo>;
|
||||
public idDivider;
|
||||
|
||||
// divider used to separate compound ID fields
|
||||
private idDivider;
|
||||
|
||||
private urlPatterns;
|
||||
|
||||
constructor(private readonly options: Options) {
|
||||
super();
|
||||
this.idDivider = options.idDivider ?? '_';
|
||||
this.urlPatterns = this.buildUrlPatterns(this.idDivider);
|
||||
const segmentCharset = options.urlSegmentNameCharset ?? 'a-zA-Z0-9-_~ %';
|
||||
this.urlPatterns = this.buildUrlPatterns(this.idDivider, segmentCharset);
|
||||
}
|
||||
|
||||
buildUrlPatterns(idDivider: string) {
|
||||
const options = { segmentValueCharset: `a-zA-Z0-9-_~ %${idDivider}` };
|
||||
buildUrlPatterns(idDivider: string, urlSegmentNameCharset: string) {
|
||||
const options = { segmentValueCharset: urlSegmentNameCharset };
|
||||
return {
|
||||
// collection operations
|
||||
collection: new UrlPattern('/:type', options),
|
||||
|
|
|
|||
|
|
@ -2549,7 +2549,12 @@ describe('REST server tests', () => {
|
|||
zodSchemas = params.zodSchemas;
|
||||
modelMeta = params.modelMeta;
|
||||
|
||||
const _handler = makeHandler({ endpoint: 'http://localhost/api', pageSize: 5, idDivider });
|
||||
const _handler = makeHandler({
|
||||
endpoint: 'http://localhost/api',
|
||||
pageSize: 5,
|
||||
idDivider,
|
||||
urlSegmentNameCharset: 'a-zA-Z0-9-_~ %@.',
|
||||
});
|
||||
handler = (args) =>
|
||||
_handler({ ...args, zodSchemas, modelMeta, url: new URL(`http://localhost/${args.path}`) });
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue