UNPKG

phonic

Version:

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FPhonic-Co%2Fphonic-node) [![npm shield](htt

159 lines (158 loc) 9.96 kB
// This file was auto-generated by Fern from our API Definition. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { normalizeClientOptionsWithAuth } from "../../../../BaseClient.mjs"; import { mergeHeaders } from "../../../../core/headers.mjs"; import * as core from "../../../../core/index.mjs"; import * as environments from "../../../../environments.mjs"; import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.mjs"; import * as errors from "../../../../errors/index.mjs"; import * as Phonic from "../../../index.mjs"; export class AuthClient { constructor(options = {}) { this._options = normalizeClientOptionsWithAuth(options); } /** * Creates a short-lived session token that can be used to authenticate WebSocket connections. Session tokens are useful for client-side applications where you don't want to expose your API key. * * @param {Phonic.CreateSessionTokenRequest} request * @param {AuthClient.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.BadRequestError} * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.ForbiddenError} * @throws {@link Phonic.InternalServerError} * * @example * await client.auth.createSessionToken({ * ttl_seconds: 300 * }) */ createSessionToken(request = {}, requestOptions) { return core.HttpResponsePromise.fromPromise(this.__createSessionToken(request, requestOptions)); } __createSessionToken() { return __awaiter(this, arguments, void 0, function* (request = {}, requestOptions) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const _authRequest = yield this._options.authProvider.getAuthRequest(); const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, "auth/session_token"), method: "POST", headers: _headers, contentType: "application/json", queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, requestType: "json", body: request, timeoutMs: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 60) * 1000, maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch, logging: this._options.logging, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse, }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 400: throw new Phonic.BadRequestError(_response.error.body, _response.rawResponse); case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 403: throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/auth/session_token"); }); } /** * Creates a short-lived conversation token scoped to a specific agent. Conversation tokens are useful for client-side applications that start a conversation with a single agent without exposing your API key. * * @param {Phonic.CreateConversationTokenRequest} request * @param {AuthClient.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.BadRequestError} * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.ForbiddenError} * @throws {@link Phonic.NotFoundError} * @throws {@link Phonic.InternalServerError} * * @example * await client.auth.createConversationToken({ * agent_id: "agent_12cf6e88-c254-4d3e-a149-a7f1bdd22783", * ttl_seconds: 30 * }) */ createConversationToken(request, requestOptions) { return core.HttpResponsePromise.fromPromise(this.__createConversationToken(request, requestOptions)); } __createConversationToken(request, requestOptions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const _authRequest = yield this._options.authProvider.getAuthRequest(); const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, "auth/conversation_token"), method: "POST", headers: _headers, contentType: "application/json", queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, requestType: "json", body: request, timeoutMs: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 60) * 1000, maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch, logging: this._options.logging, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse, }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 400: throw new Phonic.BadRequestError(_response.error.body, _response.rawResponse); case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 403: throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse); case 404: throw new Phonic.NotFoundError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/auth/conversation_token"); }); } }