feat: subscriptions (#257)

This commit is contained in:
Marcos Susaña 2024-08-29 20:34:40 -04:00 committed by GitHub
parent 3d36024e17
commit e16bd42092
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 136 additions and 9 deletions

View File

@ -57,7 +57,6 @@ export interface ApplicationRoutes {
get(
args?: RestArguments<ProxyRequestMethod.Get>,
): Promise<RESTGetAPIGuildApplicationCommandsPermissionsResult>;
// put(args?: RestArguments<ProxyRequestMethod.Put, RESTPutAPIGuildApplicationCommandsPermissionsJSONBody>): Promise<RESTPutAPIGuildApplicationCommandsPermissionsResult>
};
(
id: string,

18
src/api/Routes/skus.ts Normal file
View File

@ -0,0 +1,18 @@
import type {
RESTGetAPISKUSubscriptionsResult,
RESTGetAPISKUSubscriptionsQuery,
RESTGetAPISKUSubscriptionResult,
} from '../../types';
import type { RestArguments } from '../api';
import type { ProxyRequestMethod } from '../Router';
export interface SKuRoutes {
skus(id: string): {
get: (
args?: RestArguments<ProxyRequestMethod.Get, RESTGetAPISKUSubscriptionsQuery>,
) => Promise<RESTGetAPISKUSubscriptionsResult>;
subscriptions(id: string): {
get: (args?: RestArguments<ProxyRequestMethod.Get>) => Promise<RESTGetAPISKUSubscriptionResult>;
};
};
}

View File

@ -0,0 +1,15 @@
import type { UsingClient } from '../../commands';
import { toCamelCase } from '../../common';
import type { APISubscription } from '../../types';
export const SUBSCRIPTION_CREATE = (_: UsingClient, data: APISubscription) => {
return toCamelCase(data);
};
export const SUBSCRIPTION_UPDATE = (_: UsingClient, data: APISubscription) => {
return toCamelCase(data);
};
export const SUBSCRIPTION_DELETE = (_: UsingClient, data: APISubscription) => {
return toCamelCase(data);
};

View File

@ -33,6 +33,7 @@ import type {
APIAuditLogEntry,
APIEntitlement,
APIPartialEmoji,
APISubscription,
} from './payloads/index';
import type { ReactionType } from './rest/index';
import type { AnimationTypes, Nullable } from './utils';
@ -479,7 +480,9 @@ export type GatewayEntitlementModifyDispatch = DataPayload<
/**
* https://discord.com/developers/docs/topics/gateway-events#entitlement-create
*/
export type GatewayEntitlementCreateDispatchData = GatewayEntitlementModifyDispatchData;
export type GatewayEntitlementCreateDispatchData = Omit<GatewayEntitlementModifyDispatchData, 'ends_at'> & {
ends_at: null;
};
/**
* https://discord.com/developers/docs/topics/gateway-events#entitlement-create
@ -1338,6 +1341,36 @@ export type GatewayStageInstanceUpdateDispatch = DataPayload<
*/
export type GatewayStageInstanceUpdateDispatchData = APIStageInstance;
/**
* https://canary.discord.com/developers/docs/topics/gateway-events#subscription-create
*/
export type GatewaySubscriptionCreateDispatch = DataPayload<
GatewayDispatchEvents.SubscriptionCreate,
GatewaySubscriptionCreateDispatchData
>;
export type GatewaySubscriptionCreateDispatchData = APISubscription;
/**
* https://canary.discord.com/developers/docs/topics/gateway-events#subscription-update
*/
export type GatewaySubscriptionUpdateDispatch = DataPayload<
GatewayDispatchEvents.SubscriptionUpdate,
GatewaySubscriptionUpdateDispatchData
>;
export type GatewaySubscriptionUpdateDispatchData = APISubscription;
/**
* https://canary.discord.com/developers/docs/topics/gateway-events#subscription-delete
*/
export type GatewaySubscriptionDeleteDispatch = DataPayload<
GatewayDispatchEvents.SubscriptionDelete,
GatewaySubscriptionDeleteDispatchData
>;
export type GatewaySubscriptionDeleteDispatchData = APISubscription;
/**
* https://discord.com/developers/docs/topics/gateway-events#thread-list-sync
*/

View File

@ -39,7 +39,7 @@ export interface APIEntitlement {
/**
* Date at which the entitlement is no longer valid. Not present when using test entitlements.
*/
ends_at?: string;
ends_at?: string | null;
/**
* For consumable items, whether or not the entitlement has been consumed
*/
@ -153,3 +153,33 @@ export enum SKUType {
*/
SubscriptionGroup = 6,
}
export interface APISubscription {
/** ID of the subscription */
id: string;
/** ID of the user who is subscribed */
user_id: string;
/** List of SKUs subscribed to */
sku_ids: string[];
/** List of entitlements granted for this subscription */
entitlements_ids: string[];
/** Start of the current subscription period */
current_period_start: string;
/** End of the current subscription period */
current_period_end: string;
/** Current status of the subscription */
status: SubscriptionStatus;
/** When the subscription was canceled */
canceled_at: string | null;
/** ISO3166-1 alpha-2 country code of the payment source used to purchase the subscription. Missing unless queried with a private OAuth scope. */
country?: string;
}
export enum SubscriptionStatus {
/** Subscription is active and scheduled to renew. */
Active,
/** Subscription is active but will not renew. */
Ending,
/** Subscription is inactive and not being charged. */
Inactive,
}

View File

@ -1,5 +1,5 @@
import type { Snowflake } from '..';
import type { APIEntitlement, APISKU } from '../payloads';
import type { APIEntitlement, APISKU, APISubscription } from '../payloads';
/**
* https://discord.com/developers/docs/monetization/entitlements#list-entitlements
@ -88,3 +88,27 @@ export type RESTGetAPISKUsResult = APISKU[];
* https://discord.com/developers/docs/monetization/entitlements#consume-an-entitlement
*/
export type RESTPostAPIEntitlementConsumeResult = never;
/**
* https://canary.discord.com/developers/docs/resources/subscription#query-string-params
*/
export interface RESTGetAPISKUSubscriptionsQuery {
/** List subscriptions before this ID */
before?: string;
/** List subscriptions after this ID */
after?: string;
/** Number of results to return (1-100) */
limit?: number;
/** User ID for which to return subscriptions. Required except for OAuth queries. */
user_id?: string;
}
/**
* https://canary.discord.com/developers/docs/resources/subscription#list-sku-subscriptions
*/
export type RESTGetAPISKUSubscriptionsResult = APISubscription[];
/**
* https://canary.discord.com/developers/docs/resources/subscription#get-sku-subscription
*/
export type RESTGetAPISKUSubscriptionResult = APISubscription;

View File

@ -59,6 +59,8 @@ import type {
APIAutoModerationRule,
APIEntitlement,
GatewayVoiceChannelEffectSendDispachData,
APISubscription,
GatewayEntitlementCreateDispatchData,
} from '../types';
import { GatewayDispatchEvents } from '../types';
@ -147,6 +149,7 @@ export interface Events {
[GatewayDispatchEvents.VoiceServerUpdate]: GatewayVoiceServerUpdateDispatchData;
[GatewayDispatchEvents.WebhooksUpdate]: GatewayWebhooksUpdateDispatchData;
[GatewayDispatchEvents.InteractionCreate]: GatewayInteractionCreateDispatchData;
[GatewayDispatchEvents.EntitlementCreate]: GatewayEntitlementCreateDispatchData;
}
export type StageSameEvents = RestToKeys<
@ -210,11 +213,15 @@ export type AutoModetaractionRuleEvents = RestToKeys<
>;
export type EntitlementEvents = RestToKeys<
[APIEntitlement, GatewayDispatchEvents.EntitlementDelete, GatewayDispatchEvents.EntitlementUpdate]
>;
export type SubscriptionEvents = RestToKeys<
[
APIEntitlement,
GatewayDispatchEvents.EntitlementCreate,
GatewayDispatchEvents.EntitlementDelete,
GatewayDispatchEvents.EntitlementUpdate,
APISubscription,
GatewayDispatchEvents.SubscriptionCreate,
GatewayDispatchEvents.SubscriptionDelete,
GatewayDispatchEvents.SubscriptionUpdate,
]
>;
@ -226,6 +233,7 @@ export type NormalizeEvents = Events &
IntegrationSameEvents &
EntitlementEvents &
PollVoteSameEvents &
StageSameEvents & { RAW: GatewayDispatchEvents };
StageSameEvents &
SubscriptionEvents & { RAW: GatewayDispatchEvents };
export type GatewayEvents = { [x in keyof NormalizeEvents]: NormalizeEvents[x] };