rtk-query-codegen icon indicating copy to clipboard operation
rtk-query-codegen copied to clipboard

Provide a way to specify the generated names

Open las3r opened this issue 3 years ago • 6 comments

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

las3r avatar Jun 07 '21 22:06 las3r

I'm having a similar problem for a FastApi generated OpenApi schema.

StefanBRas avatar Jun 10 '21 15:06 StefanBRas

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 avatar Jun 10 '21 15:06 phryneas

@phryneas I would prefer option two her - preferably integrated in a configuration file.

StefanBRas avatar Jun 10 '21 17:06 StefanBRas

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

las3r avatar Jun 13 '21 07:06 las3r

Isn't "first" option already supported with operationId?

(uses getOperationName from oazapfts) https://github.com/cellular/oazapfts/blob/master/src/codegen/generate.ts#L49

pomali avatar Jun 14 '21 17:06 pomali

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 ;)

Evgenyx82 avatar Jul 21 '21 14:07 Evgenyx82