diff --git a/package-lock.json b/package-lock.json index 732df91..139f4f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -777,6 +777,12 @@ "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=", "dev": true }, + "@types/mime-types": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.0.tgz", + "integrity": "sha1-nKUs2jY/aZxpRmwqbM2q2RPqenM=", + "dev": true + }, "@types/minimist": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.0.tgz", @@ -4307,6 +4313,19 @@ "integrity": "sha512-RZKhC3EmpBchfTGBVb8fb+RL2cWyw/32lshnsETttkBAyAUXSGHxbEJWWRXc751DrIxG1q04b8QwMbAwkRPpUA==", "dev": true }, + "mime-db": { + "version": "1.44.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", + "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==" + }, + "mime-types": { + "version": "2.1.27", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", + "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", + "requires": { + "mime-db": "1.44.0" + } + }, "mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", diff --git a/package.json b/package.json index a4ea752..adfac8f 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "homepage": "https://github.com/NeuraLegion/postman2har#readme", "dependencies": { "faker": "^4.1.0", + "mime-types": "^2.1.27", "semver": "^7.3.2", "tslib": "~1.11.1" }, @@ -67,6 +68,7 @@ "@types/debug": "^4.1.5", "@types/faker": "^4.1.12", "@types/har-format": "^1.2.4", + "@types/mime-types": "^2.1.0", "@types/mocha": "~7.0.2", "@types/node": "~14.0.13", "@types/semver": "^7.3.1", diff --git a/src/converter/DefaultConverter.ts b/src/converter/DefaultConverter.ts index 335409a..a672057 100644 --- a/src/converter/DefaultConverter.ts +++ b/src/converter/DefaultConverter.ts @@ -2,9 +2,11 @@ import { Converter } from './Converter'; import { Validator } from '../validator'; import { VariableParser, VariableParserFactory } from '../parser'; import Har from 'har-format'; +import { lookup } from 'mime-types'; import { ok } from 'assert'; import { format, parse, UrlObject } from 'url'; import { parse as parseQS, ParsedUrlQuery, stringify } from 'querystring'; +import { basename, extname } from 'path'; enum AuthLocation { QUERY = 'queryString', @@ -83,7 +85,7 @@ export class DefaultConverter implements Converter { const request: Har.Request = { url, method: (method ?? 'GET').toUpperCase(), - headers: this.convertHeaders(header!, variables), + headers: this.convertHeaders(header ?? '', variables), queryString: this.convertQuery(url, variables), cookies: [], postData: body && this.convertBody(body, variables), @@ -308,11 +310,25 @@ export class DefaultConverter implements Converter { return { mimeType: 'multipart/form-data', params: Array.isArray(body.formdata) - ? body.formdata.map((x: Postman.FormParam) => ({ - name: parser.parse(x.key ?? ''), - value: parser.parse(x.value ?? ''), - contentType: x.contentType - })) + ? body.formdata.map((x: Postman.FormParam) => { + const fileName: string | undefined = x.src + ? basename(Array.isArray(x.src) ? x.src.pop()! : x.src) + : undefined; + + const extension: string | undefined = fileName + ? extname(fileName) + : fileName; + + const contentType: string | undefined = + x.contentType ?? (lookup(extension ?? '') || undefined); + + return { + fileName, + contentType, + name: parser.parse(x.key ?? ''), + value: parser.parse(x.value ?? '') + }; + }) : [], text: '' }; @@ -330,7 +346,7 @@ export class DefaultConverter implements Converter { value: parser.parse(x.value ?? '') })); } else { - params = Object.entries(parseQS(body.urlencoded!)).map( + params = Object.entries(parseQS(body.urlencoded ?? '')).map( ([name, value]) => ({ name, value: Array.isArray(value) ? value.join('&') : value @@ -343,7 +359,7 @@ export class DefaultConverter implements Converter { ? body.urlencoded : stringify( Object.fromEntries( - body.urlencoded!.map((x: Postman.QueryParam) => [ + (body.urlencoded ?? []).map((x: Postman.QueryParam) => [ parser.parse(x.key ?? ''), parser.parse(x.value ?? '') ])