Skip to content

Commit b4bb458

Browse files
authored
Merge pull request #5 from trojs/feature/first-version
Handle empty file uploads
2 parents 29973a7 + f2e9f49 commit b4bb458

5 files changed

Lines changed: 18 additions & 20 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@trojs/formdata-parser",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"description": "Parse the form data",
55
"type": "module",
66
"main": "src/form-data.js",

src/form-data.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55

66
/**
77
* @param {string} file
8-
* @returns {string}
8+
* @returns {string=}
99
*/
1010
const getFileName = (file) => {
1111
const fileName = file.split(`filename="`)
1212
if (fileName.length > 1) {
13-
return fileName[1].split(`"\r\n`)[0]
13+
return fileName[1].split('"')[0]
1414
}
15-
throw new Error('No file name')
15+
return undefined
1616
}
1717

1818
/**
1919
* @param {string} file
20-
* @returns {string}
20+
* @returns {string=}
2121
*/
2222
const getFileData = (file) => {
2323
const fileData = file.split(`\r\n\r\n`)
2424
if (fileData.length > 1) {
2525
return fileData[1].split(`\r\n`)[0]
2626
}
27-
throw new Error('No file data')
27+
return undefined
2828
}
2929

3030
/**
@@ -34,20 +34,20 @@ const getFileData = (file) => {
3434
const getField = (file) => {
3535
const fieldName = file.split(`name="`)
3636
if (fieldName.length > 1) {
37-
return fieldName[1].split(`";`)[0]
37+
return fieldName[1].split(`"`)[0]
3838
}
3939
throw new Error('No field')
4040
}
4141
/**
4242
* @param {string} file
43-
* @returns {string}
43+
* @returns {string=}
4444
*/
4545
const getContentType = (file) => {
4646
const contentType = file.split(`Content-Type: `)
4747
if (contentType.length > 1) {
4848
return contentType[1].split(`\r\n`)[0]
4949
}
50-
throw new Error('No content type')
50+
return undefined
5151
}
5252

5353
/**

src/form-data.test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ const example = {
99
'42\n' +
1010
'\r\n' +
1111
'-----------------------------12946965154256166883262710838\r\n' +
12-
'Content-Disposition: form-data; name="fileName2"; filename="test.xml"\r\n' +
13-
'Content-Type: text/xml\r\n' +
12+
'Content-Disposition: form-data; name="fileName2"\r\n' +
1413
'\r\n' +
15-
'43\n' +
1614
'\r\n' +
1715
'-----------------------------12946965154256166883262710838--\r\n',
1816
type: 'multipart/form-data; boundary=---------------------------12946965154256166883262710838'
@@ -66,11 +64,11 @@ test('Test the form data helper', async (t) => {
6664

6765
assert.deepEqual(
6866
response[1].fileData,
69-
'43\n'
67+
''
7068
)
7169
assert.deepEqual(
7270
response[1].fileName,
73-
'test.xml'
71+
undefined
7472
)
7573
assert.deepEqual(
7674
response[1].boundary,
@@ -82,7 +80,7 @@ test('Test the form data helper', async (t) => {
8280
)
8381
assert.deepEqual(
8482
response[1].contentType,
85-
'text/xml'
83+
undefined
8684
)
8785
})
8886
})

src/types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export type FormData = {
2-
fileName: string;
3-
fileData: string;
2+
fileName?: string;
3+
fileData?: string;
44
field: string;
5-
contentType: string;
5+
contentType?: string;
66
boundary: string;
77
};

0 commit comments

Comments
 (0)