import {HandlerFn1} from "../routes"; /** * A value (usually message payload) along with some security context. * * For example, `TSecurity` might be a token/key on the client-side, and some claims derived from a token/key on the server side. */ export type WithSecurity = { security: TSecurity value: T } export type UnsecuredFn = THandler extends HandlerFn1, infer TResponse> ? (req: TRequest) => Promise : never; /** * For `TApi` where each method takes a parameter of `WithSecurity`, maps each function to take `T` * * ```typescript * // For interface: * * interface MySecuredApi { * post(request: WithSecurity): Promise>; * } * * // We can say: * * type MyUnsecuredApi = Unsecured; * * // Which is equivalent to: * * interface MyUnsecuredApi { * post(request: Doc): Promise>; * } * ``` */ export type UnsecuredApi = { [K in keyof TApi]: UnsecuredFn } export type SecuredFn = THandler extends HandlerFn1 ? (req: WithSecurity) => Promise : never; /** * For `TApi` where each method takes a parameter of `T`, maps each function to take `WithSecurity` * * ```typescript * // For interface: * * interface MyUnsecuredApi { * post(request: Doc): Promise>; * } * * // We can say: * * type MySecuredApi = Secured; * * // Which is equivalent to: * * interface MyUnsecuredApi { * post(request: WithSecurity): Promise>; * } * ``` */ export type SecuredApi = { [K in keyof TApi]: SecuredFn }