@pylonsync/functions 0.22.31 → 0.22.33

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/dist/runtime.d.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  * each ctx.db / ctx.scheduler / ctx.runMutation call), so the map never
15
15
  * needs to queue multiple RPCs per call_id.
16
16
  */
17
- import type { DbReader, DbWriter, Llm, Rooms, Workflows, Audit } from "./types";
17
+ import type { DbReader, DbWriter, Llm, Rooms, Workflows, Audit, ApiKeys } from "./types";
18
18
  export declare function buildDbReader(callId: string, ssrRead?: boolean): DbReader;
19
19
  export declare function buildSsrFnCaller(callId: string): (name: string, args?: Record<string, unknown>) => Promise<unknown>;
20
20
  export declare function buildDbWriter(callId: string): DbWriter;
@@ -48,4 +48,6 @@ export declare function buildRooms(callId: string): Rooms;
48
48
  */
49
49
  /** Build `ctx.audit` — round-trips `audit_op` frames to the host. */
50
50
  export declare function buildAudit(callId: string): Audit;
51
+ /** Build `ctx.apiKeys` — round-trips `api_key_op` frames to the host. */
52
+ export declare function buildApiKeys(callId: string): ApiKeys;
51
53
  export declare function buildWorkflows(callId: string): Workflows;
package/dist/types.d.ts CHANGED
@@ -628,6 +628,46 @@ export interface AuditLog {
628
628
  id: string;
629
629
  }>;
630
630
  }
631
+ /**
632
+ * `ctx.apiKeys`: API keys for the app's own users, minted from server
633
+ * code. For a service user the app runs (for example one per AI agent),
634
+ * where the HTTP route `POST /api/auth/api-keys` does not fit because it
635
+ * mints a key for the signed-in caller only.
636
+ *
637
+ * Actions only. The action must be `internal: true` or have called
638
+ * `ctx.auth.elevate({ admin: true, reason })` after checking its own
639
+ * caller; anything else is refused with `API_KEYS_FORBIDDEN`. Not in
640
+ * mutations: a key is not part of the mutation's transaction.
641
+ * Every call is in the server log with the function name and user id.
642
+ */
643
+ export interface ApiKeys {
644
+ /**
645
+ * Mint a key for `userId`. The plaintext `key` is returned once and is
646
+ * never readable again; store it where the key's holder can use it.
647
+ * `expiresAt` is unix seconds. `name` is a label (at most 100
648
+ * characters).
649
+ */
650
+ create(options: {
651
+ userId: string;
652
+ name?: string;
653
+ expiresAt?: number;
654
+ }): Promise<{
655
+ key: string;
656
+ id: string;
657
+ prefix: string;
658
+ expiresAt: number | null;
659
+ }>;
660
+ /**
661
+ * Revoke key `keyId` of `userId`. `revoked` is false when no key of
662
+ * that user has that id.
663
+ */
664
+ revoke(options: {
665
+ userId: string;
666
+ keyId: string;
667
+ }): Promise<{
668
+ revoked: boolean;
669
+ }>;
670
+ }
631
671
  /**
632
672
  * `ctx.audit` on actions: {@link AuditLog.log} plus reads. Mutations get
633
673
  * only `log`: in a Postgres mutation the transaction holds a connection,
@@ -1371,6 +1411,8 @@ export interface ActionCtx<R extends AuthRequirement = "optional"> {
1371
1411
  workflows: Workflows;
1372
1412
  /** Application audit log — see {@link Audit}. */
1373
1413
  audit: Audit;
1414
+ /** API keys for the app's own users, from server code — see {@link ApiKeys}. */
1415
+ apiKeys: ApiKeys;
1374
1416
  /** Run an `agent()` as a named user from server code — see {@link Agents}. */
1375
1417
  agents: Agents;
1376
1418
  /** Attach / register custom domains for your app's OWN end-customers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pylonsync/functions",
3
- "version": "0.22.31",
3
+ "version": "0.22.33",
4
4
  "description": "TypeScript function runtime for pylon — defines server-side queries, mutations, and actions.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/runtime.ts CHANGED
@@ -39,6 +39,7 @@ import type {
39
39
  WorkflowRunSummary,
40
40
  Audit,
41
41
  AuditEntry,
42
+ ApiKeys,
42
43
  Connections,
43
44
  Domains,
44
45
  TenantDomainResult,
@@ -1102,6 +1103,37 @@ export function buildAudit(callId: string): Audit {
1102
1103
  };
1103
1104
  }
1104
1105
 
1106
+ /** Build `ctx.apiKeys` — round-trips `api_key_op` frames to the host. */
1107
+ export function buildApiKeys(callId: string): ApiKeys {
1108
+ return {
1109
+ async create(options) {
1110
+ // A value Rust cannot read as u64 would be dropped by the host and
1111
+ // leave the call waiting; NaN would turn into "never expires".
1112
+ const exp = options.expiresAt;
1113
+ if (exp !== undefined && !(Number.isSafeInteger(exp) && exp >= 0)) {
1114
+ const err = new Error("ctx.apiKeys.create expiresAt must be a whole number of unix seconds, 0 or more");
1115
+ (err as { code?: string }).code = "INVALID_ARGS";
1116
+ throw err;
1117
+ }
1118
+ return (await rpc(callId, {
1119
+ type: "api_key_op",
1120
+ op: "create",
1121
+ user_id: options.userId,
1122
+ name: options.name ?? null,
1123
+ expires_at: options.expiresAt ?? null,
1124
+ })) as { key: string; id: string; prefix: string; expiresAt: number | null };
1125
+ },
1126
+ async revoke(options) {
1127
+ return (await rpc(callId, {
1128
+ type: "api_key_op",
1129
+ op: "revoke",
1130
+ user_id: options.userId,
1131
+ key_id: options.keyId,
1132
+ })) as { revoked: boolean };
1133
+ },
1134
+ };
1135
+ }
1136
+
1105
1137
  export function buildWorkflows(callId: string): Workflows {
1106
1138
  return {
1107
1139
  async start(name: string, input?: unknown, opts?: { key?: string }) {
@@ -1356,6 +1388,7 @@ function buildActionCtx(
1356
1388
  connections,
1357
1389
  workflows: buildWorkflows(callId),
1358
1390
  audit: buildAudit(callId),
1391
+ apiKeys: buildApiKeys(callId),
1359
1392
  agents: {
1360
1393
  async run(name, options) {
1361
1394
  if (!options || typeof options.as?.userId !== "string") {
package/src/types.ts CHANGED
@@ -705,6 +705,37 @@ export interface AuditLog {
705
705
  }): Promise<{ id: string }>;
706
706
  }
707
707
 
708
+ /**
709
+ * `ctx.apiKeys`: API keys for the app's own users, minted from server
710
+ * code. For a service user the app runs (for example one per AI agent),
711
+ * where the HTTP route `POST /api/auth/api-keys` does not fit because it
712
+ * mints a key for the signed-in caller only.
713
+ *
714
+ * Actions only. The action must be `internal: true` or have called
715
+ * `ctx.auth.elevate({ admin: true, reason })` after checking its own
716
+ * caller; anything else is refused with `API_KEYS_FORBIDDEN`. Not in
717
+ * mutations: a key is not part of the mutation's transaction.
718
+ * Every call is in the server log with the function name and user id.
719
+ */
720
+ export interface ApiKeys {
721
+ /**
722
+ * Mint a key for `userId`. The plaintext `key` is returned once and is
723
+ * never readable again; store it where the key's holder can use it.
724
+ * `expiresAt` is unix seconds. `name` is a label (at most 100
725
+ * characters).
726
+ */
727
+ create(options: {
728
+ userId: string;
729
+ name?: string;
730
+ expiresAt?: number;
731
+ }): Promise<{ key: string; id: string; prefix: string; expiresAt: number | null }>;
732
+ /**
733
+ * Revoke key `keyId` of `userId`. `revoked` is false when no key of
734
+ * that user has that id.
735
+ */
736
+ revoke(options: { userId: string; keyId: string }): Promise<{ revoked: boolean }>;
737
+ }
738
+
708
739
  /**
709
740
  * `ctx.audit` on actions: {@link AuditLog.log} plus reads. Mutations get
710
741
  * only `log`: in a Postgres mutation the transaction holds a connection,
@@ -1500,6 +1531,8 @@ export interface ActionCtx<R extends AuthRequirement = "optional"> {
1500
1531
  workflows: Workflows;
1501
1532
  /** Application audit log — see {@link Audit}. */
1502
1533
  audit: Audit;
1534
+ /** API keys for the app's own users, from server code — see {@link ApiKeys}. */
1535
+ apiKeys: ApiKeys;
1503
1536
  /** Run an `agent()` as a named user from server code — see {@link Agents}. */
1504
1537
  agents: Agents;
1505
1538
  /** Attach / register custom domains for your app's OWN end-customers