rtk-query-codegen
rtk-query-codegen copied to clipboard
Provide a way to specify the generated names
Love the generator, it is speeding up my workflow like crazy, so first off - thanks for that.
I'm using a .NET Core api, that provides me with nice OpenApi spec docs, that work perfectly with the codegenerator, however:
The names it generates for types, args, and endpoints is a bit verbose.
I've got an api defined as following:
{
"/api/v1/Organization/{organizationId}/Departments": {
"get": {
"tags": [
"Organization"
],
"parameters": [
{
"name": "organizationId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/DepartmentInfoDto"
}
}
}
}
},
"401": {
"description": "Unauthorized",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
}
},
"security": [
{
"Azure_Active_Directory_B2C": [
"https://xxxx.onmicrosoft.com/yyyy/zzzz"
]
}
]
}
},
}
This resolves to:
Hook: useGetApiV1OrganizationByOrganizationIdDepartmentsQuery
Endpoint:
getApiV1OrganizationByOrganizationIdDepartments: build.query<
GetApiV1OrganizationByOrganizationIdDepartmentsApiResponse,
GetApiV1OrganizationByOrganizationIdDepartmentsApiArg
>({
query: (queryArg) => ({ url: `/api/v1/Organization/${queryArg.organizationId}/Departments` }),
})
As you can see these names are bit verbose, and I'd like more control over them without having to change my Api endpoints. Perhaps it's possible to override part of the generated name? I like the prefix/suffix options, but those won't do (unless you want an even longer string).
I'd like to be able to either provide a custom function for naming. In this case I'd like to be able to transform getApiV1OrganizationByOrganizationIdDepartments
into getOrganizationDepartments
I'm having a similar problem for a FastApi generated OpenApi schema.
Hmm. I would think about it two ways:
- either allow the schema to contain metadata on how an endpoint should be named. This would be cleaner but probably not everyone would have access to that
- create some kind of
endpointNameMapping.json
file that looks like
{
"getApiV1OrganizationByOrganizationIdDepartments": "getOrganization",
"getApiV2OrganizationByOrganizationIdDepartments": false
}
Where the false
would cause endpoints to be skipped in generation
@phryneas I would prefer option two her - preferably integrated in a configuration file.
Hmm. I would think about it two ways:
- either allow the schema to contain metadata on how an endpoint should be named. This would be cleaner but probably not everyone would have access to that
- create some kind of
endpointNameMapping.json
file that looks like{ "getApiV1OrganizationByOrganizationIdDepartments": "getOrganization", "getApiV2OrganizationByOrganizationIdDepartments": false }
Where the
false
would cause endpoints to be skipped in generation
I would also opt for the second option. That would really help as right now I'm basically re-exporting them myself with pretty names, but the 'old' hook-names are still available! The first option won't do, as many times we're forced to consume a 3rd party api, so we're unable to change those api's
Isn't "first" option already supported with operationId
?
(uses getOperationName
from oazapfts
)
https://github.com/cellular/oazapfts/blob/master/src/codegen/generate.ts#L49
Same here.
My OpenApi3.json about 15k rows, and i got the result:
``json
delete15: build.mutation<Delete15ApiResponse, Delete15ApiArg>({
query: (queryArg) => ({
url: `/deviceparam/${queryArg.id}`,
method: "DELETE",
}),
}),
getAll5: build.query<GetAll5ApiResponse, GetAll5ApiArg>({
query: () => ({ url: `/tsoperations` }),
}),
create25: build.mutation<Create25ApiResponse, Create25ApiArg>({
query: (queryArg) => ({
url: `/tsoperations`,
method: "POST",
body: queryArg.tsOperation,
}),
}),
setDelete: build.mutation<SetDeleteApiResponse, SetDeleteApiArg>({
query: (queryArg) => ({
url: `/datasource/delete/${queryArg.id}/${queryArg.state}`,
method: "PUT",
}),
}),
getById11: build.query<GetById11ApiResponse, GetById11ApiArg>({
query: (queryArg) => ({
url: `/tsvalue/time/${queryArg.meastime}/meta/${queryArg.meta}`,
}),
}),
delete6: build.mutation<Delete6ApiResponse, Delete6ApiArg>({
query: (queryArg) => ({
url: `/tsvalue/time/${queryArg.meastime}/meta/${queryArg.meta}`,
method: "DELETE",
}),
}),
create8: build.mutation<Create8ApiResponse, Create8ApiArg>({
query: (queryArg) => ({
url: `/entitytype`,
method: "POST",
body: queryArg.vocEntityType,
}),
}),
``
i can't use this ;(
-- second problem
if PathVariable does not match it throw an error, but not really informative
``json
"/auth/keycloak/{role}/users1": {
"get": {
"tags": ["security-rest-controller"],
"operationId": "roleUsers1",
"parameters": [
{
"name": "roleName",
"in": "path",
"required": true,
"schema": { "type": "string" }
}
],
"responses": {
"200": {
"description": "default response",
"content": {
"application/json": { "schema": { "type": "object" } }
}
}
}
}
},
`` if {role} does not match "name": "roleName", got the error
Error: path parameter role does not seem to be defined?
ofc i have found ) that type on 10k row... but in manual mode ;)