import { APIExecuter } from "../api/defs"; export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head'; export type RawArgType = Record | Record[] | undefined; export interface BasicAPICallT< ArgType extends RawArgType, ResultType extends ValidResponse, ErrorType extends InvalidResponse > { (args: ArgType): Promise; (args: ArgType, returnError: false): Promise; (args: ArgType, returnError: true): Promise; } export interface EmptyAPICallT { (): Promise; (args: undefined): Promise; (args: undefined, returnError: false): Promise; (args: undefined, returnError: true): Promise; } // eslint-disable-next-line @typescript-eslint/no-explicit-any export type ArgTypeOf = T extends APICallT ? A : never; export type APICallT< ArgType extends RawArgType, ResultType extends ValidResponse = ValidResponse, ErrorType extends InvalidResponse = InvalidResponse > = ArgType extends undefined ? EmptyAPICallT : BasicAPICallT; export type APITail = typeof GET | typeof POST | typeof PUT | typeof PATCH | typeof HEAD | typeof DELETE; const httpMethodMap = new Map([ [GET, "get"], [POST, "post"], [PUT, "put"], [PATCH, "patch"], [HEAD, "head"], [DELETE, "delete"], ]); export function extractHttpMethod(tail: APITail): HttpMethod { return httpMethodMap.get(tail) ?? "post"; } export async function GET( args?: ArgType ): Promise; export async function GET( args: ArgType | undefined, returnError: false ): Promise; export async function GET< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends undefined = undefined >(args: ArgType | undefined, returnError: true): Promise; export async function GET< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends undefined = undefined >(args?: ArgType, returnError = false): Promise { throw `Can't directly call GET. ${args}, ${returnError}. Use auto-generated path API.` } export async function POST( args?: ArgType ): Promise; export async function POST( args: ArgType | undefined, returnError: false ): Promise; export async function POST< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType >(args: ArgType | undefined, returnError: true): Promise; export async function POST< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType >(args?: ArgType, returnError = false): Promise { throw `Can't directly call POST. ${args}, ${returnError}. Use auto-generated path API.` } export async function PUT( args?: ArgType ): Promise; export async function PUT( args: ArgType | undefined, returnError: false ): Promise; export async function PUT< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType >(args: ArgType | undefined, returnError: true): Promise; export async function PUT< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType >(args?: ArgType, returnError = false): Promise { throw `Can't directly call PUT. ${args}, ${returnError}. Use auto-generated path API.`; } export async function PATCH( args?: ArgType ): Promise; export async function PATCH( args: ArgType | undefined, returnError: false ): Promise; export async function PATCH< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType >(args: ArgType | undefined, returnError: true): Promise; export async function PATCH< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType >(args?: ArgType, returnError = false): Promise { throw `Can't directly call PATCH. ${args}, ${returnError}. Use auto-generated path API.` } export async function HEAD( args?: ArgType ): Promise; export async function HEAD( args: ArgType | undefined, returnError: false ): Promise; export async function HEAD< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends undefined = undefined >(args: ArgType | undefined, returnError: true): Promise; export async function HEAD< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends undefined = undefined >(args?: ArgType, returnError = false): Promise { throw `Can't directly call HEAD. ${args}, ${returnError}. Use auto-generated path API.`; } export async function DELETE( args?: ArgType ): Promise; export async function DELETE( args: ArgType | undefined, returnError: false ): Promise; export async function DELETE< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType >(args: ArgType | undefined, returnError: true): Promise; export async function DELETE< ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType >(args?: ArgType, returnError = false): Promise { throw `Can't directly call DELETE. ${args}, ${returnError}. Use auto-generated path API.`; } export interface ValidResponse { result: true; } export type recoveryMethod = 'refreshEntirePage' | 'retryAPI' | 'gameLogin' | 'gatewayLogin' | 'gateway2FA' | 'gameQuota'; export interface InvalidResponse { result: false; reason: string; recovery?: recoveryMethod; } type ExtractValid = T extends ValidResponse ? T : never; type ExtractInvalid = T extends InvalidResponse ? T : never; export type Callable = (...args: any)=>any; export type ExtractResponse = ExtractValid>>; export type ExtractError = ExtractInvalid>>; export type ExtractQuery = Parameters[0]; export type DefAPINamespace = { [key: string]: DefAPINamespace | typeof GET | typeof POST | typeof PUT | typeof DELETE | typeof PATCH | typeof HEAD ; } export type APICompatType = T extends APICallT ? APICallT : never;