Skip to content

Commit e8b48af

Browse files
committed
refactor!: consolidated options parameters
1 parent b66f52f commit e8b48af

File tree

5 files changed

+65
-25
lines changed

5 files changed

+65
-25
lines changed

packages/core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"@discordjs/ws": "workspace:^",
7171
"@sapphire/snowflake": "^3.5.5",
7272
"@vladfrangu/async_event_emitter": "^2.4.6",
73-
"discord-api-types": "^0.38.23"
73+
"discord-api-types": "^0.38.23",
74+
"type-fest": "^5.0.0"
7475
},
7576
"devDependencies": {
7677
"@discordjs/api-extractor": "workspace:^",

packages/core/src/api/applicationCommands.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
type RESTPutAPIApplicationGuildCommandsResult,
2828
type Snowflake,
2929
} from 'discord-api-types/v10';
30+
import type { APIOptions } from '../util/api-options.js';
3031

3132
export class ApplicationCommandsAPI {
3233
public constructor(private readonly rest: REST) {}
@@ -35,19 +36,21 @@ export class ApplicationCommandsAPI {
3536
* Fetches all global commands for a application
3637
*
3738
* @see {@link https://discord.com/developers/docs/interactions/application-commands#get-global-application-commands}
38-
* @param applicationId - The application id to fetch commands for
39-
* @param query - The query options for fetching commands
4039
* @param options - The options for fetching commands
4140
*/
42-
public async getGlobalCommands(
43-
applicationId: Snowflake,
44-
query: RESTGetAPIApplicationCommandsQuery = {},
45-
{ auth, signal }: Pick<RequestData, 'auth' | 'signal'> = {},
46-
) {
41+
public async getGlobalCommands({
42+
query,
43+
route: { applicationId },
44+
options,
45+
}: APIOptions<
46+
Pick<RequestData, 'auth' | 'signal'>,
47+
{ applicationId: Snowflake },
48+
never,
49+
RESTGetAPIApplicationCommandsQuery
50+
>) {
4751
return this.rest.get(Routes.applicationCommands(applicationId), {
48-
auth,
52+
...options,
4953
query: makeURLSearchParams(query),
50-
signal,
5154
}) as Promise<RESTGetAPIApplicationCommandsResult>;
5255
}
5356

packages/core/src/api/voice.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody,
1313
type RESTPatchAPIGuildVoiceStateUserResult,
1414
} from 'discord-api-types/v10';
15+
import type { APIOptions } from '../util/api-options.js';
1516

1617
export class VoiceAPI {
1718
public constructor(private readonly rest: REST) {}
@@ -22,8 +23,8 @@ export class VoiceAPI {
2223
* @see {@link https://discord.com/developers/docs/resources/voice#list-voice-regions}
2324
* @param options - The options for fetching the voice regions
2425
*/
25-
public async getVoiceRegions({ auth, signal }: Pick<RequestData, 'auth' | 'signal'> = {}) {
26-
return this.rest.get(Routes.voiceRegions(), { auth, signal }) as Promise<RESTGetAPIVoiceRegionsResult>;
26+
public async getVoiceRegions({ options }: APIOptions<Pick<RequestData, 'auth' | 'signal'>> = {}) {
27+
return this.rest.get(Routes.voiceRegions(), options) as Promise<RESTGetAPIVoiceRegionsResult>;
2728
}
2829

2930
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { RequestData } from '@discordjs/rest';
2+
import type { If, IsNever, RequiredKeysOf } from 'type-fest';
3+
4+
/**
5+
* Creates the input type for an API method with optional properties based on the provided type parameters.
6+
*
7+
* @typeParam Options - Options for the request.
8+
* @typeParam Route - Route parameters for the endpoint.
9+
* @typeParam Body - Body for the request.
10+
* @typeParam Query - Query parameters for the endpoint.
11+
*/
12+
export type APIOptions<
13+
Options extends RequestData,
14+
Route extends object = never,
15+
Body extends object = never,
16+
Query extends object = never,
17+
> = 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 };

pnpm-lock.yaml

Lines changed: 31 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)