-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Instead of having all those enums with endpoints, maybe do something like this instead:
type Item = { id: number; name: string; };
type EndpointMap<Schema> = {
'/v2/build': {
type: { build: string }
},
'/v2/items': {
bulk: {
id: number
type: Item
}
},
'/v2/quaggans': {
bulk: {
id: string
type: { id: string; name: string; }
all: true
}
}
}
type GetEndpointsMatching<Schema, T> = { [X in keyof EndpointMap<Schema>]: EndpointMap<Schema>[X] extends T ? X: never }[keyof EndpointMap<Schema>]
type SimpleEndpoints<Schema> = GetEndpointsMatching<Schema, { type: any }>
type BulkEndpoints<Schema> = GetEndpointsMatching<Schema, { bulk: any }>
type BulkEndpointSingleUrlMap<Schema> = { [Endpoint in BulkEndpoints<Schema> as `${Endpoint}/${EndpointMap<Schema>[Endpoint]['bulk']['id']}` | `${Endpoint}?id=${EndpointMap<Schema>[Endpoint]['bulk']['id']}`]: Endpoint }
type BulkEndpointAllUrlMap<Schema> = { [Endpoint in BulkEndpoints<Schema> as `${Endpoint}?ids=all`]: EndpointMap<Schema>[Endpoint]['bulk'] extends { all: true } ? Endpoint : never }
type BulkEndpointManyUrlMap<Schema> = { [Endpoint in BulkEndpoints<Schema> as `${Endpoint}?ids=${string}`]: Endpoint }
type EndpointFromUrl<Url, Schema> =
Url extends SimpleEndpoints<Schema> ? { endpoint: Url, type: EndpointMap<Schema>[Url]['type'] } :
Url extends BulkEndpoints<Schema> ? { endpoint: Url, type: EndpointMap<Schema>[Url]['bulk']['id'][] } :
Url extends keyof BulkEndpointSingleUrlMap<Schema> ? { endpoint: BulkEndpointSingleUrlMap<Schema>[Url], type: EndpointMap<Schema>[BulkEndpointSingleUrlMap<Schema>[Url]]['bulk']['type'] } :
Url extends keyof BulkEndpointAllUrlMap<Schema> ? { endpoint: BulkEndpointAllUrlMap<Schema>[Url], type: EndpointMap<Schema>[BulkEndpointAllUrlMap<Schema>[Url]]['bulk']['type'][] } :
Url extends keyof BulkEndpointManyUrlMap<Schema> ? { endpoint: BulkEndpointManyUrlMap<Schema>[Url], type: EndpointMap<Schema>[BulkEndpointManyUrlMap<Schema>[Url]]['bulk']['type'][] } :
{ endpoint: unknown, type: unknown };
type EndpointType<Url, Schema> = EndpointFromUrl<Url, Schema>['type'];
type GetEndpointDefinition<Url, Schema> = EndpointFromUrl<Url, Schema>['endpoint'] extends keyof EndpointMap<Schema> ? EndpointMap<Schema>[EndpointFromUrl<Url, Schema>['endpoint']] : unknown;Metadata
Metadata
Assignees
Labels
No labels