UNPKG

@adobe/aio-commerce-lib-core

Version:

Core utilities for AIO Commerce SDK Libraries

188 lines 7.6 kB
//#region source/responses/helpers.d.ts type HeadersRecord = Record<string, string>; type BodyRecord = Record<string, unknown>; type BodyRecordWithMessage = BodyRecord & { message: string; }; /** * Common payload structure for runtime action responses * @template TBody - Response body properties * @template THeaders - Custom response headers */ type ResponsePayload<TBody extends BodyRecord = BodyRecord, THeaders extends HeadersRecord = HeadersRecord> = { statusCode: number; body?: TBody; headers?: THeaders; }; /** * Represents an error response from a runtime action * @template TBody - Additional error body properties beyond the required message field * @template THeaders - Custom response headers */ type ErrorResponse<TBody extends BodyRecordWithMessage = BodyRecordWithMessage, THeaders extends HeadersRecord = HeadersRecord> = { type: "error"; error: ResponsePayload<TBody, THeaders>; }; /** * Represents a successful response from a runtime action * @template TBody - Response body properties * @template THeaders - Custom response headers */ type SuccessResponse<TBody extends BodyRecord = BodyRecord, THeaders extends HeadersRecord = HeadersRecord> = ResponsePayload<TBody, THeaders> & { type: "success"; }; /** * Union type representing either a successful or error response from a runtime action * @template TBody - Response/error body properties * @template THeaders - Custom response headers */ type ActionResponse<TBody extends BodyRecordWithMessage = BodyRecordWithMessage, THeaders extends HeadersRecord = HeadersRecord> = SuccessResponse<TBody, THeaders> | ErrorResponse<TBody, THeaders>; /** * Creates a standardized error response for runtime actions * @see https://developer.adobe.com/app-builder/docs/guides/runtime_guides/creating-actions#unsuccessful-response * * @template TBody - Additional error body properties beyond the required message field * @template THeaders - Custom response headers * * @param statusCode - HTTP status code (e.g., 400, 404, 500) * @param payload - Error response configuration * @param payload.message - Human-readable error message (required) * @param payload.body - Optional additional error details to include in the response body * @param payload.headers - Optional custom response headers * * @returns Standardized error response object with type discriminator * * @example * ```typescript * // Simple error with just a message * const error = buildErrorResponse(404, { * message: 'Resource not found' * }); * * // Error with additional body data * const error = buildErrorResponse(400, { * message: 'Invalid request', * body: { field: 'email', code: 'INVALID_FORMAT' } * }); * * // Error with custom headers * const error = buildErrorResponse(429, { * message: 'Rate limit exceeded', * headers: { 'Retry-After': '60' } * }); * ``` */ declare function buildErrorResponse<TBody extends BodyRecordWithMessage = BodyRecordWithMessage, THeaders extends HeadersRecord = HeadersRecord>(statusCode: number, payload: { body: TBody; headers?: THeaders; }): ErrorResponse<TBody, THeaders>; /** * Creates a standardized success response for runtime actions * @see https://developer.adobe.com/app-builder/docs/guides/runtime_guides/creating-actions#successful-response * * @template TBody - Response body properties * @template THeaders - Custom response headers * * @param statusCode - HTTP status code (typically 200, 201, 204, etc.) * @param payload - Success response configuration * @param payload.message - Human-readable success message (required) * @param payload.body - Optional additional response data to include in the response body * @param payload.headers - Optional custom response headers * * @returns Standardized success response object with type discriminator * * @example * ```typescript * // Simple success response * const response = buildSuccessResponse(200, { * message: 'Operation successful' * }); * * // Success with additional body data * const response = buildSuccessResponse(201, { * message: 'Resource created', * body: { id: '456', created: true }, * headers: { 'Location': '/api/resources/456' } * }); * ``` */ declare function buildSuccessResponse<TBody extends BodyRecord = BodyRecord, THeaders extends HeadersRecord = HeadersRecord>(statusCode: number, payload?: { body?: TBody; headers?: THeaders; }): SuccessResponse<TBody, THeaders>; //#endregion //#region source/responses/presets.d.ts declare const HTTP_OK = 200; declare const HTTP_CREATED = 201; declare const HTTP_NON_AUTHORITATIVE_INFORMATION = 203; declare const HTTP_BAD_REQUEST = 400; declare const HTTP_UNAUTHORIZED = 401; declare const HTTP_FORBIDDEN = 403; declare const HTTP_NOT_FOUND = 404; declare const HTTP_INTERNAL_SERVER_ERROR = 500; /** * Creates a success response with the HTTP status code 200. * See {@link buildSuccessResponse} for details on the response payload. */ declare const ok: (payload?: string | { body?: BodyRecord; headers?: HeadersRecord; }) => SuccessResponse<BodyRecord, HeadersRecord>; /** * Creates a success response with the HTTP status code 201. * See {@link buildSuccessResponse} for details on the response payload. */ declare const created: (payload?: string | { body?: BodyRecord; headers?: HeadersRecord; }) => SuccessResponse<BodyRecord, HeadersRecord>; /** * Creates a success response with the HTTP status code 203. * See {@link buildSuccessResponse} for details on the response payload. */ declare const nonAuthoritativeInformation: (payload?: string | { body?: BodyRecord; headers?: HeadersRecord; }) => SuccessResponse<BodyRecord, HeadersRecord>; /** * Creates an error response with the HTTP status code 400. * See {@link buildErrorResponse} for details on the response payload. */ declare const badRequest: (payload: string | { body: BodyRecordWithMessage; headers?: HeadersRecord; }) => ErrorResponse<BodyRecordWithMessage, HeadersRecord>; /** * Creates an error response with the HTTP status code 401. * See {@link buildErrorResponse} for details on the response payload. */ declare const unauthorized: (payload: string | { body: BodyRecordWithMessage; headers?: HeadersRecord; }) => ErrorResponse<BodyRecordWithMessage, HeadersRecord>; /** * Creates an error response with the HTTP status code 403. * See {@link buildErrorResponse} for details on the response payload. */ declare const forbidden: (payload: string | { body: BodyRecordWithMessage; headers?: HeadersRecord; }) => ErrorResponse<BodyRecordWithMessage, HeadersRecord>; /** * Creates an error response with the HTTP status code 404. * See {@link buildErrorResponse} for details on the response payload. */ declare const notFound: (payload: string | { body: BodyRecordWithMessage; headers?: HeadersRecord; }) => ErrorResponse<BodyRecordWithMessage, HeadersRecord>; /** * Creates an error response with the HTTP status code 500. * See {@link buildErrorResponse} for details on the response payload. */ declare const internalServerError: (payload: string | { body: BodyRecordWithMessage; headers?: HeadersRecord; }) => ErrorResponse<BodyRecordWithMessage, HeadersRecord>; //#endregion export { type ActionResponse, type ErrorResponse, HTTP_BAD_REQUEST, HTTP_CREATED, HTTP_FORBIDDEN, HTTP_INTERNAL_SERVER_ERROR, HTTP_NON_AUTHORITATIVE_INFORMATION, HTTP_NOT_FOUND, HTTP_OK, HTTP_UNAUTHORIZED, type SuccessResponse, badRequest, buildErrorResponse, buildSuccessResponse, created, forbidden, internalServerError, nonAuthoritativeInformation, notFound, ok, unauthorized };