From 7f43345d5f01800651a51037d15835cf8606b07a Mon Sep 17 00:00:00 2001 From: Kelp Date: Fri, 17 Jun 2022 04:37:18 +0800 Subject: [PATCH] feat: don't remove empty string and null of query string value --- __tests__/__snapshots__/route.js.snap | 2 +- __tests__/route.js | 6 +++++- lib/route.js | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/__tests__/__snapshots__/route.js.snap b/__tests__/__snapshots__/route.js.snap index 50f9e8c..be69e9e 100644 --- a/__tests__/__snapshots__/route.js.snap +++ b/__tests__/__snapshots__/route.js.snap @@ -78,7 +78,7 @@ Route { } `; -exports[`Generate the URI of the route with params 1`] = `"/users/AWgrmJp1SjjuUM2bzZXM/projects?index=0&sort=asc"`; +exports[`Generate the URI of the route with params 1`] = `"/users/AWgrmJp1SjjuUM2bzZXM/projects?array=a&array=b&empty=&index=0&nullValue&sort=asc"`; exports[`Initial Route with a parent. 1`] = ` Route { diff --git a/__tests__/route.js b/__tests__/route.js index 82439f5..11d9463 100644 --- a/__tests__/route.js +++ b/__tests__/route.js @@ -68,7 +68,7 @@ test('Get an error on generating a route with a resolve key called "params".', ( test('Generate the URI of the route with params', () => { const fakeRoute = new Route({ name: 'web', - uri: '/users/{userId:[\\w-]{20}}/projects?index?sort', + uri: '/users/{userId:[\\w-]{20}}/projects?index?sort?nullValue?empty?array?undefinedValue', resolve: { user: ({userId}) => Promise.resolve({id: userId, name: 'User'}), projects: () => Promise.resolve([{id: 'AWgrmJp1SjjuUM2bzZXM', title: 'Project'}]), @@ -79,6 +79,10 @@ test('Generate the URI of the route with params', () => { userId: 'AWgrmJp1SjjuUM2bzZXM', index: 0, sort: 'asc', + nullValue: null, + empty: '', + undefinedValue: undefined, + array: ['a', 'b'], }); expect(uri).toMatchSnapshot(); }); diff --git a/lib/route.js b/lib/route.js index 315f353..b19d3f1 100644 --- a/lib/route.js +++ b/lib/route.js @@ -104,7 +104,7 @@ module.exports = class Route { const value = params[key]; if (uri.indexOf(`{${key}}`) >= 0) { uri = uri.replace(`{${key}}`, value); - } else if (this.uriParamKeys.indexOf(`?${key}`) >= 0 && (value || value === 0)) { + } else if (this.uriParamKeys.indexOf(`?${key}`) >= 0) { query[key] = value; } });