Skip to content

Commit 86ab526

Browse files
authored
refactor(files): file data can be much more than buffer (#7238)
1 parent d8efba2 commit 86ab526

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

packages/rest/__tests__/REST.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ test('postFile empty', async () => {
115115
});
116116
});
117117

118-
test('postFile file', async () => {
118+
test('postFile file (string)', async () => {
119119
expect(
120120
await api.post('/postFile', {
121-
files: [{ fileName: 'out.txt', rawBuffer: Buffer.from('Hello') }],
121+
files: [{ fileName: 'out.txt', fileData: 'Hello' }],
122122
}),
123123
).toStrictEqual({
124124
body: [
@@ -133,7 +133,7 @@ test('postFile file', async () => {
133133
test('postFile file and JSON', async () => {
134134
expect(
135135
await api.post('/postFile', {
136-
files: [{ fileName: 'out.txt', rawBuffer: Buffer.from('Hello') }],
136+
files: [{ fileName: 'out.txt', fileData: Buffer.from('Hello') }],
137137
body: { foo: 'bar' },
138138
}),
139139
).toStrictEqual({
@@ -153,8 +153,8 @@ test('postFile files and JSON', async () => {
153153
expect(
154154
await api.post('/postFile', {
155155
files: [
156-
{ fileName: 'out.txt', rawBuffer: Buffer.from('Hello') },
157-
{ fileName: 'out.txt', rawBuffer: Buffer.from('Hi') },
156+
{ fileName: 'out.txt', fileData: Buffer.from('Hello') },
157+
{ fileName: 'out.txt', fileData: Buffer.from('Hi') },
158158
],
159159
body: { files: [{ id: 0, description: 'test' }] },
160160
}),
@@ -178,7 +178,7 @@ test('postFile files and JSON', async () => {
178178
test('postFile sticker and JSON', async () => {
179179
expect(
180180
await api.post('/postFile', {
181-
files: [{ key: 'file', fileName: 'sticker.png', rawBuffer: Buffer.from('Sticker') }],
181+
files: [{ key: 'file', fileName: 'sticker.png', fileData: Buffer.from('Sticker') }],
182182
body: { foo: 'bar' },
183183
appendToFormData: true,
184184
}),

packages/rest/src/lib/RequestManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface RawFile {
2828
/**
2929
* The actual data for the file
3030
*/
31-
rawBuffer: Buffer;
31+
fileData: string | number | boolean | Buffer;
3232
}
3333

3434
/**
@@ -280,7 +280,7 @@ export class RequestManager extends EventEmitter {
280280

281281
// Attach all files to the request
282282
for (const [index, file] of request.files.entries()) {
283-
formData.append(file.key ?? `files[${index}]`, file.rawBuffer, file.fileName);
283+
formData.append(file.key ?? `files[${index}]`, file.fileData, file.fileName);
284284
}
285285

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

0 commit comments

Comments
 (0)