| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
-
-
- import { Stream } from "./streaming.js";
- import { APIError } from "./error.js";
- import { type Readable, type Agent, type RequestInfo, type RequestInit, type Response, type HeadersInit } from "./_shims/index.js";
- export { type Response };
- import { BlobLike } from "./uploads.js";
- export { maybeMultipartFormRequestOptions, multipartFormRequestOptions, createForm, type Uploadable, } from "./uploads.js";
- export type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;
- /**
- * An alias to the builtin `Array` type so we can
- * easily alias it in import statements if there are name clashes.
- */
- type _Array<T> = Array<T>;
- /**
- * An alias to the builtin `Record` type so we can
- * easily alias it in import statements if there are name clashes.
- */
- type _Record<K extends keyof any, T> = Record<K, T>;
- export type { _Array as Array, _Record as Record };
- type PromiseOrValue<T> = T | Promise<T>;
- type APIResponseProps = {
- response: Response;
- options: FinalRequestOptions;
- controller: AbortController;
- };
- type WithRequestID<T> = T extends Array<any> | Response | AbstractPage<any> ? T : T extends Record<string, any> ? T & {
- _request_id?: string | null;
- } : T;
- /**
- * A subclass of `Promise` providing additional helper methods
- * for interacting with the SDK.
- */
- export declare class APIPromise<T> extends Promise<WithRequestID<T>> {
- private responsePromise;
- private parseResponse;
- private parsedPromise;
- constructor(responsePromise: Promise<APIResponseProps>, parseResponse?: (props: APIResponseProps) => PromiseOrValue<WithRequestID<T>>);
- _thenUnwrap<U>(transform: (data: T, props: APIResponseProps) => U): APIPromise<U>;
- /**
- * Gets the raw `Response` instance instead of parsing the response
- * data.
- *
- * If you want to parse the response body but still get the `Response`
- * instance, you can use {@link withResponse()}.
- *
- * 👋 Getting the wrong TypeScript type for `Response`?
- * Try setting `"moduleResolution": "NodeNext"` if you can,
- * or add one of these imports before your first `import … from 'openai'`:
- * - `import 'openai/shims/node'` (if you're running on Node)
- * - `import 'openai/shims/web'` (otherwise)
- */
- asResponse(): Promise<Response>;
- /**
- * Gets the parsed response data, the raw `Response` instance and the ID of the request,
- * returned via the X-Request-ID header which is useful for debugging requests and reporting
- * issues to OpenAI.
- *
- * If you just want to get the raw `Response` instance without parsing it,
- * you can use {@link asResponse()}.
- *
- *
- * 👋 Getting the wrong TypeScript type for `Response`?
- * Try setting `"moduleResolution": "NodeNext"` if you can,
- * or add one of these imports before your first `import … from 'openai'`:
- * - `import 'openai/shims/node'` (if you're running on Node)
- * - `import 'openai/shims/web'` (otherwise)
- */
- withResponse(): Promise<{
- data: T;
- response: Response;
- request_id: string | null | undefined;
- }>;
- private parse;
- then<TResult1 = WithRequestID<T>, TResult2 = never>(onfulfilled?: ((value: WithRequestID<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
- catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<WithRequestID<T> | TResult>;
- finally(onfinally?: (() => void) | undefined | null): Promise<WithRequestID<T>>;
- }
- export declare abstract class APIClient {
- baseURL: string;
- maxRetries: number;
- timeout: number;
- httpAgent: Agent | undefined;
- private fetch;
- protected idempotencyHeader?: string;
- constructor({ baseURL, maxRetries, timeout, // 10 minutes
- httpAgent, fetch: overriddenFetch, }: {
- baseURL: string;
- maxRetries?: number | undefined;
- timeout: number | undefined;
- httpAgent: Agent | undefined;
- fetch: Fetch | undefined;
- });
- protected authHeaders(opts: FinalRequestOptions): Headers;
- /**
- * Override this to add your own default headers, for example:
- *
- * {
- * ...super.defaultHeaders(),
- * Authorization: 'Bearer 123',
- * }
- */
- protected defaultHeaders(opts: FinalRequestOptions): Headers;
- protected abstract defaultQuery(): DefaultQuery | undefined;
- /**
- * Override this to add your own headers validation:
- */
- protected validateHeaders(headers: Headers, customHeaders: Headers): void;
- protected defaultIdempotencyKey(): string;
- get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>;
- post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>;
- patch<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>;
- put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>;
- delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp>;
- private methodRequest;
- getAPIList<Item, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(path: string, Page: new (...args: any[]) => PageClass, opts?: RequestOptions<any>): PagePromise<PageClass, Item>;
- private calculateContentLength;
- buildRequest<Req>(inputOptions: FinalRequestOptions<Req>, { retryCount }?: {
- retryCount?: number;
- }): {
- req: RequestInit;
- url: string;
- timeout: number;
- };
- private buildHeaders;
- /**
- * Used as a callback for mutating the given `FinalRequestOptions` object.
- */
- protected prepareOptions(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 prepareRequest(request: RequestInit, { url, options }: {
- url: string;
- options: FinalRequestOptions;
- }): Promise<void>;
- protected parseHeaders(headers: HeadersInit | null | undefined): Record<string, string>;
- protected makeStatusError(status: number | undefined, error: Object | undefined, message: string | undefined, headers: Headers | undefined): APIError;
- request<Req, Rsp>(options: PromiseOrValue<FinalRequestOptions<Req>>, remainingRetries?: number | null): APIPromise<Rsp>;
- private makeRequest;
- requestAPIList<Item = unknown, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass, options: FinalRequestOptions): PagePromise<PageClass, Item>;
- buildURL<Req>(path: string, query: Req | null | undefined): string;
- protected stringifyQuery(query: Record<string, unknown>): string;
- fetchWithTimeout(url: RequestInfo, init: RequestInit | undefined, ms: number, controller: AbortController): Promise<Response>;
- private shouldRetry;
- private retryRequest;
- private calculateDefaultRetryTimeoutMillis;
- private getUserAgent;
- }
- export type PageInfo = {
- url: URL;
- } | {
- params: Record<string, unknown> | null;
- };
- export declare abstract class AbstractPage<Item> implements AsyncIterable<Item> {
- #private;
- protected options: FinalRequestOptions;
- protected response: Response;
- protected body: unknown;
- constructor(client: APIClient, response: Response, body: unknown, options: FinalRequestOptions);
- /**
- * @deprecated Use nextPageInfo instead
- */
- abstract nextPageParams(): Partial<Record<string, unknown>> | null;
- abstract nextPageInfo(): PageInfo | null;
- abstract getPaginatedItems(): Item[];
- hasNextPage(): boolean;
- getNextPage(): Promise<this>;
- iterPages(): AsyncGenerator<this>;
- [Symbol.asyncIterator](): AsyncGenerator<Item>;
- }
- /**
- * This subclass of Promise will resolve to an instantiated Page once the request completes.
- *
- * It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:
- *
- * for await (const item of client.items.list()) {
- * console.log(item)
- * }
- */
- export declare class PagePromise<PageClass extends AbstractPage<Item>, Item = ReturnType<PageClass['getPaginatedItems']>[number]> extends APIPromise<PageClass> implements AsyncIterable<Item> {
- constructor(client: APIClient, request: Promise<APIResponseProps>, Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass);
- /**
- * Allow auto-paginating iteration on an unawaited list call, eg:
- *
- * for await (const item of client.items.list()) {
- * console.log(item)
- * }
- */
- [Symbol.asyncIterator](): AsyncGenerator<Item>;
- }
- export declare const createResponseHeaders: (headers: Awaited<ReturnType<Fetch>>['headers']) => Record<string, string>;
- type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
- export type RequestClient = {
- fetch: Fetch;
- };
- export type Headers = Record<string, string | null | undefined>;
- export type DefaultQuery = Record<string, string | undefined>;
- export type KeysEnum<T> = {
- [P in keyof Required<T>]: true;
- };
- export type RequestOptions<Req = unknown | Record<string, unknown> | Readable | BlobLike | ArrayBufferView | ArrayBuffer> = {
- method?: HTTPMethod;
- path?: string;
- query?: Req | undefined;
- body?: Req | null | undefined;
- headers?: Headers | undefined;
- maxRetries?: number;
- stream?: boolean | undefined;
- timeout?: number;
- httpAgent?: Agent;
- signal?: AbortSignal | undefined | null;
- idempotencyKey?: string;
- __metadata?: Record<string, unknown>;
- __binaryRequest?: boolean | undefined;
- __binaryResponse?: boolean | undefined;
- __streamClass?: typeof Stream;
- };
- export declare const isRequestOptions: (obj: unknown) => obj is RequestOptions<unknown>;
- export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable | DataView> = RequestOptions<Req> & {
- method: HTTPMethod;
- path: string;
- };
- export declare const safeJSON: (text: string) => any;
- export declare const sleep: (ms: number) => Promise<unknown>;
- export declare const castToError: (err: any) => Error;
- export declare const ensurePresent: <T>(value: T | null | undefined) => T;
- /**
- * Read an environment variable.
- *
- * Trims beginning and trailing whitespace.
- *
- * Will return undefined if the environment variable doesn't exist or cannot be accessed.
- */
- export declare const readEnv: (env: string) => string | undefined;
- export declare const coerceInteger: (value: unknown) => number;
- export declare const coerceFloat: (value: unknown) => number;
- export declare const coerceBoolean: (value: unknown) => boolean;
- export declare const maybeCoerceInteger: (value: unknown) => number | undefined;
- export declare const maybeCoerceFloat: (value: unknown) => number | undefined;
- export declare const maybeCoerceBoolean: (value: unknown) => boolean | undefined;
- export declare function isEmptyObj(obj: Object | null | undefined): boolean;
- export declare function hasOwn(obj: Object, key: string): boolean;
- export declare function debug(action: string, ...args: any[]): void;
- export declare const isRunningInBrowser: () => boolean;
- export interface HeadersProtocol {
- get: (header: string) => string | null | undefined;
- }
- export type HeadersLike = Record<string, string | string[] | undefined> | HeadersProtocol;
- export declare const isHeadersProtocol: (headers: any) => headers is HeadersProtocol;
- export declare const getRequiredHeader: (headers: HeadersLike | Headers, header: string) => string;
- export declare const getHeader: (headers: HeadersLike | Headers, header: string) => string | undefined;
- /**
- * Encodes a string to Base64 format.
- */
- export declare const toBase64: (str: string | null | undefined) => string;
- /**
- * Converts a Base64 encoded string to a Float32Array.
- * @param base64Str - The Base64 encoded string.
- * @returns An Array of numbers interpreted as Float32 values.
- */
- export declare const toFloat32Array: (base64Str: string) => Array<number>;
- export declare function isObj(obj: unknown): obj is Record<string, unknown>;
- //# sourceMappingURL=core.d.ts.map
|