UNPKG

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