Skip to main content

API Client API

Complete reference for the API client methods and types.

createApiClient​

Creates a new API client instance.

function createApiClient(config?: Partial<WebdriverIO.Config> | ApiClientOptions): ApiClient

Parameters​

ParameterTypeDescription
configPartial<WebdriverIO.Config> | ApiClientOptionsConfiguration options

Returns​

ApiClient - The API client instance

Example​

import { createApiClient } from 'wdio-api-runner'

const api = createApiClient({
baseUrl: 'https://api.example.com',
timeout: 30000
})

HTTP Methods​

get​

get<T>(url: string, options?: RequestOptions): Promise<ApiResponse<T>>

post​

post<T>(url: string, body?: any, options?: RequestOptions): Promise<ApiResponse<T>>

put​

put<T>(url: string, body?: any, options?: RequestOptions): Promise<ApiResponse<T>>

patch​

patch<T>(url: string, body?: any, options?: RequestOptions): Promise<ApiResponse<T>>

delete​

delete<T>(url: string, options?: RequestOptions): Promise<ApiResponse<T>>
head(url: string, options?: RequestOptions): Promise<ApiResponse<void>>

options​

options(url: string, options?: RequestOptions): Promise<ApiResponse<void>>

request​

request<T>(url: string, options?: RequestOptions): Promise<ApiResponse<T>>

Configuration Methods​

setBaseUrl​

Sets the base URL for all requests.

setBaseUrl(url: string): void

getBaseUrl​

Gets the current base URL.

getBaseUrl(): string

setHeader​

Sets a single header.

setHeader(name: string, value: string): void

setHeaders​

Sets multiple headers.

setHeaders(headers: Record<string, string>): void

removeHeader​

Removes a header.

removeHeader(name: string): void

getHeaders​

Gets all current headers.

getHeaders(): Record<string, string>

Interceptors​

addRequestInterceptor​

Adds a request interceptor.

addRequestInterceptor(interceptor: RequestInterceptor): void

addResponseInterceptor​

Adds a response interceptor.

addResponseInterceptor(interceptor: ResponseInterceptor): void

clearInterceptors​

Clears all interceptors.

clearInterceptors(): void

clearRequestInterceptors​

Clears only request interceptors.

clearRequestInterceptors(): void

clearResponseInterceptors​

Clears only response interceptors.

clearResponseInterceptors(): void

Types​

ApiResponse​

interface ApiResponse<T> {
status: number
statusText: string
headers: Headers
data: T
ok: boolean
duration: number
}

RequestOptions​

interface RequestOptions extends RequestInit {
timeout?: number
retries?: number
retryDelay?: number
}

ApiClientOptions​

interface ApiClientOptions {
baseUrl?: string
timeout?: number
headers?: Record<string, string>
verbose?: boolean
retries?: number
retryDelay?: number
}

RequestInterceptor​

type RequestInterceptor = (
url: string,
options: RequestInit
) => Promise<RequestInit & { url?: string }>

ResponseInterceptor​

type ResponseInterceptor = <T>(
response: ApiResponse<T>
) => Promise<ApiResponse<T>>