Skip to content

Generate service method request body types as model types when not references #1343

Open
@rcbevans

Description

@rcbevans

The generated client service methods declare request body types inline as part of the method definition:

    public static patchUserMe({
        requestBody,
    }: {
        requestBody: {
            /**
             * The new name of the user
             */
            name?: string | null;
            ...
        },
    }): CancelablePromise<DetailedUser> {
        return __request(OpenAPI, {
            method: 'PATCH',
            url: '/user/me',
            body: requestBody,
            mediaType: 'application/json',
        });
    }

I am utilizing openapi-typescript-codgen to have a single source of truth for API definition, and so would like to pass the defined request body type to express as ReqBody generic params.

I am able to extract the types I was using some more typescript generics, but it would be great if the request bodies were declared as separately usable types for direct access with a naming convention like UserServicePatchUserMeRequestBody.

export type ClassMethodType<T, M extends keyof T> = T[M] extends Function
  ? T[M]
  : never;

export type RequestBody<T extends { requestBody: unknown }> = T['requestBody'];

export type PatchUserMeRequestBody = RequestBody<Params<ClassMethodType<UserService, 'patchUserMe'>[0]>

As a work around I think I could also declare the request body schema as a component then reference it, but since these bodies are typically only used for one path, I prefer having them declared in place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions