types(core): use result types instead of direct types (#11140)

* types(core): use result types instead of direct types

fix: import pains

chore: apply suggestion from review

Co-authored-by: Almeida <github@almeidx.dev>

chore: fmt script

chore: requested change

* chore: rename script, fix build

* chore: types

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Vlad Frangu
2025-10-24 18:36:54 +03:00
committed by GitHub
parent a990eefa31
commit 0c1ff5ea29
3 changed files with 35 additions and 6 deletions

View File

@@ -1,7 +1,7 @@
import { Routes } from 'discord-api-types/v10'; import { Routes } from 'discord-api-types/v10';
import { glob, readFile } from 'node:fs/promises'; import { glob, readFile } from 'node:fs/promises';
const usedRoutes = new Set(); const usedRoutes = new Set<string>();
const ignoredRoutes = new Set([ const ignoredRoutes = new Set([
// Deprecated // Deprecated
@@ -13,12 +13,14 @@ const ignoredRoutes = new Set([
'nitroStickerPacks', 'nitroStickerPacks',
]); ]);
for await (const file of glob('src/api/*.ts')) { const cwd = new URL('../src/api/', import.meta.url);
const content = await readFile(file, 'utf-8');
for await (const file of glob('**/*.ts', { cwd })) {
const content = await readFile(new URL(file, cwd), 'utf-8');
const routes = content.matchAll(/Routes\.([\w\d_]+)/g); const routes = content.matchAll(/Routes\.([\w\d_]+)/g);
for (const route of routes) { for (const route of routes) {
usedRoutes.add(route[1]); usedRoutes.add(route[1]!);
} }
} }

View File

@@ -0,0 +1,28 @@
import { glob, readFile } from 'node:fs/promises';
const cwd = new URL('../src/api/', import.meta.url);
const results: string[] = [];
for await (const file of glob('**/*.ts', { cwd })) {
const content = await readFile(new URL(file, cwd), { encoding: 'utf-8' });
const matches = content.matchAll(/as Promise<(?<returnType>\w+)>/g);
for (const match of matches) {
const returnType = match.groups!.returnType!;
if (!returnType.startsWith('REST') || !returnType.includes('Result')) {
results.push(`in file core/src/api/${file}: ${returnType}`);
}
}
}
if (results.length > 0) {
console.warn('Found return types that are not REST return types:');
for (const result of results) {
console.warn(` - ${result}`);
}
} else {
console.log('No return types that are not REST return types found');
}

View File

@@ -3,7 +3,6 @@
import { makeURLSearchParams, type RawFile, type RequestData, type REST } from '@discordjs/rest'; import { makeURLSearchParams, type RawFile, type RequestData, type REST } from '@discordjs/rest';
import { import {
Routes, Routes,
type APIThreadChannel,
type RESTDeleteAPIChannelResult, type RESTDeleteAPIChannelResult,
type RESTGetAPIChannelInvitesResult, type RESTGetAPIChannelInvitesResult,
type RESTGetAPIChannelMessageReactionUsersQuery, type RESTGetAPIChannelMessageReactionUsersQuery,
@@ -566,7 +565,7 @@ export class ChannelsAPI {
body, body,
reason, reason,
signal, signal,
}) as Promise<APIThreadChannel>; }) as Promise<RESTPostAPIChannelThreadsResult>;
} }
/** /**