@orbit-app/functions 0.3.0 → 0.4.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
@@ -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>;
@@ -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
@@ -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)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbit-app/functions",
3
- "version": "0.3.0",
3
+ "version": "0.4.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": {