|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | + |
| 5 | +import * as environments from "../../../../environments.js"; |
| 6 | +import * as core from "../../../../core/index.js"; |
| 7 | +import * as Zep from "../../../index.js"; |
| 8 | +import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.js"; |
| 9 | +import * as serializers from "../../../../serialization/index.js"; |
| 10 | +import * as errors from "../../../../errors/index.js"; |
| 11 | + |
| 12 | +export declare namespace Task { |
| 13 | + export interface Options { |
| 14 | + environment?: core.Supplier<environments.ZepEnvironment | string>; |
| 15 | + /** Specify a custom URL to connect the client to. */ |
| 16 | + baseUrl?: core.Supplier<string>; |
| 17 | + apiKey?: core.Supplier<string | undefined>; |
| 18 | + /** Additional headers to include in requests. */ |
| 19 | + headers?: Record<string, string | core.Supplier<string | undefined> | undefined>; |
| 20 | + fetcher?: core.FetchFunction; |
| 21 | + } |
| 22 | + |
| 23 | + export interface RequestOptions { |
| 24 | + /** The maximum time to wait for a response in seconds. */ |
| 25 | + timeoutInSeconds?: number; |
| 26 | + /** The number of times to retry the request. Defaults to 2. */ |
| 27 | + maxRetries?: number; |
| 28 | + /** A hook to abort the request. */ |
| 29 | + abortSignal?: AbortSignal; |
| 30 | + /** Additional headers to include in the request. */ |
| 31 | + headers?: Record<string, string | core.Supplier<string | undefined> | undefined>; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export class Task { |
| 36 | + protected readonly _options: Task.Options; |
| 37 | + |
| 38 | + constructor(_options: Task.Options = {}) { |
| 39 | + this._options = _options; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets a task by its ID |
| 44 | + * |
| 45 | + * @param {string} taskId - Task ID |
| 46 | + * @param {Task.RequestOptions} requestOptions - Request-specific configuration. |
| 47 | + * |
| 48 | + * @throws {@link Zep.NotFoundError} |
| 49 | + * @throws {@link Zep.InternalServerError} |
| 50 | + * |
| 51 | + * @example |
| 52 | + * await client.task.get("task_id") |
| 53 | + */ |
| 54 | + public get(taskId: string, requestOptions?: Task.RequestOptions): core.HttpResponsePromise<Zep.GetTaskResponse> { |
| 55 | + return core.HttpResponsePromise.fromPromise(this.__get(taskId, requestOptions)); |
| 56 | + } |
| 57 | + |
| 58 | + private async __get( |
| 59 | + taskId: string, |
| 60 | + requestOptions?: Task.RequestOptions, |
| 61 | + ): Promise<core.WithRawResponse<Zep.GetTaskResponse>> { |
| 62 | + const _response = await (this._options.fetcher ?? core.fetcher)({ |
| 63 | + url: core.url.join( |
| 64 | + (await core.Supplier.get(this._options.baseUrl)) ?? |
| 65 | + (await core.Supplier.get(this._options.environment)) ?? |
| 66 | + environments.ZepEnvironment.Default, |
| 67 | + `tasks/${encodeURIComponent(taskId)}`, |
| 68 | + ), |
| 69 | + method: "GET", |
| 70 | + headers: mergeHeaders( |
| 71 | + this._options?.headers, |
| 72 | + mergeOnlyDefinedHeaders({ ...(await this._getCustomAuthorizationHeaders()) }), |
| 73 | + requestOptions?.headers, |
| 74 | + ), |
| 75 | + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, |
| 76 | + maxRetries: requestOptions?.maxRetries, |
| 77 | + abortSignal: requestOptions?.abortSignal, |
| 78 | + }); |
| 79 | + if (_response.ok) { |
| 80 | + return { |
| 81 | + data: serializers.GetTaskResponse.parseOrThrow(_response.body, { |
| 82 | + unrecognizedObjectKeys: "passthrough", |
| 83 | + allowUnrecognizedUnionMembers: true, |
| 84 | + allowUnrecognizedEnumValues: true, |
| 85 | + skipValidation: true, |
| 86 | + breadcrumbsPrefix: ["response"], |
| 87 | + }), |
| 88 | + rawResponse: _response.rawResponse, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + if (_response.error.reason === "status-code") { |
| 93 | + switch (_response.error.statusCode) { |
| 94 | + case 404: |
| 95 | + throw new Zep.NotFoundError( |
| 96 | + serializers.ApiError.parseOrThrow(_response.error.body, { |
| 97 | + unrecognizedObjectKeys: "passthrough", |
| 98 | + allowUnrecognizedUnionMembers: true, |
| 99 | + allowUnrecognizedEnumValues: true, |
| 100 | + skipValidation: true, |
| 101 | + breadcrumbsPrefix: ["response"], |
| 102 | + }), |
| 103 | + _response.rawResponse, |
| 104 | + ); |
| 105 | + case 500: |
| 106 | + throw new Zep.InternalServerError( |
| 107 | + serializers.ApiError.parseOrThrow(_response.error.body, { |
| 108 | + unrecognizedObjectKeys: "passthrough", |
| 109 | + allowUnrecognizedUnionMembers: true, |
| 110 | + allowUnrecognizedEnumValues: true, |
| 111 | + skipValidation: true, |
| 112 | + breadcrumbsPrefix: ["response"], |
| 113 | + }), |
| 114 | + _response.rawResponse, |
| 115 | + ); |
| 116 | + default: |
| 117 | + throw new errors.ZepError({ |
| 118 | + statusCode: _response.error.statusCode, |
| 119 | + body: _response.error.body, |
| 120 | + rawResponse: _response.rawResponse, |
| 121 | + }); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + switch (_response.error.reason) { |
| 126 | + case "non-json": |
| 127 | + throw new errors.ZepError({ |
| 128 | + statusCode: _response.error.statusCode, |
| 129 | + body: _response.error.rawBody, |
| 130 | + rawResponse: _response.rawResponse, |
| 131 | + }); |
| 132 | + case "timeout": |
| 133 | + throw new errors.ZepTimeoutError("Timeout exceeded when calling GET /tasks/{task_id}."); |
| 134 | + case "unknown": |
| 135 | + throw new errors.ZepError({ |
| 136 | + message: _response.error.errorMessage, |
| 137 | + rawResponse: _response.rawResponse, |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + protected async _getCustomAuthorizationHeaders() { |
| 143 | + const apiKeyValue = (await core.Supplier.get(this._options.apiKey)) ?? process?.env["ZEP_API_KEY"]; |
| 144 | + return { Authorization: `Api-Key ${apiKeyValue}` }; |
| 145 | + } |
| 146 | +} |
0 commit comments