Skip to content

Commit 514f919

Browse files
committed
minimal changes for compilation
1 parent 1785a24 commit 514f919

6 files changed

Lines changed: 83 additions & 52 deletions

File tree

apps/website/app/api/supabase/content-embedding/batch/route.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
KNOWN_EMBEDDING_TABLES,
1414
} from "~/utils/supabase/dbUtils";
1515
import {
16-
ApiInputEmbeddingItem,
17-
ApiOutputEmbeddingRecord,
16+
type ApiInputEmbeddingItem,
17+
type ApiOutputEmbeddingRecord,
1818
embeddingInputProcessing,
1919
embeddingOutputProcessing,
2020
} from "~/utils/supabase/validators";
@@ -35,7 +35,7 @@ const batchInsertEmbeddingsProcess = async (
3535
if (acc[model] === undefined) {
3636
acc[model] = [];
3737
}
38-
acc[model]!.push(item);
38+
acc[model].push(item);
3939
return acc;
4040
}, byModel);
4141
} catch (error) {
@@ -81,6 +81,7 @@ const batchInsertEmbeddingsProcess = async (
8181
return {
8282
data: globalResults,
8383
error: null,
84+
success: true,
8485
status: has_400 ? 400 : 500,
8586
count,
8687
statusText: partialErrors.join("; "),
@@ -89,6 +90,7 @@ const batchInsertEmbeddingsProcess = async (
8990
return {
9091
data: globalResults,
9192
error: null,
93+
success: true,
9294
status: created ? 201 : 200,
9395
count,
9496
statusText: created ? "created" : "success",

apps/website/app/api/supabase/document/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type DocumentRecord = Tables<"Document">;
1717
const validateDocument: ItemValidator<DocumentDataInput> = (data) => {
1818
if (!data || typeof data !== "object")
1919
return "Invalid request body: expected a JSON object.";
20+
// eslint-disable-next-line @typescript-eslint/naming-convention
2021
const { space_id, author_id, source_local_id } = data;
2122

2223
if (!author_id) return "Missing required author_id field.";

apps/website/app/api/supabase/platform-account/route.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "~/utils/supabase/apiUtils";
1111
import { type TablesInsert, Constants } from "@repo/database/dbTypes";
1212

13+
// eslint-disable-next-line @typescript-eslint/naming-convention
1314
const { AgentType, Platform } = Constants.public.Enums;
1415

1516
type PlatformAccountDataInput = TablesInsert<"PlatformAccount">;
@@ -19,6 +20,7 @@ const accountValidator: ItemValidator<PlatformAccountDataInput> = (
1920
) => {
2021
if (!account || typeof account !== "object")
2122
return "Invalid request body: expected a JSON object.";
23+
/* eslint-disable @typescript-eslint/naming-convention */
2224
const {
2325
name,
2426
platform,
@@ -29,6 +31,7 @@ const accountValidator: ItemValidator<PlatformAccountDataInput> = (
2931
metadata,
3032
dg_account,
3133
} = account;
34+
/* eslint-enable @typescript-eslint/naming-convention */
3235

3336
if (!name || typeof name !== "string" || name.trim() === "")
3437
return "Missing or invalid name";

apps/website/app/utils/supabase/dbUtils.ts

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
PostgrestResponse,
44
PostgrestSingleResponse,
55
} from "@supabase/supabase-js";
6+
import { PostgrestError } from "@supabase/supabase-js";
67
import type { Database, Tables, TablesInsert } from "@repo/database/dbTypes";
78

89
export const KNOWN_EMBEDDING_TABLES: {
@@ -95,7 +96,9 @@ export const getOrCreateEntity = async <
9596
}): Promise<PostgrestSingleResponse<Tables<TableName>>> => {
9697
const result: PostgrestSingleResponse<Tables<TableName>> = await supabase
9798
.from(tableName)
98-
.upsert(insertData, {
99+
// Typescript gets confused with latest supabase
100+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
101+
.upsert(insertData as any, {
99102
onConflict: uniqueOn === undefined ? undefined : uniqueOn.join(","),
100103
ignoreDuplicates: false,
101104
count: "estimated",
@@ -138,14 +141,15 @@ export const getOrCreateEntity = async <
138141
console.log(`Found ${tableName} on re-fetch:`, reFetchedEntity);
139142
// Note: Using a PostgrestResult means I cannot have both data and error non-null...
140143
return {
141-
error: {
144+
error: new PostgrestError({
142145
...result.error,
143146
message: `Upsert failed because of conflict with this entity: ${reFetchedEntity}"`,
144-
},
147+
}),
145148
statusText: result.statusText,
146149
data: null,
147150
count: null,
148151
status: 400,
152+
success: false,
149153
};
150154
}
151155
}
@@ -183,7 +187,9 @@ export const InsertValidatedBatch = async <
183187
}): Promise<PostgrestResponse<Tables<TableName>>> => {
184188
const result: PostgrestResponse<Tables<TableName>> = await supabase
185189
.from(tableName)
186-
.upsert(items, {
190+
// Typescript gets confused with latest supabase
191+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
192+
.upsert(items as any, {
187193
onConflict: uniqueOn === undefined ? undefined : uniqueOn.join(","),
188194
ignoreDuplicates: false,
189195
count: "estimated",
@@ -231,14 +237,14 @@ export const validateAndInsertBatch = async <
231237
const validationErrors: { index: number; error: string }[] = [];
232238
if (!Array.isArray(items) || items.length === 0) {
233239
return {
234-
error: {
240+
error: new PostgrestError({
235241
message: `Request body must be a non-empty array of ${tableName} items.`,
236242
details: "",
237-
hint: "",
243+
hint: "nonempty",
238244
code: "1",
239-
name: "nonempty",
240-
},
245+
}),
241246
status: 400,
247+
success: false,
242248
data: null,
243249
count: null,
244250
statusText: "Empty input",
@@ -269,13 +275,13 @@ export const validateAndInsertBatch = async <
269275

270276
if (validationErrors.length > 0) {
271277
return {
272-
error: {
278+
error: new PostgrestError({
273279
message: `Validation failed for one or more ${tableName} items.`,
274280
details: `${validationErrors}`,
275-
hint: "",
281+
hint: "invalid",
276282
code: "2",
277-
name: "invalid",
278-
},
283+
}),
284+
success: false,
279285
status: 400,
280286
data: null,
281287
count: null,
@@ -322,20 +328,21 @@ export const validateAndInsertBatch = async <
322328
// Erring on the side of returning data with an error in status.
323329
return {
324330
error: null,
331+
success: true,
325332
status: 500,
326333
data: validatedResults,
327334
count: validatedResults.length,
328335
statusText: `Validation failed for one or more ${tableName} items, and succeeded for ${validatedResults.length}/${result.data.length}.`,
329336
};
330337
} else {
331338
return {
332-
error: {
339+
error: new PostgrestError({
333340
message: `Post-validation failed for all ${tableName} items.`,
334341
details: `${validationErrors}`,
335-
hint: "",
342+
hint: "invalid",
336343
code: "2",
337-
name: "invalid",
338-
},
344+
}),
345+
success: false,
339346
status: 500,
340347
data: null,
341348
count: null,
@@ -366,17 +373,17 @@ export const processAndInsertBatch = async <
366373
inputProcessor: ItemProcessor<InputType, TablesInsert<TableName>>;
367374
outputProcessor: ItemProcessor<Tables<TableName>, OutputType>;
368375
}): Promise<PostgrestResponse<OutputType>> => {
369-
let processedItems: TablesInsert<TableName>[] = [];
376+
const processedItems: TablesInsert<TableName>[] = [];
370377
const validationErrors: { index: number; error: string }[] = [];
371378
if (!Array.isArray(items) || items.length === 0) {
372379
return {
373-
error: {
380+
error: new PostgrestError({
374381
message: `Request body must be a non-empty array of ${tableName} items.`,
375382
details: "",
376-
hint: "",
383+
hint: "nonempty",
377384
code: "1",
378-
name: "nonempty",
379-
},
385+
}),
386+
success: false,
380387
status: 400,
381388
data: null,
382389
count: null,
@@ -407,13 +414,13 @@ export const processAndInsertBatch = async <
407414

408415
if (validationErrors.length > 0) {
409416
return {
410-
error: {
417+
error: new PostgrestError({
411418
message: `Validation failed for one or more ${tableName} items.`,
412419
details: `${validationErrors}`,
413-
hint: "",
420+
hint: "invalid",
414421
code: "2",
415-
name: "invalid",
416-
},
422+
}),
423+
success: false,
417424
status: 400,
418425
data: null,
419426
count: null,
@@ -457,19 +464,20 @@ export const processAndInsertBatch = async <
457464
return {
458465
error: null,
459466
status: 500,
467+
success: true,
460468
data: processedResults,
461469
count: processedResults.length,
462470
statusText: `Validation failed for one or more ${tableName} items, and succeeded for ${processedResults.length}/${result.data.length}.`,
463471
};
464472
} else {
465473
return {
466-
error: {
474+
error: new PostgrestError({
467475
message: `Post-validation failed for all ${tableName} items.`,
468476
details: `${validationErrors}`,
469-
hint: "",
477+
hint: "invalid",
470478
code: "2",
471-
name: "invalid",
472-
},
479+
}),
480+
success: false,
473481
status: 500,
474482
data: null,
475483
count: null,

apps/website/app/utils/supabase/validators.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ export type ContentRecord = Tables<"Content">;
125125

126126
export const contentInputValidation: ItemValidator<ContentDataInput> = (
127127
data: ContentDataInput,
128-
) => {
128+
): string | null => {
129129
if (!data || typeof data !== "object")
130130
return "Invalid request body: expected a JSON object.";
131+
// eslint-disable-next-line @typescript-eslint/naming-convention
131132
const { author_id, created, last_modified, scale, space_id, text } = data;
132133

133134
if (!text || typeof text !== "string") return "Invalid or missing text.";

packages/database/src/lib/contextFunctions.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { createClient } from "@supabase/supabase-js";
2-
import type { Database, Enums, Tables, TablesInsert } from "@repo/database/dbTypes";
3-
import type { PostgrestSingleResponse } from "@supabase/supabase-js";
2+
import type {
3+
Database,
4+
Enums,
5+
Tables,
6+
TablesInsert,
7+
} from "@repo/database/dbTypes";
8+
import {
9+
type PostgrestSingleResponse,
10+
PostgrestError,
11+
} from "@supabase/supabase-js";
412
import type { FunctionsResponse } from "@supabase/functions-js";
513
import { nextApiRoot } from "@repo/utils/execContext";
614
import type { DGSupabaseClient } from "@repo/database/lib/client";
@@ -24,13 +32,13 @@ export const asPostgrestFailure = (
2432
): PostgrestSingleResponse<any> => {
2533
return {
2634
data: null,
27-
error: {
35+
success: false,
36+
error: new PostgrestError({
2837
message,
2938
code,
3039
details: "",
31-
hint: "",
32-
name: code,
33-
},
40+
hint: code,
41+
}),
3442
count: null,
3543
statusText: code,
3644
status,
@@ -73,9 +81,10 @@ export const fetchOrCreateSpaceIndirect = async (
7381
`Platform API failed: ${response.status} ${response.statusText} ${await response.text()}`,
7482
);
7583
}
76-
const data = await response.json() as SpaceRecord;
84+
const data = (await response.json()) as SpaceRecord;
7785
return {
7886
data,
87+
success: true,
7988
error: null,
8089
count: 1,
8190
status: 200,
@@ -96,16 +105,18 @@ const createSingletonClient = (uniqueKey: string): DGSupabaseClient | null => {
96105
throw new FatalError("Missing required Supabase environment variables");
97106
}
98107
if (lastStorageKey !== undefined && lastStorageKey !== uniqueKey) {
99-
console.error("Changed storage key")
108+
console.error("Changed storage key");
100109
// I.e. working on a new vault. Should never happen.
101110
// Break singleton pattern in that edge case.
102111
client = undefined;
103112
}
104113
if (client === undefined) {
105-
client = createClient<Database, "public">(url, key, {auth: {storageKey: `sb-${uniqueKey}-auth-token`}});
106-
if (client) {
107-
lastStorageKey = uniqueKey;
108-
}
114+
client = createClient<Database, "public">(url, key, {
115+
auth: { storageKey: `sb-${uniqueKey}-auth-token` },
116+
});
117+
if (client) {
118+
lastStorageKey = uniqueKey;
119+
}
109120
}
110121
return client;
111122
};
@@ -117,8 +128,11 @@ export const fetchOrCreateSpaceDirect = async (
117128
if (error !== null) return asPostgrestFailure(error, "invalid space");
118129
data.url = data.url.trim().replace(/\/$/, "");
119130
// Distinguish local, or various supabase branches
120-
const supabaseUrlFirstFragment = new URL(process.env.SUPABASE_URL || 'http://null').hostname.split('.')[0];
121-
const urlSlug = supabaseUrlFirstFragment+":"+data.name.replaceAll(/\W/g,"");
131+
const supabaseUrlFirstFragment = new URL(
132+
process.env.SUPABASE_URL || "http://null",
133+
).hostname.split(".")[0];
134+
const urlSlug =
135+
supabaseUrlFirstFragment + ":" + data.name.replaceAll(/\W/g, "");
122136
const supabase = createSingletonClient(urlSlug);
123137
if (!supabase) return asPostgrestFailure("No database", "");
124138
const session = await supabase.auth.getSession();
@@ -129,13 +143,14 @@ export const fetchOrCreateSpaceDirect = async (
129143
.select()
130144
.eq("url", data.url)
131145
.maybeSingle();
132-
if (result.error && result.status >= 500)
133-
return result;
146+
if (result.error && result.status >= 500) return result;
134147
if (result.data !== null)
135148
return result as PostgrestSingleResponse<SpaceRecord>;
136149
// space does not exist, or not visible from this account;
137150
// logout to be sure
138-
console.warn(`Creating a space while already logged in as ${session.data.session.user.email}; logging out`);
151+
console.warn(
152+
`Creating a space while already logged in as ${session.data.session.user.email}; logging out`,
153+
);
139154
await supabase.auth.refreshSession(); // this will clear an invalid session
140155
}
141156
// If it does not exist, create it
@@ -160,6 +175,7 @@ export const fetchOrCreateSpaceDirect = async (
160175
return {
161176
data: result2.data,
162177
error: null,
178+
success: true,
163179
count: 1,
164180
status: 200,
165181
statusText: "OK",
@@ -181,9 +197,9 @@ export const createLoggedInClient = async ({
181197
const session = await client.auth.getSession();
182198
if (session.data.session) {
183199
if (session.data.session.user.email === email)
184-
return client; // already logged in
200+
return client; // already logged in
185201
else {
186-
console.warn("Email crosstalk")
202+
console.warn("Email crosstalk");
187203
await client.auth.signOut();
188204
}
189205
}

0 commit comments

Comments
 (0)