UNPKG

8.54 kBTypeScriptView Raw
1/// <reference types="node" />
2/// <reference types="node" />
3import { Agent } from 'http';
4import { SecureContextOptions } from 'tls';
5import { FilesCompleteUploadExternalResponse } from './types/response';
6import { Methods } from './methods';
7import type { FilesUploadV2Arguments } from './types/request/files';
8import { LogLevel, Logger } from './logger';
9import { RetryOptions } from './retry-policies';
10export interface WebClientOptions {
11 slackApiUrl?: string;
12 logger?: Logger;
13 logLevel?: LogLevel;
14 maxRequestConcurrency?: number;
15 retryConfig?: RetryOptions;
16 agent?: Agent;
17 tls?: TLSOptions;
18 timeout?: number;
19 rejectRateLimitedCalls?: boolean;
20 headers?: Record<string, string>;
21 teamId?: string;
22}
23export type TLSOptions = Pick<SecureContextOptions, 'pfx' | 'key' | 'passphrase' | 'cert' | 'ca'>;
24export declare enum WebClientEvent {
25 RATE_LIMITED = "rate_limited"
26}
27export interface WebAPICallResult {
28 ok: boolean;
29 error?: string;
30 response_metadata?: {
31 warnings?: string[];
32 next_cursor?: string;
33 scopes?: string[];
34 acceptedScopes?: string[];
35 retryAfter?: number;
36 messages?: string[];
37 };
38}
39export interface PaginatePredicate {
40 (page: WebAPICallResult): boolean | undefined | void;
41}
42export interface PageReducer<A = any> {
43 (accumulator: A | undefined, page: WebAPICallResult, index: number): A;
44}
45export type PageAccumulator<R extends PageReducer> = R extends (accumulator: (infer A) | undefined, page: WebAPICallResult, index: number) => infer A ? A : never;
46/**
47 * A client for Slack's Web API
48 *
49 * This client provides an alias for each {@link https://api.slack.com/methods|Web API method}. Each method is
50 * a convenience wrapper for calling the {@link WebClient#apiCall} method using the method name as the first parameter.
51 */
52export declare class WebClient extends Methods {
53 /**
54 * The base URL for reaching Slack's Web API. Consider changing this value for testing purposes.
55 */
56 readonly slackApiUrl: string;
57 /**
58 * Authentication and authorization token for accessing Slack Web API (usually begins with `xoxp` or `xoxb`)
59 */
60 readonly token?: string;
61 /**
62 * Configuration for retry operations. See {@link https://github.com/tim-kos/node-retry|node-retry} for more details.
63 */
64 private retryConfig;
65 /**
66 * Queue of requests in which a maximum of {@link WebClientOptions.maxRequestConcurrency} can concurrently be
67 * in-flight.
68 */
69 private requestQueue;
70 /**
71 * Axios HTTP client instance used by this client
72 */
73 private axios;
74 /**
75 * Configuration for custom TLS handling
76 */
77 private tlsConfig;
78 /**
79 * Preference for immediately rejecting API calls which result in a rate-limited response
80 */
81 private rejectRateLimitedCalls;
82 /**
83 * The name used to prefix all logging generated from this object
84 */
85 private static loggerName;
86 /**
87 * This object's logger instance
88 */
89 private logger;
90 /**
91 * This object's teamId value
92 */
93 private teamId?;
94 /**
95 * @param token - An API token to authenticate/authorize with Slack (usually start with `xoxp`, `xoxb`)
96 */
97 constructor(token?: string, { slackApiUrl, logger, logLevel, maxRequestConcurrency, retryConfig, agent, tls, timeout, rejectRateLimitedCalls, headers, teamId, }?: WebClientOptions);
98 /**
99 * Generic method for calling a Web API method
100 * @param method - the Web API method to call {@link https://api.slack.com/methods}
101 * @param options - options
102 */
103 apiCall(method: string, options?: Record<string, unknown>): Promise<WebAPICallResult>;
104 /**
105 * Iterate over the result pages of a cursor-paginated Web API method. This method can return two types of values,
106 * depending on which arguments are used. When up to two parameters are used, the return value is an async iterator
107 * which can be used as the iterable in a for-await-of loop. When three or four parameters are used, the return
108 * value is a promise that resolves at the end of iteration. The third parameter, `shouldStop`, is a function that is
109 * called with each `page` and can end iteration by returning `true`. The fourth parameter, `reduce`, is a function
110 * that is called with three arguments: `accumulator`, `page`, and `index`. The `accumulator` is a value of any type
111 * you choose, but it will contain `undefined` when `reduce` is called for the first time. The `page` argument and
112 * `index` arguments are exactly what they say they are. The `reduce` function's return value will be passed in as
113 * `accumulator` the next time its called, and the returned promise will resolve to the last value of `accumulator`.
114 *
115 * The for-await-of syntax is part of ES2018. It is available natively in Node starting with v10.0.0. You may be able
116 * to use it in earlier JavaScript runtimes by transpiling your source with a tool like Babel. However, the
117 * transpiled code will likely sacrifice performance.
118 * @param method - the cursor-paginated Web API method to call {@link https://api.slack.com/docs/pagination}
119 * @param options - options
120 * @param shouldStop - a predicate that is called with each page, and should return true when pagination can end.
121 * @param reduce - a callback that can be used to accumulate a value that the return promise is resolved to
122 */
123 paginate(method: string, options?: Record<string, unknown>): AsyncIterable<WebAPICallResult>;
124 paginate(method: string, options: Record<string, unknown>, shouldStop: PaginatePredicate): Promise<void>;
125 paginate<R extends PageReducer, A extends PageAccumulator<R>>(method: string, options: Record<string, unknown>, shouldStop: PaginatePredicate, reduce?: PageReducer<A>): Promise<A>;
126 /**
127 * This wrapper method provides an easy way to upload files using the following endpoints:
128 *
129 * **#1**: For each file submitted with this method, submit filenames
130 * and file metadata to {@link https://api.slack.com/methods/files.getUploadURLExternal files.getUploadURLExternal} to request a URL to
131 * which to send the file data to and an id for the file
132 *
133 * **#2**: for each returned file `upload_url`, upload corresponding file to
134 * URLs returned from step 1 (e.g. https://files.slack.com/upload/v1/...\")
135 *
136 * **#3**: Complete uploads {@link https://api.slack.com/methods/files.completeUploadExternal files.completeUploadExternal}
137 * @param options
138 */
139 filesUploadV2(options: FilesUploadV2Arguments): Promise<WebAPICallResult & {
140 files: FilesCompleteUploadExternalResponse[];
141 }>;
142 /**
143 * For each file submitted with this method, submits filenames
144 * and file metadata to files.getUploadURLExternal to request a URL to
145 * which to send the file data to and an id for the file
146 * @param fileUploads
147 */
148 private fetchAllUploadURLExternal;
149 /**
150 * Complete uploads.
151 * @param fileUploads
152 * @returns
153 */
154 private completeFileUploads;
155 /**
156 * for each returned file upload URL, upload corresponding file
157 * @param fileUploads
158 * @returns
159 */
160 private postFileUploadsToExternalURL;
161 /**
162 * @param options All file uploads arguments
163 * @returns An array of file upload entries
164 */
165 private getAllFileUploads;
166 /**
167 * Low-level function to make a single API request. handles queuing, retries, and http-level errors
168 */
169 private makeRequest;
170 /**
171 * Transforms options (a simple key-value object) into an acceptable value for a body. This can be either
172 * a string, used when posting with a content-type of url-encoded. Or, it can be a readable stream, used
173 * when the options contain a binary (a stream or a buffer) and the upload should be done with content-type
174 * multipart/form-data.
175 * @param options - arguments for the Web API method
176 * @param headers - a mutable object representing the HTTP headers for the outgoing request
177 */
178 private serializeApiCallOptions;
179 /**
180 * Processes an HTTP response into a WebAPICallResult by performing JSON parsing on the body and merging relevant
181 * HTTP headers into the object.
182 * @param response - an http response
183 */
184 private buildResult;
185}
186export default WebClient;
187export declare function buildThreadTsWarningMessage(method: string): string;
188//# sourceMappingURL=WebClient.d.ts.map
\No newline at end of file