Skip to content

Generate request params as particular type #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
indapublic opened this issue Apr 9, 2021 · 3 comments · Fixed by #245
Closed

Generate request params as particular type #233

indapublic opened this issue Apr 9, 2021 · 3 comments · Fixed by #245
Assignees

Comments

@indapublic
Copy link

Hello!

I'm using this command for generate npx swagger-typescript-api -p public/swagger/v2.json -o ./assets/v2/src/api -n apiClient.ts --type-prefix=T

and I have this route in generated output.

      data: {
        firstName?: string;
        middleName?: string | null;
        lastName?: string;
        birthday?: string;
        gender?: TGender;
        phones?: string[];
        addresses?: TAddress[];
        contacts?: TContact[];
        avatar?: string | null;
        timezone?: string;
      },
      params: RequestParams = {},
    ) =>
      this.request<void, void>({
        path: `/user/profile`,
        method: "PUT",
        body: data,
        secure: true,
        type: ContentType.Json,
        ...params,
      }),

Is it possible to have particular interface with request type, e.g.

        middleName?: string | null;
        lastName?: string;
        birthday?: string;
        gender?: TGender;
        phones?: string[];
        addresses?: TAddress[];
        contacts?: TContact[];
        avatar?: string | null;
        timezone?: string;

Tried --extract-request-params and --route-types but no luck

@js2me js2me self-assigned this Apr 23, 2021
@js2me js2me linked a pull request Apr 23, 2021 that will close this issue
@js2me
Copy link
Member

js2me commented Apr 28, 2021

Next release will have --extract-request-body option

@indapublic
Copy link
Author

Thanks @js2me

But seems like this feature doesn't use prefix.

Command:

npx swagger-typescript-api -p public/swagger.json -o ./assets/app/api -n apiClient.ts --type-prefix=T --extract-request-params --extract-request-body --route-types

Generated file contains correct declaration with prefix needed (T in my example):

export interface TAppControllerApiControllerPostRequestActionPayload {
  goods: TRequest[];
}

but lines below are looking for AppControllerApiControllerPostRequestActionPayload, without prefix:

export namespace AppControllerApiControllerPostRequestAction {
    export type RequestParams = {};
    export type RequestQuery = {};
    export type RequestBody = AppControllerApiControllerPostRequestActionPayload;
    export type RequestHeaders = {};
    export type ResponseBody = { uid: string; code: string };
  }
appControllerApiControllerPostRequestAction: (
      data: AppControllerApiControllerPostRequestActionPayload,
      params: RequestParams = {},
    ) =>
      this.request<{ uid: string; code: string }, any>({
        path: `/requests`,
        method: "GET",
        body: data,
        type: ContentType.Json,
        format: "json",
        ...params,
      }),

@js2me js2me reopened this Apr 29, 2021
@indapublic
Copy link
Author

Checked it with recent version and it works like a charm. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants