UNPKG

hono-json-response

Version:

A Hono middleware for standardized JSON responses

77 lines (75 loc) 2.65 kB
import * as hono1 from "hono"; //#region src/types.d.ts type JSONResponseConfig = { [key: string]: { code: number | string; defaultMsg?: string; }; }; interface JSONResponseFormat { code?: string; data?: string; msg?: string; } type JsonResponseHandler = <T>(data?: T, msg?: string) => Response; type JR = <T>(code: number | string, data?: T, msg?: string) => Response; //# sourceMappingURL=types.d.ts.map //#endregion //#region src/index.d.ts declare module 'hono' { interface Context { /** * Success response with JSON. * * @template T The type of the response data. * @param {T} [data] The data to be included in the response. * @param {string} [msg] An optional message for the response. * @returns {Response} The JSON response with success status code. */ ok: JsonResponseHandler; /** * Unauthorized response with JSON. * * @template T The type of the response data. * @param {T} [data] The data to be included in the response. * @param {string} [msg] An optional message for the response. * @returns {Response} The JSON response with unauthorized status code. */ unauth: JsonResponseHandler; /** * Business error response with JSON. * * @template T The type of the response data. * @param {T} [data] The data to be included in the response. * @param {string} [msg] An optional message for the response. * @returns {Response} - The JSON response with business error status code. */ bizerr: JsonResponseHandler; /** * System error response with JSON. * * @template T The type of the response data. * @param {T} [data] The data to be included in the response. * @param {string} [msg] An optional message for the response. * @returns {Response} The JSON response with system error status code. */ syserr: JsonResponseHandler; /** * Custom JSON response with specified status code. * * JSONResponse(jr) * * @template T The type of the response data. * @param {number | string} code The custom status code for the response. * @param {T} [data] The data to be included in the response. * @param {string} [msg] An optional message for the response. * @returns {Response} The JSON response with custom status code. */ jr: JR; } } declare const jsonResponse: (userConfig?: JSONResponseConfig | null, jsonResponseFormat?: JSONResponseFormat) => hono1.MiddlewareHandler<any, string, {}>; //# sourceMappingURL=index.d.ts.map //#endregion export { jsonResponse }; //# sourceMappingURL=index.d.ts.map