acacode / swagger-typescript-api

Generate the API Client for Fetch or Axios from an OpenAPI Specification
MIT License
3.29k stars 356 forks source link

Question about Axios template #409

Open mxmory opened 2 years ago

mxmory commented 2 years ago

Generated axios request template:

Screenshot 2022-09-07 at 23 28 24

I have 2 different schemas for request and response:

Request schema: (id and color are optional to create this entity)

StageModel {
  id?: number;
  name: string;
  color?: string;
  ...
  ...
}

Response schema: (no optional fields because I'm sure they will be presented in response and don't want typescript to complain like 'id' is possibly undefined in future use in my react components)

StageModelResponse {
  id: number;
  name: string;
  color: string;
  ...
  ...
}

So the question is: can I generate different types and put them to axios request and response respectively? Or maybe I'm doing this all wrong and this can be achieved in more correct/elegant way? Someone help please! Thanks in advance 🙏

Liwoj commented 1 year ago

imho T represents response type. Request doesn't need to be a generic type as it is passed only to a generated action method where it is transformed into FullRequestParams. See an example below:

authorizationAuthorizeClient: (cisId: string, params: RequestParams = {}) =>
      this.request<ClientAuthorizationResponseModel, any>({
        path: `/auth/client/${cisId}`,
        method: "POST",
        format: "json",
        ...params,
      }),