@orbit-app/functions 0.3.0 → 0.5.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
@@ -1,6 +1,6 @@
1
1
  # @orbit-app/functions
2
2
 
3
- Write Orbit Functions: TypeScript that runs in Orbit's sandbox for one app, as the store's org. An app calls an action function. A provider event, such as a Slack reaction, runs a webhook function. A service outside Orbit runs a delegate function through a signed link, which an action or webhook function makes with `orbit.defineDelegates().sign()`.
3
+ Write Orbit Functions: TypeScript that runs in Orbit's sandbox for one app, as the store's org. An app calls an action function. A provider event, such as a Slack reaction, runs a webhook function. A cron schedule in `orbit.json` runs a schedule function. A service outside Orbit runs a delegate function through a signed link, which an action, webhook or schedule function makes with `orbit.defineDelegates().sign()`.
4
4
 
5
5
  ```bash
6
6
  npm install --save-dev @orbit-app/functions esbuild
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { type ObjectsApi, type ObjectsClient } from "@orbit-app/web";
2
- export { OrbitObjectError, type ObjectsApi, type ObjectsClient } from "@orbit-app/web";
1
+ import { type ObjectsApi, type ObjectsClient, type ObjectsOptions } from "@orbit-app/web";
2
+ export { OrbitObjectError, type ObjectsApi, type ObjectsClient, type ObjectsOptions, type RetryOptions } from "@orbit-app/web";
3
3
  export type ActionTrigger = {
4
4
  type: "action";
5
5
  };
@@ -33,6 +33,20 @@ export type WebhookEvent<Payload = any> = {
33
33
  export type DelegateTrigger = {
34
34
  type: "delegate";
35
35
  };
36
+ export type ScheduleTrigger = {
37
+ type: "schedule";
38
+ };
39
+ export type ScheduleContext = {
40
+ runId: string;
41
+ env: string;
42
+ attempt: number;
43
+ };
44
+ export type ScheduleTick = {
45
+ scheduledAt: string;
46
+ cron: string;
47
+ timezone: string;
48
+ handle: string;
49
+ };
36
50
  export type DelegateTypes = {
37
51
  context: Record<string, unknown>;
38
52
  body: unknown;
@@ -66,7 +80,7 @@ export type Delegates<M extends Record<string, DelegateTypes>> = {
66
80
  sign<N extends keyof M & string>(name: N, opts: SignOptions<M[N]["context"]>): Promise<Delegate>;
67
81
  revoke(id: string): Promise<void>;
68
82
  };
69
- export type OrbitDelegateErrorCode = "invalid" | "invalid_allow_from" | "limit_reached" | "not_declared" | "not_found" | "forbidden" | "rate_limited" | "unavailable";
83
+ export type OrbitDelegateErrorCode = "invalid" | "invalid_allow_from" | "limit_reached" | "not_declared" | "not_found" | "forbidden" | "sign_not_allowed" | "rate_limited" | "unavailable";
70
84
  export declare class OrbitDelegateError extends Error {
71
85
  readonly code: OrbitDelegateErrorCode;
72
86
  readonly fields?: Record<string, string>;
@@ -98,7 +112,7 @@ export type FunctionOrbit = {
98
112
  objects: ObjectsApi;
99
113
  defineObjects<Types extends Record<string, Record<string, unknown>>, Required extends {
100
114
  [K in keyof Types]?: keyof Types[K];
101
- } = {}>(): ObjectsClient<Types, Required>;
115
+ } = {}>(options?: ObjectsOptions): ObjectsClient<Types, Required>;
102
116
  connections: {
103
117
  get(provider: string): Promise<OrgConnection | null>;
104
118
  };
@@ -114,11 +128,16 @@ export type WebhookFunction<Payload = any> = {
114
128
  trigger: WebhookTrigger;
115
129
  run(event: WebhookEvent<Payload>, orbit: FunctionOrbit, ctx: WebhookContext): void | Promise<void>;
116
130
  };
131
+ export type ScheduleFunction = {
132
+ trigger: ScheduleTrigger;
133
+ run(tick: ScheduleTick, orbit: FunctionOrbit, ctx: ScheduleContext): void | Promise<void>;
134
+ };
117
135
  export type DelegateFunction<D extends DelegateTypes = DelegateTypes, Output = unknown> = {
118
136
  trigger: DelegateTrigger;
119
137
  run(body: D["body"], orbit: FunctionOrbit, ctx: DelegateContext<D["context"]>): Output | Promise<Output>;
120
138
  };
121
139
  export declare function defineFunction<Input = unknown, Output = unknown>(def: ActionFunction<Input, Output>): ActionFunction<Input, Output>;
140
+ export declare function defineFunction(def: ScheduleFunction): ScheduleFunction;
122
141
  export declare function defineFunction<Payload = any>(def: WebhookFunction<Payload>): WebhookFunction<Payload>;
123
142
  export declare function defineFunction<D extends DelegateTypes, Output = unknown>(def: DelegateFunction<D, Output>): DelegateFunction<D, Output>;
124
143
  export declare class OrbitFunctionError extends Error {
@@ -128,7 +147,7 @@ export declare class OrbitFunctionError extends Error {
128
147
  export type DispatchRequest = {
129
148
  v: 1;
130
149
  fn: string;
131
- kind: "action" | "connection_webhook" | "delegate";
150
+ kind: "action" | "connection_webhook" | "delegate" | "schedule";
132
151
  token: string;
133
152
  apiBase: string;
134
153
  input: unknown;
@@ -155,6 +174,6 @@ export type DispatchReply = {
155
174
  };
156
175
  log?: string;
157
176
  };
158
- export declare function serve(def: ActionFunction<any, any> | WebhookFunction | DelegateFunction<any, any>, name: string): {
177
+ export declare function serve(def: ActionFunction<any, any> | WebhookFunction | DelegateFunction<any, any> | ScheduleFunction, name: string): {
159
178
  invoke(req: DispatchRequest): Promise<DispatchReply>;
160
179
  };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Orbit Functions: publisher code that runs in Orbit's sandbox, never on a member's Mac. A function
2
2
  // acts as the store's org. It reaches only the Orbit API, with the token of its run.
3
- import { OrbitObjectError, typedObjects, } from "@orbit-app/web";
3
+ import { OrbitObjectError, typedObjects, withRetry, } from "@orbit-app/web";
4
4
  export { OrbitObjectError } from "@orbit-app/web";
5
5
  const DELEGATE_CODES = [
6
6
  "invalid",
@@ -9,6 +9,7 @@ const DELEGATE_CODES = [
9
9
  "not_declared",
10
10
  "not_found",
11
11
  "forbidden",
12
+ "sign_not_allowed",
12
13
  "rate_limited",
13
14
  "unavailable",
14
15
  ];
@@ -97,6 +98,13 @@ export function serve(def, name) {
97
98
  attempt: req.ctx.attempt ?? 1,
98
99
  });
99
100
  }
101
+ else if (def.trigger.type === "schedule") {
102
+ await def.run({ ...req.input }, createOrbit(req), {
103
+ runId: req.ctx.runId,
104
+ env: req.ctx.env,
105
+ attempt: req.ctx.attempt ?? 1,
106
+ });
107
+ }
100
108
  else {
101
109
  // A trigger that this SDK does not know never runs as another kind.
102
110
  return crash(`${name} has the unknown trigger ${String(def.trigger.type)}`);
@@ -148,7 +156,7 @@ function createOrbit(req) {
148
156
  });
149
157
  }
150
158
  const enc = encodeURIComponent;
151
- const objects = {
159
+ const once = {
152
160
  create: (input) => call("POST", "/v1/objects", input),
153
161
  get: (id) => call("GET", `/v1/objects/${enc(id)}`),
154
162
  getByHandle: (type, handle) => call("GET", `/v1/objects/by-handle/${enc(type)}/${enc(handle)}`),
@@ -159,6 +167,7 @@ function createOrbit(req) {
159
167
  await call("DELETE", `/v1/objects/${enc(id)}`);
160
168
  },
161
169
  };
170
+ const objects = withRetry(once);
162
171
  function connection(c) {
163
172
  return Object.freeze({
164
173
  provider: c.provider,
@@ -188,7 +197,7 @@ function createOrbit(req) {
188
197
  }
189
198
  return {
190
199
  objects,
191
- defineObjects: () => typedObjects(objects),
200
+ defineObjects: (options) => typedObjects(options ? withRetry(once, options.retry) : objects),
192
201
  connections: {
193
202
  async get(provider) {
194
203
  const list = (await call("GET", "/v1/connections?owner=org"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbit-app/functions",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Write Orbit Functions: publisher code that runs in Orbit's sandbox as the store's org.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -12,14 +12,8 @@
12
12
  "files": [
13
13
  "dist"
14
14
  ],
15
- "scripts": {
16
- "build": "tsc -p tsconfig.build.json",
17
- "typecheck": "tsc --noEmit",
18
- "test": "tsc --noEmit && vitest run",
19
- "prepack": "pnpm build"
20
- },
21
15
  "dependencies": {
22
- "@orbit-app/web": "^0.1.0"
16
+ "@orbit-app/web": "^0.1.1"
23
17
  },
24
18
  "devDependencies": {
25
19
  "typescript": "^7.0.2",
@@ -27,5 +21,10 @@
27
21
  },
28
22
  "publishConfig": {
29
23
  "access": "public"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc -p tsconfig.build.json",
27
+ "typecheck": "tsc --noEmit",
28
+ "test": "tsc --noEmit && vitest run"
30
29
  }
31
- }
30
+ }