mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
refactor!: consolidated options parameters
This commit is contained in:
@@ -70,7 +70,8 @@
|
||||
"@discordjs/ws": "workspace:^",
|
||||
"@sapphire/snowflake": "^3.5.5",
|
||||
"@vladfrangu/async_event_emitter": "^2.4.6",
|
||||
"discord-api-types": "^0.38.23"
|
||||
"discord-api-types": "^0.38.23",
|
||||
"type-fest": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@discordjs/api-extractor": "workspace:^",
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
type RESTPutAPIApplicationGuildCommandsResult,
|
||||
type Snowflake,
|
||||
} from 'discord-api-types/v10';
|
||||
import type { APIOptions } from '../util/api-options.js';
|
||||
|
||||
export class ApplicationCommandsAPI {
|
||||
public constructor(private readonly rest: REST) {}
|
||||
@@ -35,19 +36,21 @@ export class ApplicationCommandsAPI {
|
||||
* Fetches all global commands for a application
|
||||
*
|
||||
* @see {@link https://discord.com/developers/docs/interactions/application-commands#get-global-application-commands}
|
||||
* @param applicationId - The application id to fetch commands for
|
||||
* @param query - The query options for fetching commands
|
||||
* @param options - The options for fetching commands
|
||||
*/
|
||||
public async getGlobalCommands(
|
||||
applicationId: Snowflake,
|
||||
query: RESTGetAPIApplicationCommandsQuery = {},
|
||||
{ auth, signal }: Pick<RequestData, 'auth' | 'signal'> = {},
|
||||
) {
|
||||
public async getGlobalCommands({
|
||||
query,
|
||||
route: { applicationId },
|
||||
options,
|
||||
}: APIOptions<
|
||||
Pick<RequestData, 'auth' | 'signal'>,
|
||||
{ applicationId: Snowflake },
|
||||
never,
|
||||
RESTGetAPIApplicationCommandsQuery
|
||||
>) {
|
||||
return this.rest.get(Routes.applicationCommands(applicationId), {
|
||||
auth,
|
||||
...options,
|
||||
query: makeURLSearchParams(query),
|
||||
signal,
|
||||
}) as Promise<RESTGetAPIApplicationCommandsResult>;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
type RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody,
|
||||
type RESTPatchAPIGuildVoiceStateUserResult,
|
||||
} from 'discord-api-types/v10';
|
||||
import type { APIOptions } from '../util/api-options.js';
|
||||
|
||||
export class VoiceAPI {
|
||||
public constructor(private readonly rest: REST) {}
|
||||
@@ -22,8 +23,8 @@ export class VoiceAPI {
|
||||
* @see {@link https://discord.com/developers/docs/resources/voice#list-voice-regions}
|
||||
* @param options - The options for fetching the voice regions
|
||||
*/
|
||||
public async getVoiceRegions({ auth, signal }: Pick<RequestData, 'auth' | 'signal'> = {}) {
|
||||
return this.rest.get(Routes.voiceRegions(), { auth, signal }) as Promise<RESTGetAPIVoiceRegionsResult>;
|
||||
public async getVoiceRegions({ options }: APIOptions<Pick<RequestData, 'auth' | 'signal'>> = {}) {
|
||||
return this.rest.get(Routes.voiceRegions(), options) as Promise<RESTGetAPIVoiceRegionsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
19
packages/core/src/util/api-options.ts
Normal file
19
packages/core/src/util/api-options.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { RequestData } from '@discordjs/rest';
|
||||
import type { If, IsNever, RequiredKeysOf } from 'type-fest';
|
||||
|
||||
/**
|
||||
* Creates the input type for an API method with optional properties based on the provided type parameters.
|
||||
*
|
||||
* @typeParam Options - Options for the request.
|
||||
* @typeParam Route - Route parameters for the endpoint.
|
||||
* @typeParam Body - Body for the request.
|
||||
* @typeParam Query - Query parameters for the endpoint.
|
||||
*/
|
||||
export type APIOptions<
|
||||
Options extends RequestData,
|
||||
Route extends object = never,
|
||||
Body extends object = never,
|
||||
Query extends object = never,
|
||||
> = If<IsNever<Body>, object, If<IsNever<RequiredKeysOf<Body>>, { body?: Body }, { body: Body }>> &
|
||||
If<IsNever<Query>, object, If<IsNever<RequiredKeysOf<Query>>, { query?: Query }, { query: Query }>> &
|
||||
If<IsNever<Route>, object, { route: Route }> & { options?: Options };
|
||||
Reference in New Issue
Block a user