Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structures #76

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@typescript-eslint/no-redeclare": "off",
"@typescript-eslint/no-unnecessary-type-assertion": "off",
"@typescript-eslint/no-redundant-type-constituents": "off",
"@typescript-eslint/require-await": "off",
"no-empty": ["warn", { "allowEmptyCatch": true }]
}
}
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"editor.tabSize": 2,
"explorer.fileNesting.enabled": true,
"explorer.fileNesting.patterns": {
"*.ts": "${capture}.js",
"*.js": "${capture}.js.map, ${capture}.min.js, ${capture}.d.ts",
"*.jsx": "${capture}.js",
"*.tsx": "${capture}.ts",
"*.ts": "",
"*.js": "",
"*.jsx": "",
"*.tsx": "",
"tsconfig.json": "tsconfig.*.json",
"package.json": "pnpm-*.yaml",
"README.md": "*.md"
Expand Down
4 changes: 2 additions & 2 deletions packages/polyfill/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@purplet/polyfill",
"version": "1.2.0",
"version": "1.2.1",
"description": "Polyfills for node.js and bun.js",
"keywords": [
"polyfill",
Expand All @@ -26,7 +26,7 @@
},
"main": "./src/index.js",
"files": [
"src/index.js",
"src",
"CHANGELOG.md"
],
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/purplet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
"@paperdave/logger": "^2.3.0",
"@paperdave/utils": "1.5.0",
"@purplet/gateway": "workspace:*",
"@purplet/polyfill": "workspace:1.1.0",
"@purplet/polyfill": "workspace:*",
"@purplet/rest": "workspace:*",
"@purplet/serialize": "workspace:*",
"@purplet/utils": "workspace:1.0.0",
"@purplet/utils": "workspace:*",
"@rollup/plugin-alias": "^4.0.0",
"@rollup/plugin-commonjs": "^23.0.1",
"@rollup/plugin-node-resolve": "^15.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/purplet/pnpm-lock.yaml

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

15 changes: 5 additions & 10 deletions packages/purplet/src/structures/interaction/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import type {
APIInteractionDataResolvedChannel,
APIInteractionDataResolvedGuildMember,
APIRole,
Snowflake} from 'purplet/types';
import {
InteractionType
Snowflake,
} from 'purplet/types';
import { InteractionType } from 'purplet/types';
import { Interaction } from './base';
import type {
InteractionResponseMixin} from './response';
import {
applyInteractionResponseMixins,
createInteractionMixinList
} from './response';
import type { InteractionResponseMixin } from './response';
import { applyInteractionResponseMixins, createInteractionMixinList } from './response';
import { EmptyTextChannel } from '../channel';
import { Message } from '../message';
import { PartialUser } from '../user';
Expand Down Expand Up @@ -79,4 +74,4 @@ const allowedMethods = createInteractionMixinList([
]);

applyInteractionResponseMixins(CommandInteraction, allowedMethods);
export type CommandInteraction = InteractionResponseMixin<typeof allowedMethods>
export type CommandInteraction = InteractionResponseMixin<typeof allowedMethods>;
9 changes: 2 additions & 7 deletions packages/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bun add @purplet/rest discord-api-types

## Polyfills

Until v18, node.js does not support `fetch`, and no version of node or bun has `FormData` built-in. We provide another package, `@purplet/polyfill` which polyfills these APIs for you:
Until v18, node.js does not support `fetch` and `FormData`, and bun has yet to implement `FormData`. We provide another package, `@purplet/polyfill` which polyfills these APIs for you:

```ts
import '@purplet/polyfill';
Expand All @@ -70,9 +70,4 @@ We generate most of our library's route metadata off of these GitHub repositorie
- [discord-api-docs](https://github.com/discord/discord-api-docs)
- [discord-api-types](https://github.com/discordjs/discord-api-types)

We do apply some patches of our own, but missing types should be contributed to the above repositories instead of as extra overrides to us. The following routes have buggy or incorrect types:

- Some thread actions are missing. ([types#526](https://github.com/discordjs/discord-api-types/issues/526)) / [purplet#50](https://github.com/CRBT-Team/Purplet/issues/50))

- `channel.listPublicArchivedThreads`
- `channel.listPrivateArchivedThreads`
We do apply some patches of our own, but missing types should be contributed to the above repositories instead of as extra overrides to us.
7 changes: 4 additions & 3 deletions packages/rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@purplet/rest",
"version": "1.1.0",
"version": "1.2.0",
"description": "Cross-runtime Discord Rest client",
"keywords": [
"discord",
Expand Down Expand Up @@ -35,10 +35,11 @@
},
"dependencies": {
"@paperdave/utils": "1.5.0",
"@purplet/shared-config": "workspace:*",
"@purplet/utils": "workspace:^1.0.0"
"@purplet/utils": "workspace:^1.0.0",
"utils:workspace": "link:@purplet/utils:workspace"
},
"devDependencies": {
"@purplet/shared-config": "workspace:*",
"discord-api-types": "^0.37.14"
},
"peerDependencies": {
Expand Down
61 changes: 3 additions & 58 deletions packages/rest/pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions packages/rest/scripts/gen-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const overrides = {
listVoiceRegions: 'Discord.RESTGetAPIGuildVoiceRegions',
createWebhook: 'Discord.RESTPostAPIChannelWebhook',
listJoinedPrivateArchivedThreads: 'Discord.RESTGetAPIChannelUsersThreadsArchived',
listPublicArchivedThreads: 'Discord.RESTGetAPIChannelUsersThreadsArchived',
listPrivateArchivedThreads: 'Discord.RESTGetAPIChannelUsersThreadsArchived',
createAutoModerationRule: 'Discord.RESTPostAPIAutoModerationRule',
},
body: {
Expand All @@ -90,6 +92,8 @@ const overrides = {
editWebhookMessage: '{ thread_id?: boolean }',
deleteWebhookMessage: '{ thread_id?: boolean }',
listJoinedPrivateArchivedThreads: '{ limit?: number; before?: string; }',
listPublicArchivedThreads: '{ limit?: number; before?: string; }',
listPrivateArchivedThreads: '{ limit?: number; before?: string; }',
},
result: {
modifyCurrentMember: 'Discord.RESTGetAPIGuildMemberResult',
Expand Down
6 changes: 4 additions & 2 deletions packages/rest/src/Fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class Fetcher {
#globalCount = 0;
#globalRateLimited?: QueueEntry[];

constructor(private readonly fetch: typeof globalThis.fetch) {}

async queue<Output>(request: RequestData) {
const { endpointId, majorId } = classifyEndpoint(
/^\/api(?:\/v\d+)?(\/.*)/.exec(request.url.pathname)![1],
Expand Down Expand Up @@ -77,7 +79,7 @@ export class Fetcher {
this.runRequest(entry).catch(reject);
}

return await promise;
return promise;
}

private setBucketTimer(bucket: Bucket, subBucket: SubBucket) {
Expand Down Expand Up @@ -136,7 +138,7 @@ export class Fetcher {
const subBucket = bucket.sub.get(entry.majorId)!;

const req = entry.request;
const response = await fetch(req.url.toString(), req.init);
const response = await this.fetch(req.url.toString(), req.init);

const serverNow = response.headers.has('Date')
? new Date(response.headers.get('Date')!).getTime()
Expand Down
3 changes: 2 additions & 1 deletion packages/rest/src/Rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Rest {
base: API_BASE_URL,
tokenType: 'Bot',
userAgent: defaultUserAgent,
fetch,
...options,
};

Expand All @@ -47,7 +48,7 @@ export class Rest {
this.options.base = this.options.base.slice(0, -versionSuffix.length);
}

this.fetcher = new Fetcher();
this.fetcher = new Fetcher(this.options.fetch);
}

setToken(token: string, tokenType: TokenType = 'Bot') {
Expand Down
Loading