@orbit-app/functions 0.2.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 +1 -1
- package/dist/index.d.ts +27 -3
- package/dist/index.js +18 -0
- package/package.json +1 -1
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
|
|
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>;
|
|
@@ -91,6 +105,9 @@ export type OrgConnection = {
|
|
|
91
105
|
body?: string | Uint8Array | null;
|
|
92
106
|
}): Promise<Response>;
|
|
93
107
|
};
|
|
108
|
+
export interface OrbitSecretNames {
|
|
109
|
+
}
|
|
110
|
+
export type OrbitSecretName = [keyof OrbitSecretNames] extends [never] ? string : Extract<keyof OrbitSecretNames, string>;
|
|
94
111
|
export type FunctionOrbit = {
|
|
95
112
|
objects: ObjectsApi;
|
|
96
113
|
defineObjects<Types extends Record<string, Record<string, unknown>>, Required extends {
|
|
@@ -101,6 +118,7 @@ export type FunctionOrbit = {
|
|
|
101
118
|
};
|
|
102
119
|
defineDelegates<M extends Record<string, DelegateTypes> = Record<string, DelegateTypes>>(): Delegates<M>;
|
|
103
120
|
org(): OrbitOrg;
|
|
121
|
+
secret(name: OrbitSecretName): string;
|
|
104
122
|
};
|
|
105
123
|
export type ActionFunction<Input = unknown, Output = unknown> = {
|
|
106
124
|
trigger: ActionTrigger;
|
|
@@ -110,11 +128,16 @@ export type WebhookFunction<Payload = any> = {
|
|
|
110
128
|
trigger: WebhookTrigger;
|
|
111
129
|
run(event: WebhookEvent<Payload>, orbit: FunctionOrbit, ctx: WebhookContext): void | Promise<void>;
|
|
112
130
|
};
|
|
131
|
+
export type ScheduleFunction = {
|
|
132
|
+
trigger: ScheduleTrigger;
|
|
133
|
+
run(tick: ScheduleTick, orbit: FunctionOrbit, ctx: ScheduleContext): void | Promise<void>;
|
|
134
|
+
};
|
|
113
135
|
export type DelegateFunction<D extends DelegateTypes = DelegateTypes, Output = unknown> = {
|
|
114
136
|
trigger: DelegateTrigger;
|
|
115
137
|
run(body: D["body"], orbit: FunctionOrbit, ctx: DelegateContext<D["context"]>): Output | Promise<Output>;
|
|
116
138
|
};
|
|
117
139
|
export declare function defineFunction<Input = unknown, Output = unknown>(def: ActionFunction<Input, Output>): ActionFunction<Input, Output>;
|
|
140
|
+
export declare function defineFunction(def: ScheduleFunction): ScheduleFunction;
|
|
118
141
|
export declare function defineFunction<Payload = any>(def: WebhookFunction<Payload>): WebhookFunction<Payload>;
|
|
119
142
|
export declare function defineFunction<D extends DelegateTypes, Output = unknown>(def: DelegateFunction<D, Output>): DelegateFunction<D, Output>;
|
|
120
143
|
export declare class OrbitFunctionError extends Error {
|
|
@@ -124,7 +147,7 @@ export declare class OrbitFunctionError extends Error {
|
|
|
124
147
|
export type DispatchRequest = {
|
|
125
148
|
v: 1;
|
|
126
149
|
fn: string;
|
|
127
|
-
kind: "action" | "connection_webhook" | "delegate";
|
|
150
|
+
kind: "action" | "connection_webhook" | "delegate" | "schedule";
|
|
128
151
|
token: string;
|
|
129
152
|
apiBase: string;
|
|
130
153
|
input: unknown;
|
|
@@ -136,6 +159,7 @@ export type DispatchRequest = {
|
|
|
136
159
|
attempt?: number;
|
|
137
160
|
delegate?: DelegateContext["delegate"];
|
|
138
161
|
org: OrbitOrg;
|
|
162
|
+
secretNonce?: string;
|
|
139
163
|
};
|
|
140
164
|
};
|
|
141
165
|
export type DispatchReply = {
|
|
@@ -150,6 +174,6 @@ export type DispatchReply = {
|
|
|
150
174
|
};
|
|
151
175
|
log?: string;
|
|
152
176
|
};
|
|
153
|
-
export declare function serve(def: ActionFunction<any, any> | WebhookFunction | DelegateFunction<any, any
|
|
177
|
+
export declare function serve(def: ActionFunction<any, any> | WebhookFunction | DelegateFunction<any, any> | ScheduleFunction, name: string): {
|
|
154
178
|
invoke(req: DispatchRequest): Promise<DispatchReply>;
|
|
155
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
|
];
|
|
@@ -28,6 +29,7 @@ export class OrbitDelegateError extends Error {
|
|
|
28
29
|
this.retryAfter = extra.retryAfter;
|
|
29
30
|
}
|
|
30
31
|
}
|
|
32
|
+
const SECRET_NAME = /^[A-Z][A-Z0-9_]{0,63}$/;
|
|
31
33
|
export function defineFunction(def) {
|
|
32
34
|
return def;
|
|
33
35
|
}
|
|
@@ -96,6 +98,13 @@ export function serve(def, name) {
|
|
|
96
98
|
attempt: req.ctx.attempt ?? 1,
|
|
97
99
|
});
|
|
98
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
|
+
}
|
|
99
108
|
else {
|
|
100
109
|
// A trigger that this SDK does not know never runs as another kind.
|
|
101
110
|
return crash(`${name} has the unknown trigger ${String(def.trigger.type)}`);
|
|
@@ -202,6 +211,15 @@ function createOrbit(req) {
|
|
|
202
211
|
},
|
|
203
212
|
}),
|
|
204
213
|
org: () => ({ ...req.ctx.org }),
|
|
214
|
+
secret: (name) => {
|
|
215
|
+
if (typeof name !== "string" || !SECRET_NAME.test(name)) {
|
|
216
|
+
throw new Error(`the secret name ${String(name)} must be 1 to 64 characters of A-Z 0-9 _ and start with a letter`);
|
|
217
|
+
}
|
|
218
|
+
const nonce = req.ctx.secretNonce;
|
|
219
|
+
if (typeof nonce !== "string" || !/^[0-9a-f]{32}$/.test(nonce))
|
|
220
|
+
throw new Error("this server gives functions no secrets");
|
|
221
|
+
return `{{orbit:${name}:${nonce}}}`;
|
|
222
|
+
},
|
|
205
223
|
};
|
|
206
224
|
}
|
|
207
225
|
// "/users.info?user=U1" → the path and a query map, as the proxy takes them.
|