UNPKG

openai

Version:

The official TypeScript library for the OpenAI API

441 lines (416 loc) • 12 kB
// File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details. import { APIResource } from '../../../../core/resource'; import * as RolesAPI from '../roles'; import { RolesPage } from '../roles'; import { APIPromise } from '../../../../core/api-promise'; import { NextCursorPage, type NextCursorPageParams, PagePromise } from '../../../../core/pagination'; import { RequestOptions } from '../../../../internal/request-options'; import { path } from '../../../../internal/utils/path'; function resolveResourceRequestOptions( options: RequestOptions | undefined, buildOptions: (options: RequestOptions | undefined) => RequestOptions | Promise<RequestOptions>, ): Promise<RequestOptions> { return Promise.resolve(options).then(buildOptions); } // Recognizable options across SDK runtime versions. Keep this independent of // private RequestOptions fields so older handwritten runtimes still compile. const normalizeRequestOptionsForQueryKeys = new Set([ 'method', 'path', 'query', 'body', 'headers', 'maxRetries', 'stream', 'timeout', 'httpAgent', 'fetchOptions', 'signal', 'idempotencyKey', 'defaultBaseURL', '__metadata', '__binaryRequest', '__binaryResponse', '__streamClass', '__streamMode', '__security', '__synthesizeEventData', ]); function normalizeRequestOptionsForQuery( value: unknown, queryKeys: ReadonlyArray<string>, options: RequestOptions | undefined, ): | ({ [K in 'headers' | 'maxRetries' | 'timeout' | 'signal' | 'idempotencyKey' | 'query']?: RequestOptions[K]; } & { [ K in | 'method' | 'path' | 'body' | 'stream' | 'httpAgent' | 'fetchOptions' | 'defaultBaseURL' | '__metadata' | '__binaryRequest' | '__binaryResponse' | '__streamClass' | '__streamMode' | '__security' | '__synthesizeEventData' ]?: never; }) | undefined { if (typeof value !== 'object' || value === null) return undefined; // Optional never fields can still be explicitly undefined unless consumers // enable exactOptionalPropertyTypes. Snapshot data without invoking getters. const entries = Object.entries(Object.getOwnPropertyDescriptors(value)).filter( ([, descriptor]) => descriptor.enumerable && (!('value' in descriptor) || descriptor.value !== undefined), ); const keys = entries.map(([key]) => key); const requestOnly = keys.some( (key) => normalizeRequestOptionsForQueryKeys.has(key) && !queryKeys.includes(key), ); if (!requestOnly) return undefined; // Declared query fields, including stream, must use the query argument. // Mixing them with request-only options is ambiguous and could change the return type. if ( options !== undefined || keys.some((key) => !normalizeRequestOptionsForQueryKeys.has(key) || queryKeys.includes(key)) ) { throw new TypeError('Query parameters and request options must be passed as separate arguments.'); } // The query position must not gain authority to change the request destination // or transport. Those overrides require the explicit request options argument. if ( keys.some( (key) => !['headers', 'maxRetries', 'timeout', 'signal', 'idempotencyKey', 'query'].includes(key), ) ) { throw new TypeError('Pass transport overrides in the explicit request options argument.'); } // Copy only the validated fields. Spreading value would reintroduce undefined // transport overrides, and deleting them would mutate the caller's object. return Object.fromEntries( entries.map(([key, descriptor]) => { if ('value' in descriptor) return [key, descriptor.value]; return [key, descriptor.get ? Reflect.apply(descriptor.get, value, []) : undefined]; }), ) as { [K in 'headers' | 'maxRetries' | 'timeout' | 'signal' | 'idempotencyKey' | 'query']?: RequestOptions[K]; } & { [ K in | 'method' | 'path' | 'body' | 'stream' | 'httpAgent' | 'fetchOptions' | 'defaultBaseURL' | '__metadata' | '__binaryRequest' | '__binaryResponse' | '__streamClass' | '__streamMode' | '__security' | '__synthesizeEventData' ]?: never; }; } export class Roles extends APIResource { /** * Creates a custom role for a project. * * @example * ```ts * const role = * await client.admin.organization.projects.roles.create( * 'project_id', * { permissions: ['string'], role_name: 'role_name' }, * ); * ``` */ create(projectID: string, body: RoleCreateParams, options?: RequestOptions): APIPromise<RolesAPI.Role> { return this._client.post( path`/projects/${projectID}/roles`, resolveResourceRequestOptions(options, (options) => ({ body, ...options, __security: { adminAPIKeyAuth: true }, })), ); } /** * Retrieves a project role. * * @example * ```ts * const role = * await client.admin.organization.projects.roles.retrieve( * 'role_id', * { project_id: 'project_id' }, * ); * ``` */ retrieve(roleID: string, params: RoleRetrieveParams, options?: RequestOptions): APIPromise<RolesAPI.Role> { const { project_id } = params; return this._client.get( path`/projects/${project_id}/roles/${roleID}`, resolveResourceRequestOptions(options, (options) => ({ ...options, __security: { adminAPIKeyAuth: true }, })), ); } /** * Updates an existing project role. * * @example * ```ts * const role = * await client.admin.organization.projects.roles.update( * 'role_id', * { project_id: 'project_id' }, * ); * ``` */ update(roleID: string, params: RoleUpdateParams, options?: RequestOptions): APIPromise<RolesAPI.Role> { const { project_id, ...body } = params; return this._client.post( path`/projects/${project_id}/roles/${roleID}`, resolveResourceRequestOptions(options, (options) => ({ body, ...options, __security: { adminAPIKeyAuth: true }, })), ); } /** * Lists the roles configured for a project. * * @example * ```ts * // Automatically fetches more pages as needed. * for await (const role of client.admin.organization.projects.roles.list( * 'project_id', * )) { * // ... * } * ``` */ list( projectID: string, query?: | (RoleListParams & ( | { [ K in | 'method' | 'path' | 'query' | 'body' | 'headers' | 'maxRetries' | 'stream' | 'timeout' | 'httpAgent' | 'fetchOptions' | 'signal' | 'idempotencyKey' | 'defaultBaseURL' | '__metadata' | '__binaryRequest' | '__binaryResponse' | '__streamClass' | '__streamMode' | '__security' | '__synthesizeEventData' ]?: never; } | null | undefined )) | null | undefined, options?: RequestOptions, ): PagePromise<RolesPage, RolesAPI.Role>; list( projectID: string, options?: { [K in 'headers' | 'maxRetries' | 'timeout' | 'signal' | 'idempotencyKey' | 'query']?: RequestOptions[K]; } & { [ K in | 'method' | 'path' | 'body' | 'stream' | 'httpAgent' | 'fetchOptions' | 'defaultBaseURL' | '__metadata' | '__binaryRequest' | '__binaryResponse' | '__streamClass' | '__streamMode' | '__security' | '__synthesizeEventData' ]?: never; }, ): PagePromise<RolesPage, RolesAPI.Role>; list( projectID: string, query: | RoleListParams | ({ [ K in 'headers' | 'maxRetries' | 'timeout' | 'signal' | 'idempotencyKey' | 'query' ]?: RequestOptions[K]; } & { [ K in | 'method' | 'path' | 'body' | 'stream' | 'httpAgent' | 'fetchOptions' | 'defaultBaseURL' | '__metadata' | '__binaryRequest' | '__binaryResponse' | '__streamClass' | '__streamMode' | '__security' | '__synthesizeEventData' ]?: never; }) | null | undefined = {}, options?: RequestOptions, ): PagePromise<RolesPage, RolesAPI.Role> { const normalizeRequestOptionsForQueryOptions = normalizeRequestOptionsForQuery( query, ['after', 'limit', 'order'], options, ); if (normalizeRequestOptionsForQueryOptions !== undefined) { options = normalizeRequestOptionsForQueryOptions; query = {}; } query = query as RoleListParams | null | undefined; return this._client.getAPIList( path`/projects/${projectID}/roles`, NextCursorPage<RolesAPI.Role>, resolveResourceRequestOptions(options, (options) => ({ query, ...options, __security: { adminAPIKeyAuth: true }, })), ); } /** * Deletes a custom role from a project. * * @example * ```ts * const role = * await client.admin.organization.projects.roles.delete( * 'role_id', * { project_id: 'project_id' }, * ); * ``` */ delete(roleID: string, params: RoleDeleteParams, options?: RequestOptions): APIPromise<RoleDeleteResponse> { const { project_id } = params; return this._client.delete( path`/projects/${project_id}/roles/${roleID}`, resolveResourceRequestOptions(options, (options) => ({ ...options, __security: { adminAPIKeyAuth: true }, })), ); } } /** * Confirmation payload returned after deleting a role. */ export interface RoleDeleteResponse { /** * Identifier of the deleted role. */ id: string; /** * Whether the role was deleted. */ deleted: boolean; /** * Always `role.deleted`. */ object: 'role.deleted'; } export interface RoleCreateParams { /** * Permissions to grant to the role. */ permissions: Array<string>; /** * Unique name for the role. */ role_name: string; /** * Optional description of the role. */ description?: string | null; } export interface RoleRetrieveParams { /** * The ID of the project. */ project_id: string; } export interface RoleUpdateParams { /** * Path param: The ID of the project to update. */ project_id: string; /** * Body param: New description for the role. */ description?: string | null; /** * Body param: Updated set of permissions for the role. */ permissions?: Array<string> | null; /** * Body param: New name for the role. */ role_name?: string | null; } export interface RoleListParams extends NextCursorPageParams { /** * Sort order for the returned roles. */ order?: 'asc' | 'desc'; } export interface RoleDeleteParams { /** * The ID of the project to update. */ project_id: string; } export declare namespace Roles { export { type RoleDeleteResponse as RoleDeleteResponse, type RoleCreateParams as RoleCreateParams, type RoleRetrieveParams as RoleRetrieveParams, type RoleUpdateParams as RoleUpdateParams, type RoleListParams as RoleListParams, type RoleDeleteParams as RoleDeleteParams, }; } export { type RolesPage };