Skip to content

Instantly share code, notes, and snippets.

@theoparis
Last active December 29, 2020 04:58
Show Gist options
  • Save theoparis/d390d04415b4767cdbe00ddb7deeece7 to your computer and use it in GitHub Desktop.
Save theoparis/d390d04415b4767cdbe00ddb7deeece7 to your computer and use it in GitHub Desktop.
typed api test
import { HTTPMethod } from "http-method-enum";
export type TypedApiMethods = {
[method in HTTPMethod]?: TypedRoute;
} & {
children?: TypedApiMethods[];
path: string;
};
export interface TypedRoute<
B = unknown,
R = unknown,
P = unknown,
Q = unknown
> {
params?: P;
query?: Q;
body?: B;
response?: R;
}
export type Path<
A extends TypedApiMethods = TypedApiMethods
> = A["path"] extends `${infer P}/`
?
| `${P}${A["children"]["length"] extends 0
? "/"
: `/${Path<A["children"][number]>}`}`
| `${P}`
: A["path"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment