|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | + |
| 5 | +import * as environments from "../../../../environments"; |
| 6 | +import * as core from "../../../../core"; |
| 7 | +import * as Credal from "../../../index"; |
| 8 | +import * as serializers from "../../../../serialization/index"; |
| 9 | +import urlJoin from "url-join"; |
| 10 | +import * as errors from "../../../../errors/index"; |
| 11 | + |
| 12 | +export declare namespace Actions { |
| 13 | + interface Options { |
| 14 | + environment?: core.Supplier<environments.CredalEnvironment | string>; |
| 15 | + apiKey?: core.Supplier<core.BearerToken | undefined>; |
| 16 | + fetcher?: core.FetchFunction; |
| 17 | + } |
| 18 | + |
| 19 | + interface RequestOptions { |
| 20 | + /** The maximum time to wait for a response in seconds. */ |
| 21 | + timeoutInSeconds?: number; |
| 22 | + /** The number of times to retry the request. Defaults to 2. */ |
| 23 | + maxRetries?: number; |
| 24 | + /** A hook to abort the request. */ |
| 25 | + abortSignal?: AbortSignal; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export class Actions { |
| 30 | + constructor(protected readonly _options: Actions.Options = {}) {} |
| 31 | + |
| 32 | + /** |
| 33 | + * Invoke an action, asking for human confirmation if necessary |
| 34 | + * |
| 35 | + * @param {Credal.InvokeActionRequest} request |
| 36 | + * @param {Actions.RequestOptions} requestOptions - Request-specific configuration. |
| 37 | + * |
| 38 | + * @example |
| 39 | + * await client.actions.invokeAction({ |
| 40 | + * actionId: "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32", |
| 41 | + * userEmail: "string", |
| 42 | + * requireHumanConfirmation: true, |
| 43 | + * humanConfirmationChannel: { |
| 44 | + * type: "directMessage", |
| 45 | + * channelId: "string" |
| 46 | + * }, |
| 47 | + * justification: "string", |
| 48 | + * auditLogId: "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32" |
| 49 | + * }) |
| 50 | + */ |
| 51 | + public async invokeAction( |
| 52 | + request: Credal.InvokeActionRequest, |
| 53 | + requestOptions?: Actions.RequestOptions |
| 54 | + ): Promise<Credal.InvokeActionResponse> { |
| 55 | + const _response = await (this._options.fetcher ?? core.fetcher)({ |
| 56 | + url: urlJoin( |
| 57 | + (await core.Supplier.get(this._options.environment)) ?? environments.CredalEnvironment.Production, |
| 58 | + "/v0/actions/invokeAction" |
| 59 | + ), |
| 60 | + method: "POST", |
| 61 | + headers: { |
| 62 | + Authorization: await this._getAuthorizationHeader(), |
| 63 | + "X-Fern-Language": "JavaScript", |
| 64 | + "X-Fern-SDK-Name": "@credal/sdk", |
| 65 | + "X-Fern-SDK-Version": "0.0.17", |
| 66 | + "User-Agent": "@credal/sdk/0.0.17", |
| 67 | + "X-Fern-Runtime": core.RUNTIME.type, |
| 68 | + "X-Fern-Runtime-Version": core.RUNTIME.version, |
| 69 | + }, |
| 70 | + contentType: "application/json", |
| 71 | + requestType: "json", |
| 72 | + body: serializers.InvokeActionRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }), |
| 73 | + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, |
| 74 | + maxRetries: requestOptions?.maxRetries, |
| 75 | + abortSignal: requestOptions?.abortSignal, |
| 76 | + }); |
| 77 | + if (_response.ok) { |
| 78 | + return serializers.InvokeActionResponse.parseOrThrow(_response.body, { |
| 79 | + unrecognizedObjectKeys: "passthrough", |
| 80 | + allowUnrecognizedUnionMembers: true, |
| 81 | + allowUnrecognizedEnumValues: true, |
| 82 | + breadcrumbsPrefix: ["response"], |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + if (_response.error.reason === "status-code") { |
| 87 | + throw new errors.CredalError({ |
| 88 | + statusCode: _response.error.statusCode, |
| 89 | + body: _response.error.body, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + switch (_response.error.reason) { |
| 94 | + case "non-json": |
| 95 | + throw new errors.CredalError({ |
| 96 | + statusCode: _response.error.statusCode, |
| 97 | + body: _response.error.rawBody, |
| 98 | + }); |
| 99 | + case "timeout": |
| 100 | + throw new errors.CredalTimeoutError(); |
| 101 | + case "unknown": |
| 102 | + throw new errors.CredalError({ |
| 103 | + message: _response.error.errorMessage, |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + protected async _getAuthorizationHeader(): Promise<string> { |
| 109 | + const bearer = (await core.Supplier.get(this._options.apiKey)) ?? process?.env["CREDAL_API_KEY"]; |
| 110 | + if (bearer == null) { |
| 111 | + throw new errors.CredalError({ |
| 112 | + message: "Please specify CREDAL_API_KEY when instantiating the client.", |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + return `Bearer ${bearer}`; |
| 117 | + } |
| 118 | +} |
0 commit comments