Last active
December 29, 2020 04:58
-
-
Save theoparis/d390d04415b4767cdbe00ddb7deeece7 to your computer and use it in GitHub Desktop.
typed api test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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