-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enable TS strict mode #425
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,9 +72,9 @@ function isFile(value: any) { | |
} | ||
|
||
function getFileFromSuppliedFiles(filename: string, files: FetchHAROptions['files']) { | ||
if (filename in files) { | ||
if (files && filename in files) { | ||
return files[filename]; | ||
} else if (decodeURIComponent(filename) in files) { | ||
} else if (files && decodeURIComponent(filename) in files) { | ||
return files[decodeURIComponent(filename)]; | ||
} | ||
|
||
|
@@ -144,7 +144,7 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) { | |
} | ||
|
||
if ('postData' in request) { | ||
if ('params' in request.postData) { | ||
if (request.postData && 'params' in request.postData) { | ||
if (!('mimeType' in request.postData)) { | ||
// @ts-expect-error HAR spec requires that `mimeType` is always present but it might not be. | ||
request.postData.mimeType = 'application/octet-stream'; | ||
|
@@ -163,7 +163,9 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) { | |
headers.set('Content-Type', request.postData.mimeType); | ||
|
||
const encodedParams = new URLSearchParams(); | ||
request.postData.params.forEach(param => encodedParams.set(param.name, param.value)); | ||
request.postData.params?.forEach(param => { | ||
if (param.value) encodedParams.set(param.name, param.value); | ||
}); | ||
|
||
options.body = encodedParams.toString(); | ||
break; | ||
|
@@ -186,8 +188,8 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) { | |
|
||
const form = new FormData(); | ||
|
||
request.postData.params.forEach(param => { | ||
if ('fileName' in param) { | ||
request.postData.params?.forEach(param => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: might DRY things up a bit to deconstruct params up here |
||
if ('fileName' in param && param.fileName) { | ||
if (opts.files) { | ||
const fileContents = getFileFromSuppliedFiles(param.fileName, opts.files); | ||
if (fileContents) { | ||
|
@@ -197,7 +199,7 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) { | |
form.append( | ||
param.name, | ||
new File([fileContents], param.fileName, { | ||
type: param.contentType || null, | ||
type: param.contentType || undefined, | ||
}), | ||
param.fileName, | ||
); | ||
|
@@ -214,15 +216,15 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) { | |
} | ||
} | ||
|
||
if ('value' in param) { | ||
if ('value' in param && param.value) { | ||
let paramBlob; | ||
const parsed = parseDataUrl(param.value); | ||
if (parsed) { | ||
// If we were able to parse out this data URL we don't need to transform its data | ||
// into a buffer for `Blob` because that supports data URLs already. | ||
paramBlob = new Blob([param.value], { type: parsed.contentType || param.contentType || null }); | ||
paramBlob = new Blob([param.value], { type: parsed.contentType || param.contentType || undefined }); | ||
} else { | ||
paramBlob = new Blob([param.value], { type: param.contentType || null }); | ||
paramBlob = new Blob([param.value], { type: param.contentType || undefined }); | ||
} | ||
|
||
form.append(param.name, paramBlob, param.fileName); | ||
|
@@ -234,17 +236,17 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) { | |
); | ||
} | ||
|
||
form.append(param.name, param.value); | ||
if (param.value) form.append(param.name, param.value); | ||
}); | ||
|
||
options.body = form; | ||
break; | ||
|
||
default: | ||
const formBody: Record<string, unknown> = {}; | ||
request.postData.params.map(param => { | ||
request.postData.params?.map(param => { | ||
try { | ||
formBody[param.name] = JSON.parse(param.value); | ||
formBody[param.name] = JSON.parse(param.value || ''); | ||
} catch (e) { | ||
formBody[param.name] = param.value; | ||
} | ||
|
@@ -254,7 +256,7 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) { | |
|
||
options.body = JSON.stringify(formBody); | ||
} | ||
} else if (request.postData.text?.length) { | ||
} else if (request.postData?.text?.length) { | ||
// If we've got `files` map content present, and this post data content contains a valid data | ||
// URL then we can substitute the payload with that file instead of the using data URL. | ||
if (opts.files) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,12 @@ | |
"baseUrl": "./src", | ||
"declaration": true, | ||
"esModuleInterop": true, | ||
"lib": ["dom", "es2020"], | ||
"lib": ["DOM", "ES2022"], | ||
"module": "NodeNext", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean it outputs ESM? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, we're doing that in #427! we're still emitting CJS in this PR, but this just gives us support for the https://www.typescriptlang.org/tsconfig/#node16nodenext-nightly-builds |
||
"noImplicitAny": true, | ||
"outDir": "dist/" | ||
"outDir": "dist/", | ||
"strict": true, | ||
"target": "ES2022" | ||
kanadgupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"include": ["./src/**/*"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kinda funky that TS wants you to check
request.postData
again here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah 😬 I wonder there's some minor detail about
in
that TS knows that we don't know here lolThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ts won't remember existence checks because side effects could change the value. You would want to throw postData into a variable at line 145 and then check that at 146, and use it at 147.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohhh that's a great callout @Dashron!