From 10dc59025a3d81ee7f9f6e73c90afb92f1c041ee Mon Sep 17 00:00:00 2001 From: Robert Lesser Date: Thu, 8 Aug 2024 13:55:36 -0400 Subject: [PATCH] first stuff on openapi fetch --- package-lock.json | 16 +++++++++++++++- package.json | 3 ++- src/viewer.ts | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0536e68..599229f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "dependencies": { "apache-arrow": "^12.0.1", "dotenv": "^16.0.3", - "js-yaml": "^4.1.0" + "js-yaml": "^4.1.0", + "openapi-fetch": "^0.10.4" }, "devDependencies": { "@auth0/auth0-react": "^2.1.1", @@ -6119,6 +6120,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/openapi-fetch": { + "version": "0.10.4", + "resolved": "https://registry.npmjs.org/openapi-fetch/-/openapi-fetch-0.10.4.tgz", + "integrity": "sha512-HmnJNF3zDKPUwu2YKjm06roQ0MwSnT01x+QPQU6nLqiurE1YnRZhgJlBABEcW2PV+sTtMYbXbO7lRbKZMKvT1Q==", + "dependencies": { + "openapi-typescript-helpers": "^0.0.11" + } + }, "node_modules/openapi-typescript": { "version": "6.7.6", "resolved": "https://registry.npmjs.org/openapi-typescript/-/openapi-typescript-6.7.6.tgz", @@ -6136,6 +6145,11 @@ "openapi-typescript": "bin/cli.js" } }, + "node_modules/openapi-typescript-helpers": { + "version": "0.0.11", + "resolved": "https://registry.npmjs.org/openapi-typescript-helpers/-/openapi-typescript-helpers-0.0.11.tgz", + "integrity": "sha512-xofUHlVFq+BMquf3nh9I8N2guHckW6mrDO/F3kaFgrL7MGbjldDnQ9TIT+rkH/+H0LiuO+RuZLnNmsJwsjwUKg==" + }, "node_modules/openapi-typescript/node_modules/supports-color": { "version": "9.4.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-9.4.0.tgz", diff --git a/package.json b/package.json index 02193f8..6c69085 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "dependencies": { "apache-arrow": "^12.0.1", "dotenv": "^16.0.3", - "js-yaml": "^4.1.0" + "js-yaml": "^4.1.0", + "openapi-fetch": "^0.10.4" } } diff --git a/src/viewer.ts b/src/viewer.ts index 8328d45..11247d5 100644 --- a/src/viewer.ts +++ b/src/viewer.ts @@ -1,5 +1,7 @@ import { Table, tableFromIPC } from 'apache-arrow'; import { version } from './version'; +import type { paths } from './type-gen/openapi'; +import createClient, { type Middleware } from 'openapi-fetch'; export class AtlasViewer { /* @@ -15,6 +17,7 @@ export class AtlasViewer { // The location of the endpoint being called. Usually api-atlas.nomic.ai, but may // differ in testing or enterprise deployments. apiLocation: string; + apiClient: ReturnType>; /**` * @@ -64,6 +67,38 @@ export class AtlasViewer { this.anonymous = true; this.credentials = Promise.resolve(null); } + + const protocol = this.apiLocation.startsWith('localhost') + ? 'http' + : 'https'; + + this.apiClient = createClient({ + baseUrl: `${protocol}://${this.apiLocation}`, + }); + + this.apiClient.use({ + // Add a middleware to add the Authorization header to all requests + onRequest: async ({ request, options }) => { + const credentials = await this.credentials; + if (credentials && credentials.token) { + request.headers.set('Authorization', `Bearer ${credentials.token}`); + } + request.headers.set('User-Agent', `ts-nomic/${version}`); + }, + // Add a middleware to handle errors + onResponse: async ({ response }) => { + // This is a holdover from the old apiCall method. Do we still want to do this? + if (response.status < 200 || response.status > 299) { + const responseBody = await response.text(); + throw new APIError( + response.status, + response.statusText, + response.headers, + responseBody + ); + } + }, + }); } /**