bc-node-sdk
Version:
BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.
81 lines (80 loc) • 4.29 kB
TypeScript
import { NextResponse } from "next/server";
import { Contracts } from "../..";
import BaseApiController from "./BaseApiController";
import IApiParams from "../../domain/contracts/api/IApiParams";
import IApiRequestHandler from "../../domain/contracts/api/IApiRequestHandler";
/**
* Class {@link ApiController} is a concrete implementation of the {@link IApiRequestHandler} interface and extends the behavior of {@link BaseApiController}.
* It delegates API requests to the appropriate HTTP method handler and returns
* the result of the API call or a JSON response in case of an error.
*/
export default class ApiController extends BaseApiController implements IApiRequestHandler {
private _apiInstance;
/**
* Constructs an instance of the ApiController class, extending the BaseApiController.
* Initializes an ApiService instance if client secret authentication is enabled.
*
* @param cacheManager - The cache manager instance used for caching operations.
*/
constructor(cacheManager: Contracts.Caching.ICacheManager);
/**
* Handles API requests by delegating them to the appropriate HTTP method handler.
*
* @param request - The IApiParams object containing request details such as endpoint, data, params, headers, and cookies.
* @param method - The HTTP request method as a string (e.g., GET, POST, PUT, DELETE, PATCH).
* @returns A Promise that resolves to the result of the API call or a JSON response indicating the method is not allowed.
*/
handleRequest(request: IApiParams, method: string): Promise<any>;
/**
* GET request handler
* @param request - The IApiParams object containing request details such as endpoint, data, params, headers, and cookies.
* @returns A Promise that resolves to the result of the API call.
* @throws An error if client ID or secret is missing when client secret authentication is enabled.
*/
protected get(request: IApiParams): Promise<any>;
/**
* POST request handler
* @param request - The IApiParams object containing request details such as endpoint, data, params, headers, and cookies.
* @returns A Promise that resolves to the result of the API call.
* @throws An error if client ID or secret is missing when client secret authentication is enabled.
*/
protected post(request: IApiParams): Promise<any>;
/**
* PUT request handler
* Handles a PUT request using the provided API parameters.
*
* @param request - The IApiParams object containing request details such as endpoint, data, headers, and cookies.
* @returns A Promise that resolves to the result of the API call.
* @throws An error if client ID or secret is missing when client secret authentication is enabled.
*/
protected put(request: IApiParams): Promise<any>;
/**
* Handles a DELETE request using the provided API parameters.
*
* @param request - The IApiParams object containing request details such as endpoint, data, headers, and cookies.
* @returns A Promise that resolves to the result of the API call.
* @throws An error if client ID or secret is missing when client secret authentication is enabled.
*/
protected delete(request: IApiParams): Promise<any>;
/**
* PATCH request handler
* Handles a PATCH request using the provided API parameters.
*
* @param request - The IApiParams object containing request details such as endpoint, data, headers, and cookies.
* @returns A Promise that resolves to the result of the API call.
* @throws An error if client ID or secret is missing when client secret authentication is enabled.
*/
protected patch(request: IApiParams): Promise<any>;
/**
* Handles errors by logging them and returning the error response to the caller.
* If the error is an HTTP 401, it also clears the site data (cookies, storage, and cache).
* @param error The error to be handled.
*/
protected errorResponse(error: any): NextResponse<{
error: any;
}>;
/**
* Returns a 200 response with a null body.
*/
protected nullResponse(): NextResponse<any>;
}