@everystack/api 0.2.9 → 0.2.10
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 +7 -7
- package/src/handler/index.ts +20 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@everystack/api",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
4
4
|
"description": "PostgREST-compatible HTTP handler + typed fetch client for Drizzle ORM",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"author": "Scalable Technology, Inc. <licensing@scalable.technology>",
|
|
@@ -30,6 +30,11 @@
|
|
|
30
30
|
"default": "./src/client/index.ts"
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"test": "jest",
|
|
35
|
+
"build": "tsc --build",
|
|
36
|
+
"lint": "tsc --noEmit"
|
|
37
|
+
},
|
|
33
38
|
"peerDependencies": {
|
|
34
39
|
"drizzle-orm": "0.41.0"
|
|
35
40
|
},
|
|
@@ -40,10 +45,5 @@
|
|
|
40
45
|
"jest": "29.7.0",
|
|
41
46
|
"ts-jest": "29.4.9",
|
|
42
47
|
"typescript": "5.9.3"
|
|
43
|
-
},
|
|
44
|
-
"scripts": {
|
|
45
|
-
"test": "jest",
|
|
46
|
-
"build": "tsc --build",
|
|
47
|
-
"lint": "tsc --noEmit"
|
|
48
48
|
}
|
|
49
|
-
}
|
|
49
|
+
}
|
package/src/handler/index.ts
CHANGED
|
@@ -564,9 +564,16 @@ export function createHandler(db: DrizzleDb, schema: Schema, options?: HandlerOp
|
|
|
564
564
|
const result = await verifyUserToken(request, options.auth);
|
|
565
565
|
if (result.error) return result.error;
|
|
566
566
|
user = result.user;
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
567
|
+
} else if (request.headers.get('authorization')) {
|
|
568
|
+
// Public RPC with token present — verify opportunistically, same
|
|
569
|
+
// policy as public table routes below. If valid, the request runs
|
|
570
|
+
// as authenticated (RLS self-visibility, PII self-tiers). If
|
|
571
|
+
// invalid, ignore and fall through as anonymous.
|
|
572
|
+
const result = await verifyUserToken(request, options.auth);
|
|
573
|
+
if (!result.error) user = result.user;
|
|
574
|
+
}
|
|
575
|
+
if (user && options.auth.onAuthenticated) {
|
|
576
|
+
options.auth.onAuthenticated(user);
|
|
570
577
|
}
|
|
571
578
|
}
|
|
572
579
|
|
|
@@ -610,7 +617,14 @@ export function createHandler(db: DrizzleDb, schema: Schema, options?: HandlerOp
|
|
|
610
617
|
kind: 'rpc', name: fnName, user, client,
|
|
611
618
|
result: 'allow', requestId, body: body as Record<string, unknown>,
|
|
612
619
|
});
|
|
613
|
-
|
|
620
|
+
// Same cache discipline as table GETs: with pgSettings configured the
|
|
621
|
+
// response can vary by identity (opportunistic auth on public RPCs),
|
|
622
|
+
// so it must forbid shared caching ITSELF — never rely on the CDN
|
|
623
|
+
// config (the cache key carries no identity; a consumer overriding
|
|
624
|
+
// the stock defaultTtl:0 policy would otherwise cache one user's
|
|
625
|
+
// personalized response and serve it to everyone).
|
|
626
|
+
const rpcCacheControl = getCacheControl(options);
|
|
627
|
+
return json(result, 200, rpcCacheControl ? { 'Cache-Control': rpcCacheControl } : undefined);
|
|
614
628
|
} catch (error) {
|
|
615
629
|
const pgCode = (error as any)?.code;
|
|
616
630
|
let status = 500;
|
|
@@ -1933,9 +1947,9 @@ function buildAllConditions(table: DrizzleTable, params: URLSearchParams): { con
|
|
|
1933
1947
|
return { conditions };
|
|
1934
1948
|
}
|
|
1935
1949
|
|
|
1936
|
-
function json(data: unknown, status: number): Response {
|
|
1950
|
+
function json(data: unknown, status: number, headers?: Record<string, string>): Response {
|
|
1937
1951
|
return new Response(JSON.stringify(data), {
|
|
1938
1952
|
status,
|
|
1939
|
-
headers: { 'Content-Type': 'application/json' },
|
|
1953
|
+
headers: { 'Content-Type': 'application/json', ...headers },
|
|
1940
1954
|
});
|
|
1941
1955
|
}
|