UNPKG

@twurple/api

Version:

Interact with Twitch's API.

111 lines (110 loc) 5.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiClient = void 0; const tslib_1 = require("tslib"); const detect_node_1 = require("@d-fischer/detect-node"); const logger_1 = require("@d-fischer/logger"); const rate_limiter_1 = require("@d-fischer/rate-limiter"); const api_call_1 = require("@twurple/api-call"); const common_1 = require("@twurple/common"); const ConfigError_1 = require("../errors/ConfigError"); const HelixRateLimiter_1 = require("../utils/HelixRateLimiter"); const BaseApiClient_1 = require("./BaseApiClient"); const NoContextApiClient_1 = require("./NoContextApiClient"); const UserContextApiClient_1 = require("./UserContextApiClient"); /** * An API client for the Twitch Helix API and other miscellaneous endpoints. * * @meta category main * @hideProtected */ let ApiClient = class ApiClient extends BaseApiClient_1.BaseApiClient { /** * Creates a new API client instance. * * @param config Configuration for the client instance. */ constructor(config) { if (!config.authProvider) { throw new ConfigError_1.ConfigError('No auth provider given. Please supply the `authProvider` option.'); } const rateLimitLoggerOptions = { name: 'twurple:api:rate-limiter', ...config.logger }; super(config, (0, logger_1.createLogger)({ name: 'twurple:api:client', ...config.logger }), detect_node_1.isNode ? new rate_limiter_1.PartitionedRateLimiter({ getPartitionKey: req => { var _a; return (_a = req.userId) !== null && _a !== void 0 ? _a : null; }, createChild: () => new HelixRateLimiter_1.HelixRateLimiter({ logger: rateLimitLoggerOptions }), }) : new rate_limiter_1.PartitionedTimeBasedRateLimiter({ logger: rateLimitLoggerOptions, bucketSize: 800, timeFrame: 64000, doRequest: async ({ options, clientId, accessToken, authorizationType, fetchOptions, mockServerPort, }) => await (0, api_call_1.callTwitchApiRaw)(options, clientId, accessToken, authorizationType, fetchOptions, mockServerPort), getPartitionKey: req => { var _a; return (_a = req.userId) !== null && _a !== void 0 ? _a : null; }, })); } /** * Creates a contextualized ApiClient that can be used to call the API in the context of a given user. * * @param user The user to use as context. * @param runner The callback to execute. * * A parameter is passed that should be used in place of the normal `ApiClient` * to ensure that all requests are executed in the given user's context. * * Please note that requests which require scope authorization ignore this context. * * The return value of your callback will be propagated to the return value of this method. */ async asUser(user, runner) { const ctx = new UserContextApiClient_1.UserContextApiClient(this._config, this._logger, this._rateLimiter, (0, common_1.extractUserId)(user)); return await runner(ctx); } /** * Creates a contextualized ApiClient that can be used to call the API in the context of a given intent. * * @param intents A list of intents. The first one that is found in your auth provider will be used. * @param runner The callback to execute. * * A parameter is passed that should be used in place of the normal `ApiClient` * to ensure that all requests are executed in the given user's context. * * Please note that requests which require scope authorization ignore this context. * * The return value of your callback will be propagated to the return value of this method. */ async asIntent(intents, runner) { if (!this._authProvider.getAccessTokenForIntent) { throw new Error('Trying to use intents with an auth provider that does not support them'); } for (const intent of intents) { const user = await this._authProvider.getAccessTokenForIntent(intent); if (user) { const ctx = new UserContextApiClient_1.UserContextApiClient(this._config, this._logger, this._rateLimiter, user.userId); return await runner(ctx); } } throw new Error(`Intents [${intents.join(', ')}] not found in auth provider`); } /** * Creates a contextualized ApiClient that can be used to call the API without the context of any user. * * This usually means that an app access token is used. * * @param runner The callback to execute. * * A parameter is passed that should be used in place of the normal `ApiClient` * to ensure that all requests are executed without user context. * * Please note that requests which require scope authorization ignore this context erasure. * * The return value of your callback will be propagated to the return value of this method. */ async withoutUser(runner) { const ctx = new NoContextApiClient_1.NoContextApiClient(this._config, this._logger, this._rateLimiter); return await runner(ctx); } }; exports.ApiClient = ApiClient; exports.ApiClient = ApiClient = tslib_1.__decorate([ (0, common_1.rtfm)('api', 'ApiClient') ], ApiClient);