Skip to content

Commit

Permalink
fix: Better support for data-params (#157)
Browse files Browse the repository at this point in the history
* Better support for input submissions

- Support json, escaped json, and url-encoded formats for data-params
- Submit element's name and value

* Escaped json: bail early if we have a result

* Refactor for clarity

* We're using entries

* Only submit element's name/value pair if we have a name and value

* Return undefined

* parseParamFormats: properly specify types for function inputs and return types

* Refactor iteration for clarity

* Don't used soft-deprecated unescape function, instead unescape escaped params and then utilize throughout `parseParamFormats`

* Simplify `parseParamFormats` to just return URLSearchParams instance, since Object.entries on top of return values from `parseParamFormats`
  • Loading branch information
rickychilcott authored Oct 25, 2021
1 parent 9fd403e commit 89b7a35
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/methodSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function MethodSubmission (element: HTMLElement): MethodSubmissionInterfa
}

options.method = maskedMethod ?? method
if (!isGetRequest(method)) options.body = getBody(method, element.getAttribute('data-params'))
if (!isGetRequest(method)) options.body = getBody(method, element)

const fetchRequest = FetchRequest(url, options)

Expand Down Expand Up @@ -91,12 +91,19 @@ function getMaskedMethod (method: string): string {
return isGetRequest(method) ? 'get' : 'post'
}

function getBody (method: string, additionalParams?: string | null): URLSearchParams {
function getBody (method: string, element: HTMLElement): URLSearchParams {
const encodedFormData = urlEncodeFormData(getFormData(method))

// add input's name and value to submission
const elName = element.getAttribute('name')
const elValue = element.getAttribute('value')
if (elName != null && elValue != null) {
encodedFormData.append(elName, elValue)
}

const additionalParams = parseParamFormats(element.getAttribute('data-params'))
if (additionalParams == null) return encodedFormData

const parsedParams = JSON.parse(additionalParams)
for (const [key, value] of Object.entries(parsedParams)) {
// @ts-expect-error
const val = value.toString()
Expand All @@ -110,3 +117,23 @@ function getBody (method: string, additionalParams?: string | null): URLSearchPa

return encodedFormData
}

function parseParamFormats (params: string | null | undefined): Object | URLSearchParams | void {
// convert encoded params to decoded params
if (containsEncodedComponents(params)) {
params = decodeURIComponent(params)
}

// json format
try { return JSON.parse(params) } catch { }

// param string format
try { return new URLSearchParams(params) } catch { }

return undefined
}

function containsEncodedComponents (x: any): boolean {
// ie ?,=,&,/ etc
return (decodeURI(x) !== decodeURIComponent(x))
}

0 comments on commit 89b7a35

Please sign in to comment.