This repository was archived by the owner on Jan 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
feat:@discord.js/type-utils package
#57
Open
suneettipirneni
wants to merge
7
commits into
discordjs:main
Choose a base branch
from
suneettipirneni:feature/typeguards
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
985a176
feat: typeguards package
suneettipirneni 4fc8e92
move discord.js to dev deps
suneettipirneni f653ffa
Update packages/typeguards/src/lib/button.ts
suneettipirneni f47760b
Update packages/typeguards/src/lib/button.ts
suneettipirneni 2ef3640
rename to type-utils
suneettipirneni df53fbd
Merge branch 'feature/typeguards' of https://github.com/suneettipirne…
suneettipirneni 954d125
fix readme
suneettipirneni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Change Log | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
| See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # `@discordjs/typeguards` | ||
|
|
||
| > The Type Guards module for Discord.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { MessageButtonOptions } from 'discord.js'; | ||
| import { isLinkButtonOptions } from '../src/lib/button'; | ||
|
|
||
| test('isLinkButtonOptions', () => { | ||
| const buttonData: MessageButtonOptions = { | ||
| style: 'LINK', | ||
| url: 'test', | ||
| }; | ||
|
|
||
| expect(isLinkButtonOptions(buttonData)).toBe(true); | ||
|
|
||
| const invalidData: MessageButtonOptions = { | ||
| style: 'PRIMARY', | ||
| customId: '1234', | ||
| }; | ||
|
|
||
| expect(isLinkButtonOptions(invalidData)).toBe(false); | ||
|
|
||
| // @ts-ignore | ||
| expect(() => isLinkButtonOptions({})).toThrowError(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { ApplicationCommandData, ApplicationCommandOptionData } from 'discord.js'; | ||
| import { | ||
| isChatInputCommandData, | ||
| isContextMenuCommandData, | ||
| optionDataSupportsChoices, | ||
| optionDataSupportsSubOptions, | ||
| } from '../src/lib/command'; | ||
| import { validateType } from '../src/lib/util/enum'; | ||
|
|
||
| test('validateType', () => { | ||
| expect(() => validateType({})).toThrowError(); | ||
| }); | ||
|
|
||
| test('isContextMenuCommand', () => { | ||
| const nonTypedData: ApplicationCommandData = { | ||
| name: 'test', | ||
| description: 'test', | ||
| }; | ||
|
|
||
| expect(isContextMenuCommandData(nonTypedData)).toBe(false); | ||
| expect(isChatInputCommandData(nonTypedData)).toBe(true); | ||
|
|
||
| const typedData: ApplicationCommandData = { | ||
| ...nonTypedData, | ||
| type: 'CHAT_INPUT', | ||
| }; | ||
|
|
||
| expect(isContextMenuCommandData(typedData)).toBe(false); | ||
|
|
||
| const invalidMessageData: ApplicationCommandData = { | ||
| name: 'test', | ||
| type: 'MESSAGE', | ||
| }; | ||
|
|
||
| expect(isContextMenuCommandData(invalidMessageData)).toBe(true); | ||
| expect(isChatInputCommandData(invalidMessageData)).toBe(false); | ||
|
|
||
| const invalidUserData: ApplicationCommandData = { | ||
| name: 'test', | ||
| type: 'USER', | ||
| }; | ||
|
|
||
| expect(isContextMenuCommandData(invalidUserData)).toBe(true); | ||
| }); | ||
|
|
||
| test('optionsDataSupportsChoices', () => { | ||
| const subCommandData: ApplicationCommandOptionData = { | ||
| name: 'test', | ||
| type: 'SUB_COMMAND', | ||
| description: 'test', | ||
| }; | ||
|
|
||
| expect(optionDataSupportsChoices(subCommandData)).toBe(false); | ||
|
|
||
| const stringData: ApplicationCommandOptionData = { | ||
| name: 'test', | ||
| type: 'STRING', | ||
| description: 'test', | ||
| }; | ||
|
|
||
| expect(optionDataSupportsChoices(stringData)).toBe(true); | ||
| }); | ||
|
|
||
| test('optionsDataSupportsSubOptions', () => { | ||
| const subCommandData: ApplicationCommandOptionData = { | ||
| name: 'test', | ||
| type: 'SUB_COMMAND', | ||
| description: 'test', | ||
| }; | ||
|
|
||
| expect(optionDataSupportsSubOptions(subCommandData)).toBe(true); | ||
|
|
||
| const stringData: ApplicationCommandOptionData = { | ||
| name: 'test', | ||
| type: 'STRING', | ||
| description: 'test', | ||
| }; | ||
|
|
||
| expect(optionDataSupportsSubOptions(stringData)).toBe(false); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/package", | ||
| "name": "@discordjs/typeguards", | ||
| "version": "0.1.0-canary.0", | ||
| "description": "Typeguard helpers for Discord.js types.", | ||
| "contributors": [ | ||
| "Suneet Tipirneni <suneettipirneni@icloud.com>", | ||
| "NotSugden <email>" | ||
| ], | ||
| "license": "Apache-2.0", | ||
| "scripts": { | ||
| "test": "echo \"Error: run tests from root\" && exit 1", | ||
| "build": "tsc --build --force" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/discordjs/discord.js-modules.git" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/discordjs/discord.js-modules/issues" | ||
| }, | ||
| "homepage": "https://github.com/discordjs/discord.js-modules/tree/main/packages/typeguards", | ||
| "keywords": [ | ||
| "discord", | ||
| "typeguards", | ||
| "typescript", | ||
| "discordapp", | ||
| "discordjs" | ||
| ], | ||
| "main": "dist/index.js", | ||
| "directories": { | ||
| "lib": "src", | ||
| "test": "__tests__" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "dependencies": { | ||
| "discord.js": "^13.1.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node-fetch": "^2.5.10" | ||
| }, | ||
| "engines": { | ||
| "node": ">=16.0.0" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './lib/command'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { MessageButtonOptions, Constants, LinkButtonOptions } from 'discord.js'; | ||
| import { isPartOfEnum } from './util/enum'; | ||
|
|
||
| const { MessageButtonStyles } = Constants; | ||
|
|
||
| /** | ||
| * Verifies if the given message button options support URL's or not. | ||
|
suneettipirneni marked this conversation as resolved.
Outdated
|
||
| * @param messageButtonOptions The message button options to check. | ||
| * @returns True if the option supports URL's, false otherwise. | ||
|
suneettipirneni marked this conversation as resolved.
Outdated
|
||
| */ | ||
| export function isLinkButtonOptions(buttonData: MessageButtonOptions): buttonData is LinkButtonOptions { | ||
| if (!('style' in buttonData)) { | ||
| throw new TypeError('INVALID_TYPE'); | ||
| } | ||
|
|
||
| return isPartOfEnum(buttonData.style, MessageButtonStyles, ['LINK']); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { | ||
| ApplicationCommandData, | ||
| UserApplicationCommandData, | ||
| MessageApplicationCommandData, | ||
| ApplicationCommandChoicesData, | ||
| ApplicationCommandOptionData, | ||
| ApplicationCommandSubCommandData, | ||
| ApplicationCommandSubGroupData, | ||
| ChatInputApplicationCommandData, | ||
| Constants, | ||
| } from 'discord.js'; | ||
| import { isPartOfEnum, validateType } from './util/enum'; | ||
|
|
||
| const { ApplicationCommandOptionTypes, ApplicationCommandTypes } = Constants; | ||
|
|
||
| /** | ||
| * Whether or not the command supports a description or options. | ||
| * @param commandData The application command data to check. | ||
| * @returns True if the command is a context menu command, false otherwise. | ||
| */ | ||
| export function isContextMenuCommandData( | ||
| commandData: ApplicationCommandData, | ||
| ): commandData is UserApplicationCommandData | MessageApplicationCommandData { | ||
| if (!commandData.type) { | ||
| return false; | ||
| } | ||
|
|
||
| validateType(commandData); | ||
|
|
||
| return isPartOfEnum(commandData.type, ApplicationCommandTypes, ['MESSAGE', 'USER']); | ||
| } | ||
|
|
||
| /** | ||
| * Whether or not the command supports a description or options. | ||
| * @param commandData The application command data to check. | ||
| * @returns True if the command is a chat input command, false otherwise. | ||
| */ | ||
| export function isChatInputCommandData( | ||
| commandData: ApplicationCommandData, | ||
| ): commandData is ChatInputApplicationCommandData { | ||
| return !isContextMenuCommandData(commandData); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies if the given command option data supports choices or not. | ||
| * @param commandOptionData The command option data to check. | ||
| * @returns True if the option supports choices, false otherwise. | ||
| */ | ||
| export function optionDataSupportsChoices( | ||
| optionData: ApplicationCommandOptionData, | ||
| ): optionData is ApplicationCommandChoicesData { | ||
| validateType(optionData); | ||
| return isPartOfEnum(optionData.type, ApplicationCommandOptionTypes, ['INTEGER', 'STRING', 'NUMBER']); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies if the given command option data supports choices or not. | ||
| * @param {ApplicationCommandOptionData} commandOptionData The command option data to check. | ||
| * @returns {boolean} True if the option supports choices, false otherwise. | ||
| */ | ||
| export function optionDataSupportsSubOptions( | ||
| optionData: ApplicationCommandOptionData, | ||
| ): optionData is ApplicationCommandSubGroupData | ApplicationCommandSubCommandData { | ||
| validateType(optionData); | ||
| return isPartOfEnum(optionData.type, ApplicationCommandOptionTypes, ['SUB_COMMAND', 'SUB_COMMAND_GROUP']); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * Resolves a given type to an enum equivalent value, and | ||
| * checks if it's part of the given enum type. | ||
| * @param type The type to resolve | ||
| * @param object The enum to resolve to | ||
| * @param fields The enum fields to check | ||
| * @returns Whether the type is part of the enum or not. | ||
| */ | ||
| export function isPartOfEnum(type: string | number, object: Record<string, unknown>, fields: string[]): boolean { | ||
| const resolvedType = typeof type === 'number' ? type : object[type]; | ||
| return fields.some((field) => object[field] === resolvedType); | ||
| } | ||
|
|
||
| /** | ||
| * Throws a type error if the object has no `type field`. | ||
| * @param object The object to type check. | ||
| * @private | ||
| */ | ||
| export function validateType(object: any) { | ||
| if (!('type' in object)) { | ||
| throw new TypeError('INVALID_TYPE'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "extends": "../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "sourceRoot": "./", | ||
| "rootDir": "./src", | ||
| "outDir": "dist", | ||
| "lib": ["ESNext"] | ||
| }, | ||
| "include": ["src"], | ||
| "references": [{ "path": "../core" }] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.