generated from deco-sites/start
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid utm campaigns with space or plus sign in VTEX legacy fetch. (#68)
* Avoid utm campaigns with space or plus sign in UTM campaigns. * Remove console.log. * Create variable to hold all QS to sanatize. * Remove console.log.
- Loading branch information
Showing
8 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { FetchOptions } from "./fetch.ts"; | ||
import { | ||
fetchAPI as fetchAPIBase, | ||
fetchSafe as fetchSafeBase, | ||
} from "./fetch.ts"; | ||
|
||
const processFetch = async ( | ||
_fetch: ( | ||
input: string | Request | URL, | ||
init?: RequestInit & FetchOptions, | ||
) => ReturnType<typeof fetch>, | ||
input: string | Request | URL, | ||
init?: RequestInit & FetchOptions, | ||
) => { | ||
let url: URL; | ||
|
||
if (typeof input === "string") { | ||
url = new URL(input); | ||
} else if (input instanceof URL) { | ||
url = input; | ||
} else { | ||
// is a request... you know what you are doing. | ||
return await _fetch(input, init); | ||
} | ||
|
||
const QS_TO_REMOVE_PLUS = ["utm_campaign"]; | ||
|
||
QS_TO_REMOVE_PLUS.forEach((qsToSanatize) => { | ||
if (url.searchParams.has(qsToSanatize)) { | ||
const searchParams = url.searchParams; | ||
const testParamValues = searchParams.getAll(qsToSanatize); | ||
const updatedTestParamValues = testParamValues.map((paramValue) => | ||
paramValue.replace(/\+/g, "").replaceAll(" ", "") | ||
); | ||
searchParams.delete(qsToSanatize); | ||
updatedTestParamValues.forEach((updatedValue) => | ||
searchParams.append(qsToSanatize, updatedValue) | ||
); | ||
} | ||
}); | ||
|
||
return await _fetch(url.toString(), init); | ||
}; | ||
|
||
export const fetchSafe = async ( | ||
input: string | Request | URL, | ||
init?: RequestInit & FetchOptions, | ||
) => { | ||
return await processFetch(fetchSafeBase, input, init); | ||
}; | ||
|
||
export const fetchAPI = async <T>( | ||
input: string | Request | URL, | ||
init?: RequestInit & FetchOptions, | ||
): Promise<T> => { | ||
return await processFetch(fetchAPIBase, input, init) as T; | ||
}; |