Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:^",
Expand Down
21 changes: 12 additions & 9 deletions packages/core/src/api/applicationCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -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>;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/api/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -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>;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/util/api-options.ts
Original file line number Diff line number Diff line change
@@ -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 };
1 change: 1 addition & 0 deletions packages/core/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type * from './api-options.js';
export * from './files.js';
44 changes: 31 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.