zimicjs / zimic

TypeScript-first HTTP request mocking
https://npmjs.com/package/zimic
MIT License
9 stars 2 forks source link

feat(#zimic): support non-numeric openapi status codes (#272) #274

Closed diego-aquino closed 3 months ago

diego-aquino commented 3 months ago

Features

As an example:

paths:
  /users-with-non-numeric-status-codes:
    post:
      responses:
        '1xx':
          description: Informational
        '100':
          description: Continue
        '201':
          $ref: '#/components/responses/userCreated'
        '2Xx':
          description: Success
        '400':
          $ref: '#/components/responses/validationError'
        '404':
          $ref: '#/components/responses/notFoundError'
        '4xX':
          $ref: '#/components/responses/clientError'
        '503':
          description: Service Unavailable
        '5XX':
          $ref: '#/components/responses/serverError'
        default:
          $ref: '#/components/responses/error'
        unknown:
          description: Unknown status code

is generated as:

export type MyServiceSchema = HttpSchema.Paths<{
  '/users-with-non-numeric-status-codes': {
    POST: {
      response: MergeHttpResponsesByStatusCode<
        [
          {
            100: {};
            201: MyServiceComponents['responses']['userCreated'];
            400: MyServiceComponents['responses']['validationError'];
            404: MyServiceComponents['responses']['notFoundError'];
            503: {};
          },
          { [StatusCode in HttpStatusCode.Information]: {} },
          { [StatusCode in HttpStatusCode.Success]: {} },
          { [StatusCode in HttpStatusCode.ClientError]: MyServiceComponents['responses']['clientError'] },
          { [StatusCode in HttpStatusCode.ServerError]: MyServiceComponents['responses']['serverError'] },
          { [StatusCode in HttpStatusCode]: MyServiceComponents['responses']['error'] },
        ]
      >;
    };
  };
}

MergeHttpResponsesByStatusCode takes a tuple of responses by status code types and returns a merged type, where the first declared status code is considered, ignoring the same codes appearing in later types. An alternative mental model is that the types are iterated from end to start of the tuple and override the result type with their status codes.

Closes #272.

github-actions[bot] commented 3 months ago

Released in v0.8.0 :tada: