@midsummer-ai/sdk 0.1.0-staging.4465 → 0.1.0-staging.4468
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/here-C2jhTheQ.mjs +1 -1
- package/package.json +1 -1
- package/webhooks/contract.d.mts +2 -2
- package/webhooks/contract.mjs +23 -0
- package/webhooks/index.d.mts +14 -4
- package/webhooks/index.mjs +33 -16
- package/webhooks/stand-in.mjs +13 -13
package/here-C2jhTheQ.mjs
CHANGED
|
@@ -12,7 +12,7 @@ function processEnv() {
|
|
|
12
12
|
* This package's version: `0.0.0-dev` here, and in a published package the version it was published at, which the
|
|
13
13
|
* release writes in its place as it stages the package (`stampSdk` in `scripts/release/packages.ts`).
|
|
14
14
|
*/
|
|
15
|
-
const SDK_VERSION = "0.1.0-staging.
|
|
15
|
+
const SDK_VERSION = "0.1.0-staging.4468";
|
|
16
16
|
/**
|
|
17
17
|
* The header that tells Midsummer which client made each of this package's calls: `Midsummer-Client: sdk/<version>
|
|
18
18
|
* <part>`, `part` the product whose client it is (`database`, `storage`, `queues`, ...), so the platform counts which
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midsummer-ai/sdk",
|
|
3
|
-
"version": "0.1.0-staging.
|
|
3
|
+
"version": "0.1.0-staging.4468",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",
|
package/webhooks/contract.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ScopedWebhooks } from "./index.mjs";
|
|
2
2
|
//#region src/webhooks/contract.d.ts
|
|
3
3
|
declare const CONTRACT_RECEIVER = "https://hooks.example.com/contract";
|
|
4
4
|
declare const CONTRACT_FAILING = "https://hooks.example.com/failing";
|
|
@@ -10,7 +10,7 @@ type Posted = () => ReadonlyArray<{
|
|
|
10
10
|
}>;
|
|
11
11
|
interface ContractCase {
|
|
12
12
|
readonly name: string;
|
|
13
|
-
run(webhooks:
|
|
13
|
+
run(webhooks: ScopedWebhooks, posted: Posted): Promise<void>;
|
|
14
14
|
}
|
|
15
15
|
declare const webhooksContract: ReadonlyArray<ContractCase>;
|
|
16
16
|
//#endregion
|
package/webhooks/contract.mjs
CHANGED
|
@@ -109,6 +109,29 @@ const webhooksContract = [
|
|
|
109
109
|
})).id, tried.id, "the replayed delivery's id");
|
|
110
110
|
}
|
|
111
111
|
},
|
|
112
|
+
{
|
|
113
|
+
name: "for one customer, another customer's endpoint, message or delivery is not found by its id",
|
|
114
|
+
async run(webhooks) {
|
|
115
|
+
const grace = someone(), ada = someone();
|
|
116
|
+
const { endpoint } = await webhooks.endpoints.create({
|
|
117
|
+
customer: grace,
|
|
118
|
+
url: CONTRACT_RECEIVER
|
|
119
|
+
});
|
|
120
|
+
const sent = await webhooks.send({
|
|
121
|
+
customer: grace,
|
|
122
|
+
type: "order.paid",
|
|
123
|
+
data: {}
|
|
124
|
+
});
|
|
125
|
+
const delivery = await eventually(async () => (await webhooks.deliveries.list({ endpoint: endpoint.id }))[0]);
|
|
126
|
+
const theirs = webhooks.customer(ada);
|
|
127
|
+
await refused(() => theirs.endpoints.show(endpoint.id), "not-found");
|
|
128
|
+
await refused(() => theirs.endpoints.remove(endpoint.id), "not-found");
|
|
129
|
+
await refused(() => theirs.messages.show(sent.id), "not-found");
|
|
130
|
+
await refused(() => theirs.deliveries.replay(delivery.id), "not-found");
|
|
131
|
+
equal(await theirs.endpoints.list(), [], "ada's endpoints");
|
|
132
|
+
equal((await webhooks.customer(grace).endpoints.show(endpoint.id)).id, endpoint.id, "grace's own endpoint");
|
|
133
|
+
}
|
|
134
|
+
},
|
|
112
135
|
{
|
|
113
136
|
name: "a customer, a type or a message that isn't one is refused before anything is sent",
|
|
114
137
|
async run(webhooks, posted) {
|
package/webhooks/index.d.mts
CHANGED
|
@@ -168,13 +168,23 @@ declare const MAX_MESSAGE_BYTES: number;
|
|
|
168
168
|
* `WebhooksError("invalid")` saying what is wrong.
|
|
169
169
|
*/
|
|
170
170
|
declare function checkCall(op: WebhooksOp, given: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>>;
|
|
171
|
-
/** Webhooks over `engine`, each call checked before it leaves. */
|
|
172
|
-
declare function webhooksOver(engine: WebhooksEngine): Webhooks
|
|
171
|
+
/** Webhooks over `engine`, each call checked before it leaves; for one customer alone when `customer` is given (`webhooks.customer`). */
|
|
172
|
+
declare function webhooksOver(engine: WebhooksEngine, customer?: string): Webhooks & {
|
|
173
|
+
customer(id: string): Webhooks;
|
|
174
|
+
};
|
|
173
175
|
/**
|
|
174
176
|
* Webhooks: `webhooks.send(…)` in a deployment, or `webhooks(engine).send(…)` over an engine you give: `client(…)`
|
|
175
177
|
* outside one, or the Stand-in in tests.
|
|
176
178
|
*/
|
|
177
|
-
declare const webhooks:
|
|
179
|
+
declare const webhooks: ScopedWebhooks & ((engine?: WebhooksEngine) => ScopedWebhooks);
|
|
180
|
+
/**
|
|
181
|
+
* Webhooks, and the same for one customer alone: `webhooks.customer(user.id)` reaches only that customer's endpoints,
|
|
182
|
+
* messages and deliveries, and one of another customer's, named by its id, is not found. Use it on a page that shows a
|
|
183
|
+
* signed-in customer their own, so an id from the page can never act on someone else's.
|
|
184
|
+
*/
|
|
185
|
+
type ScopedWebhooks = Webhooks & {
|
|
186
|
+
customer(id: string): Webhooks;
|
|
187
|
+
};
|
|
178
188
|
/**
|
|
179
189
|
* Webhooks through Midsummer's API, for code outside a deployment: `project` is `<team>/<project>`, `token` a token
|
|
180
190
|
* whose person or agent may deploy it (`midsummer token create`). `environment` is production unless given; `api` the
|
|
@@ -221,4 +231,4 @@ declare function verifyWebhook(headers: Headers | Readonly<Record<string, string
|
|
|
221
231
|
readonly now?: number;
|
|
222
232
|
}): Promise<boolean>;
|
|
223
233
|
//#endregion
|
|
224
|
-
export { BoundWebhooksAnswer, Delivery, Endpoint, EventType, MAX_MESSAGE_BYTES, Message, OutgoingMessage, Try, Webhooks, WebhooksEngine, WebhooksEnvironment, WebhooksError, WebhooksErrorCode, WebhooksOp, boundWebhooks, checkCall, client, serveWebhooks, verifyWebhook, webhooks, webhooksOver };
|
|
234
|
+
export { BoundWebhooksAnswer, Delivery, Endpoint, EventType, MAX_MESSAGE_BYTES, Message, OutgoingMessage, ScopedWebhooks, Try, Webhooks, WebhooksEngine, WebhooksEnvironment, WebhooksError, WebhooksErrorCode, WebhooksOp, boundWebhooks, checkCall, client, serveWebhooks, verifyWebhook, webhooks, webhooksOver };
|
package/webhooks/index.mjs
CHANGED
|
@@ -9,10 +9,11 @@ import { verifyEvent } from "../apps.mjs";
|
|
|
9
9
|
*
|
|
10
10
|
* await webhooks.send({ customer: user.id, type: "order.paid", data: { order: 1043, total: 40 }, key: "order-1043-paid" })
|
|
11
11
|
*
|
|
12
|
-
* // On your customer's settings page, from your server,
|
|
13
|
-
* const
|
|
14
|
-
* const
|
|
15
|
-
* await
|
|
12
|
+
* // On your customer's settings page, from your server, for the customer signed in and nobody else:
|
|
13
|
+
* const theirs = webhooks.customer(user.id)
|
|
14
|
+
* const { endpoint, secret } = await theirs.endpoints.create({ customer: user.id, url, types: ["order.paid"] })
|
|
15
|
+
* const failed = await theirs.deliveries.list({ status: "failed" })
|
|
16
|
+
* await theirs.deliveries.replay(failed[0].id)
|
|
16
17
|
*
|
|
17
18
|
* Nothing is made first: a customer is your app's own id for them, and a type any `noun.verb` name. A message sent to a
|
|
18
19
|
* customer with no endpoint is kept and sent nowhere. Sending the same `key` again within 30 days returns the first
|
|
@@ -95,6 +96,7 @@ function checkCall(op, given) {
|
|
|
95
96
|
}
|
|
96
97
|
case "endpoint.edit": return defined({
|
|
97
98
|
endpoint: idOf(given.endpoint, "endpoint"),
|
|
99
|
+
customer: optional(given.customer, customerOf),
|
|
98
100
|
url: optional(given.url, String),
|
|
99
101
|
types: given.types === null ? null : optional(given.types, (types) => Array.isArray(types) ? types.map(typeOf) : (() => {
|
|
100
102
|
throw new WebhooksError("invalid", "types is a list");
|
|
@@ -105,21 +107,32 @@ function checkCall(op, given) {
|
|
|
105
107
|
case "endpoint.replay": {
|
|
106
108
|
const since = given.since instanceof Date ? given.since.getTime() : given.since;
|
|
107
109
|
if (typeof since !== "number" || !Number.isFinite(since) || since < 0) throw new WebhooksError("invalid", "since is a time");
|
|
108
|
-
return {
|
|
110
|
+
return defined({
|
|
109
111
|
endpoint: idOf(given.endpoint, "endpoint"),
|
|
110
|
-
since: Math.floor(since)
|
|
111
|
-
|
|
112
|
+
since: Math.floor(since),
|
|
113
|
+
customer: optional(given.customer, customerOf)
|
|
114
|
+
});
|
|
112
115
|
}
|
|
113
116
|
case "endpoint.test": return defined({
|
|
114
117
|
endpoint: idOf(given.endpoint, "endpoint"),
|
|
115
|
-
key: optional(given.key, (key) => idOf(key, "test's key"))
|
|
118
|
+
key: optional(given.key, (key) => idOf(key, "test's key")),
|
|
119
|
+
customer: optional(given.customer, customerOf)
|
|
116
120
|
});
|
|
117
121
|
case "endpoint.show":
|
|
118
122
|
case "endpoint.rotate":
|
|
119
|
-
case "endpoint.delete": return {
|
|
120
|
-
|
|
123
|
+
case "endpoint.delete": return defined({
|
|
124
|
+
endpoint: idOf(given.endpoint, "endpoint"),
|
|
125
|
+
customer: optional(given.customer, customerOf)
|
|
126
|
+
});
|
|
127
|
+
case "message.show": return defined({
|
|
128
|
+
message: idOf(given.message, "message"),
|
|
129
|
+
customer: optional(given.customer, customerOf)
|
|
130
|
+
});
|
|
121
131
|
case "delivery.show":
|
|
122
|
-
case "delivery.replay": return {
|
|
132
|
+
case "delivery.replay": return defined({
|
|
133
|
+
delivery: idOf(given.delivery, "delivery"),
|
|
134
|
+
customer: optional(given.customer, customerOf)
|
|
135
|
+
});
|
|
123
136
|
case "endpoint.list":
|
|
124
137
|
case "message.list": return defined({
|
|
125
138
|
customer: optional(given.customer, customerOf),
|
|
@@ -138,9 +151,12 @@ function checkCall(op, given) {
|
|
|
138
151
|
case "type.list": return {};
|
|
139
152
|
}
|
|
140
153
|
}
|
|
141
|
-
/** Webhooks over `engine`, each call checked before it leaves. */
|
|
142
|
-
function webhooksOver(engine) {
|
|
143
|
-
const call = async (op, input) => await engine.call(op, checkCall(op, input
|
|
154
|
+
/** Webhooks over `engine`, each call checked before it leaves; for one customer alone when `customer` is given (`webhooks.customer`). */
|
|
155
|
+
function webhooksOver(engine, customer) {
|
|
156
|
+
const call = async (op, input) => await engine.call(op, checkCall(op, customer === void 0 || op === "type.list" ? input : {
|
|
157
|
+
...input,
|
|
158
|
+
customer: input.customer ?? customer
|
|
159
|
+
}));
|
|
144
160
|
return {
|
|
145
161
|
send: (message) => call("send", { ...message }),
|
|
146
162
|
endpoints: {
|
|
@@ -173,7 +189,8 @@ function webhooksOver(engine) {
|
|
|
173
189
|
show: (id) => call("delivery.show", { delivery: id }),
|
|
174
190
|
replay: (id) => call("delivery.replay", { delivery: id })
|
|
175
191
|
},
|
|
176
|
-
types: () => call("type.list", {})
|
|
192
|
+
types: () => call("type.list", {}),
|
|
193
|
+
customer: (id) => webhooksOver(engine, customerOf(id))
|
|
177
194
|
};
|
|
178
195
|
}
|
|
179
196
|
/** `midsummer dev`'s webhooks: one Stand-in for the process, posting to the endpoints added there, this computer's too. */
|
|
@@ -284,7 +301,7 @@ function client(options) {
|
|
|
284
301
|
...rest,
|
|
285
302
|
...SCOPED.has(op) && options.environment ? { environment: options.environment } : {}
|
|
286
303
|
};
|
|
287
|
-
const query = method === "GET" ? `?${new URLSearchParams(Object.entries(fields).map(([key, value]) => [key, String(value)]))}` : "";
|
|
304
|
+
const query = method === "GET" || method === "DELETE" ? `?${new URLSearchParams(Object.entries(fields).map(([key, value]) => [key, String(value)]))}` : "";
|
|
288
305
|
let response;
|
|
289
306
|
try {
|
|
290
307
|
response = await send(`${base}${path(input)}${query === "?" ? "" : query}`, {
|
package/webhooks/stand-in.mjs
CHANGED
|
@@ -38,14 +38,14 @@ function standIn(options = {}) {
|
|
|
38
38
|
if (options.local && (url.protocol === "http:" || url.protocol === "https:")) return url.toString();
|
|
39
39
|
throw new WebhooksError("invalid", "an endpoint's address is https on the public internet");
|
|
40
40
|
}
|
|
41
|
-
function endpointOf(endpointId) {
|
|
41
|
+
function endpointOf(endpointId, customer) {
|
|
42
42
|
const kept = endpoints.get(String(endpointId));
|
|
43
|
-
if (!kept) throw new WebhooksError("not-found", `No endpoint ${String(endpointId)} here.`);
|
|
43
|
+
if (!kept || customer !== void 0 && kept.endpoint.customer !== customer) throw new WebhooksError("not-found", `No endpoint ${String(endpointId)} here.`);
|
|
44
44
|
return kept;
|
|
45
45
|
}
|
|
46
|
-
function deliveryOf(deliveryId) {
|
|
46
|
+
function deliveryOf(deliveryId, customer) {
|
|
47
47
|
const kept = deliveries.get(String(deliveryId));
|
|
48
|
-
if (!kept) throw new WebhooksError("not-found", `No delivery ${String(deliveryId)} here.`);
|
|
48
|
+
if (!kept || customer !== void 0 && kept.delivery.customer !== customer) throw new WebhooksError("not-found", `No delivery ${String(deliveryId)} here.`);
|
|
49
49
|
return kept;
|
|
50
50
|
}
|
|
51
51
|
/** One try of a delivery, signed with the endpoint's secrets: delivered on a 2xx, failed otherwise. */
|
|
@@ -153,7 +153,7 @@ function standIn(options = {}) {
|
|
|
153
153
|
},
|
|
154
154
|
async "message.show"(input) {
|
|
155
155
|
const message = messages.get(String(input.message));
|
|
156
|
-
if (!message) throw new WebhooksError("not-found", `No message ${String(input.message)} here.`);
|
|
156
|
+
if (!message || input.customer !== void 0 && message.customer !== input.customer) throw new WebhooksError("not-found", `No message ${String(input.message)} here.`);
|
|
157
157
|
return {
|
|
158
158
|
...shown(message),
|
|
159
159
|
deliveries: message.deliveries.map((one) => deliveries.get(one).delivery)
|
|
@@ -196,10 +196,10 @@ function standIn(options = {}) {
|
|
|
196
196
|
return [...endpoints.values()].map(({ endpoint }) => endpoint).filter((endpoint) => input.customer === void 0 || endpoint.customer === input.customer).reverse();
|
|
197
197
|
},
|
|
198
198
|
async "endpoint.show"(input) {
|
|
199
|
-
return endpointOf(input.endpoint).endpoint;
|
|
199
|
+
return endpointOf(input.endpoint, input.customer).endpoint;
|
|
200
200
|
},
|
|
201
201
|
async "endpoint.edit"(input) {
|
|
202
|
-
const kept = endpointOf(input.endpoint);
|
|
202
|
+
const kept = endpointOf(input.endpoint, input.customer);
|
|
203
203
|
const { types: _types, disabled: _off, description: _description, ...rest } = kept.endpoint;
|
|
204
204
|
const typesNamed = input.types === void 0 ? kept.endpoint.types : input.types === null ? void 0 : input.types;
|
|
205
205
|
const description = input.description === void 0 ? kept.endpoint.description : input.description === null ? void 0 : String(input.description);
|
|
@@ -215,7 +215,7 @@ function standIn(options = {}) {
|
|
|
215
215
|
return kept.endpoint;
|
|
216
216
|
},
|
|
217
217
|
async "endpoint.rotate"(input) {
|
|
218
|
-
const kept = endpointOf(input.endpoint);
|
|
218
|
+
const kept = endpointOf(input.endpoint, input.customer);
|
|
219
219
|
kept.previous = kept.secret;
|
|
220
220
|
kept.secret = secret();
|
|
221
221
|
kept.endpoint = {
|
|
@@ -228,11 +228,11 @@ function standIn(options = {}) {
|
|
|
228
228
|
};
|
|
229
229
|
},
|
|
230
230
|
async "endpoint.delete"(input) {
|
|
231
|
-
endpointOf(input.endpoint);
|
|
231
|
+
endpointOf(input.endpoint, input.customer);
|
|
232
232
|
endpoints.delete(String(input.endpoint));
|
|
233
233
|
},
|
|
234
234
|
async "endpoint.test"(input) {
|
|
235
|
-
const { endpoint } = endpointOf(input.endpoint);
|
|
235
|
+
const { endpoint } = endpointOf(input.endpoint, input.customer);
|
|
236
236
|
return attempt(deliver(endpoint, {
|
|
237
237
|
id: id("ping_"),
|
|
238
238
|
customer: endpoint.customer,
|
|
@@ -247,7 +247,7 @@ function standIn(options = {}) {
|
|
|
247
247
|
})));
|
|
248
248
|
},
|
|
249
249
|
async "endpoint.replay"(input) {
|
|
250
|
-
const { endpoint } = endpointOf(input.endpoint);
|
|
250
|
+
const { endpoint } = endpointOf(input.endpoint, input.customer);
|
|
251
251
|
if (!endpoint.enabled) throw new WebhooksError("invalid", `${endpoint.id} is off. Turn it on first.`);
|
|
252
252
|
const since = Number(input.since);
|
|
253
253
|
const theirs = [...messages.values()].filter((message) => message.customer === endpoint.customer && message.createdAt >= since && takes(endpoint, message.type));
|
|
@@ -278,14 +278,14 @@ function standIn(options = {}) {
|
|
|
278
278
|
return [...deliveries.values()].map((kept) => kept.delivery).filter((delivery) => (input.endpoint === void 0 || delivery.endpointId === input.endpoint) && (input.customer === void 0 || delivery.customer === input.customer) && (input.status === void 0 || delivery.status === input.status)).reverse().slice(0, Number(input.limit ?? 50));
|
|
279
279
|
},
|
|
280
280
|
async "delivery.show"(input) {
|
|
281
|
-
const kept = deliveryOf(input.delivery);
|
|
281
|
+
const kept = deliveryOf(input.delivery, input.customer);
|
|
282
282
|
return {
|
|
283
283
|
...kept.delivery,
|
|
284
284
|
tries: [...kept.tries]
|
|
285
285
|
};
|
|
286
286
|
},
|
|
287
287
|
async "delivery.replay"(input) {
|
|
288
|
-
const kept = deliveryOf(input.delivery);
|
|
288
|
+
const kept = deliveryOf(input.delivery, input.customer);
|
|
289
289
|
if (!endpointOf(kept.delivery.endpointId).endpoint.enabled) throw new WebhooksError("invalid", `${kept.delivery.endpointId} is off. Turn it on first.`);
|
|
290
290
|
kept.delivery = {
|
|
291
291
|
...kept.delivery,
|