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,6 +1,5 @@
import { URL } from 'node:url';
import type { REST } from '@discordjs/rest';
import { makeURLSearchParams } from '@discordjs/rest';
import { type RequestData, type REST, makeURLSearchParams } from '@discordjs/rest';
import {
Routes,
RouteBases,
@@ -34,15 +33,20 @@ export class OAuth2API {
* Performs an OAuth2 token exchange, giving you an access token
*
* @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-access-token-exchange-example}
* @param body - The body of the token exchange request
* @param options - The options for the token exchange request
*/
public async tokenExchange(options: RESTPostOAuth2AccessTokenURLEncodedData) {
public async tokenExchange(
body: RESTPostOAuth2AccessTokenURLEncodedData,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.rest.post(Routes.oauth2TokenExchange(), {
body: makeURLSearchParams(options),
body: makeURLSearchParams(body),
passThroughBody: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
signal,
}) as Promise<RESTPostOAuth2AccessTokenResult>;
}
@@ -50,15 +54,20 @@ export class OAuth2API {
* Refreshes an OAuth2 access token, giving you a new one
*
* @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-refresh-token-exchange-example}
* @param body - The options for the refresh token request
* @param options - The options for the refresh token request
*/
public async refreshToken(options: RESTPostOAuth2RefreshTokenURLEncodedData) {
public async refreshToken(
body: RESTPostOAuth2RefreshTokenURLEncodedData,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.rest.post(Routes.oauth2TokenExchange(), {
body: makeURLSearchParams(options),
body: makeURLSearchParams(body),
passThroughBody: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
signal,
}) as Promise<RESTPostOAuth2RefreshTokenResult>;
}
@@ -68,15 +77,20 @@ export class OAuth2API {
* @remarks
* This is primarily used for testing purposes
* @see {@link https://discord.com/developers/docs/topics/oauth2#client-credentials-grant}
* @param body - The options for the client credentials grant request
* @param options - The options for the client credentials grant request
*/
public async getToken(options: RESTPostOAuth2ClientCredentialsURLEncodedData) {
public async getToken(
body: RESTPostOAuth2ClientCredentialsURLEncodedData,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.rest.post(Routes.oauth2TokenExchange(), {
body: makeURLSearchParams(options),
body: makeURLSearchParams(body),
passThroughBody: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
signal,
}) as Promise<RESTPostOAuth2ClientCredentialsResult>;
}
@@ -84,17 +98,23 @@ export class OAuth2API {
* Fetches the current bot's application information
*
* @see {@link https://discord.com/developers/docs/topics/oauth2#get-current-bot-application-information}
* @param options - The options for the current bot application information request
*/
public async getCurrentBotApplicationInformation() {
return this.rest.get(Routes.oauth2CurrentApplication()) as Promise<RESTGetAPIOAuth2CurrentApplicationResult>;
public async getCurrentBotApplicationInformation({ signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.oauth2CurrentApplication(), {
signal,
}) as Promise<RESTGetAPIOAuth2CurrentApplicationResult>;
}
/**
* Fetches the current authorization information
*
* @see {@link https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information}
* @param options - The options for the current authorization information request
*/
public async getCurrentAuthorizationInformation() {
return this.rest.get(Routes.oauth2CurrentAuthorization()) as Promise<RESTGetAPIOAuth2CurrentAuthorizationResult>;
public async getCurrentAuthorizationInformation({ signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.oauth2CurrentAuthorization(), {
signal,
}) as Promise<RESTGetAPIOAuth2CurrentAuthorizationResult>;
}
}