@discordeno/rest 19.0.0-next.7a69c02 → 19.0.0-next.7b5d99e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAmEA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;AAKrH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CAmsDhF"}
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAiEA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;AAKrH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CA2pDhF"}
package/dist/manager.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable no-const-assign */ import { InteractionResponseTypes } from '@discordeno/types';
2
- import { calculateBits, camelize, camelToSnakeCase, delay, encode, getBotIdFromToken, isGetMessagesAfter, isGetMessagesAround, isGetMessagesBefore, isGetMessagesLimit, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
2
+ import { calculateBits, camelize, camelToSnakeCase, delay, getBotIdFromToken, isGetMessagesAfter, isGetMessagesAround, isGetMessagesBefore, isGetMessagesLimit, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
3
3
  import fetch from 'node-fetch';
4
4
  import { createInvalidRequestBucket } from './invalidBucket.js';
5
5
  import { Queue } from './queue.js';
@@ -754,7 +754,7 @@ export function createRestManager(options) {
754
754
  }
755
755
  return parts.join('/');
756
756
  },
757
- processRequest (request) {
757
+ async processRequest (request) {
758
758
  const route = request.url.substring(request.url.indexOf('api/'));
759
759
  const parts = route.split('/');
760
760
  // Remove the api/
@@ -764,6 +764,10 @@ export function createRestManager(options) {
764
764
  // Set the full url to discord api in case it was recieved in a proxy rest
765
765
  request.url = `${rest.baseUrl}/v${rest.version}/${parts.join('/')}`;
766
766
  const url = rest.simplifyUrl(request.url, request.method);
767
+ if (request.runThroughQueue === false) {
768
+ await rest.sendRequest(request);
769
+ return;
770
+ }
767
771
  const queue = rest.queues.get(url);
768
772
  if (queue !== undefined) {
769
773
  queue.makeRequest(request);
@@ -781,34 +785,7 @@ export function createRestManager(options) {
781
785
  },
782
786
  async makeRequest (method, url, options) {
783
787
  if (!rest.baseUrl.startsWith('https://discord.com') && url[0] === '/') {
784
- // Special handling for sending blobs across http to proxy
785
- // TODO: fix this hacky handling
786
- if (!(options?.body instanceof FormData) && !Array.isArray(options?.body) && options?.body?.file) {
787
- if (!Array.isArray(options.body.file)) {
788
- options.body.file = [
789
- options.body.file
790
- ];
791
- }
792
- // convert blobs to string before sending to proxy
793
- options.body.file = await Promise.all(options.body.file.map(async (f)=>{
794
- const url = encode(await f.blob.arrayBuffer());
795
- return {
796
- name: f.name,
797
- blob: `data:${f.blob.type};base64,${url}`
798
- };
799
- }));
800
- }
801
- const headers = {
802
- Authorization: rest.authorization ?? ''
803
- };
804
- if (options?.body) {
805
- headers['Content-Type'] = 'application/json';
806
- }
807
- const result = await fetch(`${rest.baseUrl}${url}`, {
808
- body: options?.body ? JSON.stringify(options.body) : undefined,
809
- headers,
810
- method
811
- });
788
+ const result = await fetch(`${rest.baseUrl}${url}`, rest.createRequestBody(method, options));
812
789
  if (!result.ok) {
813
790
  const err = await result.json().catch(()=>{});
814
791
  // Legacy Handling to not break old code or when body is missing
@@ -817,21 +794,22 @@ export function createRestManager(options) {
817
794
  }
818
795
  return result.status !== 204 ? await result.json() : undefined;
819
796
  }
820
- return await new Promise((resolve, reject)=>{
797
+ return await new Promise(async (resolve, reject)=>{
821
798
  const payload = {
822
799
  url,
823
800
  method,
824
801
  requestBodyOptions: options,
825
802
  retryCount: 0,
826
803
  retryRequest: async function(payload) {
827
- rest.processRequest(payload);
804
+ await rest.processRequest(payload);
828
805
  },
829
806
  resolve: (data)=>{
830
807
  resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
831
808
  },
832
- reject
809
+ reject,
810
+ runThroughQueue: options?.runThroughQueue
833
811
  };
834
- rest.processRequest(payload);
812
+ await rest.processRequest(payload);
835
813
  });
836
814
  },
837
815
  async get (url, options) {
@@ -990,7 +968,9 @@ export function createRestManager(options) {
990
968
  });
991
969
  },
992
970
  async deleteFollowupMessage (token, messageId) {
993
- await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId));
971
+ await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
972
+ unauthorized: true
973
+ });
994
974
  },
995
975
  async deleteGlobalApplicationCommand (commandId) {
996
976
  await rest.delete(rest.routes.interactions.commands.command(rest.applicationId, commandId));
@@ -1033,7 +1013,9 @@ export function createRestManager(options) {
1033
1013
  });
1034
1014
  },
1035
1015
  async deleteOriginalInteractionResponse (token) {
1036
- await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token));
1016
+ await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token), {
1017
+ unauthorized: true
1018
+ });
1037
1019
  },
1038
1020
  async deleteOwnReaction (channelId, messageId, reaction) {
1039
1021
  reaction = processReactionString(reaction);
@@ -1125,7 +1107,8 @@ export function createRestManager(options) {
1125
1107
  async editFollowupMessage (token, messageId, body) {
1126
1108
  return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
1127
1109
  body,
1128
- files: body.files
1110
+ files: body.files,
1111
+ unauthorized: true
1129
1112
  });
1130
1113
  },
1131
1114
  async editGlobalApplicationCommand (commandId, body) {
@@ -1180,7 +1163,8 @@ export function createRestManager(options) {
1180
1163
  type: InteractionResponseTypes.UpdateMessage,
1181
1164
  data: options
1182
1165
  },
1183
- files: options.files
1166
+ files: options.files,
1167
+ unauthorized: true
1184
1168
  });
1185
1169
  },
1186
1170
  async editOwnVoiceState (guildId, options) {
@@ -1319,7 +1303,9 @@ export function createRestManager(options) {
1319
1303
  return await rest.get(rest.routes.guilds.emojis(guildId));
1320
1304
  },
1321
1305
  async getFollowupMessage (token, messageId) {
1322
- return await rest.get(rest.routes.interactions.responses.message(rest.applicationId, token, messageId));
1306
+ return await rest.get(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
1307
+ unauthorized: true
1308
+ });
1323
1309
  },
1324
1310
  async getGatewayBot () {
1325
1311
  return await rest.get(rest.routes.gatewayBot());
@@ -1372,7 +1358,9 @@ export function createRestManager(options) {
1372
1358
  return await rest.get(rest.routes.nitroStickerPacks());
1373
1359
  },
1374
1360
  async getOriginalInteractionResponse (token) {
1375
- return await rest.get(rest.routes.interactions.responses.original(rest.applicationId, token));
1361
+ return await rest.get(rest.routes.interactions.responses.original(rest.applicationId, token), {
1362
+ unauthorized: true
1363
+ });
1376
1364
  },
1377
1365
  async getPinnedMessages (channelId) {
1378
1366
  return await rest.get(rest.routes.channels.pins(channelId));
@@ -1472,47 +1460,19 @@ export function createRestManager(options) {
1472
1460
  async removeThreadMember (channelId, userId) {
1473
1461
  await rest.delete(rest.routes.channels.threads.user(channelId, userId));
1474
1462
  },
1475
- // TODO: why that
1476
1463
  async sendFollowupMessage (token, options) {
1477
- return await new Promise((resolve, reject)=>{
1478
- rest.sendRequest({
1479
- url: rest.routes.webhooks.webhook(rest.applicationId, token),
1480
- method: 'POST',
1481
- requestBodyOptions: {
1482
- body: options,
1483
- files: options.files
1484
- },
1485
- retryCount: 0,
1486
- retryRequest: async function(options) {
1487
- // TODO: should change to reprocess queue item
1488
- await rest.sendRequest(options);
1489
- },
1490
- resolve: (data)=>{
1491
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1492
- },
1493
- reject
1494
- });
1464
+ return await rest.post(rest.routes.webhooks.webhook(rest.applicationId, token), {
1465
+ body: options,
1466
+ files: options.files,
1467
+ unauthorized: true
1495
1468
  });
1496
1469
  },
1497
- // TODO: why that
1498
1470
  async sendInteractionResponse (interactionId, token, options) {
1499
- await new Promise((resolve, reject)=>{
1500
- rest.sendRequest({
1501
- url: rest.routes.interactions.responses.callback(interactionId, token),
1502
- method: 'POST',
1503
- requestBodyOptions: {
1504
- body: options
1505
- },
1506
- retryCount: 0,
1507
- retryRequest: async function(options) {
1508
- // TODO: should change to reprocess queue item
1509
- await rest.sendRequest(options);
1510
- },
1511
- resolve: (data)=>{
1512
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1513
- },
1514
- reject
1515
- });
1471
+ return await rest.post(rest.routes.interactions.responses.callback(interactionId, token), {
1472
+ body: options,
1473
+ files: options.data?.files,
1474
+ runThroughQueue: false,
1475
+ unauthorized: true
1516
1476
  });
1517
1477
  },
1518
1478
  async sendMessage (channelId, body) {