|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { Collection } = require('@discordjs/collection'); |
| 4 | +const { makeURLSearchParams } = require('@discordjs/rest'); |
| 5 | +const { Routes } = require('discord-api-types/v10'); |
| 6 | +const CachedManager = require('./CachedManager'); |
| 7 | +const { DiscordjsTypeError, ErrorCodes } = require('../errors/index'); |
| 8 | +const { Subscription } = require('../structures/Subscription'); |
| 9 | +const { resolveSKUId } = require('../util/Util'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Manages API methods for subscriptions and stores their cache. |
| 13 | + * @extends {CachedManager} |
| 14 | + */ |
| 15 | +class SubscriptionManager extends CachedManager { |
| 16 | + constructor(client, iterable) { |
| 17 | + super(client, Subscription, iterable); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * The cache of this manager |
| 22 | + * @type {Collection<Snowflake, Subscription>} |
| 23 | + * @name SubscriptionManager#cache |
| 24 | + */ |
| 25 | + |
| 26 | + /** |
| 27 | + * Options used to fetch a subscription |
| 28 | + * @typedef {BaseFetchOptions} FetchSubscriptionOptions |
| 29 | + * @property {SKUResolvable} sku The SKU to fetch the subscription for |
| 30 | + * @property {Snowflake} subscriptionId The id of the subscription to fetch |
| 31 | + */ |
| 32 | + |
| 33 | + /** |
| 34 | + * Options used to fetch subscriptions |
| 35 | + * @typedef {Object} FetchSubscriptionsOptions |
| 36 | + * @property {Snowflake} [after] Consider only subscriptions after this subscription id |
| 37 | + * @property {Snowflake} [before] Consider only subscriptions before this subscription id |
| 38 | + * @property {number} [limit] The maximum number of subscriptions to fetch |
| 39 | + * @property {SKUResolvable} sku The SKU to fetch subscriptions for |
| 40 | + * @property {UserResolvable} user The user to fetch entitlements for |
| 41 | + * <warn>If both `before` and `after` are provided, only `before` is respected</warn> |
| 42 | + */ |
| 43 | + |
| 44 | + /** |
| 45 | + * Fetches subscriptions for this application |
| 46 | + * @param {FetchSubscriptionOptions|FetchSubscriptionsOptions} [options={}] Options for fetching the subscriptions |
| 47 | + * @returns {Promise<Subscription|Collection<Snowflake, Subscription>>} |
| 48 | + */ |
| 49 | + async fetch(options = {}) { |
| 50 | + if (typeof options !== 'object') throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true); |
| 51 | + |
| 52 | + const { after, before, cache, limit, sku, subscriptionId, user } = options; |
| 53 | + |
| 54 | + const skuId = resolveSKUId(sku); |
| 55 | + |
| 56 | + if (!skuId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'sku', 'SKUResolvable'); |
| 57 | + |
| 58 | + if (subscriptionId) { |
| 59 | + const subscription = await this.client.rest.get(Routes.skuSubscription(skuId, subscriptionId)); |
| 60 | + |
| 61 | + return this._add(subscription, cache); |
| 62 | + } |
| 63 | + |
| 64 | + const query = makeURLSearchParams({ |
| 65 | + limit, |
| 66 | + user_id: this.client.users.resolveId(user) ?? undefined, |
| 67 | + sku_id: skuId, |
| 68 | + before, |
| 69 | + after, |
| 70 | + }); |
| 71 | + |
| 72 | + const subscriptions = await this.client.rest.get(Routes.skuSubscriptions(skuId), { query }); |
| 73 | + |
| 74 | + return subscriptions.reduce( |
| 75 | + (coll, subscription) => coll.set(subscription.id, this._add(subscription, cache)), |
| 76 | + new Collection(), |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +exports.SubscriptionManager = SubscriptionManager; |
0 commit comments