UNPKG

beckn-typescript

Version:

Beckn Protocol Client & Server Tools for Typescript

113 lines (110 loc) 3.47 kB
import { NextFunction, Request as Request$1, Response as Response$1 } from 'express'; 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` middleware. * * @example * import express from "express"; * import { transactionServer } from "beckn-typescript/server/express"; * * const app = express(); * app.use(express.json()); // Ensure body-parser middleware is used * * // Use the transactionServer middleware * app.use("/:path", transactionServer({ * search: async (c, body) => { * return { * message: { * ack: { * status: "ACK", * }, * }, * }; * } * // more tools * })); * * app.listen(3000, () => { * console.log("Server is running on port 3000"); * }); */ export declare const transactionServer: (tools: Tools<transactionPath>) => (req: Request, res: Response, next: NextFunction) => Promise<void>; /** * Example usage of the `metaServer` middleware. * * @example * import express from "express"; * import { metaServer } from "beckn-typescript/server/express"; * * const app = express(); * app.use(express.json()); // Ensure body-parser middleware is used * * // Use the metaServer middleware * app.use("/:path", metaServer({ * // more tools * })); * * app.listen(3000, () => { * console.log("Server is running on port 3000"); * }); */ export declare const metaServer: (tools: Tools<metaPath>) => (req: Request, res: Response, next: NextFunction) => Promise<void>; /** * Example usage of the `registryServer` middleware. * * @example * import express from "express"; * import { registryServer } from "beckn-typescript/server/express"; * * const app = express(); * app.use(express.json()); // Ensure body-parser middleware is used * * // Use the registryServer middleware * app.use("/:path", registryServer({ * // more tools * })); * * app.listen(3000, () => { * console.log("Server is running on port 3000"); * }); */ export declare const registryServer: (tools: Tools<registryPath>) => (req: Request, res: Response, next: NextFunction) => Promise<void>; export {};