feat(core): Add AbortSignal support. (#9042)

* feat: add abort signal to guilds api

* feat: add to application commands, channels, and users classes

* chore: finish up

* chore: centralize types

* chore: make requested changes

* chore: make requested changes

* refactor: consistently use empty objects

* Update packages/core/src/api/webhook.ts

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>

* chore: make requested changes

* refactor: update `setVoiceState` after rebase

* chore: requested changes

* refactor: use -types interface for query

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
Suneet Tipirneni
2023-04-01 17:11:37 -04:00
committed by GitHub
parent e2f39ccc32
commit 907eb1b470
12 changed files with 959 additions and 444 deletions

View File

@@ -1,4 +1,4 @@
import { makeURLSearchParams, type REST } from '@discordjs/rest';
import { makeURLSearchParams, type RequestData, type REST } from '@discordjs/rest';
import {
Routes,
type RESTGetAPICurrentUserApplicationRoleConnectionResult,
@@ -26,29 +26,33 @@ export class UsersAPI {
*
* @see {@link https://discord.com/developers/docs/resources/user#get-user}
* @param userId - The id of the user to fetch
* @param options - The options to use when fetching the user
*/
public async get(userId: Snowflake) {
return this.rest.get(Routes.user(userId)) as Promise<RESTGetAPIUserResult>;
public async get(userId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.user(userId), { signal }) as Promise<RESTGetAPIUserResult>;
}
/**
* Returns the user object of the requester's account
*
* @see {@link https://discord.com/developers/docs/resources/user#get-current-user}
* @param options - The options to use when fetching the current user
*/
public async getCurrent() {
return this.rest.get(Routes.user('@me')) as Promise<RESTGetAPICurrentUserResult>;
public async getCurrent({ signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.user('@me'), { signal }) as Promise<RESTGetAPICurrentUserResult>;
}
/**
* Returns a list of partial guild objects the current user is a member of
*
* @see {@link https://discord.com/developers/docs/resources/user#get-current-user-guilds}
* @param options - The options to use when fetching the current user's guilds
* @param query - The query options to use when fetching the current user's guilds
* @param options - The options to use when fetching the guilds
*/
public async getGuilds(options: RESTGetAPICurrentUserGuildsQuery = {}) {
public async getGuilds(query: RESTGetAPICurrentUserGuildsQuery = {}, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.userGuilds(), {
query: makeURLSearchParams(options),
query: makeURLSearchParams(query),
signal,
}) as Promise<RESTGetAPICurrentUserGuildsResult>;
}
@@ -57,19 +61,21 @@ export class UsersAPI {
*
* @see {@link https://discord.com/developers/docs/resources/user#leave-guild}
* @param guildId - The id of the guild
* @param options - The options for leaving the guild
*/
public async leaveGuild(guildId: Snowflake) {
await this.rest.delete(Routes.userGuild(guildId));
public async leaveGuild(guildId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
await this.rest.delete(Routes.userGuild(guildId), { signal });
}
/**
* Edits the current user
*
* @see {@link https://discord.com/developers/docs/resources/user#modify-current-user}
* @param user - The new data for the current user
* @param body - The new data for the current user
* @param options - The options for editing the user
*/
public async edit(user: RESTPatchAPICurrentUserJSONBody) {
return this.rest.patch(Routes.user('@me'), { body: user }) as Promise<RESTPatchAPICurrentUserResult>;
public async edit(body: RESTPatchAPICurrentUserJSONBody, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.patch(Routes.user('@me'), { body, signal }) as Promise<RESTPatchAPICurrentUserResult>;
}
/**
@@ -77,9 +83,10 @@ export class UsersAPI {
*
* @see {@link https://discord.com/developers/docs/resources/user#get-current-user-guild-member}
* @param guildId - The id of the guild
* @param options - The options for fetching the guild member
*/
public async getGuildMember(guildId: Snowflake) {
return this.rest.get(Routes.userGuildMember(guildId)) as Promise<RESTGetCurrentUserGuildMemberResult>;
public async getGuildMember(guildId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.userGuildMember(guildId), { signal }) as Promise<RESTGetCurrentUserGuildMemberResult>;
}
/**
@@ -87,13 +94,18 @@ export class UsersAPI {
*
* @see {@link https://discord.com/developers/docs/resources/guild#modify-current-member}
* @param guildId - The id of the guild
* @param member - The new data for the guild member
* @param reason - The reason for editing this guild member
* @param body - The new data for the guild member
* @param options - The options for editing the guild member
*/
public async editGuildMember(guildId: Snowflake, member: RESTPatchAPIGuildMemberJSONBody = {}, reason?: string) {
public async editCurrentGuildMember(
guildId: Snowflake,
body: RESTPatchAPIGuildMemberJSONBody = {},
{ reason, signal }: Pick<RequestData, 'reason' | 'signal'> = {},
) {
return this.rest.patch(Routes.guildMember(guildId, '@me'), {
reason,
body: member,
body,
signal,
}) as Promise<RESTPatchAPIGuildMemberResult>;
}
@@ -102,10 +114,12 @@ export class UsersAPI {
*
* @see {@link https://discord.com/developers/docs/resources/user#create-dm}
* @param userId - The id of the user to open a DM channel with
* @param options - The options for opening the DM
*/
public async createDM(userId: Snowflake) {
public async createDM(userId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.post(Routes.userChannels(), {
body: { recipient_id: userId },
signal,
}) as Promise<RESTPostAPICurrentUserCreateDMChannelResult>;
}
@@ -113,9 +127,10 @@ export class UsersAPI {
* Gets the current user's connections
*
* @see {@link https://discord.com/developers/docs/resources/user#get-user-connections}
* @param options - The options for fetching the user's connections
*/
public async getConnections() {
return this.rest.get(Routes.userConnections()) as Promise<RESTGetAPICurrentUserConnectionsResult>;
public async getConnections({ signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.userConnections(), { signal }) as Promise<RESTGetAPICurrentUserConnectionsResult>;
}
/**
@@ -123,11 +138,12 @@ export class UsersAPI {
*
* @see {@link https://discord.com/developers/docs/resources/user#get-user-application-role-connection}
* @param applicationId - The id of the application
* @param options - The options for fetching the role connections
*/
public async getApplicationRoleConnection(applicationId: Snowflake) {
return this.rest.get(
Routes.userApplicationRoleConnection(applicationId),
) as Promise<RESTGetAPICurrentUserApplicationRoleConnectionResult>;
public async getApplicationRoleConnection(applicationId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.userApplicationRoleConnection(applicationId), {
signal,
}) as Promise<RESTGetAPICurrentUserApplicationRoleConnectionResult>;
}
/**
@@ -135,14 +151,17 @@ export class UsersAPI {
*
* @see {@link https://discord.com/developers/docs/resources/user#update-user-application-role-connection}
* @param applicationId - The id of the application
* @param body - The data to use when updating the application role connection
* @param options - The options to use when updating the application role connection
*/
public async updateApplicationRoleConnection(
applicationId: Snowflake,
options: RESTPutAPICurrentUserApplicationRoleConnectionJSONBody,
body: RESTPutAPICurrentUserApplicationRoleConnectionJSONBody,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.rest.put(Routes.userApplicationRoleConnection(applicationId), {
body: options,
body,
signal,
}) as Promise<RESTPutAPICurrentUserApplicationRoleConnectionResult>;
}
}