-
Notifications
You must be signed in to change notification settings - Fork 6
ENG-1909 Add no-op Next.js content upsert endpoint #1220
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
base: eng-2017-add-converter-for-crossapp-schemas-to-localconceptdatainput
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { NextResponse, NextRequest } from "next/server"; | ||
|
|
||
| import { createClient } from "~/utils/supabase/server"; | ||
| import { | ||
| createApiResponse, | ||
| handleRouteError, | ||
| defaultOptionsHandler, | ||
| } from "~/utils/supabase/apiUtils"; | ||
| import { asPostgrestFailure } from "@repo/database/lib/contextFunctions"; | ||
| import type { Json } from "@repo/database/dbTypes"; | ||
| import { StandaloneCrossAppContent } from "@repo/database/crossAppContracts"; | ||
| import { crossAppStandaloneContentToDbContent } from "@repo/database/lib/crossAppConverters"; | ||
| import { getAccountId } from "~/utils/supabase/account"; | ||
|
|
||
| type ApiParams = Promise<{ id: string }>; | ||
| export type SegmentDataType = { params: ApiParams }; | ||
|
|
||
| export const POST = async ( | ||
| request: NextRequest, | ||
| segmentData: SegmentDataType, | ||
| ): Promise<NextResponse> => { | ||
| const { id: spaceIdS } = await segmentData.params; | ||
| const spaceId = Number.parseInt(spaceIdS); | ||
| if (Number.isNaN(spaceId)) | ||
| return createApiResponse( | ||
| request, | ||
| asPostgrestFailure("Cannot parse space id", "invalid", 403), | ||
| ); | ||
|
|
||
| const supabase = await createClient(); | ||
|
|
||
| const userId = await getAccountId(supabase); | ||
| if (userId === undefined) | ||
| return createApiResponse( | ||
| request, | ||
| asPostgrestFailure("Please login", "invalid", 401), | ||
| ); | ||
| try { | ||
| const body = (await request.json()) as StandaloneCrossAppContent[]; | ||
| // TODO: Zed validator | ||
| const content = body | ||
| .map((c) => | ||
| crossAppStandaloneContentToDbContent( | ||
| { | ||
| ...c, | ||
| createdAt: new Date(c.createdAt), | ||
| modifiedAt: new Date(c.modifiedAt || c.createdAt), | ||
| }, | ||
| { dbId: spaceId }, | ||
| ), | ||
| ) | ||
| .filter((c) => c !== undefined); | ||
| if (content.length === 0) throw new Error("Could not translate content"); | ||
| const result = await supabase.rpc("upsert_content", { | ||
| data: content as Json, | ||
| v_space_id: spaceId, | ||
| v_creator_id: userId, | ||
| content_as_document: true, | ||
| }); | ||
| return createApiResponse(request, result); | ||
| } catch (e: unknown) { | ||
| return handleRouteError(request, e, "/api/supabase/space/[id]/content"); | ||
| } | ||
| }; | ||
|
|
||
| export const OPTIONS = defaultOptionsHandler; | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,12 +3,14 @@ import { | |||||
| Ref, | ||||||
| CrossAppEmbedding, | ||||||
| InlineCrossAppContent, | ||||||
| StandaloneCrossAppContent, | ||||||
| CrossAppBase, | ||||||
| CrossAppNode, | ||||||
| } from "../crossAppContracts"; | ||||||
| import { LocalContentDataInput, LocalConceptDataInput } from "../inputTypes"; | ||||||
| import { Enums, CompositeTypes } from "../dbTypes"; | ||||||
|
|
||||||
| type ContentDataInput = CompositeTypes<"content_local_input">; | ||||||
| type InlineEmbeddingInput = CompositeTypes<"inline_embedding_input">; | ||||||
| type InlineAbstractBase = Partial<CrossAppBase>; | ||||||
|
|
||||||
|
|
@@ -39,6 +41,47 @@ const decodeRef = <DbVarName extends string, LocalVarName extends string>( | |||||
| return decodeLocalRef(ref, localVarName); | ||||||
| }; | ||||||
|
|
||||||
| const decodeRefWithNulls = < | ||||||
| DbVarName extends string, | ||||||
| LocalVarName extends string, | ||||||
| >( | ||||||
| ref: Ref | undefined, | ||||||
| dbVarName: DbVarName, | ||||||
| localVarName: LocalVarName, | ||||||
| ): Record<DbVarName, number | null> & Record<LocalVarName, string | null> => { | ||||||
| return { | ||||||
| [dbVarName]: ref && "dbId" in ref ? ref.dbId : null, | ||||||
| [localVarName]: ref && "localId" in ref ? ref.localId : null, | ||||||
| } as Record<DbVarName, number | null> & Record<LocalVarName, string | null>; | ||||||
| }; | ||||||
|
|
||||||
| const decodeRefWithInlineNulls = < | ||||||
| DbVarName extends string, | ||||||
| LocalVarName extends string, | ||||||
| InlineVarName extends string, | ||||||
| >({ | ||||||
| ref, | ||||||
| dbVarName, | ||||||
| localVarName, | ||||||
| inlineVarName, | ||||||
| }: { | ||||||
| ref?: Ref; | ||||||
| dbVarName: DbVarName; | ||||||
| localVarName: LocalVarName; | ||||||
| inlineVarName: InlineVarName; | ||||||
| }): Record<DbVarName, number | null> & | ||||||
| Record<LocalVarName, string | null> & | ||||||
| Record<InlineVarName, null> => { | ||||||
| return { | ||||||
| [dbVarName]: null, | ||||||
| [localVarName]: null, | ||||||
| [inlineVarName]: null, | ||||||
| ...decodeRef(ref, dbVarName, localVarName), | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Runtime error when Fix by adding a conditional check: return {
[dbVarName]: null,
[localVarName]: null,
[inlineVarName]: null,
...(ref ? decodeRef(ref, dbVarName, localVarName) : {}),
} as Record<DbVarName, number | null> &
Record<LocalVarName, string | null> &
Record<InlineVarName, null>;
Suggested change
Spotted by Graphite |
||||||
| } as Record<DbVarName, number | null> & | ||||||
| Record<LocalVarName, string | null> & | ||||||
| Record<InlineVarName, null>; | ||||||
| }; | ||||||
|
|
||||||
| const crossAppEmbeddingToDbEmbedding = ( | ||||||
| embedding: CrossAppEmbedding | undefined, | ||||||
| ): InlineEmbeddingInput | undefined => | ||||||
|
|
@@ -76,6 +119,43 @@ const inlineCrossAppContentToDbContent = ( | |||||
| }); | ||||||
| }; | ||||||
|
|
||||||
| export const crossAppStandaloneContentToDbContent = ( | ||||||
| content: StandaloneCrossAppContent | undefined, | ||||||
| space: Ref, | ||||||
| ): ContentDataInput | undefined => { | ||||||
| if (content === undefined) return undefined; | ||||||
| return { | ||||||
| source_local_id: content.localId, | ||||||
| text: content.value, | ||||||
| scale: content.scale || "document", | ||||||
| content_type: content.contentType || "text/plain", | ||||||
| variant: content.variant, | ||||||
| created: content.createdAt.toISOString(), | ||||||
| last_modified: (content.modifiedAt || content.createdAt).toISOString(), | ||||||
|
maparent marked this conversation as resolved.
|
||||||
| embedding_inline: crossAppEmbeddingToDbEmbedding(content.embedding) || null, | ||||||
| ...decodeRefWithInlineNulls({ | ||||||
| ref: content.author, | ||||||
| dbVarName: "author_id", | ||||||
| localVarName: "author_local_id", | ||||||
| inlineVarName: "author_inline", | ||||||
| }), | ||||||
| ...decodeRefWithNulls(space, "space_id", "space_url"), | ||||||
| // provide other explicit null values for type completion | ||||||
| ...decodeRefWithInlineNulls({ | ||||||
| dbVarName: "creator_id", | ||||||
| localVarName: "creator_local_id", | ||||||
| inlineVarName: "creator_inline", | ||||||
| }), | ||||||
| ...decodeRefWithInlineNulls({ | ||||||
| dbVarName: "document_id", | ||||||
| localVarName: "document_local_id", | ||||||
| inlineVarName: "document_inline", | ||||||
| }), | ||||||
| ...decodeRefWithNulls(undefined, "part_of_id", "part_of_local_id"), | ||||||
| metadata: null, | ||||||
| }; | ||||||
| }; | ||||||
|
|
||||||
| export const crossAppNodeToDbContent = ( | ||||||
| node: CrossAppNode | undefined, | ||||||
| variant: "full" | "direct", | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.