Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: introduce openapi fetch for object info fetching #63

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
35 changes: 35 additions & 0 deletions src/viewer.ts
Original file line number Diff line number Diff line change
@@ -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 {
/*
Expand All @@ -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<typeof createClient<paths>>;

/**`
*
Expand Down Expand Up @@ -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<paths>({
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
);
}
},
});
}

/**
Expand Down