Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/openai/core.ts>APIClient

Deno build of the official Typescript library for the OpenAI API.
Extremely Popular
Latest
class APIClient
Abstract
import { APIClient } from "https://deno.land/x/openai@v4.69.0/core.ts";

Constructors

new
APIClient(unnamed 0: { baseURL: string; maxRetries?: number | undefined; timeout: number | undefined; httpAgent: Agent | undefined; fetch: Fetch | undefined; })

Properties

private
fetch: Fetch
protected
optional
idempotencyHeader: string
baseURL: string
httpAgent: Agent | undefined
maxRetries: number
timeout: number

Methods

private
buildHeaders(unnamed 0: { options: FinalRequestOptions; headers: Record<string, string | null | undefined>; contentLength: string | null | undefined; retryCount: number; }): Record<string, string>
private
calculateContentLength(body: unknown): string | null
private
calculateDefaultRetryTimeoutMillis(retriesRemaining: number, maxRetries: number): number
private
getUserAgent(): string
private
makeRequest<Req>(optionsInput: PromiseOrValue<FinalRequestOptions<Req>>, retriesRemaining: number | null): Promise<APIResponseProps>
private
methodRequest<Req, Rsp>(
method: HTTPMethod,
path: string,
opts?: PromiseOrValue<RequestOptions<Req>>,
): APIPromise<Rsp>
private
retryRequest(
retriesRemaining: number,
responseHeaders?: Headers | undefined,
): Promise<APIResponseProps>
private
shouldRetry(response: Response): boolean

Override this to add your own default headers, for example:

{ ...super.defaultHeaders(), Authorization: 'Bearer 123', }

protected
defaultIdempotencyKey(): string
protected
abstract
defaultQuery(): DefaultQuery | undefined
protected
makeStatusError(
status: number | undefined,
error: Object | undefined,
message: string | undefined,
headers: Headers | undefined,
)
protected
parseHeaders(headers: HeadersInit | null | undefined): Record<string, string>
protected
prepareOptions(options: FinalRequestOptions): Promise<void>

Used as a callback for mutating the given FinalRequestOptions object.

protected
prepareRequest(request: RequestInit, unnamed 1: { url: string; options: FinalRequestOptions; }): Promise<void>

Used as a callback for mutating the given RequestInit object.

This is useful for cases where you want to add certain headers based off of the request properties, e.g. method or url.

protected
stringifyQuery(query: Record<string, unknown>): string
protected
validateHeaders(headers: Headers, customHeaders: Headers)

Override this to add your own headers validation:

buildRequest<Req>(options: FinalRequestOptions<Req>, unnamed 1?: { retryCount?: number; }): { req: RequestInit; url: string; timeout: number; }
buildURL<Req>(path: string, query: Req | null | undefined): string
delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>
fetchWithTimeout(
init: RequestInit | undefined,
ms: number,
controller: AbortController,
): Promise<Response>
get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>
getAPIList<Item, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(
path: string,
Page: new (...args: any[]) => PageClass,
opts?: RequestOptions<any>,
): PagePromise<PageClass, Item>
patch<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>
post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>
put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>
request<Req, Rsp>(options: PromiseOrValue<FinalRequestOptions<Req>>, remainingRetries?: number | null): APIPromise<Rsp>
requestAPIList<Item = unknown, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(Page: new (...args: ConstructorParameters<AbstractPage>) => PageClass, options: FinalRequestOptions): PagePromise<PageClass, Item>