@orbit-app/functions 0.10.0 → 0.12.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/README.md CHANGED
@@ -22,7 +22,7 @@ export default defineFunction({
22
22
  });
23
23
  ```
24
24
 
25
- A run reads and writes objects with the access of its person. An action run acts as its caller, and a delegate run as the member who signed it: it reads only what that person's app can read, and an object that it creates without `owner` belongs to that person. A webhook run, a schedule run and a delegate run that an event or a schedule signed act as the app: they read the objects that the app owns and the objects shared with the whole store. `orbit.app.objects` and `orbit.app.defineObjects()` call as the app, only within the `grants` of the function in `orbit.json`, such as `{ "kudos": ["create"] }`. The approval dialog shows each grant (0.10.0 and later; the doc `migrations/functions-caller-access` gives the move).
25
+ A run reads and writes objects with the access of its person. An action run acts as its caller, and a delegate run as the member who signed it: it reads only what that person's app can read, and an object that it creates without `owner` belongs to that person. A webhook run, a schedule run and a delegate run that an event or a schedule signed act as the app: they read the objects that the app owns and the objects shared with the whole store. `orbit.app.objects` and `orbit.app.defineObjects()` call as the app, only within the `grants` of the function in `orbit.json`, such as `{ "kudos": ["create"] }`. The approval dialog shows each grant (0.10.0 and later; the doc `migrations/functions-caller-access` gives the move). From 0.11.0, a typed `update` or `delete` as the app needs no `read` grant: Orbit checks the type (the doc `migrations/functions-app-writes`).
26
26
 
27
27
  `orbit.runOrg()` gives the org that the run acts in, in the shape of `getOrg()` of `@orbit-app/web`: the caller's org of an action, the signer's org of a delegate, else the org that owns the app. `orbit.appOrg()` gives the org that owns the app. `orbit.org()` is the same as `appOrg()` and goes in a later release (0.10.0 and later; the doc `migrations/functions-run-org` gives the move).
28
28
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type OperationTypes, type ObjectsApi, type ObjectsClient, type ObjectsOptions } from "@orbit-app/web";
2
- export { OrbitConnectionError, OrbitObjectError, type ObjectsApi, type ObjectsClient, type ObjectsOptions, type OperationTypes, type OrbitConnectionErrorCode, type RetryOptions, } from "@orbit-app/web";
2
+ export { OrbitConnectionError, OrbitObjectError, type ObjectsApi, type ObjectsClient, type ObjectsOptions, type OperationTypes, type OrbitConnectionErrorCode, type RetryOptions, type WriteOptions, } from "@orbit-app/web";
3
3
  export type ActionTrigger = {
4
4
  type: "action";
5
5
  };
@@ -36,6 +36,22 @@ export type DelegateTrigger = {
36
36
  export type ScheduleTrigger = {
37
37
  type: "schedule";
38
38
  };
39
+ export type ApiTrigger = {
40
+ type: "api";
41
+ };
42
+ export type ApiInput<Body = unknown> = {
43
+ body: Body;
44
+ key: {
45
+ id: string;
46
+ name: string;
47
+ type: string;
48
+ };
49
+ };
50
+ export type ApiContext = {
51
+ runId: string;
52
+ env: string;
53
+ callId: string | null;
54
+ };
39
55
  export type ScheduleContext = {
40
56
  runId: string;
41
57
  env: string;
@@ -161,10 +177,15 @@ export type DelegateFunction<D extends DelegateTypes = DelegateTypes, Output = u
161
177
  trigger: DelegateTrigger;
162
178
  run(body: D["body"], orbit: FunctionOrbit, ctx: DelegateContext<D["context"]>): Output | Promise<Output>;
163
179
  };
180
+ export type ApiFunction<Body = unknown, Output = unknown> = {
181
+ trigger: ApiTrigger;
182
+ run(input: ApiInput<Body>, orbit: FunctionOrbit, ctx: ApiContext): Output | Promise<Output>;
183
+ };
164
184
  export declare function defineFunction<Input = unknown, Output = unknown>(def: ActionFunction<Input, Output>): ActionFunction<Input, Output>;
165
185
  export declare function defineFunction(def: ScheduleFunction): ScheduleFunction;
166
186
  export declare function defineFunction<Payload = any>(def: WebhookFunction<Payload>): WebhookFunction<Payload>;
167
187
  export declare function defineFunction<D extends DelegateTypes, Output = unknown>(def: DelegateFunction<D, Output>): DelegateFunction<D, Output>;
188
+ export declare function defineFunction<Body = unknown, Output = unknown>(def: ApiFunction<Body, Output>): ApiFunction<Body, Output>;
168
189
  export declare class OrbitFunctionError extends Error {
169
190
  readonly code: string;
170
191
  constructor(code: string, message: string);
@@ -172,7 +193,7 @@ export declare class OrbitFunctionError extends Error {
172
193
  export type DispatchRequest = {
173
194
  v: 1;
174
195
  fn: string;
175
- kind: "action" | "connection_webhook" | "delegate" | "schedule";
196
+ kind: "action" | "connection_webhook" | "delegate" | "schedule" | "api";
176
197
  token: string;
177
198
  apiBase: string;
178
199
  input: unknown;
@@ -183,6 +204,7 @@ export type DispatchRequest = {
183
204
  caller: Caller | null;
184
205
  attempt?: number;
185
206
  delegate?: DelegateContext["delegate"];
207
+ callId?: string;
186
208
  org: OrbitOrg;
187
209
  appOrg?: OrbitOrg;
188
210
  runOrg?: OrbitRunOrg;
@@ -201,6 +223,6 @@ export type DispatchReply = {
201
223
  };
202
224
  log?: string;
203
225
  };
204
- export declare function serve(def: ActionFunction<any, any> | WebhookFunction | DelegateFunction<any, any> | ScheduleFunction, name: string): {
226
+ export declare function serve(def: ActionFunction<any, any> | WebhookFunction | DelegateFunction<any, any> | ScheduleFunction | ApiFunction<any, any>, name: string): {
205
227
  invoke(req: DispatchRequest): Promise<DispatchReply>;
206
228
  };
package/dist/index.js CHANGED
@@ -93,6 +93,16 @@ export function serve(def, name) {
93
93
  delegate: { ...delegate },
94
94
  });
95
95
  }
96
+ else if (def.trigger.type === "api") {
97
+ const input = req.input;
98
+ if (!input?.key)
99
+ return crash("a key call has no key");
100
+ value = await def.run({ body: input.body, key: { ...input.key } }, createOrbit(req), {
101
+ runId: req.ctx.runId,
102
+ env: req.ctx.env,
103
+ callId: req.ctx.callId ?? null,
104
+ });
105
+ }
96
106
  else if (def.trigger.type === "connection_webhook") {
97
107
  await def.run(req.input, createOrbit(req), {
98
108
  runId: req.ctx.runId,
@@ -171,15 +181,17 @@ function createOrbit(req) {
171
181
  });
172
182
  }
173
183
  const enc = encodeURIComponent;
184
+ const typeQuery = (options) => (options?.type === undefined ? "" : `?type=${enc(options.type)}`);
174
185
  const objectsAs = (asApp) => ({
175
186
  create: (input) => call("POST", "/v1/objects", input, objectError, asApp),
176
187
  get: (id) => call("GET", `/v1/objects/${enc(id)}`, undefined, objectError, asApp),
177
188
  getByHandle: (type, handle) => call("GET", `/v1/objects/by-handle/${enc(type)}/${enc(handle)}`, undefined, objectError, asApp),
178
189
  list: (input) => call("POST", "/v1/objects/query", input, objectError, asApp),
179
- update: (id, input) => call("PATCH", `/v1/objects/${enc(id)}`, input, objectError, asApp),
190
+ // ?type= makes the API check the type, so a typed write needs no read grant.
191
+ update: (id, input, options) => call("PATCH", `/v1/objects/${enc(id)}${typeQuery(options)}`, input, objectError, asApp),
180
192
  upsert: (type, handle, input = {}) => call("PUT", `/v1/objects/by-handle/${enc(type)}/${enc(handle)}`, input, objectError, asApp),
181
- delete: async (id) => {
182
- await call("DELETE", `/v1/objects/${enc(id)}`, undefined, objectError, asApp);
193
+ delete: async (id, options) => {
194
+ await call("DELETE", `/v1/objects/${enc(id)}${typeQuery(options)}`, undefined, objectError, asApp);
183
195
  },
184
196
  });
185
197
  const once = objectsAs(false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbit-app/functions",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "Write Orbit Functions: publisher code that runs in Orbit's sandbox as its caller or as the app.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -19,7 +19,7 @@
19
19
  "prepack": "pnpm build"
20
20
  },
21
21
  "dependencies": {
22
- "@orbit-app/web": "^0.5.0"
22
+ "@orbit-app/web": "^0.6.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "typescript": "^7.0.2",