UNPKG

slack-rest-api-types

Version:

TypeScript types and utilities for Slack Web API REST endpoints

113 lines (112 loc) 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSuccessResponse = isSuccessResponse; exports.isErrorResponse = isErrorResponse; exports.isRateLimited = isRateLimited; exports.isMissingScope = isMissingScope; exports.isAuthError = isAuthError; /** * Type guard to check if a Slack API response is successful. * * @param response - The API response to check * @returns True if the response has ok: true, false otherwise * * @example * ```typescript * const response = await client.chat.postMessage({ channel: 'C123', text: 'Hello' }); * * if (isSuccessResponse(response)) { * console.log('Message posted:', response.ts); * } else { * console.error('Error:', response.error); * } * ``` */ function isSuccessResponse(response) { return response.ok === true; } /** * Type guard to check if a Slack API response is an error. * * @param response - The API response to check * @returns True if the response has ok: false, false otherwise * * @example * ```typescript * const response = await client.chat.postMessage({ channel: 'C123', text: 'Hello' }); * * if (isErrorResponse(response)) { * console.error('API Error:', response.error); * } * ``` */ function isErrorResponse(response) { return response.ok === false; } /** * Type guard to check if a response indicates rate limiting. * * @param response - The API response to check * @returns True if the response indicates rate limiting * * @example * ```typescript * const response = await client.chat.postMessage({ channel: 'C123', text: 'Hello' }); * * if (isRateLimited(response)) { * console.log('Rate limited. Please retry later.'); * } * ``` */ function isRateLimited(response) { return response.ok === false && response.error === 'rate_limited'; } /** * Type guard to check if a response indicates missing permissions/scopes. * * @param response - The API response to check * @returns True if the response indicates missing scope * * @example * ```typescript * const response = await client.chat.postMessage({ channel: 'C123', text: 'Hello' }); * * if (isMissingScope(response)) { * console.error('Missing required scope:', response.needed); * } * ``` */ function isMissingScope(response) { return response.ok === false && response.error === 'missing_scope'; } /** * Type guard to check if a response indicates invalid authentication. * * @param response - The API response to check * @returns True if the response indicates authentication failure * * @example * ```typescript * const response = await client.auth.test(); * * if (isAuthError(response)) { * console.error('Authentication failed'); * } * ``` */ function isAuthError(response) { if (response.ok === false && response.error) { const authErrors = [ 'invalid_auth', 'not_authed', 'account_inactive', 'token_revoked', 'token_expired', 'no_permission', 'org_login_required', 'ekm_access_denied', ]; return authErrors.includes(response.error); } return false; }