feat(codegen): add operationIdTransformer option#5236
feat(codegen): add operationIdTransformer option#5236aryaemami59 wants to merge 1 commit intoreduxjs:masterfrom
operationIdTransformer option#5236Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit be58ecb:
|
Co-authored-by: Issy Szemeti <48881813+issy@users.noreply.github.com>
fa9c5f7 to
be58ecb
Compare
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
I'm glad this is gaining traction! Thanks for the acknowledgment. I like this idea |
Glad to hear it, and thanks for the original PR. It's what got the ball rolling on this! The function form already lets you plug in any transform today (e.g. a |
Summary
operationIdTransformerconfig option to@rtk-query/codegen-openapithat controls how OpenAPIoperationIdvalues are transformed into endpoint names.Motivation
@rtk-query/codegen-openapicurrently passes everyoperationIdthroughoazapfts, which applies lodash'scamelCase.This mangles consecutive uppercase acronyms.
For example,
fetchMyJWTPleasebecomesfetchMyJwtPlease, making it impossible to match the originaloperationIdexactly.This causes two categories of pain:
filterEndpointsbreaks on acronym-heavy names. If your schema hasoperationId: fetchMyJWTPlease, you must filter by'fetchMyJwtPlease'(the mangled form) even though the schema saysfetchMyJWTPlease. This is surprising and easy to get wrong. See remove implicit conversion to camel case of openapi operation id. #4322.operationIdas a stable identifier (e.g. for analytics, logging, or cross-service contracts) find the camelCase conversion breaks their assumptions. See RTK-Query Codegen: Camel case applied incorrectly in some situations #2181.Solution
operationIdTransformer?: 'camelCase' | 'none' | ((operationId: string) => string)toCommonOptions.'camelCase'(default)camelCaseviaoazapfts. Fully backwards-compatible.'none'operationIdverbatim, preserving exact casing from the schema.(operationId: string) => stringWhen
'none'or a function is used, every operation must have anoperationIddefined. The codegen throws an informative error if any operation is missing one.filterEndpointsis always matched against the transformed name, consistent regardless of which transformer is active.Usage
Preserve exact casing.
fetchMyJWTPleasestaysfetchMyJWTPlease:Custom transform:
Related
exactOperationIdsoption togenerateEndpointsconfig #5174.PR #5174 by @issy proposed a
booleanexactOperationIdsflag. Abooleanis simpler to configure but can only toggle between two fixed behaviors.operationIdTransformeris a strict superset: passing'none'is equivalent toexactOperationIds: true, while the function form covers everything else. The implementation PR takes direct inspiration from #5174's approach and @issy will be added as a co-author of the commit.