Skip to content

Commit

Permalink
refactor(files): file data can be much more than buffer (#7238)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Jan 11, 2022
1 parent d8efba2 commit 86ab526
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packages/rest/__tests__/REST.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ test('postFile empty', async () => {
});
});

test('postFile file', async () => {
test('postFile file (string)', async () => {
expect(
await api.post('/postFile', {
files: [{ fileName: 'out.txt', rawBuffer: Buffer.from('Hello') }],
files: [{ fileName: 'out.txt', fileData: 'Hello' }],
}),
).toStrictEqual({
body: [
Expand All @@ -133,7 +133,7 @@ test('postFile file', async () => {
test('postFile file and JSON', async () => {
expect(
await api.post('/postFile', {
files: [{ fileName: 'out.txt', rawBuffer: Buffer.from('Hello') }],
files: [{ fileName: 'out.txt', fileData: Buffer.from('Hello') }],
body: { foo: 'bar' },
}),
).toStrictEqual({
Expand All @@ -153,8 +153,8 @@ test('postFile files and JSON', async () => {
expect(
await api.post('/postFile', {
files: [
{ fileName: 'out.txt', rawBuffer: Buffer.from('Hello') },
{ fileName: 'out.txt', rawBuffer: Buffer.from('Hi') },
{ fileName: 'out.txt', fileData: Buffer.from('Hello') },
{ fileName: 'out.txt', fileData: Buffer.from('Hi') },
],
body: { files: [{ id: 0, description: 'test' }] },
}),
Expand All @@ -178,7 +178,7 @@ test('postFile files and JSON', async () => {
test('postFile sticker and JSON', async () => {
expect(
await api.post('/postFile', {
files: [{ key: 'file', fileName: 'sticker.png', rawBuffer: Buffer.from('Sticker') }],
files: [{ key: 'file', fileName: 'sticker.png', fileData: Buffer.from('Sticker') }],
body: { foo: 'bar' },
appendToFormData: true,
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/rest/src/lib/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface RawFile {
/**
* The actual data for the file
*/
rawBuffer: Buffer;
fileData: string | number | boolean | Buffer;
}

/**
Expand Down Expand Up @@ -280,7 +280,7 @@ export class RequestManager extends EventEmitter {

// Attach all files to the request
for (const [index, file] of request.files.entries()) {
formData.append(file.key ?? `files[${index}]`, file.rawBuffer, file.fileName);
formData.append(file.key ?? `files[${index}]`, file.fileData, file.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 86ab526

Please sign in to comment.