UNPKG

@igniter-js/plugin-better-auth

Version:

Igniter.js plugin that wires BetterAuth API into an 'auth' controller automatically.

89 lines (85 loc) 3.36 kB
import * as _igniter_js_core from '@igniter-js/core'; import { MutationMethod, IgniterControllerBaseAction, IgniterControllerConfig } from '@igniter-js/core'; type MethodKey = "get" | "post" | "put" | "patch" | "delete"; type UpperMethod<M extends MethodKey> = M extends "get" ? "GET" : M extends "post" ? "POST" : M extends "put" ? "PUT" : M extends "patch" ? "PATCH" : "DELETE"; type AwaitedReturn<T> = T extends Promise<infer U> ? U : T; type BuildActionFromFn<Fn, M extends "GET" | MutationMethod> = Fn extends (...args: any[]) => any ? Fn extends (arg: infer A) => infer R ? IgniterControllerBaseAction & { $Infer: { $Input: M extends "GET" ? { query: A; params: any; headers?: Record<string, string>; cookies?: Record<string, string>; credentials?: unknown; } : { body: A; params: any; headers?: Record<string, string>; cookies?: Record<string, string>; credentials?: unknown; }; $Output: AwaitedReturn<R> extends { data: infer D; } ? D : AwaitedReturn<R>; $Response: { data?: AwaitedReturn<R>; error?: unknown; }; }; } : IgniterControllerBaseAction : IgniterControllerBaseAction; type MapTopLevelFunctions<T> = { [K in keyof T & string as T[K] extends (...args: any[]) => any ? K : never]: BuildActionFromFn<T[K], "POST">; }; type MapTopLevelMethod<T, M extends MethodKey> = { [K in keyof T & string as T[K] extends Record<M, (...args: any[]) => any> ? `${K}_${M}` : never]: T[K] extends Record<M, (...args: any[]) => any> ? BuildActionFromFn<NonNullable<T[K][M]>, UpperMethod<M>> : never; }; type ApiToControllerActions<TApi> = MapTopLevelFunctions<TApi> & MapTopLevelMethod<TApi, "get"> & MapTopLevelMethod<TApi, "post"> & MapTopLevelMethod<TApi, "put"> & MapTopLevelMethod<TApi, "patch"> & MapTopLevelMethod<TApi, "delete">; declare function createBetterAuthController<TAuth extends { api: Record<string, unknown>; }>(auth: TAuth): IgniterControllerConfig<ApiToControllerActions<TAuth["api"]>>; /** * Create a BetterAuth integration for Igniter.js by passing only the `auth` instance. * Returns a pair containing: * - `plugin`: a minimal Igniter plugin (for PluginManager, events, etc.) * - `controllers`: an `auth` controller with strongly-typed actions inferred from `auth.api` */ declare function createBetterAuthPlugin<TAuth extends { api: Record<string, unknown>; }>(auth: TAuth): { name: string; $meta: { title: string; description: string; }; $config: {}; $actions: {}; $controllers: { auth: _igniter_js_core.IgniterControllerConfig<ApiToControllerActions<TAuth["api"]>>; }; $events: { emits: {}; listens: {}; }; registration: { discoverable: boolean; version: string; requiresFramework: string; category: string[]; author: string; }; dependencies: { requires: never[]; optional: never[]; conflicts: never[]; }; hooks: {}; middleware: { global: never[]; routes: {}; }; resources: { resources: never[]; cleanup: () => void; }; }; export { createBetterAuthController, createBetterAuthPlugin };