Skip to content

Commit

Permalink
chore(RequestManager): use new file upload format
Browse files Browse the repository at this point in the history
Ported from discordjs/discord.js#6871

Co-authored-by: Jake Ward <geek@gaminggeek.dev>
  • Loading branch information
ckohen and GamingGeek committed Dec 7, 2021
1 parent c22081d commit 3b9eca2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
32 changes: 29 additions & 3 deletions packages/rest/__tests__/REST.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
.post('/postEcho')
.reply(200, (_, body) => body)
.post('/postAttachment')
.times(4)
.times(5)
.reply(200, (_, body) => ({
body: body
.replace(/\r\n/g, '\n')
Expand Down Expand Up @@ -116,7 +116,7 @@ test('postAttachment attachment', async () => {
}),
).toStrictEqual({
body: [
'Content-Disposition: form-data; name="out.txt"; filename="out.txt"',
'Content-Disposition: form-data; name="files[0]"; filename="out.txt"',
'Content-Type: text/plain',
'',
'Hello',
Expand All @@ -132,7 +132,7 @@ test('postAttachment attachment and JSON', async () => {
}),
).toStrictEqual({
body: [
'Content-Disposition: form-data; name="out.txt"; filename="out.txt"',
'Content-Disposition: form-data; name="files[0]"; filename="out.txt"',
'Content-Type: text/plain',
'',
'Hello',
Expand All @@ -143,6 +143,32 @@ test('postAttachment attachment and JSON', async () => {
});
});

test('postAttachment attachments and JSON', async () => {
expect(
await api.post('/postAttachment', {
attachments: [
{ fileName: 'out.txt', rawBuffer: Buffer.from('Hello') },
{ fileName: 'out.txt', rawBuffer: Buffer.from('Hi') },
],
body: { attachments: [{ id: 0, description: 'test' }] },
}),
).toStrictEqual({
body: [
'Content-Disposition: form-data; name="files[0]"; filename="out.txt"',
'Content-Type: text/plain',
'',
'Hello',
'Content-Disposition: form-data; name="files[1]"; filename="out.txt"',
'Content-Type: text/plain',
'',
'Hi',
'Content-Disposition: form-data; name="payload_json"',
'',
'{"attachments":[{"id":0,"description":"test"}]}',
].join('\n'),
});
});

test('postAttachment sticker and JSON', async () => {
expect(
await api.post('/postAttachment', {
Expand Down
8 changes: 5 additions & 3 deletions packages/rest/src/lib/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export interface RawAttachment {
*/
fileName: string;
/**
* A key to use for the name of the formdata field for this attachment, otherwise the name is used.
* An explicit key to use for key of the formdata field for this attachment.
* When not provided, the index of the file in the attachments array is used in the form `files[${index}]`.
* If you wish to alter the placeholder snowflake, you must provide this property in the same form (`files[${placeholder}]`)
*/
key?: string;
/**
Expand Down Expand Up @@ -271,8 +273,8 @@ export class RequestManager extends EventEmitter {
const formData = new FormData();

// Attach all files to the request
for (const attachment of request.attachments) {
formData.append(attachment.key ?? attachment.fileName, attachment.rawBuffer, attachment.fileName);
for (const [index, attachment] of request.attachments.entries()) {
formData.append(attachment.key ?? `files[${index}]`, attachment.rawBuffer, attachment.fileName);
}

// If a JSON body was added as well, attach it to the form data, using payload_json unless otherwise specified
Expand Down

0 comments on commit 3b9eca2

Please sign in to comment.