@im-more/dsh-slack 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/cordis.patch.yml +13 -0
- package/lib/index.js +2659 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +66 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +165 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +49 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +54 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +126 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +396 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +61 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +196 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +27 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +35 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +14 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +14 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/modes/mrkdwn.d.ts +17 -0
- package/lib/types/modes/mrkdwn.d.ts.map +1 -0
- package/lib/types/modes/mrkdwn.js +123 -0
- package/lib/types/modes/mrkdwn.js.map +1 -0
- package/lib/types/modes/shared.d.ts +164 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +411 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/mrkdwn.d.ts +41 -0
- package/lib/types/mrkdwn.d.ts.map +1 -0
- package/lib/types/mrkdwn.js +137 -0
- package/lib/types/mrkdwn.js.map +1 -0
- package/lib/types/request.d.ts +225 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +330 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +74 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +200 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/transport/socket.d.ts +58 -0
- package/lib/types/transport/socket.d.ts.map +1 -0
- package/lib/types/transport/socket.js +297 -0
- package/lib/types/transport/socket.js.map +1 -0
- package/lib/types/transport/webhook.d.ts +48 -0
- package/lib/types/transport/webhook.d.ts.map +1 -0
- package/lib/types/transport/webhook.js +210 -0
- package/lib/types/transport/webhook.js.map +1 -0
- package/lib/types/types/admin.d.ts +682 -0
- package/lib/types/types/admin.d.ts.map +1 -0
- package/lib/types/types/admin.js +172 -0
- package/lib/types/types/admin.js.map +1 -0
- package/lib/types/types/api.d.ts +20 -0
- package/lib/types/types/api.d.ts.map +1 -0
- package/lib/types/types/api.js +7 -0
- package/lib/types/types/api.js.map +1 -0
- package/lib/types/types/apps.d.ts +138 -0
- package/lib/types/types/apps.d.ts.map +1 -0
- package/lib/types/types/apps.js +28 -0
- package/lib/types/types/apps.js.map +1 -0
- package/lib/types/types/auth.d.ts +36 -0
- package/lib/types/types/auth.d.ts.map +1 -0
- package/lib/types/types/auth.js +10 -0
- package/lib/types/types/auth.js.map +1 -0
- package/lib/types/types/bots.d.ts +32 -0
- package/lib/types/types/bots.d.ts.map +1 -0
- package/lib/types/types/bots.js +7 -0
- package/lib/types/types/bots.js.map +1 -0
- package/lib/types/types/calls.d.ts +82 -0
- package/lib/types/types/calls.d.ts.map +1 -0
- package/lib/types/types/calls.js +22 -0
- package/lib/types/types/calls.js.map +1 -0
- package/lib/types/types/chat.d.ts +208 -0
- package/lib/types/types/chat.d.ts.map +1 -0
- package/lib/types/types/chat.js +34 -0
- package/lib/types/types/chat.js.map +1 -0
- package/lib/types/types/conversations.d.ts +266 -0
- package/lib/types/types/conversations.d.ts.map +1 -0
- package/lib/types/types/conversations.js +58 -0
- package/lib/types/types/conversations.js.map +1 -0
- package/lib/types/types/definition.d.ts +505 -0
- package/lib/types/types/definition.d.ts.map +1 -0
- package/lib/types/types/definition.js +2 -0
- package/lib/types/types/definition.js.map +1 -0
- package/lib/types/types/dialog.d.ts +20 -0
- package/lib/types/types/dialog.d.ts.map +1 -0
- package/lib/types/types/dialog.js +7 -0
- package/lib/types/types/dialog.js.map +1 -0
- package/lib/types/types/dnd.d.ts +70 -0
- package/lib/types/types/dnd.d.ts.map +1 -0
- package/lib/types/types/dnd.js +19 -0
- package/lib/types/types/dnd.js.map +1 -0
- package/lib/types/types/emoji.d.ts +18 -0
- package/lib/types/types/emoji.d.ts.map +1 -0
- package/lib/types/types/emoji.js +7 -0
- package/lib/types/types/emoji.js.map +1 -0
- package/lib/types/types/events/base-events.d.ts +235 -0
- package/lib/types/types/events/base-events.d.ts.map +1 -0
- package/lib/types/types/events/base-events.js +3 -0
- package/lib/types/types/events/base-events.js.map +1 -0
- package/lib/types/types/events/index.d.ts +97 -0
- package/lib/types/types/events/index.d.ts.map +1 -0
- package/lib/types/types/events/index.js +3 -0
- package/lib/types/types/events/index.js.map +1 -0
- package/lib/types/types/events/message-events.d.ts +215 -0
- package/lib/types/types/events/message-events.d.ts.map +1 -0
- package/lib/types/types/events/message-events.js +3 -0
- package/lib/types/types/events/message-events.js.map +1 -0
- package/lib/types/types/files.d.ts +189 -0
- package/lib/types/types/files.d.ts.map +1 -0
- package/lib/types/types/files.js +43 -0
- package/lib/types/types/files.js.map +1 -0
- package/lib/types/types/index.d.ts +82 -0
- package/lib/types/types/index.d.ts.map +1 -0
- package/lib/types/types/index.js +29 -0
- package/lib/types/types/index.js.map +1 -0
- package/lib/types/types/internal.d.ts +17 -0
- package/lib/types/types/internal.d.ts.map +1 -0
- package/lib/types/types/internal.js +60 -0
- package/lib/types/types/internal.js.map +1 -0
- package/lib/types/types/migration.d.ts +25 -0
- package/lib/types/types/migration.d.ts.map +1 -0
- package/lib/types/types/migration.js +7 -0
- package/lib/types/types/migration.js.map +1 -0
- package/lib/types/types/oauth.d.ts +50 -0
- package/lib/types/types/oauth.d.ts.map +1 -0
- package/lib/types/types/oauth.js +13 -0
- package/lib/types/types/oauth.js.map +1 -0
- package/lib/types/types/pins.d.ts +39 -0
- package/lib/types/types/pins.d.ts.map +1 -0
- package/lib/types/types/pins.js +13 -0
- package/lib/types/types/pins.js.map +1 -0
- package/lib/types/types/reactions.d.ts +75 -0
- package/lib/types/types/reactions.d.ts.map +1 -0
- package/lib/types/types/reactions.js +16 -0
- package/lib/types/types/reactions.js.map +1 -0
- package/lib/types/types/reminders.d.ts +64 -0
- package/lib/types/types/reminders.d.ts.map +1 -0
- package/lib/types/types/reminders.js +19 -0
- package/lib/types/types/reminders.js.map +1 -0
- package/lib/types/types/rtm.d.ts +30 -0
- package/lib/types/types/rtm.d.ts.map +1 -0
- package/lib/types/types/rtm.js +7 -0
- package/lib/types/types/rtm.js.map +1 -0
- package/lib/types/types/search.d.ts +24 -0
- package/lib/types/types/search.d.ts.map +1 -0
- package/lib/types/types/search.js +7 -0
- package/lib/types/types/search.js.map +1 -0
- package/lib/types/types/stars.d.ts +56 -0
- package/lib/types/types/stars.d.ts.map +1 -0
- package/lib/types/types/stars.js +13 -0
- package/lib/types/types/stars.js.map +1 -0
- package/lib/types/types/team.d.ts +98 -0
- package/lib/types/types/team.d.ts.map +1 -0
- package/lib/types/types/team.js +19 -0
- package/lib/types/types/team.js.map +1 -0
- package/lib/types/types/usergroups.d.ts +103 -0
- package/lib/types/types/usergroups.d.ts.map +1 -0
- package/lib/types/types/usergroups.js +25 -0
- package/lib/types/types/usergroups.js.map +1 -0
- package/lib/types/types/users.d.ts +169 -0
- package/lib/types/types/users.d.ts.map +1 -0
- package/lib/types/types/users.js +40 -0
- package/lib/types/types/users.js.map +1 -0
- package/lib/types/types/views.d.ts +56 -0
- package/lib/types/types/views.d.ts.map +1 -0
- package/lib/types/types/views.js +16 -0
- package/lib/types/types/views.js.map +1 -0
- package/lib/types/types/workflows.d.ts +45 -0
- package/lib/types/types/workflows.d.ts.map +1 -0
- package/lib/types/types/workflows.js +13 -0
- package/lib/types/types/workflows.js.map +1 -0
- package/package.json +65 -3
- package/src/bot.ts +273 -0
- package/src/config.ts +92 -0
- package/src/decode.ts +489 -0
- package/src/errors.ts +230 -0
- package/src/identity.ts +59 -0
- package/src/index.ts +64 -0
- package/src/modes/mrkdwn.ts +159 -0
- package/src/modes/shared.ts +515 -0
- package/src/mrkdwn.ts +146 -0
- package/src/request.ts +449 -0
- package/src/send.ts +300 -0
- package/src/transport/socket.ts +393 -0
- package/src/transport/webhook.ts +269 -0
- package/src/types/admin.ts +910 -0
- package/src/types/api.ts +28 -0
- package/src/types/apps.ts +175 -0
- package/src/types/auth.ts +49 -0
- package/src/types/bots.ts +40 -0
- package/src/types/calls.ts +110 -0
- package/src/types/chat.ts +255 -0
- package/src/types/conversations.ts +342 -0
- package/src/types/definition.ts +530 -0
- package/src/types/dialog.ts +28 -0
- package/src/types/dnd.ts +94 -0
- package/src/types/emoji.ts +26 -0
- package/src/types/events/base-events.ts +281 -0
- package/src/types/events/index.ts +125 -0
- package/src/types/events/message-events.ts +253 -0
- package/src/types/files.ts +245 -0
- package/src/types/index.ts +84 -0
- package/src/types/internal.ts +60 -0
- package/src/types/migration.ts +34 -0
- package/src/types/oauth.ts +66 -0
- package/src/types/pins.ts +55 -0
- package/src/types/reactions.ts +95 -0
- package/src/types/reminders.ts +88 -0
- package/src/types/rtm.ts +38 -0
- package/src/types/search.ts +32 -0
- package/src/types/stars.ts +72 -0
- package/src/types/team.ts +122 -0
- package/src/types/usergroups.ts +135 -0
- package/src/types/users.ts +221 -0
- package/src/types/views.ts +76 -0
- package/src/types/workflows.ts +61 -0
- package/README.md +0 -3
package/src/request.ts
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slack Web API transport: one request path for every method the adapter calls, the `ok` envelope, `Retry-After`,
|
|
3
|
+
* credential-scoped file downloads and the two-step file upload.
|
|
4
|
+
*
|
|
5
|
+
* The migrated `Internal` class in `types/internal.ts` keeps the complete documented method surface for
|
|
6
|
+
* `bot.internal`; the adapter's own calls go through this module because `Internal` hands back the parsed envelope
|
|
7
|
+
* only, while classification needs the one thing the envelope does not carry: the HTTP status and `Retry-After` of a
|
|
8
|
+
* 429 answer.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
12
|
+
import type { Http } from '@deepseek-ai/cordis-plugin-http'
|
|
13
|
+
import { SlackApiError } from './errors.ts'
|
|
14
|
+
|
|
15
|
+
/** Everything one bot needs to make Slack Web API requests. */
|
|
16
|
+
export interface SlackApi {
|
|
17
|
+
readonly ctx: Context
|
|
18
|
+
/** Web API root, without a trailing slash (`https://slack.com/api`). */
|
|
19
|
+
readonly base: string
|
|
20
|
+
/** `xoxb-…`: every method except `apps.connections.open`. */
|
|
21
|
+
readonly botToken: string
|
|
22
|
+
/** `xapp-…`: only `apps.connections.open`. */
|
|
23
|
+
readonly appToken: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One request body: flat values, with arrays sent as their JSON text in a form field. */
|
|
27
|
+
export type SlackParams = Record<string, unknown>
|
|
28
|
+
|
|
29
|
+
/** One Web API endpoint this adapter calls. */
|
|
30
|
+
interface SlackRoute {
|
|
31
|
+
path: string
|
|
32
|
+
method: 'GET' | 'POST'
|
|
33
|
+
/** Whether a POST body is JSON; otherwise it is form-encoded (the flag `Internal.define`'s table carries). */
|
|
34
|
+
json: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The endpoints the adapter calls, keyed by method name.
|
|
39
|
+
*
|
|
40
|
+
* The method → path/method/body mapping is the one `Internal.define` installs for the migrated declarations; the two
|
|
41
|
+
* `files.*` upload methods are absent from those 2023 declarations, so their entries are the current API's.
|
|
42
|
+
*/
|
|
43
|
+
export const ROUTES = {
|
|
44
|
+
authTest: { path: '/auth.test', method: 'GET', json: false },
|
|
45
|
+
appsConnectionsOpen: {
|
|
46
|
+
path: '/apps.connections.open',
|
|
47
|
+
method: 'POST',
|
|
48
|
+
json: false,
|
|
49
|
+
},
|
|
50
|
+
chatDelete: { path: '/chat.delete', method: 'POST', json: true },
|
|
51
|
+
chatPostMessage: { path: '/chat.postMessage', method: 'POST', json: true },
|
|
52
|
+
conversationsHistory: {
|
|
53
|
+
path: '/conversations.history',
|
|
54
|
+
method: 'GET',
|
|
55
|
+
json: false,
|
|
56
|
+
},
|
|
57
|
+
filesInfo: { path: '/files.info', method: 'GET', json: false },
|
|
58
|
+
reactionsAdd: { path: '/reactions.add', method: 'POST', json: true },
|
|
59
|
+
reactionsRemove: { path: '/reactions.remove', method: 'POST', json: true },
|
|
60
|
+
usersList: { path: '/users.list', method: 'GET', json: false },
|
|
61
|
+
getUploadUrlExternal: {
|
|
62
|
+
path: '/files.getUploadURLExternal',
|
|
63
|
+
method: 'POST',
|
|
64
|
+
json: false,
|
|
65
|
+
},
|
|
66
|
+
completeUploadExternal: {
|
|
67
|
+
path: '/files.completeUploadExternal',
|
|
68
|
+
method: 'POST',
|
|
69
|
+
json: false,
|
|
70
|
+
},
|
|
71
|
+
} as const satisfies Record<string, SlackRoute>
|
|
72
|
+
|
|
73
|
+
/** One Web API method this adapter calls. */
|
|
74
|
+
export type SlackMethod = keyof typeof ROUTES
|
|
75
|
+
|
|
76
|
+
/** The fields every Slack answer carries, plus the two warning carriers. */
|
|
77
|
+
export interface SlackEnvelope {
|
|
78
|
+
ok?: boolean
|
|
79
|
+
error?: string
|
|
80
|
+
/** `chat.*` reports a single warning here (`message_truncated`). */
|
|
81
|
+
warning?: string
|
|
82
|
+
/** Other methods report warnings here. */
|
|
83
|
+
response_metadata?: { next_cursor?: string; warnings?: string[] }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface CallOptions {
|
|
87
|
+
/** Use the app-level token (`apps.connections.open`) instead of the bot token. */
|
|
88
|
+
app?: boolean
|
|
89
|
+
signal?: AbortSignal
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Build the request endpoints of one bot.
|
|
94
|
+
* @param ctx - the context whose `http` service performs the requests.
|
|
95
|
+
* @param config - `apiBaseUrl` of the profile row.
|
|
96
|
+
* @param botToken - the bot token.
|
|
97
|
+
* @param appToken - the app-level token.
|
|
98
|
+
* @returns the request dependencies, shared by every call of the bot.
|
|
99
|
+
*/
|
|
100
|
+
export function createSlackApi(
|
|
101
|
+
ctx: Context,
|
|
102
|
+
config: { apiBaseUrl: string; botToken: string; token: string },
|
|
103
|
+
): SlackApi {
|
|
104
|
+
return {
|
|
105
|
+
ctx,
|
|
106
|
+
base: config.apiBaseUrl.replace(/\/+$/, ''),
|
|
107
|
+
botToken: config.botToken,
|
|
108
|
+
appToken: config.token,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A request body encoded as form fields; an array becomes its JSON text, as Slack's file methods expect.
|
|
114
|
+
* @param params - the request body.
|
|
115
|
+
* @returns the body to send with `application/x-www-form-urlencoded`.
|
|
116
|
+
*/
|
|
117
|
+
export function encodeForm(params: SlackParams): URLSearchParams {
|
|
118
|
+
const form = new URLSearchParams()
|
|
119
|
+
for (const [key, value] of Object.entries(params)) {
|
|
120
|
+
if (value === undefined || value === null) continue
|
|
121
|
+
form.append(
|
|
122
|
+
key,
|
|
123
|
+
typeof value === 'object' ? JSON.stringify(value) : String(value),
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
return form
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Whether a caught value is the abort error of a cancelled request. */
|
|
130
|
+
function isAbortError(error: unknown): boolean {
|
|
131
|
+
return (
|
|
132
|
+
(error instanceof Error || error instanceof DOMException) &&
|
|
133
|
+
error.name === 'AbortError'
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* `Retry-After` of a 429 answer, in milliseconds.
|
|
139
|
+
* @param response - the answer.
|
|
140
|
+
* @returns the delay in milliseconds, or undefined when the header is absent or unreadable.
|
|
141
|
+
*/
|
|
142
|
+
export function retryAfterOf(response: Response): number | undefined {
|
|
143
|
+
const header = response.headers.get('retry-after')
|
|
144
|
+
if (header === null) return undefined
|
|
145
|
+
const seconds = Number(header)
|
|
146
|
+
if (!Number.isFinite(seconds) || seconds < 0) return undefined
|
|
147
|
+
return seconds * 1000
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Read a JSON answer; an unreadable body is reported as absence rather than a parse error. */
|
|
151
|
+
async function jsonOf(response: Response): Promise<SlackEnvelope | undefined> {
|
|
152
|
+
let body: unknown
|
|
153
|
+
try {
|
|
154
|
+
body = await response.json()
|
|
155
|
+
} catch (error) {
|
|
156
|
+
// The answer carried no JSON (a proxy error page, an empty body): the caller reports the status instead.
|
|
157
|
+
void error
|
|
158
|
+
return undefined
|
|
159
|
+
}
|
|
160
|
+
if (typeof body !== 'object' || body === null) return undefined
|
|
161
|
+
return body
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Refuse an answer that does not say `ok: true`.
|
|
166
|
+
*
|
|
167
|
+
* `Internal` hands the raw envelope back, so every call site used to have to remember this check. A successful answer
|
|
168
|
+
* may still carry a warning (`message_truncated`), which is logged: the text was accepted but shortened, so it is not
|
|
169
|
+
* an error and must not fail the send.
|
|
170
|
+
*
|
|
171
|
+
* @param api - the request dependencies, for the logger.
|
|
172
|
+
* @param method - Slack method name, for the message.
|
|
173
|
+
* @param body - the parsed envelope.
|
|
174
|
+
* @param status - HTTP status of the answer.
|
|
175
|
+
* @returns the same envelope, for the caller to read its payload.
|
|
176
|
+
* @throws SlackApiError when the platform refused the call.
|
|
177
|
+
*/
|
|
178
|
+
export function ensureOk<V extends SlackEnvelope>(
|
|
179
|
+
api: SlackApi,
|
|
180
|
+
method: SlackMethod,
|
|
181
|
+
body: V,
|
|
182
|
+
status = 200,
|
|
183
|
+
): V {
|
|
184
|
+
const name = ROUTES[method].path.slice(1)
|
|
185
|
+
if (body.ok !== true) {
|
|
186
|
+
throw new SlackApiError(
|
|
187
|
+
`slack ${name} failed: ${body.error ?? 'the answer does not say ok'}`,
|
|
188
|
+
{
|
|
189
|
+
method: name,
|
|
190
|
+
status,
|
|
191
|
+
...(body.error === undefined ? {} : { code: body.error }),
|
|
192
|
+
},
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
const warnings = [
|
|
196
|
+
...(body.warning === undefined ? [] : [body.warning]),
|
|
197
|
+
...(body.response_metadata?.warnings ?? []),
|
|
198
|
+
]
|
|
199
|
+
if (warnings.length > 0) {
|
|
200
|
+
api.ctx.logger.warn(
|
|
201
|
+
'slack %s answered warnings: %s',
|
|
202
|
+
name,
|
|
203
|
+
warnings.join(', '),
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
return body
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Call one Web API method and return its `ok: true` answer.
|
|
211
|
+
*
|
|
212
|
+
* @param api - the request dependencies.
|
|
213
|
+
* @param method - Slack method name.
|
|
214
|
+
* @param params - query parameters of a GET, or the request body.
|
|
215
|
+
* @param options - app-token selection and cancellation.
|
|
216
|
+
* @returns the parsed answer.
|
|
217
|
+
* @throws SlackApiError for a status, a refusal or an unreadable answer.
|
|
218
|
+
*/
|
|
219
|
+
export async function callApi<V extends SlackEnvelope>(
|
|
220
|
+
api: SlackApi,
|
|
221
|
+
method: SlackMethod,
|
|
222
|
+
params: SlackParams = {},
|
|
223
|
+
options: CallOptions = {},
|
|
224
|
+
): Promise<V> {
|
|
225
|
+
const route: SlackRoute = ROUTES[method]
|
|
226
|
+
const token = options.app === true ? api.appToken : api.botToken
|
|
227
|
+
const config: Http.RequestConfig = {
|
|
228
|
+
method: route.method,
|
|
229
|
+
headers: { authorization: `Bearer ${token}` },
|
|
230
|
+
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
231
|
+
}
|
|
232
|
+
if (route.method === 'GET') config.params = params
|
|
233
|
+
else if (route.json) config.data = params
|
|
234
|
+
else {
|
|
235
|
+
config.data = encodeForm(params)
|
|
236
|
+
config.headers = {
|
|
237
|
+
...config.headers,
|
|
238
|
+
'content-type': 'application/x-www-form-urlencoded',
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
let response: Response
|
|
243
|
+
try {
|
|
244
|
+
response = await api.ctx.http(`${api.base}${route.path}`, config)
|
|
245
|
+
} catch (cause) {
|
|
246
|
+
if (options.signal?.aborted === true) throw options.signal.reason
|
|
247
|
+
if (isAbortError(cause)) throw cause
|
|
248
|
+
throw new SlackApiError(`slack ${route.path.slice(1)} request failed`, {
|
|
249
|
+
method: route.path.slice(1),
|
|
250
|
+
cause,
|
|
251
|
+
})
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const status = response.status
|
|
255
|
+
const retryAfterMs = retryAfterOf(response)
|
|
256
|
+
const body = await jsonOf(response)
|
|
257
|
+
if (body === undefined) {
|
|
258
|
+
throw new SlackApiError(
|
|
259
|
+
`slack ${route.path.slice(1)} answered ${status} without a JSON envelope`,
|
|
260
|
+
{
|
|
261
|
+
method: route.path.slice(1),
|
|
262
|
+
status,
|
|
263
|
+
...(retryAfterMs === undefined ? {} : { retryAfterMs }),
|
|
264
|
+
},
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
if (status === 429) {
|
|
268
|
+
throw new SlackApiError(`slack ${route.path.slice(1)} was rate limited`, {
|
|
269
|
+
method: route.path.slice(1),
|
|
270
|
+
status,
|
|
271
|
+
...(body.error === undefined ? {} : { code: body.error }),
|
|
272
|
+
...(retryAfterMs === undefined ? {} : { retryAfterMs }),
|
|
273
|
+
})
|
|
274
|
+
}
|
|
275
|
+
return ensureOk(api, method, { ...body } as V, status)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Whether a URL names a host the bot token may be sent to.
|
|
280
|
+
*
|
|
281
|
+
* Slack's guidance is to never send the credentials to a host outside Slack, and a `url_private` of a remote file
|
|
282
|
+
* names an external host. The comparison is exact on the parsed hostname: `files.slack.com.evil.example` and
|
|
283
|
+
* `evil-slack.com` are both refused, and plain HTTP is refused because the token would travel unencrypted.
|
|
284
|
+
*
|
|
285
|
+
* @param url - the file URL.
|
|
286
|
+
* @returns whether the bot token may be presented to it.
|
|
287
|
+
*/
|
|
288
|
+
export function isSlackFileUrl(url: URL): boolean {
|
|
289
|
+
if (url.protocol !== 'https:') return false
|
|
290
|
+
const host = url.hostname.toLowerCase()
|
|
291
|
+
return host === 'slack.com' || host.endsWith('.slack.com')
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Download one Slack-hosted file with the bot token.
|
|
296
|
+
*
|
|
297
|
+
* @param api - the request dependencies.
|
|
298
|
+
* @param src - `url_private` of a `files.info` answer.
|
|
299
|
+
* @param signal - cancellation.
|
|
300
|
+
* @returns the response whose body is the file.
|
|
301
|
+
* @throws SlackApiError when the host is not a Slack file host.
|
|
302
|
+
*/
|
|
303
|
+
export async function downloadFile(
|
|
304
|
+
api: SlackApi,
|
|
305
|
+
src: string,
|
|
306
|
+
signal?: AbortSignal,
|
|
307
|
+
): Promise<Response> {
|
|
308
|
+
let url: URL
|
|
309
|
+
try {
|
|
310
|
+
url = new URL(src)
|
|
311
|
+
} catch (cause) {
|
|
312
|
+
throw new SlackApiError(`slack: "${src}" is not a URL`, {
|
|
313
|
+
method: 'files.info',
|
|
314
|
+
cause,
|
|
315
|
+
})
|
|
316
|
+
}
|
|
317
|
+
if (!isSlackFileUrl(url)) {
|
|
318
|
+
throw new SlackApiError(
|
|
319
|
+
`slack: refusing to send the bot token to ${url.hostname}`,
|
|
320
|
+
{ method: 'files.info' },
|
|
321
|
+
)
|
|
322
|
+
}
|
|
323
|
+
try {
|
|
324
|
+
return await api.ctx.http(url, {
|
|
325
|
+
headers: { authorization: `Bearer ${api.botToken}` },
|
|
326
|
+
...(signal === undefined ? {} : { signal }),
|
|
327
|
+
})
|
|
328
|
+
} catch (cause) {
|
|
329
|
+
if (signal?.aborted === true) throw signal.reason
|
|
330
|
+
if (isAbortError(cause)) throw cause
|
|
331
|
+
throw new SlackApiError('slack file download failed', {
|
|
332
|
+
method: 'files.info',
|
|
333
|
+
cause,
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** A ticket of the two-step upload. */
|
|
339
|
+
export interface SlackUploadTicket {
|
|
340
|
+
/** Pre-signed URL of the byte upload; it is not on `slack.com/api` and carries no credentials. */
|
|
341
|
+
uploadUrl: string
|
|
342
|
+
fileId: string
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Ask for the upload URL and the file id (`files.getUploadURLExternal`).
|
|
347
|
+
* @param api - the request dependencies.
|
|
348
|
+
* @param file - name and byte length the platform records.
|
|
349
|
+
* @param signal - cancellation.
|
|
350
|
+
* @returns the ticket of the byte upload.
|
|
351
|
+
*/
|
|
352
|
+
export async function startUpload(
|
|
353
|
+
api: SlackApi,
|
|
354
|
+
file: { name: string; length: number },
|
|
355
|
+
signal?: AbortSignal,
|
|
356
|
+
): Promise<SlackUploadTicket> {
|
|
357
|
+
const answer = await callApi<
|
|
358
|
+
SlackEnvelope & { upload_url?: string; file_id?: string }
|
|
359
|
+
>(
|
|
360
|
+
api,
|
|
361
|
+
'getUploadUrlExternal',
|
|
362
|
+
{ filename: file.name, length: file.length },
|
|
363
|
+
signal === undefined ? {} : { signal },
|
|
364
|
+
)
|
|
365
|
+
if (answer.upload_url === undefined || answer.file_id === undefined) {
|
|
366
|
+
throw new SlackApiError(
|
|
367
|
+
'slack files.getUploadURLExternal answered without upload_url/file_id',
|
|
368
|
+
{ method: 'getUploadUrlExternal' },
|
|
369
|
+
)
|
|
370
|
+
}
|
|
371
|
+
return { uploadUrl: answer.upload_url, fileId: answer.file_id }
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Upload the bytes to the ticket's URL.
|
|
376
|
+
*
|
|
377
|
+
* The URL is pre-signed, so no credential travels with it; the answer is plain text (`OK - <size>`), so a 2xx is the
|
|
378
|
+
* whole success signal.
|
|
379
|
+
*
|
|
380
|
+
* @param api - the request dependencies.
|
|
381
|
+
* @param ticket - what {@link startUpload} returned.
|
|
382
|
+
* @param data - the file bytes.
|
|
383
|
+
* @param signal - cancellation.
|
|
384
|
+
*/
|
|
385
|
+
export async function putUploadBytes(
|
|
386
|
+
api: SlackApi,
|
|
387
|
+
ticket: SlackUploadTicket,
|
|
388
|
+
data: Uint8Array,
|
|
389
|
+
signal?: AbortSignal,
|
|
390
|
+
): Promise<void> {
|
|
391
|
+
let response: Response
|
|
392
|
+
try {
|
|
393
|
+
response = await api.ctx.http(ticket.uploadUrl, {
|
|
394
|
+
method: 'POST',
|
|
395
|
+
headers: { 'content-type': 'application/octet-stream' },
|
|
396
|
+
data,
|
|
397
|
+
...(signal === undefined ? {} : { signal }),
|
|
398
|
+
})
|
|
399
|
+
} catch (cause) {
|
|
400
|
+
if (signal?.aborted === true) throw signal.reason
|
|
401
|
+
if (isAbortError(cause)) throw cause
|
|
402
|
+
throw new SlackApiError('slack file upload failed', {
|
|
403
|
+
method: 'getUploadUrlExternal',
|
|
404
|
+
cause,
|
|
405
|
+
})
|
|
406
|
+
}
|
|
407
|
+
if (!response.ok) {
|
|
408
|
+
throw new SlackApiError(`slack file upload answered ${response.status}`, {
|
|
409
|
+
method: 'getUploadUrlExternal',
|
|
410
|
+
status: response.status,
|
|
411
|
+
})
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** One file of the `files.completeUploadExternal` answer. */
|
|
416
|
+
export interface SlackUploadedFile {
|
|
417
|
+
id?: string
|
|
418
|
+
created?: number
|
|
419
|
+
shares?: unknown
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Share an uploaded file into a channel (`files.completeUploadExternal`).
|
|
424
|
+
* @param api - the request dependencies.
|
|
425
|
+
* @param files - ids and titles to share.
|
|
426
|
+
* @param channel - channel the file message appears in.
|
|
427
|
+
* @param threadTs - thread to post the file into, when the message answers a thread.
|
|
428
|
+
* @param signal - cancellation.
|
|
429
|
+
* @returns the shared files, from which the message timestamp is read.
|
|
430
|
+
*/
|
|
431
|
+
export async function finishUpload(
|
|
432
|
+
api: SlackApi,
|
|
433
|
+
files: readonly { id: string; title: string }[],
|
|
434
|
+
channel: string,
|
|
435
|
+
threadTs: string | undefined,
|
|
436
|
+
signal?: AbortSignal,
|
|
437
|
+
): Promise<SlackUploadedFile[]> {
|
|
438
|
+
const answer = await callApi<SlackEnvelope & { files?: SlackUploadedFile[] }>(
|
|
439
|
+
api,
|
|
440
|
+
'completeUploadExternal',
|
|
441
|
+
{
|
|
442
|
+
files: [...files],
|
|
443
|
+
channel_id: channel,
|
|
444
|
+
...(threadTs === undefined ? {} : { thread_ts: threadTs }),
|
|
445
|
+
},
|
|
446
|
+
signal === undefined ? {} : { signal },
|
|
447
|
+
)
|
|
448
|
+
return answer.files ?? []
|
|
449
|
+
}
|