UNPKG

@japa/api-client

Version:

Browser and API testing client for Japa. Built on top of Playwright

93 lines (92 loc) 3.87 kB
import Macroable from '@poppinss/macroable'; import type { Assert } from '@japa/assert'; import { ApiRequest } from './request.js'; import { type SetupHandler, type TeardownHandler, type CookiesSerializer, type InferBody, type InferResponse, type InferQuery, type RoutesRegistry, type PatternSerializer, type UserRoutesRegistry, type InferRouteBody, type InferRouteQuery, type InferRouteResponse, type InferRouteParams, type IsEmptyObject } from './types.js'; /** * ApiClient exposes the API to make HTTP requests in context of * testing. */ export declare class ApiClient extends Macroable { #private; constructor(baseUrl?: string, assert?: Assert); /** * Remove all globally registered setup hooks */ static clearSetupHooks(): typeof ApiClient; /** * Remove all globally registered teardown hooks */ static clearTeardownHooks(): typeof ApiClient; /** * Clear on request handlers registered using "onRequest" * method */ static clearRequestHandlers(): typeof ApiClient; /** * Register a handler to be invoked everytime a new request * instance is created */ static onRequest(handler: (request: ApiRequest) => void): typeof ApiClient; /** * Register setup hooks. Setup hooks are called before the request */ static setup(handler: SetupHandler): typeof ApiClient; /** * Register teardown hooks. Teardown hooks are called before the request */ static teardown(handler: TeardownHandler): typeof ApiClient; /** * Register a custom cookies serializer */ static cookiesSerializer(serailizer: CookiesSerializer): typeof ApiClient; /** * Register a routes registry for type-safe named routes */ static setRoutes(registry: RoutesRegistry): typeof ApiClient; /** * Register a custom pattern serializer */ static setPatternSerializer(serializer: PatternSerializer): typeof ApiClient; /** * Clear the routes registry */ static clearRoutes(): typeof ApiClient; /** * Create an instance of the request */ request(endpoint: string, method: string): ApiRequest<any, any, any>; /** * Create an instance of the request for GET method */ get<P extends string>(endpoint: P): ApiRequest<never, InferResponse<P>, InferQuery<P>>; /** * Create an instance of the request for POST method */ post<P extends string>(endpoint: P): ApiRequest<InferBody<P>, InferResponse<P>, InferQuery<P>>; /** * Create an instance of the request for PUT method */ put<P extends string>(endpoint: P): ApiRequest<InferBody<P>, InferResponse<P>, InferQuery<P>>; /** * Create an instance of the request for PATCH method */ patch<P extends string>(endpoint: P): ApiRequest<InferBody<P>, InferResponse<P>, InferQuery<P>>; /** * Create an instance of the request for DELETE method */ delete<P extends string>(endpoint: P): ApiRequest<InferBody<P>, InferResponse<P>, InferQuery<P>>; /** * Create an instance of the request for HEAD method */ head<P extends string>(endpoint: P): ApiRequest<never, InferResponse<P>, InferQuery<P>>; /** * Create an instance of the request for OPTIONS method */ options<P extends string>(endpoint: P): ApiRequest<never, InferResponse<P>, InferQuery<P>>; /** * Create a type-safe request using a named route from the registry. * The route name must be registered in both the runtime registry * (via ApiClient.setRoutes()) and the type registry (UserRoutesRegistry). */ visit<Name extends keyof UserRoutesRegistry>(...args: IsEmptyObject<InferRouteParams<Name>> extends true ? [name: Name] : [name: Name, params: InferRouteParams<Name>]): ApiRequest<InferRouteBody<Name>, InferRouteResponse<Name>, InferRouteQuery<Name>>; }