Skip to content

Commit b2ec865

Browse files
suneettipirneniimranbarbhuiyakyranetkodiakhq[bot]
authored
* feat: add @discordjs/util * fix: builders test * refactor: make rest use lazy for ESM import * chore: make requested changes * Apply suggestions from code review Co-authored-by: Parbez <[email protected]> Co-authored-by: A. Román <[email protected]> * chore: make requested changes and add tests * chore: regen lockfile * test: add type tests * chore: push missing files * chore: make requested changes * chore: update CI stuff * chore: fix lockfile * chore: make requested changes Co-authored-by: Parbez <[email protected]> Co-authored-by: A. Román <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 3f86561 commit b2ec865

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1304
-1319
lines changed

.github/labeler.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@
3737
'packages:ws':
3838
- packages/ws/*
3939
- packages/ws/**/*
40+
41+
'packages:util':
42+
- packages/util/*
43+
- packages/util/**/*

.github/labels.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
color: 'fbca04'
6767
- name: 'packages:ws'
6868
color: 'fbca04'
69+
- name: 'packages:util'
70+
color: 'fbca04'
6971
- name: 'performance'
7072
color: '80c042'
7173
- name: 'permissions'

packages/actions/src/uploadCoverage/action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ runs:
5151
files: ./packages/ws/coverage/cobertura-coverage.xml
5252
flags: ws
5353

54+
- name: Upload Util Coverage
55+
uses: codecov/codecov-action@v3
56+
with:
57+
files: ./packages/util/coverage/cobertura-coverage.xml
58+
flags: util
59+
5460
- name: Upload Utilities Coverage
5561
uses: codecov/codecov-action@v3
5662
with:

packages/builders/__tests__/util.test.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,5 @@
11
import { describe, test, expect } from 'vitest';
2-
import {
3-
isJSONEncodable,
4-
isEquatable,
5-
ActionRowBuilder,
6-
enableValidators,
7-
disableValidators,
8-
isValidationEnabled,
9-
} from '../src/index.js';
10-
11-
describe('isEquatable', () => {
12-
test('returns true if the object is equatable', () => {
13-
expect(isEquatable({ equals: () => true })).toBeTruthy();
14-
});
15-
16-
test('returns false if the object is not equatable', () => {
17-
expect(isEquatable({})).toBeFalsy();
18-
});
19-
});
20-
21-
describe('isJSONEncodable', () => {
22-
test('returns true if the object is JSON encodable', () => {
23-
expect(isJSONEncodable({ toJSON: () => ({}) })).toBeTruthy();
24-
expect(isJSONEncodable(new ActionRowBuilder())).toBeTruthy();
25-
});
26-
27-
test('returns false if the object is not JSON encodable', () => {
28-
expect(isJSONEncodable({})).toBeFalsy();
29-
});
30-
});
2+
import { enableValidators, disableValidators, isValidationEnabled } from '../src/index.js';
313

324
describe('validation', () => {
335
test('enables validation', () => {

packages/builders/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
},
5555
"homepage": "https://discord.js.org",
5656
"dependencies": {
57+
"@discordjs/util": "workspace:^",
5758
"@sapphire/shapeshift": "^3.6.0",
5859
"discord-api-types": "^0.37.11",
5960
"fast-deep-equal": "^3.1.3",

packages/builders/src/components/Component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import type { JSONEncodable } from '@discordjs/util';
12
import type {
23
APIActionRowComponent,
34
APIActionRowComponentTypes,
45
APIBaseComponent,
56
ComponentType,
67
} from 'discord-api-types/v10';
7-
import type { JSONEncodable } from '../util/jsonEncodable';
88

99
export type AnyAPIActionRowComponent = APIActionRowComponent<APIActionRowComponentTypes> | APIActionRowComponentTypes;
1010

packages/builders/src/components/selectMenu/SelectMenuOption.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { JSONEncodable } from '@discordjs/util';
12
import type { APIMessageComponentEmoji, APISelectMenuOption } from 'discord-api-types/v10';
2-
import type { JSONEncodable } from '../../util/jsonEncodable.js';
33
import {
44
defaultValidator,
55
emojiValidator,

packages/builders/src/components/textInput/TextInput.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { isJSONEncodable, type Equatable, type JSONEncodable } from '@discordjs/util';
12
import { ComponentType, type TextInputStyle, type APITextInputComponent } from 'discord-api-types/v10';
23
import isEqual from 'fast-deep-equal';
3-
import type { Equatable } from '../../util/equatable';
4-
import { isJSONEncodable, type JSONEncodable } from '../../util/jsonEncodable.js';
54
import { customIdValidator } from '../Assertions.js';
65
import { ComponentBuilder } from '../Component.js';
76
import {

packages/builders/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ export * from './interactions/slashCommands/mixins/SharedSlashCommandOptions.js'
3636
export * as ContextMenuCommandAssertions from './interactions/contextMenuCommands/Assertions.js';
3737
export * from './interactions/contextMenuCommands/ContextMenuCommandBuilder.js';
3838

39-
export * from './util/jsonEncodable.js';
40-
export * from './util/equatable.js';
4139
export * from './util/componentUtil.js';
4240
export * from './util/normalizeArray.js';
4341
export * from './util/validation.js';
42+
export * from '@discordjs/util';

packages/builders/src/interactions/modals/Modal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { JSONEncodable } from '@discordjs/util';
12
import type {
23
APIActionRowComponent,
34
APIModalActionRowComponent,
@@ -6,7 +7,6 @@ import type {
67
import { ActionRowBuilder, type ModalActionRowComponentBuilder } from '../../components/ActionRow.js';
78
import { customIdValidator } from '../../components/Assertions.js';
89
import { createComponentBuilder } from '../../components/Components.js';
9-
import type { JSONEncodable } from '../../util/jsonEncodable';
1010
import { normalizeArray, type RestOrArray } from '../../util/normalizeArray.js';
1111
import { titleValidator, validateRequiredParameters } from './Assertions.js';
1212

packages/discord.js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@discordjs/builders": "workspace:^",
5353
"@discordjs/collection": "workspace:^",
5454
"@discordjs/rest": "workspace:^",
55+
"@discordjs/util": "workspace:^",
5556
"@sapphire/snowflake": "^3.2.2",
5657
"@types/ws": "^8.5.3",
5758
"discord-api-types": "^0.37.11",

packages/discord.js/src/structures/ContextMenuCommandInteraction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3+
const { lazy } = require('@discordjs/util');
34
const { ApplicationCommandOptionType } = require('discord-api-types/v10');
45
const CommandInteraction = require('./CommandInteraction');
56
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
6-
const { lazy } = require('../util/Util');
77

88
const getMessage = lazy(() => require('./Message').Message);
99

packages/discord.js/src/structures/MessageComponentInteraction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3+
const { lazy } = require('@discordjs/util');
34
const BaseInteraction = require('./BaseInteraction');
45
const InteractionWebhook = require('./InteractionWebhook');
56
const InteractionResponses = require('./interfaces/InteractionResponses');
6-
const { lazy } = require('../util/Util');
77

88
const getMessage = lazy(() => require('./Message').Message);
99

packages/discord.js/src/structures/MessagePayload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
const { Buffer } = require('node:buffer');
44
const { isJSONEncodable } = require('@discordjs/builders');
5+
const { lazy } = require('@discordjs/util');
56
const { MessageFlags } = require('discord-api-types/v10');
67
const ActionRowBuilder = require('./ActionRowBuilder');
78
const { RangeError, ErrorCodes } = require('../errors');
89
const DataResolver = require('../util/DataResolver');
910
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
10-
const { basename, verifyString, lazy } = require('../util/Util');
11+
const { basename, verifyString } = require('../util/Util');
1112

1213
const getBaseInteraction = lazy(() => require('./BaseInteraction'));
1314

packages/discord.js/src/structures/ModalSubmitInteraction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3+
const { lazy } = require('@discordjs/util');
34
const BaseInteraction = require('./BaseInteraction');
45
const InteractionWebhook = require('./InteractionWebhook');
56
const ModalSubmitFields = require('./ModalSubmitFields');
67
const InteractionResponses = require('./interfaces/InteractionResponses');
7-
const { lazy } = require('../util/Util');
88

99
const getMessage = lazy(() => require('./Message').Message);
1010

packages/discord.js/src/structures/Webhook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

33
const { makeURLSearchParams } = require('@discordjs/rest');
4+
const { lazy } = require('@discordjs/util');
45
const { DiscordSnowflake } = require('@sapphire/snowflake');
56
const { Routes, WebhookType } = require('discord-api-types/v10');
67
const MessagePayload = require('./MessagePayload');
78
const { Error, ErrorCodes } = require('../errors');
89
const DataResolver = require('../util/DataResolver');
9-
const { lazy } = require('../util/Util');
1010

1111
const getMessage = lazy(() => require('./Message').Message);
1212

packages/discord.js/src/util/Channels.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3+
const { lazy } = require('@discordjs/util');
34
const { ChannelType } = require('discord-api-types/v10');
4-
const { lazy } = require('./Util');
55

66
const getCategoryChannel = lazy(() => require('../structures/CategoryChannel'));
77
const getDMChannel = lazy(() => require('../structures/DMChannel'));

packages/discord.js/src/util/Util.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -509,16 +509,6 @@ function cleanCodeBlockContent(text) {
509509
return text.replaceAll('```', '`\u200b``');
510510
}
511511

512-
/**
513-
* Lazily evaluates a callback function
514-
* @param {Function} cb The callback to lazily evaluate
515-
* @returns {Function}
516-
*/
517-
function lazy(cb) {
518-
let defaultValue;
519-
return () => (defaultValue ??= cb());
520-
}
521-
522512
/**
523513
* Parses a webhook URL for the id and token.
524514
* @param {string} url The URL to parse
@@ -562,7 +552,6 @@ module.exports = {
562552
basename,
563553
cleanContent,
564554
cleanCodeBlockContent,
565-
lazy,
566555
parseWebhookURL,
567556
};
568557

packages/discord.js/typings/index.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
hyperlink,
1313
inlineCode,
1414
italic,
15-
JSONEncodable,
1615
quote,
1716
roleMention,
1817
SelectMenuBuilder as BuilderSelectMenuComponent,
@@ -31,6 +30,7 @@ import {
3130
ComponentBuilder,
3231
type RestOrArray,
3332
} from '@discordjs/builders';
33+
import { Awaitable, JSONEncodable } from '@discordjs/util';
3434
import { Collection } from '@discordjs/collection';
3535
import { BaseImageURLOptions, ImageURLOptions, RawFile, REST, RESTOptions } from '@discordjs/rest';
3636
import {
@@ -4144,8 +4144,6 @@ export interface AuditLogChange {
41444144
new?: APIAuditLogChange['new_value'];
41454145
}
41464146

4147-
export type Awaitable<T> = T | PromiseLike<T>;
4148-
41494147
export type AwaitMessageComponentOptions<T extends CollectedMessageInteraction> = Omit<
41504148
MessageComponentCollectorOptions<T>,
41514149
'max' | 'maxComponents' | 'maxUsers'
@@ -5835,3 +5833,4 @@ export type InternalDiscordGatewayAdapterCreator = (
58355833
export * from 'discord-api-types/v10';
58365834
export * from '@discordjs/builders';
58375835
export * from '@discordjs/rest';
5836+
export * from '@discordjs/util';

packages/proxy/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"homepage": "https://discord.js.org",
5656
"dependencies": {
5757
"@discordjs/rest": "^1.0.0",
58+
"@discordjs/util": "workspace:^",
5859
"tslib": "^2.4.0",
5960
"undici": "^5.10.0"
6061
},

packages/proxy/src/util/util.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import type { IncomingMessage, ServerResponse } from 'node:http';
2-
3-
/**
4-
* Represents a potentially awaitable value
5-
*/
6-
export type Awaitable<T> = PromiseLike<T> | T;
2+
import type { Awaitable } from '@discordjs/util';
73

84
/**
95
* Represents a simple HTTP request handler

packages/rest/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"homepage": "https://discord.js.org",
5454
"dependencies": {
5555
"@discordjs/collection": "workspace:^",
56+
"@discordjs/util": "workspace:^",
5657
"@sapphire/async-queue": "^1.5.0",
5758
"@sapphire/snowflake": "^3.2.2",
5859
"discord-api-types": "^0.37.11",

packages/rest/src/lib/RequestManager.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EventEmitter } from 'node:events';
33
import { setInterval, clearInterval } from 'node:timers';
44
import type { URLSearchParams } from 'node:url';
55
import { Collection } from '@discordjs/collection';
6+
import { lazy } from '@discordjs/util';
67
import { DiscordSnowflake } from '@sapphire/snowflake';
78
import { FormData, type RequestInit, type BodyInit, type Dispatcher, type Agent } from 'undici';
89
import type { RESTOptions, RestEvents, RequestOptions } from './REST.js';
@@ -12,12 +13,7 @@ import { DefaultRestOptions, DefaultUserAgent, RESTEvents } from './utils/consta
1213
import { resolveBody } from './utils/utils.js';
1314

1415
// Make this a lazy dynamic import as file-type is a pure ESM package
15-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
16-
const getFileType = async (): Promise<typeof import('file-type')> => {
17-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
18-
let cached: Promise<typeof import('file-type')>;
19-
return (cached ??= import('file-type'));
20-
};
16+
const getFileType = lazy(async () => import('file-type'));
2117

2218
/**
2319
* Represents a file to be added to the request

packages/util/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../.eslintrc.json"
3+
}

packages/util/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Packages
2+
node_modules/
3+
4+
# Log files
5+
logs/
6+
*.log
7+
npm-debug.log*
8+
9+
# Runtime data
10+
pids
11+
*.pid
12+
*.seed
13+
14+
# Env
15+
.env
16+
17+
# Dist
18+
dist/
19+
typings/
20+
21+
docs/**/*
22+
23+
# Miscellaneous
24+
.tmp/
25+
coverage/

packages/util/.lintstagedrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../.lintstagedrc.json');

packages/util/.prettierignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Autogenerated
2+
CHANGELOG.md
3+
.turbo
4+
dist/
5+
docs/**/*
6+
!docs/index.yml
7+
!docs/README.md
8+
coverage/

packages/util/.prettierrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../.prettierrc.json');

0 commit comments

Comments
 (0)