@rocapine/community 0.21.1 → 0.21.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocapine/community",
3
- "version": "0.21.1",
3
+ "version": "0.21.3",
4
4
  "description": "CLI to install, upgrade and adopt the Rocapine Community SDK's Supabase backend (migrations, Edge Functions) into a project.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,6 +1,9 @@
1
1
  // Shared Supabase admin client, caller identity helpers + JSON response helper.
2
2
 
3
3
  import { createClient } from "jsr:@supabase/supabase-js@2";
4
+ import { corsHeaders } from "jsr:@supabase/supabase-js@2/cors";
5
+
6
+ export { corsHeaders };
4
7
 
5
8
  export const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
6
9
 
@@ -70,9 +73,15 @@ export async function callerUserId(req: Request): Promise<string | null> {
70
73
 
71
74
  // The client consumes responses via supabase.functions.invoke, which only
72
75
  // parses the body as JSON when the Content-Type says so. Without this header
73
- // invoke returns a raw string and verdict.status is undefined.
76
+ // invoke returns a raw string and verdict.status is undefined. The CORS
77
+ // headers let a browser (the app's web build) read the response.
74
78
  export const json = (body: unknown, status = 200) =>
75
79
  new Response(JSON.stringify(body), {
76
80
  status,
77
- headers: { "Content-Type": "application/json" },
81
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
78
82
  });
83
+
84
+ // A browser preflights every functions.invoke call with OPTIONS before the
85
+ // POST. Answer it before reading the body, or the browser never sends the call.
86
+ export const preflight = (req: Request) =>
87
+ req.method === "OPTIONS" ? new Response("ok", { headers: corsHeaders }) : null;
@@ -4,6 +4,7 @@
4
4
  "jsr:@std/assert@1": "1.0.19",
5
5
  "jsr:@std/internal@^1.0.12": "1.0.14",
6
6
  "jsr:@supabase/supabase-js@2": "2.116.0",
7
+ "jsr:@supabase/supabase-js@2.116.0": "2.116.0",
7
8
  "npm:@supabase/auth-js@2.116.0": "2.116.0",
8
9
  "npm:@supabase/functions-js@2.116.0": "2.116.0",
9
10
  "npm:@supabase/postgrest-js@2.116.0": "2.116.0",
@@ -10,7 +10,7 @@
10
10
  // `status = 'pending'` so a row the author deleted mid-flight is never
11
11
  // resurrected.
12
12
 
13
- import { adminClient, callerUserId, isServiceCaller, json } from "../_shared/client.ts";
13
+ import { adminClient, callerUserId, isServiceCaller, json, preflight } from "../_shared/client.ts";
14
14
  import { fetchImagePath, POST_IMAGES_BUCKET, postImageUrl } from "../_shared/images.ts";
15
15
  import { assertModerationConfigured, moderateItem } from "../_shared/moderation.ts";
16
16
  import { postToSlack } from "../_shared/slack.ts";
@@ -20,6 +20,8 @@ assertModerationConfigured();
20
20
  const supabase = adminClient();
21
21
 
22
22
  Deno.serve(async (req) => {
23
+ const pre = preflight(req);
24
+ if (pre) return pre;
23
25
  const { kind, id } = (await req.json().catch(() => ({}))) as {
24
26
  kind?: "post" | "comment";
25
27
  id?: string;
@@ -9,7 +9,7 @@
9
9
  // time, so partial application is not observable in practice.
10
10
 
11
11
  import { createClient } from "jsr:@supabase/supabase-js@2";
12
- import { adminClient, json, SUPABASE_URL } from "../_shared/client.ts";
12
+ import { adminClient, json, preflight, SUPABASE_URL } from "../_shared/client.ts";
13
13
  import { assertModerationConfigured, moderateInput } from "../_shared/moderation.ts";
14
14
  import { containsAppName, isReservedHandle, suggestHandle } from "../_shared/names.ts";
15
15
 
@@ -18,6 +18,8 @@ assertModerationConfigured();
18
18
  const admin = adminClient();
19
19
 
20
20
  Deno.serve(async (req) => {
21
+ const pre = preflight(req);
22
+ if (pre) return pre;
21
23
  const caller = createClient(SUPABASE_URL, Deno.env.get("SUPABASE_ANON_KEY")!, {
22
24
  global: { headers: { Authorization: req.headers.get("Authorization") ?? "" } },
23
25
  });