Skip to content

Commit 86a9f96

Browse files
almeidxSyjalo
andauthored
refactor(rest): ensure types follow naming pattern (#1065)
Co-authored-by: Synbulat Biishev <[email protected]>
1 parent b90fddc commit 86a9f96

30 files changed

+468
-103
lines changed

.eslint-plugin-local/index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ const schema = [
3737
},
3838
] as const;
3939

40+
const REST_TYPE_NAME_REGEX =
41+
/^REST(?:Get|Patch|Post|Put|Delete)[a-zA-Z0-9]+(?:JSONBody|FormDataBody|URLEncodedData|Result|Query)$/;
42+
4043
export = {
4144
rules: {
4245
'explicitly-optional-undefined-properties': ESLintUtils.RuleCreator.withoutDocs<Options, 'missingOptional'>({
@@ -131,5 +134,54 @@ export = {
131134
},
132135
defaultOptions: [{ interfaceEndings: [] }],
133136
}),
137+
'rest-type-naming-convention': ESLintUtils.RuleCreator.withoutDocs<[{ whitelist: string[] }], 'invalidName'>({
138+
create: (context) => {
139+
const { whitelist } = context.options[0];
140+
const whitelistSet = new Set(whitelist);
141+
142+
return {
143+
'TSTypeAliasDeclaration, TSInterfaceDeclaration': (
144+
node: TSESTree.TSTypeAliasDeclaration | TSESTree.TSInterfaceDeclaration,
145+
) => {
146+
if (node.id.type !== AST_NODE_TYPES.Identifier) {
147+
return;
148+
}
149+
150+
const { name } = node.id;
151+
if (whitelistSet.has(name)) {
152+
return;
153+
}
154+
155+
if (!REST_TYPE_NAME_REGEX.test(name)) {
156+
context.report({
157+
node: node.id,
158+
messageId: 'invalidName',
159+
data: { name },
160+
});
161+
}
162+
},
163+
};
164+
},
165+
meta: {
166+
messages: {
167+
invalidName: `{{ name }} does not match REST type naming convention. Must match ${REST_TYPE_NAME_REGEX.source}.`,
168+
},
169+
type: 'problem',
170+
schema: [
171+
{
172+
type: 'object',
173+
properties: {
174+
whitelist: {
175+
type: 'array',
176+
items: {
177+
type: 'string',
178+
},
179+
},
180+
},
181+
},
182+
] as const,
183+
},
184+
defaultOptions: [{ whitelist: [] }],
185+
}),
134186
},
135187
};

.eslintrc.json

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,54 @@
1717
"typescript-sort-keys/string-enum": "off",
1818
"unicorn/prefer-math-trunc": "off",
1919
"jsdoc/no-undefined-types": "off"
20-
}
20+
},
21+
"overrides": [
22+
{
23+
"files": ["rest/v10/*.ts", "rest/v9/*.ts"],
24+
"excludedFiles": ["rest/v10/index.ts", "rest/v9/index.ts"],
25+
"rules": {
26+
"local/rest-type-naming-convention": [
27+
"error",
28+
{
29+
"whitelist": [
30+
"RESTAPIAttachment",
31+
"RESTAPIChannelPatchOverwrite",
32+
"RESTAPIGuildChannelResolvable",
33+
"RESTAPIGuildCreateOverwrite",
34+
"RESTAPIGuildCreatePartialChannel",
35+
"RESTAPIGuildCreateRole",
36+
"RESTAPIGuildOnboardingPrompt",
37+
"RESTAPIGuildOnboardingPromptOption",
38+
"RESTAPIMessageReference",
39+
"RESTAPIPartialCurrentUserGuild",
40+
"RESTAPIPoll",
41+
42+
"RESTOAuth2AdvancedBotAuthorizationQuery",
43+
"RESTOAuth2AdvancedBotAuthorizationQueryResult",
44+
"RESTOAuth2AuthorizationQuery",
45+
"RESTOAuth2BotAuthorizationQuery",
46+
"RESTOAuth2ImplicitAuthorizationQuery",
47+
"RESTOAuth2ImplicitAuthorizationURLFragmentResult",
48+
49+
// Deprecated types
50+
"APIChannelPatchOverwrite",
51+
"APIGuildChannelResolvable",
52+
"APIGuildCreateOverwrite",
53+
"APIGuildCreatePartialChannel",
54+
"APIGuildCreateRole",
55+
"APIMessageReferenceSend",
56+
"GetAPIVoiceRegionsResult",
57+
"RESTAPIModifyGuildOnboardingPromptData",
58+
"RESTAPIModifyGuildOnboardingPromptOptionData",
59+
"RESTAPIPollCreate",
60+
"RESTDeleteAPIChannelMessageOwnReaction",
61+
"RESTGetAPIStickerPack",
62+
"RESTOAuth2AuthorizationQueryResult",
63+
"RESTPostAPIEntitlementBody"
64+
]
65+
}
66+
]
67+
}
68+
}
69+
]
2170
}

deno/rest/v10/channel.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ import type {
2626
ChannelFlags,
2727
} from '../../payloads/v10/mod.ts';
2828
import type { AddUndefinedToPossiblyUndefinedPropertiesOfInterface, StrictPartial } from '../../utils/internals.ts';
29-
import type { RESTAPIPollCreate } from './poll.ts';
29+
import type { RESTAPIPoll } from './poll.ts';
3030

31-
export interface APIChannelPatchOverwrite extends RESTPutAPIChannelPermissionJSONBody {
31+
export interface RESTAPIChannelPatchOverwrite extends RESTPutAPIChannelPermissionJSONBody {
3232
id: Snowflake;
3333
}
3434

35+
/**
36+
* @deprecated Use {@link RESTAPIChannelPatchOverwrite} instead
37+
*/
38+
export type APIChannelPatchOverwrite = RESTAPIChannelPatchOverwrite;
39+
3540
/**
3641
* https://discord.com/developers/docs/resources/channel#get-channel
3742
*/
@@ -98,7 +103,7 @@ export interface RESTPatchAPIChannelJSONBody {
98103
*
99104
* Channel types: all excluding newsThread, publicThread, privateThread
100105
*/
101-
permission_overwrites?: APIChannelPatchOverwrite[] | null | undefined;
106+
permission_overwrites?: RESTAPIChannelPatchOverwrite[] | null | undefined;
102107
/**
103108
* ID of the new parent category for a channel
104109
*
@@ -237,7 +242,7 @@ export type RESTGetAPIChannelMessageResult = APIMessage;
237242
/**
238243
* https://discord.com/developers/docs/resources/channel#message-reference-object-message-reference-structure
239244
*/
240-
export type APIMessageReferenceSend = AddUndefinedToPossiblyUndefinedPropertiesOfInterface<
245+
export type RESTAPIMessageReference = AddUndefinedToPossiblyUndefinedPropertiesOfInterface<
241246
Required<Pick<APIMessageReference, 'message_id'>>
242247
> &
243248
StrictPartial<APIMessageReference> & {
@@ -249,6 +254,11 @@ export type APIMessageReferenceSend = AddUndefinedToPossiblyUndefinedPropertiesO
249254
fail_if_not_exists?: boolean | undefined;
250255
};
251256

257+
/**
258+
* @deprecated Use {@link RESTAPIMessageReference} instead
259+
*/
260+
export type APIMessageReferenceSend = RESTAPIMessageReference;
261+
252262
/**
253263
* https://discord.com/developers/docs/resources/channel#attachment-object
254264
*/
@@ -300,7 +310,7 @@ export interface RESTPostAPIChannelMessageJSONBody {
300310
*
301311
* See https://discord.com/developers/docs/resources/channel#message-reference-object-message-reference-structure
302312
*/
303-
message_reference?: APIMessageReferenceSend | undefined;
313+
message_reference?: RESTAPIMessageReference | undefined;
304314
/**
305315
* The components to include with the message
306316
*
@@ -329,7 +339,7 @@ export interface RESTPostAPIChannelMessageJSONBody {
329339
/**
330340
* A poll!
331341
*/
332-
poll?: RESTAPIPollCreate | undefined;
342+
poll?: RESTAPIPoll | undefined;
333343
}
334344

335345
/**
@@ -362,7 +372,12 @@ export type RESTPutAPIChannelMessageReactionResult = never;
362372
/**
363373
* https://discord.com/developers/docs/resources/channel#delete-own-reaction
364374
*/
365-
export type RESTDeleteAPIChannelMessageOwnReaction = never;
375+
export type RESTDeleteAPIChannelMessageOwnReactionResult = never;
376+
377+
/**
378+
* @deprecated Use {@link RESTDeleteAPIChannelMessageOwnReactionResult} instead
379+
*/
380+
export type RESTDeleteAPIChannelMessageOwnReaction = RESTDeleteAPIChannelMessageOwnReactionResult;
366381

367382
/**
368383
* https://discord.com/developers/docs/resources/channel#delete-user-reaction

deno/rest/v10/guild.ts

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,25 @@ import type {
3737
} from '../../utils/internals.ts';
3838
import type { RESTPutAPIChannelPermissionJSONBody } from './channel.ts';
3939

40-
export interface APIGuildCreateOverwrite extends RESTPutAPIChannelPermissionJSONBody {
40+
export interface RESTAPIGuildCreateOverwrite extends RESTPutAPIChannelPermissionJSONBody {
4141
id: number | string;
4242
}
4343

44-
export type APIGuildChannelResolvable = Exclude<APIChannel, APIDMChannel | APIGroupDMChannel>;
45-
export type APIGuildCreatePartialChannel = StrictPartial<
44+
/**
45+
* @deprecated Use {@link RESTAPIGuildCreateOverwrite} instead
46+
*/
47+
export type APIGuildCreateOverwrite = RESTAPIGuildCreateOverwrite;
48+
49+
export type RESTAPIGuildChannelResolvable = Exclude<APIChannel, APIDMChannel | APIGroupDMChannel>;
50+
51+
/**
52+
* @deprecated Use {@link RESTAPIGuildChannelResolvable} instead
53+
*/
54+
export type APIGuildChannelResolvable = RESTAPIGuildChannelResolvable;
55+
56+
export type RESTAPIGuildCreatePartialChannel = StrictPartial<
4657
DistributivePick<
47-
APIGuildChannelResolvable,
58+
RESTAPIGuildChannelResolvable,
4859
| 'available_tags'
4960
| 'bitrate'
5061
| 'default_auto_archive_duration'
@@ -66,13 +77,23 @@ export type APIGuildCreatePartialChannel = StrictPartial<
6677
name: string;
6778
id?: number | string | undefined;
6879
parent_id?: number | string | null | undefined;
69-
permission_overwrites?: APIGuildCreateOverwrite[] | undefined;
80+
permission_overwrites?: RESTAPIGuildCreateOverwrite[] | undefined;
7081
};
7182

72-
export interface APIGuildCreateRole extends RESTPostAPIGuildRoleJSONBody {
83+
/**
84+
* @deprecated Use {@link RESTAPIGuildCreatePartialChannel} instead
85+
*/
86+
export type APIGuildCreatePartialChannel = RESTAPIGuildCreatePartialChannel;
87+
88+
export interface RESTAPIGuildCreateRole extends RESTPostAPIGuildRoleJSONBody {
7389
id: number | string;
7490
}
7591

92+
/**
93+
* @deprecated Use {@link RESTAPIGuildCreateRole} instead
94+
*/
95+
export type APIGuildCreateRole = RESTAPIGuildCreateRole;
96+
7697
/**
7798
* https://discord.com/developers/docs/resources/guild#create-guild
7899
*/
@@ -124,7 +145,7 @@ export interface RESTPostAPIGuildsJSONBody {
124145
*
125146
* See https://discord.com/developers/docs/topics/permissions#role-object
126147
*/
127-
roles?: APIGuildCreateRole[] | undefined;
148+
roles?: RESTAPIGuildCreateRole[] | undefined;
128149
/**
129150
* New guild's channels
130151
*
@@ -138,7 +159,7 @@ export interface RESTPostAPIGuildsJSONBody {
138159
*
139160
* See https://discord.com/developers/docs/resources/channel#channel-object
140161
*/
141-
channels?: APIGuildCreatePartialChannel[] | undefined;
162+
channels?: RESTAPIGuildCreatePartialChannel[] | undefined;
142163
/**
143164
* ID for afk channel
144165
*/
@@ -333,7 +354,7 @@ export type RESTGetAPIGuildChannelsResult = APIChannel[];
333354
/**
334355
* https://discord.com/developers/docs/resources/guild#create-guild-channel
335356
*/
336-
export type RESTPostAPIGuildChannelJSONBody = DistributiveOmit<APIGuildCreatePartialChannel, 'id'>;
357+
export type RESTPostAPIGuildChannelJSONBody = DistributiveOmit<RESTAPIGuildCreatePartialChannel, 'id'>;
337358

338359
/**
339360
* https://discord.com/developers/docs/resources/guild#create-guild-channel
@@ -930,20 +951,25 @@ export type RESTPutAPIGuildOnboardingJSONBody = AddUndefinedToPossiblyUndefinedP
930951
/**
931952
* Prompts shown during onboarding and in customize community
932953
*/
933-
prompts?: RESTAPIModifyGuildOnboardingPromptData[] | undefined;
954+
prompts?: RESTAPIGuildOnboardingPrompt[] | undefined;
934955
};
935956

936-
export type RESTAPIModifyGuildOnboardingPromptData = AddUndefinedToPossiblyUndefinedPropertiesOfInterface<
957+
export type RESTAPIGuildOnboardingPrompt = AddUndefinedToPossiblyUndefinedPropertiesOfInterface<
937958
Partial<Omit<APIGuildOnboardingPrompt, 'guild_id' | 'id' | 'options' | 'title'>>
938959
> &
939960
Pick<APIGuildOnboardingPrompt, 'id' | 'title'> & {
940961
/**
941962
* Options available within the prompt
942963
*/
943-
options: RESTAPIModifyGuildOnboardingPromptOptionData[];
964+
options: RESTAPIGuildOnboardingPromptOption[];
944965
};
945966

946-
export type RESTAPIModifyGuildOnboardingPromptOptionData = AddUndefinedToPossiblyUndefinedPropertiesOfInterface<
967+
/**
968+
* @deprecated Use {@link RESTAPIGuildOnboardingPrompt} instead.
969+
*/
970+
export type RESTAPIModifyGuildOnboardingPromptData = RESTAPIGuildOnboardingPrompt;
971+
972+
export type RESTAPIGuildOnboardingPromptOption = AddUndefinedToPossiblyUndefinedPropertiesOfInterface<
947973
Partial<Omit<APIGuildOnboardingPromptOption, 'emoji' | 'guild_id' | 'title'>>
948974
> &
949975
Pick<APIGuildOnboardingPromptOption, 'title'> & {
@@ -961,6 +987,11 @@ export type RESTAPIModifyGuildOnboardingPromptOptionData = AddUndefinedToPossibl
961987
emoji_animated?: boolean | null | undefined;
962988
};
963989

990+
/**
991+
* @deprecated Use {@link RESTAPIGuildOnboardingPromptOption} instead.
992+
*/
993+
export type RESTAPIModifyGuildOnboardingPromptOptionData = RESTAPIGuildOnboardingPromptOption;
994+
964995
/**
965996
* https://discord.com/developers/docs/resources/guild#modify-guild-onboarding
966997
*/

deno/rest/v10/monetization.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export type RESTGetAPIEntitlementsResult = APIEntitlement[];
4646
/**
4747
* https://discord.com/developers/docs/monetization/entitlements#create-test-entitlement
4848
*/
49-
export interface RESTPostAPIEntitlementBody {
49+
export interface RESTPostAPIEntitlementJSONBody {
5050
/**
5151
* ID of the SKU to grant the entitlement to
5252
*/
@@ -61,6 +61,11 @@ export interface RESTPostAPIEntitlementBody {
6161
owner_type: EntitlementOwnerType;
6262
}
6363

64+
/**
65+
* @deprecated Use {@link RESTPostAPIEntitlementJSONBody} instead
66+
*/
67+
export type RESTPostAPIEntitlementBody = RESTPostAPIEntitlementJSONBody;
68+
6469
/**
6570
* https://discord.com/developers/docs/monetization/entitlements#create-test-entitlement
6671
*/

deno/rest/v10/oauth2.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ export interface RESTPostOAuth2TokenRevocationQuery {
5151
/**
5252
* https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-redirect-url-example
5353
*/
54-
export interface RESTOAuth2AuthorizationQueryResult {
54+
export interface RESTPostOAuth2AuthorizationQueryResult {
5555
code: string;
5656
state?: string;
5757
}
5858

59+
/**
60+
* @deprecated Use {@link RESTPostOAuth2AuthorizationQueryResult} instead
61+
*/
62+
export type RESTOAuth2AuthorizationQueryResult = RESTPostOAuth2AuthorizationQueryResult;
63+
5964
/**
6065
* https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-redirect-url-example
6166
*/

deno/rest/v10/poll.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface RESTGetAPIPollAnswerVotersQuery {
2020
/**
2121
* https://discord.com/developers/docs/resources/poll#poll-create-request-object-poll-create-request-object-structure
2222
*/
23-
export interface RESTAPIPollCreate
23+
export interface RESTAPIPoll
2424
extends Omit<APIPoll, 'allow_multiselect' | 'answers' | 'expiry' | 'layout_type' | 'results'>,
2525
Partial<Pick<APIPoll, 'allow_multiselect' | 'layout_type'>> {
2626
/**
@@ -35,6 +35,11 @@ export interface RESTAPIPollCreate
3535
duration?: number;
3636
}
3737

38+
/**
39+
* @deprecated Use {@link RESTAPIPoll} instead
40+
*/
41+
export type RESTAPIPollCreate = RESTAPIPoll;
42+
3843
/**
3944
* https://discord.com/developers/docs/resources/poll#get-answer-voters
4045
*/

0 commit comments

Comments
 (0)