/* eslint-disable @typescript-eslint/no-unused-vars */ import { Endpoints } from '../v1'; type ReplacePlaceholders = string extends TPath ? TPath : TPath extends `${infer Start}:${infer _Param}/${infer Rest}` ? `${Start}${string}/${ReplacePlaceholders}` : TPath extends `${infer Start}:${infer _Param}` ? `${Start}${string}` : TPath; type KeyOfEach = T extends any ? keyof T : never; type GetParams = TOperation extends (...args: any) => any ? Parameters[0] extends void ? void : Parameters[0] : never; type GetResult = TOperation extends (...args: any) => any ? ReturnType : never; type OperationsByPathPatternAndMethod< TPathPattern extends keyof Endpoints, TMethod extends KeyOfEach = KeyOfEach > = TMethod extends any ? { pathPattern: TPathPattern; method: TMethod; path: ReplacePlaceholders; params: GetParams; result: GetResult; } : never; type OperationsByPathPattern = TPathPattern extends any ? OperationsByPathPatternAndMethod : never; type Operations = OperationsByPathPattern; type Method = Operations['method']; export type PathPattern = Operations['pathPattern']; type Path = Operations['path']; // export type Serialized = T extends Date ? Exclude | string : T extends boolean | number | string | null | undefined ? T : T extends {} ? { [K in keyof T]: Serialized; } : null; export type MatchPathPattern = TPath extends any ? Extract['pathPattern'] : never; export type OperationResult< TMethod extends Method, TPathPattern extends PathPattern > = TMethod extends keyof Endpoints[TPathPattern] ? GetResult : never; export type PathFor = TMethod extends any ? Extract['path'] : never; export type OperationParams< TMethod extends Method, TPathPattern extends PathPattern > = TMethod extends keyof Endpoints[TPathPattern] ? GetParams : never; type SuccessResult = T & { success: true }; type FailureResult = { success: false; error: T; stack: TStack; errorType: TErrorType; details: TErrorDetails; }; type UnauthorizedResult = { success: false; error: T | 'unauthorized'; }; export type ResultFor = | SuccessResult> | FailureResult | UnauthorizedResult;