import type { JoinPathPattern, Method, MethodOf, OperationParams, OperationResult, PathPattern, UrlParams } from '../../../definition/rest'; import type { IUser } from '../../../definition/IUser'; import { IMethodConnection } from '../../../definition/IMethodThisType'; import { ITwoFactorOptions } from '../../2fa/server/code'; type SuccessResult = { statusCode: 200; body: T extends object ? { success: true } & T : T; }; type FailureResult = { statusCode: 400; body: T extends object ? { success: false } & T : { success: false; error: T; stack: TStack; errorType: TErrorType; details: TErrorDetails; } & (undefined extends TErrorType ? {} : { errorType: TErrorType }) & (undefined extends TErrorDetails ? {} : { details: TErrorDetails extends string ? unknown : TErrorDetails }); }; type UnauthorizedResult = { statusCode: 403; body: { success: false; error: T | 'unauthorized'; }; }; type NotFoundResult = { statusCode: 403; body: { success: false; error: T | 'Resource not found'; }; }; export type NonEnterpriseTwoFactorOptions = { authRequired: true; forceTwoFactorAuthenticationForNonEnterprise: true; twoFactorRequired: true; permissionsRequired?: string[]; twoFactorOptions: ITwoFactorOptions; }; type Options = | { permissionsRequired?: string[]; authRequired?: boolean; forceTwoFactorAuthenticationForNonEnterprise?: boolean; } | { authRequired: true; twoFactorRequired: true; twoFactorOptions?: ITwoFactorOptions; }; type Request = { method: 'GET' | 'POST' | 'PUT' | 'DELETE'; url: string; headers: Record; body: any; }; type ActionThis = { urlParams: UrlParams; // TODO make it unsafe readonly queryParams: TMethod extends 'GET' ? Partial> : Record; // TODO make it unsafe readonly bodyParams: TMethod extends 'GET' ? Record : Partial>; readonly request: Request; requestParams(): OperationParams; getPaginationItems(): { readonly offset: number; readonly count: number; }; parseJsonQuery(): { sort: Record; fields: Record; query: Record; }; getUserFromParams(): IUser; } & (TOptions extends { authRequired: true } ? { readonly user: IUser; readonly userId: string; } : { readonly user: null; readonly userId: null; }); export type ResultFor = | SuccessResult> | FailureResult | UnauthorizedResult; type Action = | ((this: ActionThis) => Promise>) | ((this: ActionThis) => ResultFor); type Operation = | Action | ({ action: Action; } & { twoFactorRequired: boolean }); type Operations = { [M in MethodOf as Lowercase]: Operation, TPathPattern, TOptions>; }; declare class APIClass { processTwoFactor({ userId, request, invocation, options, connection, }: { userId: string; request: Request; invocation: { twoFactorChecked: boolean }; options?: Options; connection: IMethodConnection; }): void; addRoute( subpath: TSubPathPattern, operations: Operations>, ): void; addRoute>( subpaths: TSubPathPattern[], operations: Operations, ): void; addRoute( subpath: TSubPathPattern, options: TOptions, operations: Operations, TOptions>, ): void; addRoute, TOptions extends Options>( subpaths: TSubPathPattern[], options: TOptions, operations: Operations, ): void; success(result: T): SuccessResult; success(): SuccessResult; failure( result: T, errorType?: TErrorType, stack?: TStack, error?: { details: TErrorDetails }, ): FailureResult; failure(result: T): FailureResult; failure(): FailureResult; unauthorized(msg?: T): UnauthorizedResult; notFound(msg?: T): NotFoundResult; defaultFieldsToExclude: { joinCode: 0; members: 0; importIds: 0; e2e: 0; }; } export declare const API: { v1: APIClass<'/v1'>; default: APIClass; };