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

feat(catalog): migrate API calls to sdk CatalogClient #1572

Merged
merged 13 commits into from
Oct 30, 2024
Merged
1 change: 1 addition & 0 deletions apps/console/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
NEXT_PUBLIC_GENERAL_API_VERSION=v1beta
NEXT_PUBLIC_MODEL_API_VERSION=v1alpha
NEXT_PUBLIC_CATALOG_API_VERSION=v1alpha
NEXT_PUBLIC_INSTILL_AI_USER_COOKIE_NAME=instill-ai-user
NEXT_PUBLIC_CONSOLE_EDITION=local-ce:dev

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "instill-sdk",
"version": "0.11.0-rc.0",
"version": "0.11.0-rc.1",
"description": "Instill AI's Typescript SDK",
"repository": "https://github.com/instill-ai/typescript-sdk.git",
"bugs": "https://github.com/instill-ai/community/issues",
Expand Down
1 change: 0 additions & 1 deletion packages/sdk/src/application/ApplicationClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { getInstillAdditionalHeaders, getQueryString } from "../helper";
import { APIResource } from "../main/resource";
import {
Expand Down
350 changes: 349 additions & 1 deletion packages/sdk/src/catalog/CatalogClient.ts
Original file line number Diff line number Diff line change
@@ -1 +1,349 @@
//TODO migrate the catalog APIs to the SDK
import { getInstillAdditionalHeaders, getQueryString } from "../helper";
import { APIResource } from "../main/resource";
import {
Catalog,
Chunk,
CreateCatalogRequest,
CreateCatalogResponse,
DeleteCatalogFileRequest,
DeleteCatalogRequest,
File,
FileContent,
GetCatalogSingleSourceOfTruthFileRequest,
GetCatalogSingleSourceOfTruthFileResponse,
GetFileDetailsRequest,
GetFileDetailsResponse,
ListCatalogFilesRequest,
ListCatalogFilesResponse,
ListCatalogsRequest,
ListCatalogsResponse,
ListChunksRequest,
ListChunksResponse,
ProcessCatalogFilesRequest,
ProcessCatalogFilesResponse,
UpdateCatalogRequest,
UpdateCatalogResponse,
UpdateChunkRequest,
UpdateChunkResponse,
UploadCatalogFileRequest,
UploadCatalogFileResponse,
} from "./types";

export class CatalogClient extends APIResource {
async listCatalogs(
props: ListCatalogsRequest & { enablePagination: true },
): Promise<ListCatalogsResponse>;
async listCatalogs(
props: ListCatalogsRequest & { enablePagination: false },
): Promise<Catalog[]>;
async listCatalogs(
props: ListCatalogsRequest & { enablePagination: boolean },
): Promise<ListCatalogsResponse | Catalog[]> {
const { pageSize, pageToken, view, enablePagination, namespaceId } = props;

try {
const catalogs: Catalog[] = [];

const queryString = getQueryString({
baseURL: `/namespaces/${namespaceId}/catalogs`,
pageSize,
pageToken,
view,
});

const data = await this._client.get<ListCatalogsResponse>(queryString);

if (enablePagination) {
return Promise.resolve(data);
}

catalogs.push(...data.catalogs);

if (data.nextPageToken) {
catalogs.push(
...(await this.listCatalogs({
pageSize,
pageToken: data.nextPageToken,
enablePagination: false,
view,
namespaceId,
})),
);
}

return Promise.resolve(catalogs);
} catch (err) {
return Promise.reject(err);
}
}

async createCatalog(
props: CreateCatalogRequest,
): Promise<CreateCatalogResponse> {
const { namespaceId, payload } = props;

try {
const queryString = getQueryString({
baseURL: `/namespaces/${namespaceId}/catalogs`,
});

const data = await this._client.post<{ catalog: Catalog }>(queryString, {
body: JSON.stringify(payload),
});

return Promise.resolve(data.catalog);
} catch (error) {
return Promise.reject(error);
}
}

async updateCatalog(
props: UpdateCatalogRequest,
): Promise<UpdateCatalogResponse> {
const { namespaceId, catalogId, ...payload } = props;

try {
const queryString = getQueryString({
baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}`,
});

const data = await this._client.put<{ catalog: Catalog }>(queryString, {
body: JSON.stringify(payload),
});

return Promise.resolve(data.catalog);
} catch (error) {
return Promise.reject(error);
}
}

async deleteCatalog(props: DeleteCatalogRequest): Promise<void> {
const { namespaceId, catalogId } = props;

try {
const queryString = getQueryString({
baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}`,
});

await this._client.delete(queryString);
return Promise.resolve();
} catch (error) {
return Promise.reject(error);
}
}

async uploadCatalogFile(
props: UploadCatalogFileRequest,
): Promise<UploadCatalogFileResponse> {
const { namespaceId, catalogId, payload } = props;

try {
const queryString = getQueryString({
baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files`,
});

const data = await this._client.post<{ file: File }>(queryString, {
body: JSON.stringify(payload),
});

return Promise.resolve(data.file);
} catch (error) {
return Promise.reject(error);
}
}

async listCatalogFiles(
props: ListCatalogFilesRequest & { enablePagination: true },
): Promise<ListCatalogFilesResponse>;
async listCatalogFiles(
props: ListCatalogFilesRequest & { enablePagination: false },
): Promise<File[]>;
async listCatalogFiles(
props: ListCatalogFilesRequest & { enablePagination: boolean },
): Promise<ListCatalogFilesResponse | File[]> {
const { namespaceId, catalogId, pageSize, pageToken, enablePagination } =
props;

try {
const files: File[] = [];

const queryString = getQueryString({
baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files`,
pageSize,
pageToken,
});

const data =
await this._client.get<ListCatalogFilesResponse>(queryString);

if (enablePagination) {
return Promise.resolve(data);
}

files.push(...data.files);

if (data.nextPageToken) {
files.push(
...(await this.listCatalogFiles({
namespaceId,
catalogId,
pageSize,
pageToken: data.nextPageToken,
enablePagination: false,
})),
);
}

return Promise.resolve(files);
} catch (error) {
return Promise.reject(error);
}
}

async deleteCatalogFile(props: DeleteCatalogFileRequest): Promise<void> {
const { fileUid } = props;

try {
await this._client.delete(`/catalogs/files?fileUid=${fileUid}`);
return Promise.resolve();
} catch (error) {
return Promise.reject(error);
}
}

async getFileDetails(
props: GetFileDetailsRequest,
): Promise<GetFileDetailsResponse> {
const { namespaceId, catalogId, fileId } = props;

try {
const queryString = getQueryString({
baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files/${fileId}`,
});

const data = await this._client.get<{ file: File }>(queryString);
return Promise.resolve(data.file);
} catch (error) {
return Promise.reject(error);
}
}

async getCatalogSingleSourceOfTruthFile(
props: GetCatalogSingleSourceOfTruthFileRequest,
): Promise<GetCatalogSingleSourceOfTruthFileResponse> {
const { namespaceId, catalogId, fileUid } = props;

try {
const queryString = getQueryString({
baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files/${fileUid}/source`,
});

const data = await this._client.get<{ sourceFile: FileContent }>(
queryString,
);
return Promise.resolve(data.sourceFile);
} catch (error) {
return Promise.reject(error);
}
}

async processCatalogFiles(
props: ProcessCatalogFilesRequest,
): Promise<ProcessCatalogFilesResponse> {
const { fileUids, requesterUid } = props;

if (!requesterUid) {
return Promise.reject(new Error("requesterUid not provided"));
}

try {
const queryString = getQueryString({
baseURL: `/catalogs/files/processAsync`,
});

const additionalHeaders = getInstillAdditionalHeaders({
requesterUid: requesterUid,
});

const data = await this._client.post<{ files: File[] }>(queryString, {
body: JSON.stringify({ file_uids: fileUids }),
additionalHeaders,
});

return Promise.resolve(data.files);
} catch (error) {
return Promise.reject(error);
}
}

async listChunks(
props: ListChunksRequest & { enablePagination: true },
): Promise<ListChunksResponse>;
async listChunks(
props: ListChunksRequest & { enablePagination: false },
): Promise<Chunk[]>;
async listChunks(
props: ListChunksRequest & { enablePagination: boolean },
): Promise<ListChunksResponse | Chunk[]> {
const {
namespaceId,
catalogId,
fileUid,
pageSize,
pageToken,
enablePagination,
} = props;

try {
const baseUrl = `/namespaces/${namespaceId}/catalogs/${catalogId}/chunks?fileUid=${fileUid}`;

const queryString = getQueryString({
baseURL: baseUrl,
pageSize,
pageToken,
});

const data = await this._client.get<ListChunksResponse>(queryString);

if (enablePagination) {
return Promise.resolve(data);
}

const chunks = data.chunks || [];
if (data.nextPageToken) {
chunks.push(
...(await this.listChunks({
namespaceId,
catalogId,
fileUid,
pageSize,
pageToken: data.nextPageToken,
enablePagination: false,
})),
);
}

return Promise.resolve(chunks);
} catch (error) {
return Promise.reject(error);
}
}

async updateChunk(props: UpdateChunkRequest): Promise<UpdateChunkResponse> {
const { chunkUid, retrievable } = props;

try {
const queryString = getQueryString({
baseURL: `/chunks/${chunkUid}`,
});

const data = await this._client.post<{ chunk: Chunk }>(queryString, {
body: JSON.stringify({ retrievable }),
});

return Promise.resolve(data.chunk);
} catch (error) {
return Promise.reject(error);
}
}
}
Loading
Loading