@eienjs/adonisjs-jsend
Version:
Simple helpers to generate JSend-compliant JSON responses
55 lines (54 loc) • 2.1 kB
TypeScript
import { HttpContext } from "@adonisjs/core/http";
import { ApplicationService } from "@adonisjs/core/types";
import { HttpError } from "@adonisjs/core/types/http";
//#region providers/jsend_provider.d.ts
declare module '@adonisjs/core/http' {
interface Response {
/**
* Send success response using jsend standard
*
* @param {*} [data] - Any data returned by the API call
* @param {number} [status=200] - Status code number
*/
jsendSuccess: (data?: unknown, status?: number) => void;
/**
* Send fail response using jsend standard
*
* @param {*} data - Any data returned by the API call
* @param {number} [status=400] - Status code number
*/
jsendFail: (data: unknown, status?: number) => void;
/**
* Send error response using jsend standard
*
* @param {string} message - A meaningful, end-user-readable (or at the least log-worthy) message, explaining what went wrong
* @param {number} [status=500] - Status code number
* @param {object} [options] - Additional options
* @param {(string|number)} [options.code] - A numeric code corresponding to the error, if applicable
* @param {*} [options.errors] - A generic container for any other information about the error, i.e. the conditions that caused the error, stack traces, etc
*/
jsendError: (message: string, status?: number, options?: {
code?: string | number;
errors?: unknown;
}) => void;
}
interface ExceptionHandler {
renderErrorAsJsend: (error: HttpError, ctx: HttpContext) => Promise<void>;
renderValidationErrorAsJsend: (error: HttpError, ctx: HttpContext) => Promise<void>;
/**
* Whether or not to using jsend standard. When set to true, the errors
* will handle using jsend standard, default is enabled.
*/
usingJsend?: boolean;
}
}
declare class JsendProvider {
protected app: ApplicationService;
constructor(app: ApplicationService);
/**
* The container bindings have booted
*/
boot(): Promise<void>;
}
//#endregion
export { JsendProvider as default };