Skip to content

Commit 1d60b72

Browse files
bayasdevmarkerikson
authored andcommitted
Add "encodeParams" option to rtkq-codegen-openapi
1 parent 259892d commit 1d60b72

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

packages/rtk-query-codegen-openapi/src/generate.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export async function generateApi(
8888
filterEndpoints,
8989
endpointOverrides,
9090
unionUndefined,
91+
encodeParams = false,
9192
flattenArg = false,
9293
useEnumType = false,
9394
mergeReadWriteOnly = false,
@@ -356,7 +357,7 @@ export async function generateApi(
356357
type: isQuery ? 'query' : 'mutation',
357358
Response: ResponseTypeName,
358359
QueryArg,
359-
queryFn: generateQueryFn({ operationDefinition, queryArg, isQuery, isFlatArg }),
360+
queryFn: generateQueryFn({ operationDefinition, queryArg, isQuery, isFlatArg, encodeParams }),
360361
extraEndpointsProps: isQuery
361362
? generateQueryEndpointProps({ operationDefinition })
362363
: generateMutationEndpointProps({ operationDefinition }),
@@ -369,11 +370,13 @@ export async function generateApi(
369370
queryArg,
370371
isFlatArg,
371372
isQuery,
373+
encodeParams,
372374
}: {
373375
operationDefinition: OperationDefinition;
374376
queryArg: QueryArgDefinitions;
375377
isFlatArg: boolean;
376378
isQuery: boolean;
379+
encodeParams: boolean;
377380
}) {
378381
const { path, verb } = operationDefinition;
379382

@@ -386,21 +389,22 @@ export async function generateApi(
386389
}
387390

388391
function createObjectLiteralProperty(parameters: QueryArgDefinition[], propertyName: string) {
389-
return parameters.length === 0
390-
? undefined
391-
: factory.createPropertyAssignment(
392-
factory.createIdentifier(propertyName),
393-
factory.createObjectLiteralExpression(
394-
parameters.map(
395-
(param) =>
396-
createPropertyAssignment(
397-
param.originalName,
398-
isFlatArg ? rootObject : accessProperty(rootObject, param.name)
399-
),
400-
true
401-
)
402-
)
403-
);
392+
if (parameters.length === 0) return undefined;
393+
394+
const properties = parameters.map((param) => {
395+
const value = isFlatArg ? rootObject : accessProperty(rootObject, param.name);
396+
return createPropertyAssignment(
397+
param.originalName,
398+
encodeParams && param.param?.in === 'query'
399+
? factory.createCallExpression(factory.createIdentifier('encodeURIComponent'), undefined, [value])
400+
: value
401+
);
402+
});
403+
404+
return factory.createPropertyAssignment(
405+
factory.createIdentifier(propertyName),
406+
factory.createObjectLiteralExpression(properties, true)
407+
);
404408
}
405409

406410
return factory.createArrowFunction(
@@ -416,7 +420,7 @@ export async function generateApi(
416420
[
417421
factory.createPropertyAssignment(
418422
factory.createIdentifier('url'),
419-
generatePathExpression(path, pickParams('path'), rootObject, isFlatArg)
423+
generatePathExpression(path, pickParams('path'), rootObject, isFlatArg, encodeParams)
420424
),
421425
isQuery && verb.toUpperCase() === 'GET'
422426
? undefined
@@ -463,7 +467,8 @@ function generatePathExpression(
463467
path: string,
464468
pathParameters: QueryArgDefinition[],
465469
rootObject: ts.Identifier,
466-
isFlatArg: boolean
470+
isFlatArg: boolean,
471+
encodeParams: boolean
467472
) {
468473
const expressions: Array<[string, string]> = [];
469474

@@ -479,14 +484,18 @@ function generatePathExpression(
479484
return expressions.length
480485
? factory.createTemplateExpression(
481486
factory.createTemplateHead(head),
482-
expressions.map(([prop, literal], index) =>
483-
factory.createTemplateSpan(
484-
isFlatArg ? rootObject : accessProperty(rootObject, prop),
487+
expressions.map(([prop, literal], index) => {
488+
const value = isFlatArg ? rootObject : accessProperty(rootObject, prop);
489+
const encodedValue = encodeParams
490+
? factory.createCallExpression(factory.createIdentifier('encodeURIComponent'), undefined, [value])
491+
: value;
492+
return factory.createTemplateSpan(
493+
encodedValue,
485494
index === expressions.length - 1
486495
? factory.createTemplateTail(literal)
487496
: factory.createTemplateMiddle(literal)
488-
)
489-
)
497+
);
498+
})
490499
)
491500
: factory.createNoSubstitutionTemplateLiteral(head);
492501
}

packages/rtk-query-codegen-openapi/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export interface CommonOptions {
6262
* @see https://redux-toolkit.js.org/rtk-query/usage/code-generation for more information
6363
*/
6464
tag?: boolean;
65+
/**
66+
* defaults to false
67+
* `true` will generate add `encodeURIComponent` to the generated query params
68+
*/
69+
encodeParams?: boolean;
6570
/**
6671
* defaults to false
6772
* `true` will "flatten" the arg so that you can do things like `useGetEntityById(1)` instead of `useGetEntityById({ entityId: 1 })`

0 commit comments

Comments
 (0)