@distilled.cloud/slack 1.0.0-rc.13 → 1.0.0-rc.14
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/lib/credentials.d.ts +2 -2
- package/lib/credentials.d.ts.map +1 -1
- package/lib/credentials.js +3 -3
- package/lib/credentials.js.map +1 -1
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -1
- package/lib/errors.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -3
- package/lib/index.js.map +1 -1
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js.map +1 -1
- package/lib/protocol.d.ts +3 -3
- package/lib/protocol.d.ts.map +1 -1
- package/lib/protocol.js +12 -18
- package/lib/protocol.js.map +1 -1
- package/lib/protocol.test.d.ts +2 -0
- package/lib/protocol.test.d.ts.map +1 -0
- package/lib/protocol.test.js +288 -0
- package/lib/protocol.test.js.map +1 -0
- package/lib/retry.d.ts +1 -1
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -1
- package/lib/retry.js.map +1 -1
- package/lib/services/admin.d.ts +236 -29
- package/lib/services/admin.d.ts.map +1 -1
- package/lib/services/admin.js +417 -749
- package/lib/services/admin.js.map +1 -1
- package/lib/services/agents.d.ts +423 -0
- package/lib/services/agents.d.ts.map +1 -0
- package/lib/services/agents.js +315 -0
- package/lib/services/agents.js.map +1 -0
- package/lib/services/api.d.ts +1 -1
- package/lib/services/api.d.ts.map +1 -1
- package/lib/services/api.js +4 -6
- package/lib/services/api.js.map +1 -1
- package/lib/services/apps.d.ts +21 -3
- package/lib/services/apps.d.ts.map +1 -1
- package/lib/services/apps.js +65 -133
- package/lib/services/apps.js.map +1 -1
- package/lib/services/assistant.d.ts +4 -4
- package/lib/services/assistant.d.ts.map +1 -1
- package/lib/services/assistant.js +16 -38
- package/lib/services/assistant.js.map +1 -1
- package/lib/services/auth.d.ts +2 -2
- package/lib/services/auth.d.ts.map +1 -1
- package/lib/services/auth.js +11 -28
- package/lib/services/auth.js.map +1 -1
- package/lib/services/blocks.d.ts +1 -1
- package/lib/services/blocks.d.ts.map +1 -1
- package/lib/services/blocks.js +5 -9
- package/lib/services/blocks.js.map +1 -1
- package/lib/services/bookmarks.d.ts +4 -4
- package/lib/services/bookmarks.d.ts.map +1 -1
- package/lib/services/bookmarks.js +14 -30
- package/lib/services/bookmarks.js.map +1 -1
- package/lib/services/bots.d.ts +1 -1
- package/lib/services/bots.d.ts.map +1 -1
- package/lib/services/bots.js +7 -15
- package/lib/services/bots.js.map +1 -1
- package/lib/services/calls.d.ts +1 -1
- package/lib/services/calls.d.ts.map +1 -1
- package/lib/services/calls.js +13 -33
- package/lib/services/calls.js.map +1 -1
- package/lib/services/canvases.d.ts +22 -2
- package/lib/services/canvases.d.ts.map +1 -1
- package/lib/services/canvases.js +37 -48
- package/lib/services/canvases.js.map +1 -1
- package/lib/services/chat.d.ts +29 -25
- package/lib/services/chat.d.ts.map +1 -1
- package/lib/services/chat.js +47 -95
- package/lib/services/chat.js.map +1 -1
- package/lib/services/conversations.d.ts +10 -8
- package/lib/services/conversations.d.ts.map +1 -1
- package/lib/services/conversations.js +43 -115
- package/lib/services/conversations.js.map +1 -1
- package/lib/services/dialog.d.ts +1 -1
- package/lib/services/dialog.d.ts.map +1 -1
- package/lib/services/dialog.js +5 -9
- package/lib/services/dialog.js.map +1 -1
- package/lib/services/dnd.d.ts +1 -1
- package/lib/services/dnd.d.ts.map +1 -1
- package/lib/services/dnd.js +10 -24
- package/lib/services/dnd.js.map +1 -1
- package/lib/services/emoji.d.ts +1 -1
- package/lib/services/emoji.d.ts.map +1 -1
- package/lib/services/emoji.js +6 -11
- package/lib/services/emoji.js.map +1 -1
- package/lib/services/entity.d.ts +1 -1
- package/lib/services/entity.d.ts.map +1 -1
- package/lib/services/entity.js +8 -20
- package/lib/services/entity.js.map +1 -1
- package/lib/services/files.d.ts +5 -5
- package/lib/services/files.d.ts.map +1 -1
- package/lib/services/files.js +39 -109
- package/lib/services/files.js.map +1 -1
- package/lib/services/functions.d.ts +1 -1
- package/lib/services/functions.d.ts.map +1 -1
- package/lib/services/functions.js +28 -75
- package/lib/services/functions.js.map +1 -1
- package/lib/services/index.d.ts +1 -0
- package/lib/services/index.d.ts.map +1 -1
- package/lib/services/index.js +1 -0
- package/lib/services/index.js.map +1 -1
- package/lib/services/lists.d.ts +5 -1
- package/lib/services/lists.d.ts.map +1 -1
- package/lib/services/lists.js +35 -146
- package/lib/services/lists.js.map +1 -1
- package/lib/services/migration.d.ts +1 -1
- package/lib/services/migration.d.ts.map +1 -1
- package/lib/services/migration.js +5 -8
- package/lib/services/migration.js.map +1 -1
- package/lib/services/oauth.d.ts +38 -2
- package/lib/services/oauth.d.ts.map +1 -1
- package/lib/services/oauth.js +67 -50
- package/lib/services/oauth.js.map +1 -1
- package/lib/services/openid.d.ts +1 -1
- package/lib/services/openid.d.ts.map +1 -1
- package/lib/services/openid.js +7 -15
- package/lib/services/openid.js.map +1 -1
- package/lib/services/pins.d.ts +1 -1
- package/lib/services/pins.d.ts.map +1 -1
- package/lib/services/pins.js +7 -15
- package/lib/services/pins.js.map +1 -1
- package/lib/services/reactions.d.ts +2 -2
- package/lib/services/reactions.d.ts.map +1 -1
- package/lib/services/reactions.js +13 -29
- package/lib/services/reactions.js.map +1 -1
- package/lib/services/reminders.d.ts +1 -1
- package/lib/services/reminders.d.ts.map +1 -1
- package/lib/services/reminders.js +13 -33
- package/lib/services/reminders.js.map +1 -1
- package/lib/services/rtm.d.ts +1 -1
- package/lib/services/rtm.d.ts.map +1 -1
- package/lib/services/rtm.js +9 -21
- package/lib/services/rtm.js.map +1 -1
- package/lib/services/search.d.ts +1 -1
- package/lib/services/search.d.ts.map +1 -1
- package/lib/services/search.js +13 -30
- package/lib/services/search.js.map +1 -1
- package/lib/services/stars.d.ts +1 -1
- package/lib/services/stars.d.ts.map +1 -1
- package/lib/services/stars.js +10 -20
- package/lib/services/stars.js.map +1 -1
- package/lib/services/team.d.ts +1 -1
- package/lib/services/team.d.ts.map +1 -1
- package/lib/services/team.js +25 -62
- package/lib/services/team.js.map +1 -1
- package/lib/services/tooling.d.ts +1 -1
- package/lib/services/tooling.d.ts.map +1 -1
- package/lib/services/tooling.js +5 -9
- package/lib/services/tooling.js.map +1 -1
- package/lib/services/usergroups.d.ts +8 -3
- package/lib/services/usergroups.d.ts.map +1 -1
- package/lib/services/usergroups.js +23 -51
- package/lib/services/usergroups.js.map +1 -1
- package/lib/services/users.d.ts +2 -2
- package/lib/services/users.d.ts.map +1 -1
- package/lib/services/users.js +28 -81
- package/lib/services/users.js.map +1 -1
- package/lib/services/views.d.ts +1 -1
- package/lib/services/views.d.ts.map +1 -1
- package/lib/services/views.js +11 -27
- package/lib/services/views.js.map +1 -1
- package/lib/services/workflows.d.ts +1 -1
- package/lib/services/workflows.d.ts.map +1 -1
- package/lib/services/workflows.js +27 -71
- package/lib/services/workflows.js.map +1 -1
- package/package.json +15 -11
- package/src/credentials.ts +8 -10
- package/src/errors.ts +17 -31
- package/src/index.ts +3 -7
- package/src/pagination.ts +2 -5
- package/src/protocol.test.ts +372 -0
- package/src/protocol.ts +34 -66
- package/src/retry.ts +4 -9
- package/src/services/admin.ts +1241 -1557
- package/src/services/agents.ts +853 -0
- package/src/services/api.ts +4 -10
- package/src/services/apps.ts +146 -243
- package/src/services/assistant.ts +93 -144
- package/src/services/auth.ts +12 -33
- package/src/services/blocks.ts +9 -24
- package/src/services/bookmarks.ts +14 -34
- package/src/services/bots.ts +7 -19
- package/src/services/calls.ts +15 -43
- package/src/services/canvases.ts +82 -85
- package/src/services/chat.ts +134 -254
- package/src/services/conversations.ts +101 -217
- package/src/services/dialog.ts +5 -13
- package/src/services/dnd.ts +11 -31
- package/src/services/emoji.ts +9 -19
- package/src/services/entity.ts +17 -45
- package/src/services/files.ts +72 -165
- package/src/services/functions.ts +95 -168
- package/src/services/index.ts +1 -0
- package/src/services/lists.ts +65 -208
- package/src/services/migration.ts +8 -17
- package/src/services/oauth.ts +127 -60
- package/src/services/openid.ts +12 -34
- package/src/services/pins.ts +7 -19
- package/src/services/reactions.ts +19 -43
- package/src/services/reminders.ts +13 -37
- package/src/services/rtm.ts +9 -25
- package/src/services/search.ts +51 -92
- package/src/services/stars.ts +20 -31
- package/src/services/team.ts +81 -155
- package/src/services/tooling.ts +5 -13
- package/src/services/usergroups.ts +37 -68
- package/src/services/users.ts +35 -102
- package/src/services/views.ts +11 -31
- package/src/services/workflows.ts +65 -129
- package/lib/response-validation.test.d.ts +0 -4
- package/lib/response-validation.test.d.ts.map +0 -1
- package/lib/response-validation.test.js +0 -40
- package/lib/response-validation.test.js.map +0 -1
- package/src/response-validation.test.ts +0 -54
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import * as ResponseValidation from "@distilled.cloud/core/response-validation";
|
|
2
|
+
import * as Duration from "effect/Duration";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as HttpClient from "effect/http/HttpClient";
|
|
5
|
+
import * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
6
|
+
import * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
7
|
+
import * as Layer from "effect/Layer";
|
|
8
|
+
import * as Redacted from "effect/Redacted";
|
|
9
|
+
import * as Result from "effect/Result";
|
|
10
|
+
import { describe, expect, test } from "vitest";
|
|
11
|
+
import { type Credentials, credentials } from "./credentials.ts";
|
|
12
|
+
import {
|
|
13
|
+
GatewayTimeout,
|
|
14
|
+
InternalServerError,
|
|
15
|
+
NotFound,
|
|
16
|
+
ServiceUnavailable,
|
|
17
|
+
SlackError,
|
|
18
|
+
SlackHttpError,
|
|
19
|
+
SlackParseError,
|
|
20
|
+
SlackRateLimited,
|
|
21
|
+
Unauthorized,
|
|
22
|
+
} from "./errors.ts";
|
|
23
|
+
import type { SlackOpContext } from "./protocol.ts";
|
|
24
|
+
import * as Retry from "./retry.ts";
|
|
25
|
+
import { bulkArchive, getFile } from "./services/admin.ts";
|
|
26
|
+
import { postMessage } from "./services/chat.ts";
|
|
27
|
+
import { addRemote } from "./services/files.ts";
|
|
28
|
+
import { migrationExchange } from "./services/migration.ts";
|
|
29
|
+
import { v2Exchange } from "./services/oauth.ts";
|
|
30
|
+
|
|
31
|
+
interface Reply {
|
|
32
|
+
readonly status?: number;
|
|
33
|
+
readonly body?: string | Uint8Array<ArrayBuffer>;
|
|
34
|
+
readonly headers?: Record<string, string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const bot = credentials({ token: Redacted.make("xoxb-test") });
|
|
38
|
+
|
|
39
|
+
/** A fake HttpClient that records each request (as a web Request) and answers with `reply`. */
|
|
40
|
+
const fakeHttp = (reply: Reply) => {
|
|
41
|
+
const requests: Array<Request> = [];
|
|
42
|
+
const layer = Layer.succeed(
|
|
43
|
+
HttpClient.HttpClient,
|
|
44
|
+
HttpClient.make((request) =>
|
|
45
|
+
Effect.sync(() => {
|
|
46
|
+
requests.push(Result.getOrThrow(HttpClientRequest.toWebResult(request)));
|
|
47
|
+
return HttpClientResponse.fromWeb(
|
|
48
|
+
request,
|
|
49
|
+
new Response(reply.body ?? "", {
|
|
50
|
+
status: reply.status ?? 200,
|
|
51
|
+
headers: reply.headers,
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
}),
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
return { requests, layer };
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const run = <A, E>(
|
|
61
|
+
operation: Effect.Effect<A, E, SlackOpContext>,
|
|
62
|
+
reply: Reply,
|
|
63
|
+
creds: Layer.Layer<Credentials> = bot,
|
|
64
|
+
) => {
|
|
65
|
+
const http = fakeHttp(reply);
|
|
66
|
+
const promise = Effect.runPromise(
|
|
67
|
+
operation.pipe(Retry.none, Effect.provide(Layer.mergeAll(http.layer, creds))),
|
|
68
|
+
);
|
|
69
|
+
return { requests: http.requests, promise };
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const succeed = <A, E>(operation: Effect.Effect<A, E, SlackOpContext>, reply: Reply) =>
|
|
73
|
+
run(operation, reply).promise;
|
|
74
|
+
|
|
75
|
+
const failWith = <A, E>(operation: Effect.Effect<A, E, SlackOpContext>, reply: Reply) =>
|
|
76
|
+
run(Effect.flip(operation), reply).promise;
|
|
77
|
+
|
|
78
|
+
const ok = (payload: Record<string, unknown>) => JSON.stringify({ ok: true, ...payload });
|
|
79
|
+
const notOk = (error: string, extra: Record<string, unknown> = {}) =>
|
|
80
|
+
JSON.stringify({ ok: false, error, ...extra });
|
|
81
|
+
|
|
82
|
+
const posted = { channel: "C1", ts: "1.0" };
|
|
83
|
+
const exchanged = {
|
|
84
|
+
app_id: "A1",
|
|
85
|
+
token_type: "bot",
|
|
86
|
+
scope: "chat:write",
|
|
87
|
+
access_token: "xoxe.xoxb-new",
|
|
88
|
+
expires_in: 43200,
|
|
89
|
+
refresh_token: "xoxe-1-refresh",
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
describe("request encoding", () => {
|
|
93
|
+
test("a JSON method POSTs to <base>/<method> with a Bearer token and a JSON body", async () => {
|
|
94
|
+
const blocks = [{ type: "section", text: { type: "mrkdwn", text: "hi" } }];
|
|
95
|
+
const { requests, promise } = run(postMessage({ channel: "C1", text: "hi", blocks }), {
|
|
96
|
+
body: ok(posted),
|
|
97
|
+
});
|
|
98
|
+
await promise;
|
|
99
|
+
expect(requests).toHaveLength(1);
|
|
100
|
+
const request = requests[0]!;
|
|
101
|
+
expect(request.method).toBe("POST");
|
|
102
|
+
expect(request.url).toBe("https://slack.com/api/chat.postMessage");
|
|
103
|
+
expect(request.headers.get("authorization")).toBe("Bearer xoxb-test");
|
|
104
|
+
expect(request.headers.get("content-type")).toContain("application/json");
|
|
105
|
+
// JSON methods keep structured values structured.
|
|
106
|
+
expect(await request.json()).toEqual({ channel: "C1", text: "hi", blocks });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("a custom apiBaseUrl replaces the default host", async () => {
|
|
110
|
+
const { requests, promise } = run(
|
|
111
|
+
postMessage({ channel: "C1" }),
|
|
112
|
+
{ body: ok(posted) },
|
|
113
|
+
credentials({ token: Redacted.make("xoxb-test"), apiBaseUrl: "https://slack.test/api" }),
|
|
114
|
+
);
|
|
115
|
+
await promise;
|
|
116
|
+
expect(requests[0]!.url).toBe("https://slack.test/api/chat.postMessage");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("form methods comma-join ID lists", async () => {
|
|
120
|
+
const { requests, promise } = run(bulkArchive({ channel_ids: ["C1", "C2"] }), {
|
|
121
|
+
body: ok({ bulk_action_id: "B1", not_added: [] }),
|
|
122
|
+
});
|
|
123
|
+
await promise;
|
|
124
|
+
const request = requests[0]!;
|
|
125
|
+
expect(request.headers.get("content-type")).toContain("application/x-www-form-urlencoded");
|
|
126
|
+
const form = new URLSearchParams(await request.text());
|
|
127
|
+
expect([...form.entries()]).toEqual([["channel_ids", "C1,C2"]]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("form methods JSON-encode lists of rich values", async () => {
|
|
131
|
+
const indexable_file_contents = [{ text: "hello" }];
|
|
132
|
+
const { requests, promise } = run(
|
|
133
|
+
addRemote({
|
|
134
|
+
external_id: "X1",
|
|
135
|
+
external_url: "https://x.test",
|
|
136
|
+
title: "T",
|
|
137
|
+
indexable_file_contents,
|
|
138
|
+
}),
|
|
139
|
+
{ body: ok({ file: { id: "F1" } }) },
|
|
140
|
+
);
|
|
141
|
+
await promise;
|
|
142
|
+
const form = new URLSearchParams(await requests[0]!.text());
|
|
143
|
+
expect(form.get("external_id")).toBe("X1");
|
|
144
|
+
expect(JSON.parse(form.get("indexable_file_contents")!)).toEqual(indexable_file_contents);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("array query args are comma-joined, not repeated", async () => {
|
|
148
|
+
const { requests, promise } = run(migrationExchange({ users: ["U1", "U2"], to_old: true }), {
|
|
149
|
+
body: ok({ team_id: "T1", enterprise_id: "E1", user_id_map: {} }),
|
|
150
|
+
});
|
|
151
|
+
await promise;
|
|
152
|
+
const request = requests[0]!;
|
|
153
|
+
expect(request.method).toBe("GET");
|
|
154
|
+
const url = new URL(request.url);
|
|
155
|
+
expect(url.pathname).toBe("/api/migration.exchange");
|
|
156
|
+
expect(url.searchParams.getAll("users")).toEqual(["U1,U2"]);
|
|
157
|
+
expect(url.searchParams.get("to_old")).toBe("true");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("an empty token omits Authorization; Redacted inputs are sent unwrapped", async () => {
|
|
161
|
+
const { requests, promise } = run(
|
|
162
|
+
v2Exchange({ client_id: "cid", client_secret: Redacted.make("shh") }),
|
|
163
|
+
{ body: ok(exchanged) },
|
|
164
|
+
credentials({ token: Redacted.make("") }),
|
|
165
|
+
);
|
|
166
|
+
await promise;
|
|
167
|
+
const request = requests[0]!;
|
|
168
|
+
expect(request.headers.get("authorization")).toBeNull();
|
|
169
|
+
const form = new URLSearchParams(await request.text());
|
|
170
|
+
expect(form.get("client_id")).toBe("cid");
|
|
171
|
+
expect(form.get("client_secret")).toBe("shh");
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe("ok:true responses", () => {
|
|
176
|
+
test("the payload shares the envelope level and `ok` rides along", async () => {
|
|
177
|
+
const result = await succeed(postMessage({ channel: "C1" }), { body: ok(posted) });
|
|
178
|
+
expect(result).toEqual({ ok: true, channel: "C1", ts: "1.0" });
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("sensitive output members are wrapped in Redacted", async () => {
|
|
182
|
+
const result = await succeed(v2Exchange({ client_id: "cid", client_secret: "s" }), {
|
|
183
|
+
body: ok(exchanged),
|
|
184
|
+
});
|
|
185
|
+
expect(Redacted.isRedacted(result.access_token)).toBe(true);
|
|
186
|
+
expect(Redacted.value(result.access_token as Redacted.Redacted<string>)).toBe("xoxe.xoxb-new");
|
|
187
|
+
expect(Redacted.value(result.refresh_token as Redacted.Redacted<string>)).toBe(
|
|
188
|
+
"xoxe-1-refresh",
|
|
189
|
+
);
|
|
190
|
+
expect(result.app_id).toBe("A1");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("a non-JSON success body is returned as raw bytes", async () => {
|
|
194
|
+
const bytes = new Uint8Array([0x1f, 0x8b, 0x08, 0x00, 0xff]);
|
|
195
|
+
const result = await succeed(getFile({ type: "member", date: "2026-01-01" }), { body: bytes });
|
|
196
|
+
expect(result).toBeInstanceOf(Uint8Array);
|
|
197
|
+
expect([...(result as Uint8Array)]).toEqual([...bytes]);
|
|
198
|
+
// A raw document output passes strict validation too.
|
|
199
|
+
const strictResult = await succeed(
|
|
200
|
+
getFile({ type: "member" }).pipe(Effect.provide(ResponseValidation.strict)),
|
|
201
|
+
{ body: bytes },
|
|
202
|
+
);
|
|
203
|
+
expect([...(strictResult as Uint8Array)]).toEqual([...bytes]);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe("ok:false envelopes", () => {
|
|
208
|
+
test("an unrecognised slug inside HTTP 200 is SlackError carrying the slug", async () => {
|
|
209
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
210
|
+
body: notOk("channel_not_found"),
|
|
211
|
+
});
|
|
212
|
+
expect(error).toBeInstanceOf(SlackError);
|
|
213
|
+
expect(error).toMatchObject({ code: "channel_not_found" });
|
|
214
|
+
// The message is omitted when it would only repeat the slug.
|
|
215
|
+
expect((error as SlackError).message).toBe("");
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("missing_scope keeps needed/provided", async () => {
|
|
219
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
220
|
+
body: notOk("missing_scope", { needed: "chat:write", provided: "channels:read" }),
|
|
221
|
+
});
|
|
222
|
+
expect(error).toBeInstanceOf(SlackError);
|
|
223
|
+
expect(error).toMatchObject({
|
|
224
|
+
code: "missing_scope",
|
|
225
|
+
needed: "chat:write",
|
|
226
|
+
provided: "channels:read",
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("response_metadata.messages become the message and the messages list", async () => {
|
|
231
|
+
const messages = ["[ERROR] missing required field: channel", "[ERROR] bad text"];
|
|
232
|
+
const error = await failWith(postMessage({ channel: "" }), {
|
|
233
|
+
body: notOk("invalid_arguments", { response_metadata: { messages } }),
|
|
234
|
+
});
|
|
235
|
+
expect(error).toBeInstanceOf(SlackError);
|
|
236
|
+
expect(error).toMatchObject({ code: "invalid_arguments", message: messages[0], messages });
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test("a bare `errors` list is used as detail too", async () => {
|
|
240
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
241
|
+
body: notOk("invalid_blocks", { errors: ["invalid block at index 0"] }),
|
|
242
|
+
});
|
|
243
|
+
expect(error).toMatchObject({
|
|
244
|
+
code: "invalid_blocks",
|
|
245
|
+
message: "invalid block at index 0",
|
|
246
|
+
messages: ["invalid block at index 0"],
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("a slug under a non-2xx status still maps by slug", async () => {
|
|
251
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
252
|
+
status: 400,
|
|
253
|
+
body: notOk("invalid_arguments"),
|
|
254
|
+
});
|
|
255
|
+
expect(error).toBeInstanceOf(SlackError);
|
|
256
|
+
expect(error).toMatchObject({ code: "invalid_arguments" });
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
for (const slug of [
|
|
260
|
+
"not_authed",
|
|
261
|
+
"invalid_auth",
|
|
262
|
+
"account_inactive",
|
|
263
|
+
"token_revoked",
|
|
264
|
+
"token_expired",
|
|
265
|
+
]) {
|
|
266
|
+
test(`${slug} is Unauthorized`, async () => {
|
|
267
|
+
const error = await failWith(postMessage({ channel: "C1" }), { body: notOk(slug) });
|
|
268
|
+
expect(error).toBeInstanceOf(Unauthorized);
|
|
269
|
+
expect(error).toMatchObject({ message: slug });
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const serverSlugs = [
|
|
274
|
+
["internal_error", InternalServerError],
|
|
275
|
+
["fatal_error", InternalServerError],
|
|
276
|
+
["service_unavailable", ServiceUnavailable],
|
|
277
|
+
["request_timeout", GatewayTimeout],
|
|
278
|
+
] as const;
|
|
279
|
+
for (const [slug, ErrorClass] of serverSlugs) {
|
|
280
|
+
test(`${slug} is ${ErrorClass.name}`, async () => {
|
|
281
|
+
const error = await failWith(postMessage({ channel: "C1" }), { body: notOk(slug) });
|
|
282
|
+
expect(error).toBeInstanceOf(ErrorClass);
|
|
283
|
+
expect(error).toMatchObject({ message: slug });
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
describe("rate limiting", () => {
|
|
289
|
+
for (const slug of ["rate_limited", "ratelimited"]) {
|
|
290
|
+
test(`the ${slug} slug inside HTTP 200 is SlackRateLimited`, async () => {
|
|
291
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
292
|
+
body: notOk(slug),
|
|
293
|
+
headers: { "retry-after": "12" },
|
|
294
|
+
});
|
|
295
|
+
expect(error).toBeInstanceOf(SlackRateLimited);
|
|
296
|
+
expect(error).toMatchObject({ code: slug });
|
|
297
|
+
expect(Duration.toSeconds((error as SlackRateLimited).retryAfter!)).toBe(12);
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
test("a bare 429 is SlackRateLimited with the default slug and Retry-After", async () => {
|
|
302
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
303
|
+
status: 429,
|
|
304
|
+
headers: { "retry-after": "30" },
|
|
305
|
+
});
|
|
306
|
+
expect(error).toBeInstanceOf(SlackRateLimited);
|
|
307
|
+
expect(error).toMatchObject({ code: "rate_limited", message: "HTTP 429" });
|
|
308
|
+
expect(Duration.toSeconds((error as SlackRateLimited).retryAfter!)).toBe(30);
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
describe("non-envelope failures", () => {
|
|
313
|
+
test("a mapped status with a text body uses the status class and the text", async () => {
|
|
314
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
315
|
+
status: 404,
|
|
316
|
+
body: "no such method",
|
|
317
|
+
});
|
|
318
|
+
expect(error).toBeInstanceOf(NotFound);
|
|
319
|
+
expect(error).toMatchObject({ message: "no such method" });
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test("an HTML 503 is ServiceUnavailable", async () => {
|
|
323
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
324
|
+
status: 503,
|
|
325
|
+
body: "<html>down</html>",
|
|
326
|
+
});
|
|
327
|
+
expect(error).toBeInstanceOf(ServiceUnavailable);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
test("an unmapped 5xx is InternalServerError", async () => {
|
|
331
|
+
const error = await failWith(postMessage({ channel: "C1" }), { status: 520, body: "" });
|
|
332
|
+
expect(error).toBeInstanceOf(InternalServerError);
|
|
333
|
+
expect(error).toMatchObject({ message: "HTTP 520" });
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test("an unmapped 4xx is SlackHttpError with the status and body", async () => {
|
|
337
|
+
const error = await failWith(postMessage({ channel: "C1" }), {
|
|
338
|
+
status: 418,
|
|
339
|
+
body: "teapot",
|
|
340
|
+
});
|
|
341
|
+
expect(error).toBeInstanceOf(SlackHttpError);
|
|
342
|
+
expect(error).toMatchObject({ status: 418, message: "teapot", body: "teapot" });
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
describe("response validation", () => {
|
|
347
|
+
const strict = <A, E>(operation: Effect.Effect<A, E, SlackOpContext>) =>
|
|
348
|
+
operation.pipe(Effect.provide(ResponseValidation.strict));
|
|
349
|
+
const mismatched = ok({ channel: 1, ts: "1.0" });
|
|
350
|
+
|
|
351
|
+
test("lenient mode returns a mismatched payload as read", async () => {
|
|
352
|
+
const result = await succeed(postMessage({ channel: "C1" }), { body: mismatched });
|
|
353
|
+
expect(result as unknown).toEqual({ ok: true, channel: 1, ts: "1.0" });
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test("strict mode fails a mismatched payload with SlackParseError", async () => {
|
|
357
|
+
const error = await failWith(strict(postMessage({ channel: "C1" })), { body: mismatched });
|
|
358
|
+
expect(error).toBeInstanceOf(SlackParseError);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
test("strict mode passes a matching payload", async () => {
|
|
362
|
+
const result = await succeed(strict(postMessage({ channel: "C1" })), { body: ok(posted) });
|
|
363
|
+
expect(result).toEqual({ ok: true, ...posted });
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test("a non-JSON body for a struct output: lenient returns bytes, strict fails", async () => {
|
|
367
|
+
const result = await succeed(postMessage({ channel: "C1" }), { body: "not json" });
|
|
368
|
+
expect(new TextDecoder().decode(result as unknown as Uint8Array)).toBe("not json");
|
|
369
|
+
const error = await failWith(strict(postMessage({ channel: "C1" })), { body: "not json" });
|
|
370
|
+
expect(error).toBeInstanceOf(SlackParseError);
|
|
371
|
+
});
|
|
372
|
+
});
|
package/src/protocol.ts
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
import * as API from "@distilled.cloud/core/api";
|
|
2
|
+
import {
|
|
3
|
+
ConfigError,
|
|
4
|
+
GatewayTimeout,
|
|
5
|
+
HTTP_STATUS_MAP,
|
|
6
|
+
InternalServerError,
|
|
7
|
+
ServiceUnavailable,
|
|
8
|
+
Unauthorized,
|
|
9
|
+
} from "@distilled.cloud/core/errors";
|
|
10
|
+
import {
|
|
11
|
+
buildRequest,
|
|
12
|
+
getAnn,
|
|
13
|
+
getProps,
|
|
14
|
+
hasPropAnn,
|
|
15
|
+
mapKeys,
|
|
16
|
+
matchTypedError,
|
|
17
|
+
} from "@distilled.cloud/core/protocol-http";
|
|
18
|
+
import { unwrapRedactedDeep, wrapSensitive } from "@distilled.cloud/core/protocol-rest";
|
|
19
|
+
import { validateResponse } from "@distilled.cloud/core/response-validation";
|
|
20
|
+
import { parseRetryAfterForStatus, parseServerRetryHint } from "@distilled.cloud/core/retry-after";
|
|
21
|
+
import { httpSymbol, querySymbol, type HttpTrait } from "@distilled.cloud/core/trait";
|
|
1
22
|
/**
|
|
2
23
|
* SlackProtocol — hand-written.
|
|
3
24
|
*
|
|
@@ -25,44 +46,13 @@
|
|
|
25
46
|
* a gzipped NDJSON file.
|
|
26
47
|
*/
|
|
27
48
|
import * as Effect from "effect/Effect";
|
|
28
|
-
import * as Layer from "effect/Layer";
|
|
29
|
-
import type * as AST from "effect/SchemaAST";
|
|
30
49
|
import type * as HttpClient from "effect/http/HttpClient";
|
|
31
50
|
import type * as HttpClientError from "effect/http/HttpClientError";
|
|
32
51
|
import type * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
33
52
|
import type * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
53
|
+
import * as Layer from "effect/Layer";
|
|
34
54
|
import * as Redacted from "effect/Redacted";
|
|
35
|
-
import * as
|
|
36
|
-
import {
|
|
37
|
-
httpSymbol,
|
|
38
|
-
querySymbol,
|
|
39
|
-
type HttpTrait,
|
|
40
|
-
} from "@distilled.cloud/core/trait";
|
|
41
|
-
import {
|
|
42
|
-
buildRequest,
|
|
43
|
-
getAnn,
|
|
44
|
-
getProps,
|
|
45
|
-
hasPropAnn,
|
|
46
|
-
mapKeys,
|
|
47
|
-
matchTypedError,
|
|
48
|
-
} from "@distilled.cloud/core/protocol-http";
|
|
49
|
-
import {
|
|
50
|
-
unwrapRedactedDeep,
|
|
51
|
-
wrapSensitive,
|
|
52
|
-
} from "@distilled.cloud/core/protocol-rest";
|
|
53
|
-
import {
|
|
54
|
-
ConfigError,
|
|
55
|
-
GatewayTimeout,
|
|
56
|
-
HTTP_STATUS_MAP,
|
|
57
|
-
InternalServerError,
|
|
58
|
-
ServiceUnavailable,
|
|
59
|
-
Unauthorized,
|
|
60
|
-
} from "@distilled.cloud/core/errors";
|
|
61
|
-
import {
|
|
62
|
-
parseRetryAfterForStatus,
|
|
63
|
-
parseServerRetryHint,
|
|
64
|
-
} from "@distilled.cloud/core/retry-after";
|
|
65
|
-
import { validateResponse } from "@distilled.cloud/core/response-validation";
|
|
55
|
+
import type * as AST from "effect/SchemaAST";
|
|
66
56
|
import { Credentials, type Config } from "./credentials.ts";
|
|
67
57
|
import {
|
|
68
58
|
type DefaultErrors,
|
|
@@ -78,10 +68,7 @@ import {
|
|
|
78
68
|
* SlackOpContext>` explicitly so the compiler never infers these back out of
|
|
79
69
|
* the schema generics.
|
|
80
70
|
*/
|
|
81
|
-
export type SlackOpError =
|
|
82
|
-
| DefaultErrors
|
|
83
|
-
| ConfigError
|
|
84
|
-
| HttpClientError.HttpClientError;
|
|
71
|
+
export type SlackOpError = DefaultErrors | ConfigError | HttpClientError.HttpClientError;
|
|
85
72
|
|
|
86
73
|
/** Context (requirements) shared by every generated Slack operation. */
|
|
87
74
|
export type SlackOpContext = Credentials | HttpClient.HttpClient;
|
|
@@ -90,8 +77,7 @@ export type SlackOpContext = Credentials | HttpClient.HttpClient;
|
|
|
90
77
|
// Slack failures are real typed errors that an operation re-surfaces via its
|
|
91
78
|
// `errors: [...]` list. Fail with the instance and erase the error type here;
|
|
92
79
|
// `API.make`'s signature reintroduces it for callers.
|
|
93
|
-
const fail = (e: unknown): Effect.Effect<never> =>
|
|
94
|
-
Effect.fail(e) as Effect.Effect<never>;
|
|
80
|
+
const fail = (e: unknown): Effect.Effect<never> => Effect.fail(e) as Effect.Effect<never>;
|
|
95
81
|
|
|
96
82
|
/**
|
|
97
83
|
* Envelope slugs that mean "the token is no good" on ANY method — mapped to
|
|
@@ -133,8 +119,7 @@ const serializeForWire = (input: unknown, inputAst: AST.AST): unknown => {
|
|
|
133
119
|
return input;
|
|
134
120
|
}
|
|
135
121
|
const isForm =
|
|
136
|
-
(getAnn(inputAst, httpSymbol) as HttpTrait | undefined)?.contentType ===
|
|
137
|
-
"form-urlencoded";
|
|
122
|
+
(getAnn(inputAst, httpSymbol) as HttpTrait | undefined)?.contentType === "form-urlencoded";
|
|
138
123
|
const props = getProps(inputAst);
|
|
139
124
|
if (props.length === 0) return input;
|
|
140
125
|
let out: Record<string, unknown> | undefined;
|
|
@@ -162,13 +147,7 @@ const serializeForWire = (input: unknown, inputAst: AST.AST): unknown => {
|
|
|
162
147
|
// request instead. Like the error channel above, the requirement is erased at
|
|
163
148
|
// this boundary (Protocol effects are typed with no requirements) and
|
|
164
149
|
// reintroduced for callers by the generated `SlackOpContext` annotations.
|
|
165
|
-
const encode = ({
|
|
166
|
-
input,
|
|
167
|
-
inputAst,
|
|
168
|
-
}: {
|
|
169
|
-
readonly input: unknown;
|
|
170
|
-
readonly inputAst: AST.AST;
|
|
171
|
-
}) =>
|
|
150
|
+
const encode = ({ input, inputAst }: { readonly input: unknown; readonly inputAst: AST.AST }) =>
|
|
172
151
|
Effect.gen(function* () {
|
|
173
152
|
const resolveCredentials = yield* Credentials;
|
|
174
153
|
const creds = yield* resolveCredentials as Effect.Effect<Config>;
|
|
@@ -196,9 +175,7 @@ const decode = ({
|
|
|
196
175
|
// Read as BYTES first: the analytics export answers a successful call
|
|
197
176
|
// with a gzipped file, which a text read would corrupt. Everything else
|
|
198
177
|
// decodes from the same buffer.
|
|
199
|
-
const bytes = new Uint8Array(
|
|
200
|
-
yield* response.arrayBuffer.pipe(Effect.orDie),
|
|
201
|
-
);
|
|
178
|
+
const bytes = new Uint8Array(yield* response.arrayBuffer.pipe(Effect.orDie));
|
|
202
179
|
const text = new TextDecoder().decode(bytes);
|
|
203
180
|
if (process.env.DISTILLED_DEBUG_HTTP) {
|
|
204
181
|
console.error(`[distilled] <- ${response.status} ${text.slice(0, 400)}`);
|
|
@@ -227,8 +204,7 @@ const decode = ({
|
|
|
227
204
|
// (e.g. `invalid_arguments`); some envelopes use a bare `errors` list.
|
|
228
205
|
const detailMessages = (
|
|
229
206
|
[
|
|
230
|
-
...((json?.response_metadata as { messages?: unknown[] } | undefined)
|
|
231
|
-
?.messages ?? []),
|
|
207
|
+
...((json?.response_metadata as { messages?: unknown[] } | undefined)?.messages ?? []),
|
|
232
208
|
...((Array.isArray(json?.errors) ? json.errors : []) as unknown[]),
|
|
233
209
|
] as unknown[]
|
|
234
210
|
).filter((m): m is string => typeof m === "string");
|
|
@@ -273,20 +249,15 @@ const decode = ({
|
|
|
273
249
|
new SlackError({
|
|
274
250
|
code: slug,
|
|
275
251
|
...(message !== slug ? { message } : {}),
|
|
276
|
-
...(typeof json?.needed === "string"
|
|
277
|
-
|
|
278
|
-
: {}),
|
|
279
|
-
...(typeof json?.provided === "string"
|
|
280
|
-
? { provided: json.provided }
|
|
281
|
-
: {}),
|
|
252
|
+
...(typeof json?.needed === "string" ? { needed: json.needed } : {}),
|
|
253
|
+
...(typeof json?.provided === "string" ? { provided: json.provided } : {}),
|
|
282
254
|
...(detailMessages.length > 0 ? { messages: detailMessages } : {}),
|
|
283
255
|
}),
|
|
284
256
|
);
|
|
285
257
|
}
|
|
286
258
|
|
|
287
259
|
// 5. No envelope at all: HTTP-status classes / retryable 5xx / raw.
|
|
288
|
-
const StatusErrorClass =
|
|
289
|
-
HTTP_STATUS_MAP[status as keyof typeof HTTP_STATUS_MAP];
|
|
260
|
+
const StatusErrorClass = HTTP_STATUS_MAP[status as keyof typeof HTTP_STATUS_MAP];
|
|
290
261
|
if (StatusErrorClass) {
|
|
291
262
|
return yield* fail(
|
|
292
263
|
new StatusErrorClass({
|
|
@@ -303,9 +274,7 @@ const decode = ({
|
|
|
303
274
|
}),
|
|
304
275
|
);
|
|
305
276
|
}
|
|
306
|
-
return yield* fail(
|
|
307
|
-
new SlackHttpError({ status, message, body: text.slice(0, 2000) }),
|
|
308
|
-
);
|
|
277
|
+
return yield* fail(new SlackHttpError({ status, message, body: text.slice(0, 2000) }));
|
|
309
278
|
}
|
|
310
279
|
|
|
311
280
|
// Successful non-JSON body: raw bytes (the analytics file download —
|
|
@@ -335,8 +304,7 @@ export const SlackProtocol: Layer.Layer<API.Protocol> = Layer.succeed(
|
|
|
335
304
|
API.Protocol,
|
|
336
305
|
API.Protocol.of({
|
|
337
306
|
// Erase encode's Credentials requirement (see comment above).
|
|
338
|
-
encode: (args) =>
|
|
339
|
-
encode(args) as Effect.Effect<HttpClientRequest.HttpClientRequest>,
|
|
307
|
+
encode: (args) => encode(args) as Effect.Effect<HttpClientRequest.HttpClientRequest>,
|
|
340
308
|
decode,
|
|
341
309
|
}),
|
|
342
310
|
);
|
package/src/retry.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as Retries from "@distilled.cloud/core/retry";
|
|
1
2
|
/**
|
|
2
3
|
* Slack retry surface — a veneer over `@distilled.cloud/core/retry`.
|
|
3
4
|
*
|
|
@@ -18,7 +19,6 @@
|
|
|
18
19
|
import * as Context from "effect/Context";
|
|
19
20
|
import * as Effect from "effect/Effect";
|
|
20
21
|
import * as Layer from "effect/Layer";
|
|
21
|
-
import * as Retries from "@distilled.cloud/core/retry";
|
|
22
22
|
|
|
23
23
|
export type Options = Retries.Options;
|
|
24
24
|
export type Factory = Retries.Factory;
|
|
@@ -31,16 +31,11 @@ export class Retry extends Context.Service<Retry, Policy>()("SlackRetry") {}
|
|
|
31
31
|
export const policy: {
|
|
32
32
|
(
|
|
33
33
|
options: Options,
|
|
34
|
-
): <A, E, R>(
|
|
35
|
-
effect: Effect.Effect<A, E, R>,
|
|
36
|
-
) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
34
|
+
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
37
35
|
(
|
|
38
36
|
factory: Factory,
|
|
39
|
-
): <A, E, R>(
|
|
40
|
-
|
|
41
|
-
) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
42
|
-
} = (optionsOrFactory: Options | Factory) =>
|
|
43
|
-
Effect.provide(Layer.succeed(Retry, optionsOrFactory));
|
|
37
|
+
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
38
|
+
} = (optionsOrFactory: Options | Factory) => Effect.provide(Layer.succeed(Retry, optionsOrFactory));
|
|
44
39
|
|
|
45
40
|
/** Disables all automatic retries. */
|
|
46
41
|
export const none: <A, E, R>(
|