Skip to content

Commit

Permalink
fix(request): discard only null or undefined in query string
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Mar 20, 2024
1 parent f6da115 commit 70c3402
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 129 deletions.
21 changes: 2 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
"lint": "eslint .",
"prepublishOnly": "npm run build",
"test": "vitest run --config vitest.config.unit.ts",
"test:update": "vitest run --config vitest.config.unit.ts --update",
"test:coverage": "vitest run --config vitest.config.unit.ts --coverage",
"test:watch": "vitest --config vitest.config.unit.ts --watch",
"test:e2e": "vitest run --config vitest.config.e2e.ts",
"test:sample": "node test/sample.cjs",
"test:update": "vitest run --config vitest.config.unit.ts --update",
"test:watch": "vitest --config vitest.config.unit.ts --watch",
"typecheck": "tsc --noEmit"
},
"engines": {
Expand Down Expand Up @@ -90,7 +90,6 @@
"@types/cross-spawn": "6.0.6",
"@types/express": "4.17.21",
"@types/node": "20.11.30",
"@types/qs": "6.9.13",
"@typescript-eslint/eslint-plugin": "7.3.1",
"@typescript-eslint/parser": "7.3.1",
"@vitest/coverage-v8": "1.4.0",
Expand All @@ -106,16 +105,14 @@
"npm-run-all2": "6.1.2",
"prettier": "3.2.5",
"puppeteer": "22.5.0",
"qs": "6.12.0",
"rimraf": "5.0.5",
"rollup": "4.13.0",
"rollup-plugin-dts": "6.1.0",
"rxjs": "7.8.1",
"ts-node": "10.9.2",
"tslib": "2.6.2",
"typescript": "5.4.2",
"vitest": "1.4.0",
"zone.js": "0.14.4"
"vitest": "1.4.0"
},
"packageManager": "npm@10.5.0+sha256.17ca6e08e7633b624e8f870db81a78f46afe119de62bcaf0a7407574139198fc"
}
34 changes: 13 additions & 21 deletions src/templates/core/functions/getQueryString.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,21 @@ export const getQueryString = (params: Record<string, unknown>): string => {
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
};

const process = (key: string, value: unknown) => {
if (value) {
if (Array.isArray(value)) {
value.forEach(v => {
process(key, v);
});
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => {
process(`${key}[${k}]`, v);
});
} else {
append(key, value);
}
const encodePair = (key: string, value: unknown) => {
if (value === undefined || value === null) {
return;
}
};

Object.entries(params).forEach(([key, value]) => {
process(key, value);
});
if (Array.isArray(value)) {
value.forEach(v => encodePair(key, v));
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v));
} else {
append(key, value);
}
};

if (qs.length > 0) {
return `?${qs.join('&')}`;
}
Object.entries(params).forEach(([key, value]) => encodePair(key, value));

return '';
return qs.length ? `?${qs.join('&')}` : '';
};
7 changes: 2 additions & 5 deletions src/templates/core/functions/getUrl.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
return substring;
});

const url = `${config.BASE}${path}`;
if (options.query) {
return `${url}${getQueryString(options.query)}`;
}
return url;
const url = config.BASE + path;
return options.query ? url + getQueryString(options.query) : url;
};
123 changes: 45 additions & 78 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -283,31 +283,23 @@ export const getQueryString = (params: Record<string, unknown>): string => {
qs.push(\`\${encodeURIComponent(key)}=\${encodeURIComponent(String(value))}\`);
};

const process = (key: string, value: unknown) => {
if (value) {
if (Array.isArray(value)) {
value.forEach(v => {
process(key, v);
});
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => {
process(\`\${key}[\${k}]\`, v);
});
} else {
append(key, value);
}
const encodePair = (key: string, value: unknown) => {
if (value === undefined || value === null) {
return;
}

if (Array.isArray(value)) {
value.forEach(v => encodePair(key, v));
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => encodePair(\`\${key}[\${k}]\`, v));
} else {
append(key, value);
}
};

Object.entries(params).forEach(([key, value]) => {
process(key, value);
});
Object.entries(params).forEach(([key, value]) => encodePair(key, value));

if (qs.length > 0) {
return \`?\${qs.join('&')}\`;
}

return '';
return qs.length ? \`?\${qs.join('&')}\` : '';
};

const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
Expand All @@ -322,11 +314,8 @@ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
return substring;
});

const url = \`\${config.BASE}\${path}\`;
if (options.query) {
return \`\${url}\${getQueryString(options.query)}\`;
}
return url;
const url = config.BASE + path;
return options.query ? url + getQueryString(options.query) : url;
};

export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
Expand Down Expand Up @@ -3261,31 +3250,23 @@ export const getQueryString = (params: Record<string, unknown>): string => {
qs.push(\`\${encodeURIComponent(key)}=\${encodeURIComponent(String(value))}\`);
};

const process = (key: string, value: unknown) => {
if (value) {
if (Array.isArray(value)) {
value.forEach(v => {
process(key, v);
});
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => {
process(\`\${key}[\${k}]\`, v);
});
} else {
append(key, value);
}
const encodePair = (key: string, value: unknown) => {
if (value === undefined || value === null) {
return;
}
};

Object.entries(params).forEach(([key, value]) => {
process(key, value);
});
if (Array.isArray(value)) {
value.forEach(v => encodePair(key, v));
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => encodePair(\`\${key}[\${k}]\`, v));
} else {
append(key, value);
}
};

if (qs.length > 0) {
return \`?\${qs.join('&')}\`;
}
Object.entries(params).forEach(([key, value]) => encodePair(key, value));

return '';
return qs.length ? \`?\${qs.join('&')}\` : '';
};

const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
Expand All @@ -3300,11 +3281,8 @@ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
return substring;
});

const url = \`\${config.BASE}\${path}\`;
if (options.query) {
return \`\${url}\${getQueryString(options.query)}\`;
}
return url;
const url = config.BASE + path;
return options.query ? url + getQueryString(options.query) : url;
};

export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
Expand Down Expand Up @@ -8352,31 +8330,23 @@ export const getQueryString = (params: Record<string, unknown>): string => {
qs.push(\`\${encodeURIComponent(key)}=\${encodeURIComponent(String(value))}\`);
};

const process = (key: string, value: unknown) => {
if (value) {
if (Array.isArray(value)) {
value.forEach(v => {
process(key, v);
});
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => {
process(\`\${key}[\${k}]\`, v);
});
} else {
append(key, value);
}
const encodePair = (key: string, value: unknown) => {
if (value === undefined || value === null) {
return;
}
};

Object.entries(params).forEach(([key, value]) => {
process(key, value);
});
if (Array.isArray(value)) {
value.forEach(v => encodePair(key, v));
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => encodePair(\`\${key}[\${k}]\`, v));
} else {
append(key, value);
}
};

if (qs.length > 0) {
return \`?\${qs.join('&')}\`;
}
Object.entries(params).forEach(([key, value]) => encodePair(key, value));

return '';
return qs.length ? \`?\${qs.join('&')}\` : '';
};

const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
Expand All @@ -8391,11 +8361,8 @@ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
return substring;
});

const url = \`\${config.BASE}\${path}\`;
if (options.query) {
return \`\${url}\${getQueryString(options.query)}\`;
}
return url;
const url = config.BASE + path;
return options.query ? url + getQueryString(options.query) : url;
};

export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
Expand Down

0 comments on commit 70c3402

Please sign in to comment.