Skip to content

Commit

Permalink
Merge pull request #5 from trojs/feature/first-version
Browse files Browse the repository at this point in the history
Handle empty file uploads
  • Loading branch information
w3nl authored Jul 4, 2024
2 parents 29973a7 + f2e9f49 commit b4bb458
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trojs/formdata-parser",
"version": "0.1.2",
"version": "0.2.0",
"description": "Parse the form data",
"type": "module",
"main": "src/form-data.js",
Expand Down
16 changes: 8 additions & 8 deletions src/form-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@

/**
* @param {string} file
* @returns {string}
* @returns {string=}
*/
const getFileName = (file) => {
const fileName = file.split(`filename="`)
if (fileName.length > 1) {
return fileName[1].split(`"\r\n`)[0]
return fileName[1].split('"')[0]
}
throw new Error('No file name')
return undefined
}

/**
* @param {string} file
* @returns {string}
* @returns {string=}
*/
const getFileData = (file) => {
const fileData = file.split(`\r\n\r\n`)
if (fileData.length > 1) {
return fileData[1].split(`\r\n`)[0]
}
throw new Error('No file data')
return undefined
}

/**
Expand All @@ -34,20 +34,20 @@ const getFileData = (file) => {
const getField = (file) => {
const fieldName = file.split(`name="`)
if (fieldName.length > 1) {
return fieldName[1].split(`";`)[0]
return fieldName[1].split(`"`)[0]
}
throw new Error('No field')
}
/**
* @param {string} file
* @returns {string}
* @returns {string=}
*/
const getContentType = (file) => {
const contentType = file.split(`Content-Type: `)
if (contentType.length > 1) {
return contentType[1].split(`\r\n`)[0]
}
throw new Error('No content type')
return undefined
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/form-data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ const example = {
'42\n' +
'\r\n' +
'-----------------------------12946965154256166883262710838\r\n' +
'Content-Disposition: form-data; name="fileName2"; filename="test.xml"\r\n' +
'Content-Type: text/xml\r\n' +
'Content-Disposition: form-data; name="fileName2"\r\n' +
'\r\n' +
'43\n' +
'\r\n' +
'-----------------------------12946965154256166883262710838--\r\n',
type: 'multipart/form-data; boundary=---------------------------12946965154256166883262710838'
Expand Down Expand Up @@ -66,11 +64,11 @@ test('Test the form data helper', async (t) => {

assert.deepEqual(
response[1].fileData,
'43\n'
''
)
assert.deepEqual(
response[1].fileName,
'test.xml'
undefined
)
assert.deepEqual(
response[1].boundary,
Expand All @@ -82,7 +80,7 @@ test('Test the form data helper', async (t) => {
)
assert.deepEqual(
response[1].contentType,
'text/xml'
undefined
)
})
})
6 changes: 3 additions & 3 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type FormData = {
fileName: string;
fileData: string;
fileName?: string;
fileData?: string;
field: string;
contentType: string;
contentType?: string;
boundary: string;
};

0 comments on commit b4bb458

Please sign in to comment.