UNPKG

beckn-typescript

Version:

Beckn Protocol Client & Server Tools for Typescript

108 lines (106 loc) 2.89 kB
export type ExamplePaths = { [K in string | number | symbol]: { post: { requestBody?: { content: { "application/json": any; }; }; responses: { default: { headers: { [name: string]: unknown; }; content: { "application/json": any; }; }; }; }; }; }; export type Prettify<T> = { [K in keyof T]: T[K]; } & {}; export type ExtractBody<B extends ExamplePaths[string]["post"]["requestBody"]> = B extends { content: { "application/json": any; }; } ? B["content"]["application/json"] : never; export type ExtractResponse<B extends ExamplePaths[string]["post"]["responses"]["default"]> = B extends { content: { "application/json": any; }; } ? B["content"]["application/json"] : never; export type ExtractPathWithoutSlash<T extends string | number | symbol> = T extends `/${infer Rest}` ? Rest : T; export type metaPath = typeof import("../type/meta"); export type registryPath = typeof import("../type/registry"); export type transactionPath = typeof import("../type/transaction"); export type Tools<P extends ExamplePaths, T extends keyof ExamplePaths = keyof P> = Record<ExtractPathWithoutSlash<T>, (req: Request, body: Prettify<ExtractBody<P[T]["post"]["requestBody"]>>) => Promise<Prettify<ExtractResponse<P[T]["post"]["responses"]["default"]>>>>; /** * Example usage of the `transactionServer` api for Next.js. * * @example * // create API path `/api/[path]/route.ts` * import { transactionServer } from "beckn-typescript/server/next"; * * export const { POST } = transactionServer({ * search: async (req, body) => { * return { * message: { * ack: { * status: "ACK", * }, * }, * }; * } * // more tools * }); * */ export declare const transactionServer: (tools: Tools<transactionPath>) => { POST: (req: Request, { params }: { params: Promise<{ path: string; }>; }) => Promise<Response>; }; /** * Example usage of the `metaServer` api for Next.js. * * @example * // create API path `/api/[path]/route.ts` * import { metaServer } from "beckn-typescript/server/next"; * * export const { POST } = metaServer({ * // more tools * }); * */ export declare const metaServer: (tools: Tools<metaPath>) => { POST: (req: Request, { params }: { params: Promise<{ path: string; }>; }) => Promise<Response>; }; /** * Example usage of the `registryServer` api for Next.js. * * @example * // create API path `/api/[path]/route.ts` * import { registryServer } from "beckn-typescript/server/next"; * * export const { POST } = registryServer({ * // more tools * }); * */ export declare const registryServer: (tools: Tools<registryPath>) => { POST: (req: Request, { params }: { params: Promise<{ path: string; }>; }) => Promise<Response>; }; export {};