diff --git a/src/Path.ts b/src/Path.ts index 9b518c3..a34c0fc 100644 --- a/src/Path.ts +++ b/src/Path.ts @@ -30,6 +30,12 @@ const upToDelimiter = (source: string, delimiter?: boolean) => { return /(\/)$/.test(source) ? source : source + '(\\/|\\?|\\.|;|$)' } +const encodeSpatParam = (value: string) => + value + .split('/') + .map(encodeURIComponent) + .join('/') + const appendQueryParam = ( params: Record, param: string, @@ -119,6 +125,10 @@ export class Path { return this.queryParams.indexOf(name) !== -1 } + public isSpatParam(name: string): boolean { + return this.spatParams.indexOf(name) !== -1 + } + public test(path: string, opts?: ITestOptions): TestMatch { const options = { strictTrailingSlash: false, queryParams: {}, ...opts } // trailingSlash: falsy => non optional, truthy => optional @@ -190,7 +200,11 @@ export class Path { } const val = params[key] - const encode = this.isQueryParam(key) ? identity : encodeURI + const encode = this.isQueryParam(key) + ? identity + : this.isSpatParam(key) + ? encodeSpatParam + : encodeURIComponent if (typeof val === 'boolean') { acc[key] = val diff --git a/test/main.test.ts b/test/main.test.ts index 993dce9..cf4cea9 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -260,7 +260,7 @@ describe('Path', function() { expect(path.partialTest('/test/%7B123-456%7D')).toEqual({ id: '{123-456}' }) }) - it('should encoded values and build paths', function() { + it('should encode values and build paths', function() { const path = new Path('/test/:id') expect(path.build({ id: '{123-456}' })).toBe('/test/%7B123-456%7D') @@ -280,7 +280,6 @@ describe('Path', function() { expect(path.partialTest('/test/he:re/test2')).toEqual({ name: 'he:re' }) expect(path.partialTest("/test/he're/test2")).toEqual({ name: "he're" }) - expect(path.build({ name: 'he:re' })).toEqual('/test/he:re/test2') expect(path.build({ name: "he're" })).toEqual("/test/he're/test2") })