Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/http-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@basketry/typescript-http-client",
"version": "0.3.2",
"version": "0.3.3",
"description": "Basketry generator for generating Express JS routers",
"main": "./lib/index.js",
"bin": {
Expand Down
40 changes: 35 additions & 5 deletions packages/http-client/src/http-client-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,41 @@ class MethodFactory {
break;
}
case undefined: {
yield `query.push(\`${
httpParam.name.value
}=$\{encodeURIComponent(${this.accessor(param, {
includeOptionalChaining: false,
})})\}\`)`;
// style:deepObject — object-typed query params (types with properties or
// mapProperties) cannot be serialized with a single encodeURIComponent
// call. Expand as bracket notation:
// map type → paramName[key]=value (for each entry)
// object type → paramName[prop]=value (for each defined property)
// Enum and primitive complex values fall through to the standard path.
const deepObjectType =
param.value.kind === 'ComplexValue'
? getTypeByName(this.service, param.value.typeName.value)
: undefined;

if (deepObjectType?.mapProperties) {
const accessor = this.accessor(param, {
includeOptionalChaining: false,
});
yield `Object.entries(${accessor}).forEach(([key, value]) => {`;
yield ` query.push(\`${httpParam.name.value}[$\{encodeURIComponent(key)\}]=$\{encodeURIComponent(value)\}\`);`;
yield `});`;
} else if (deepObjectType?.properties?.length) {
const accessor = this.accessor(param, {
includeOptionalChaining: false,
});
for (const property of deepObjectType.properties) {
const propName = property.name.value;
yield `if (${accessor}.${propName} !== undefined) {`;
yield ` query.push(\`${httpParam.name.value}[${propName}]=$\{encodeURIComponent(${accessor}.${propName})\}\`);`;
yield `}`;
}
} else {
yield `query.push(\`${
httpParam.name.value
}=$\{encodeURIComponent(${this.accessor(param, {
includeOptionalChaining: false,
})})\}\`)`;
}
break;
}
default: {
Expand Down
Loading