Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe refactor type map #67

Open
darthmaim opened this issue Jul 27, 2024 · 0 comments
Open

Maybe refactor type map #67

darthmaim opened this issue Jul 27, 2024 · 0 comments

Comments

@darthmaim
Copy link
Member

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant