Skip to content

Commit

Permalink
Expose warnings in upsertDocuments API result (#40)
Browse files Browse the repository at this point in the history
* Expose warnings in upsertDocuments API result

* Improve error message

* Switch to cortexes API path

* Fix up status code expectations
  • Loading branch information
chaosrealm authored Jul 30, 2024
1 parent 879972e commit a2efb7b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
24 changes: 16 additions & 8 deletions src/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export type DocumentPaginationOpts = {

export type CreateCatalogConfig = CatalogConfig & { catalogName: string };

export type UpsertDocumentsResult = { warnings: string[] };

export class Catalog {
private deleted = false;
private constructor(
Expand All @@ -49,7 +51,7 @@ export class Catalog {

static async get(apiClient: CortexApiClient, name: string): Promise<Catalog> {
const res = await apiClient.GET(`/catalogs/${name}`);
if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to get catalog: ${res.statusText}`);
}
const body = await res.json();
Expand Down Expand Up @@ -77,7 +79,7 @@ export class Catalog {
res = await apiClient.PUT(`/catalogs/${name}`, config);
}

if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to configure catalog: ${res.statusText}`);
}
return new Catalog(config, apiClient, name);
Expand Down Expand Up @@ -119,12 +121,14 @@ export class Catalog {
public async truncate() {
this.checkDeleted();
const res = await this.apiClient.POST(`/catalogs/${this.name}/truncate`);
if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to get catalog: ${res.statusText}`);
}
}

public async upsertDocuments(batch: DocumentBatch) {
public async upsertDocuments(
batch: DocumentBatch,
): Promise<UpsertDocumentsResult> {
if (batch.length === 0) {
throw new Error("Document batch must not be empty");
}
Expand All @@ -135,8 +139,9 @@ export class Catalog {
let hasJson = false;
let hasUrl = false;
let hasSitemapUrl = false;
for (const doc of batch) {
switch (doc.contentType) {
for (const [index, doc] of batch.entries()) {
const contentType = doc.contentType;
switch (contentType) {
case "markdown":
case "text":
hasText = true;
Expand All @@ -155,8 +160,7 @@ export class Catalog {
break;
default:
throw new Error(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
`unsupported content type: ${(doc as any).contentType}`,
`Unsupported content type: ${contentType} for document at index ${index}`,
);
}
}
Expand Down Expand Up @@ -192,6 +196,10 @@ export class Catalog {
if (res.status > 202) {
throw new Error(`Failed to upsert documents: ${res.statusText}`);
}

const body = await res.json();

return { warnings: body?.warnings ?? [] };
}

async delete() {
Expand Down
8 changes: 4 additions & 4 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export class Content {
`/content/${this._id}/version/${version}`,
);

if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to revert content: ${res.statusText}`);
}

Expand All @@ -391,7 +391,7 @@ export class Content {
status,
});

if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to set content status: ${res.statusText}`);
}

Expand All @@ -408,7 +408,7 @@ export class Content {
publishTarget,
});

if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to publish content: ${res.statusText}`);
}

Expand All @@ -421,7 +421,7 @@ export class Content {
async unpublish() {
const res = await this.apiClient.POST(`/content/${this._id}/unpublish`);

if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to unpublish content: ${res.statusText}`);
}

Expand Down
12 changes: 6 additions & 6 deletions src/cortex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class Cortex {
) {}

static async get(apiClient: CortexApiClient, name: string): Promise<Cortex> {
const res = await apiClient.GET(`/cortex-config/${name}`);
const res = await apiClient.GET(`/cortexes/${name}`);
if (res.status !== 200) {
throw new Error(`Failed to get cortex: ${res.statusText}`);
}
Expand Down Expand Up @@ -162,15 +162,15 @@ export class Cortex {
personality: config.customizations?.personality,
rules: config.customizations?.rules,
};
const getRes = await apiClient.GET(`/cortex-config/${name}`);
const getRes = await apiClient.GET(`/cortexes/${name}`);
let res: Response;
if (getRes.status !== 200) {
res = await apiClient.POST("/cortex-config", input);
res = await apiClient.POST("/cortexes", input);
} else {
res = await apiClient.PUT(`/cortex-config/${name}`, input);
res = await apiClient.PUT(`/cortexes/${name}`, input);
}

if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to configure cortex: ${res.statusText}`);
}
return new Cortex(config, apiClient, name);
Expand All @@ -179,7 +179,7 @@ export class Cortex {
async delete() {
this.checkDeleted();
this.deleted = true;
await this.apiClient.DELETE(`/cortex-config/${this.name}`);
await this.apiClient.DELETE(`/cortexes/${this.name}`);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class Document {
const res = await this.apiClient.DELETE(
`/catalogs/${this.catalog.name}/documents/${encodeURIComponent(this.documentId)}`,
);
if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to delete document: ${res.statusText}`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/org.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class OrgConfig {
res = await client.PUT("/org-config", config);
}

if (res.status !== 200) {
if (res.status > 201) {
throw new Error(`Failed to configure org: ${res.statusText}`);
}

Expand Down

0 comments on commit a2efb7b

Please sign in to comment.