Skip to content
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: add replaceparameters function (#158) #186

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/utils/replaceParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface Parameter {
[key: string]: string;
}

/**
* Replaces path parameters in the given URL string e.g. "/{portalURL}/overview" with the corresponding value.
* @param str - URL string, with parameters.
* @param parameter - Parameter.
* @returns string with parameters replaced.
*/
export function replaceParameters(str: string, parameter: Parameter): string {
const result = Object.entries(parameter).reduce(
(acc, [parameterKey, parameterValue]) => {
const regex = new RegExp(`\\{${parameterKey}}`, "g");
return acc.replace(regex, parameterValue);
},
str
);
if (/\{\w+}/.test(result)) {
throw new Error(`URL still contains path parameters: ${result}`);
}
return result;
}
Loading