@robodev-ai/sdk 0.2.0 → 0.3.0
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 +2 -2
- package/src/api.ts +9 -1
- package/src/auth.ts +13 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@robodev-ai/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Robodev SDK — defineDatabase, defineApi, and the injected Drizzle db",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Robodev server SDK — defineDatabase, defineApi, auth, and the injected Drizzle db",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
package/src/api.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import type { ApiAuth, AuthUser } from "./auth.js";
|
|
2
3
|
import type { RobodevDb } from "./db.js";
|
|
3
4
|
|
|
4
5
|
/** Marker written onto every `defineApi(...)` result so Starbase can register the handler. */
|
|
5
6
|
export const API_KIND = "robodevApi";
|
|
6
7
|
|
|
7
|
-
/** Injected into every handler: tenant `db`, parsed query/body, and
|
|
8
|
+
/** Injected into every handler: tenant `db`, parsed query/body, headers, and optional `user`. */
|
|
8
9
|
export type ApiHandlerContext<TQuery, TBody> = {
|
|
9
10
|
db: RobodevDb;
|
|
10
11
|
query: TQuery;
|
|
11
12
|
body: TBody;
|
|
12
13
|
headers: Record<string, string | string[] | undefined>;
|
|
14
|
+
user: AuthUser | null;
|
|
13
15
|
};
|
|
14
16
|
|
|
15
17
|
export type ApiDefinition<TQuery = unknown, TBody = unknown, TResponse = unknown> = {
|
|
@@ -17,12 +19,16 @@ export type ApiDefinition<TQuery = unknown, TBody = unknown, TResponse = unknown
|
|
|
17
19
|
query?: z.ZodType<TQuery>;
|
|
18
20
|
body?: z.ZodType<TBody>;
|
|
19
21
|
response?: z.ZodType<TResponse>;
|
|
22
|
+
auth: ApiAuth;
|
|
20
23
|
handler: (ctx: ApiHandlerContext<TQuery, TBody>) => Promise<TResponse> | TResponse;
|
|
21
24
|
};
|
|
22
25
|
|
|
23
26
|
/**
|
|
24
27
|
* Defines one HTTP handler. Export as `get` / `post` / `put` / `patch` / `delete`
|
|
25
28
|
* from `api/<name>.ts` to serve that method at `/<name>`.
|
|
29
|
+
*
|
|
30
|
+
* `auth` defaults to `false` (public). Use `"required"` for Robodev Auth, or a
|
|
31
|
+
* custom verifier that returns `{ id, email }` from request headers.
|
|
26
32
|
*/
|
|
27
33
|
export function defineApi<
|
|
28
34
|
TQuery extends z.ZodTypeAny | undefined = undefined,
|
|
@@ -32,6 +38,7 @@ export function defineApi<
|
|
|
32
38
|
query?: TQuery;
|
|
33
39
|
body?: TBody;
|
|
34
40
|
response?: TResponse;
|
|
41
|
+
auth?: ApiAuth;
|
|
35
42
|
handler: (
|
|
36
43
|
ctx: ApiHandlerContext<
|
|
37
44
|
TQuery extends z.ZodTypeAny ? z.infer<TQuery> : Record<string, never>,
|
|
@@ -50,6 +57,7 @@ export function defineApi<
|
|
|
50
57
|
query: config.query,
|
|
51
58
|
body: config.body,
|
|
52
59
|
response: config.response,
|
|
60
|
+
auth: config.auth ?? false,
|
|
53
61
|
handler: config.handler,
|
|
54
62
|
} as ApiDefinition<
|
|
55
63
|
TQuery extends z.ZodTypeAny ? z.infer<TQuery> : Record<string, never>,
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** End-user on a project host. Distinct from Starbase control-plane accounts. */
|
|
2
|
+
export type AuthUser = {
|
|
3
|
+
id: string;
|
|
4
|
+
email: string;
|
|
5
|
+
name?: string | null;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/** Custom header verifier. Return the same user shape, or null to 401. */
|
|
9
|
+
export type AuthVerifier = (input: {
|
|
10
|
+
headers: Record<string, string | string[] | undefined>;
|
|
11
|
+
}) => AuthUser | null | Promise<AuthUser | null>;
|
|
12
|
+
|
|
13
|
+
export type ApiAuth = false | "required" | "optional" | AuthVerifier;
|