import { type NextApiRequest, type NextApiResponse } from "next"; import {type IQueryParams} from "../link"; import {type IQuery} from "../source"; export interface INextApiRequest< TQuery extends IQueryParams = any, TRequest = undefined, > extends Omit { readonly query: TQuery; readonly body: TRequest; } export interface IEndpointParams { readonly req: INextApiRequest; readonly res: NextApiResponse; readonly request: TRequest; readonly query: TQueryParams; end(chunk?: any): void; } /** * Generic endpoint; SDK generates as POST by default. */ export interface IEndpoint { handler(params: IEndpointParams): Promise; } export type IEndpointCallback = (req: INextApiRequest, res: NextApiResponse) => void; /** * When fetching an individual item, done by GET. */ export type IGetEndpoint = IEndpoint; /** * When fetching a list of items (arrayed by default), done by GET. */ export type IListEndpoint = IEndpoint; /** * Mutation endpoint is a general endpoint used to do some server-side effect (some updated data or so). * * Defaults to POST. */ export type IMutationEndpoint = IEndpoint; export type IEntityEndpoint = IEndpoint; /** * Generic request/response. */ export type IRequestEndpoint = IEndpoint; export class ClientError extends Error { readonly code: number; constructor(message: string, code = 400) { super(message); this.code = code; } }