Skip to content

Instantly share code, notes, and snippets.

@harada-shota
Last active December 2, 2021 03:16
Show Gist options
  • Save harada-shota/caad6315467e67415a3052a5e2d36b85 to your computer and use it in GitHub Desktop.
Save harada-shota/caad6315467e67415a3052a5e2d36b85 to your computer and use it in GitHub Desktop.
// .msw/openAPISpec.ts
export const endpoints = [
"/sample-endpoint",
] as const;
export type Endpoints = typeof endpoints[number];
export type Methods = "get" | "post" | "put" | "delete";
type ExampleKindAndResponseType = {
"/sample-endpoint": {
get: {
responseType: SampleEndpointResponse;
exampleKind:
| "example1"
| "example2"
};
put: never;
post: never;
delete: never;
};
};
export type ExampleKind<
Path extends Endpoints,
Method extends Methods
> = ExampleKindAndResponseType[Path][Method]["exampleKind"];
export type ResponseType<
Path extends Endpoints,
Method extends Methods
> = ExampleKindAndResponseType[Path][Method]["responseType"];
export type OpenAPISpec = {
paths: {
[Path in Endpoints]: {
[Method in Methods]: {
description: string;
operationId: string;
parameters: Record<string, unknown>[];
tags: string[];
responses: {
[status: string]: {
description: string;
content: {
"application/json": {
schema: Record<string, unknown>;
example?: ResponseType<Path, Method>;
examples?: {
[Example in ExampleKind<Path, Method>]: {
value: ResponseType<Path, Method>;
};
};
};
};
};
};
};
};
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment