Skip to content

Commit

Permalink
feat: add api url maker
Browse files Browse the repository at this point in the history
  • Loading branch information
timepresent95 committed Aug 20, 2024
1 parent 4b4b59c commit 4d51a6b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type OptionType = string | number | boolean;

interface Options {
param?: {[key: string]: OptionType};
query?: {[key: string]: OptionType};
}
export function createUrl(path: string, options?: Options) {
const pathTokens = path.split('/');
const replacedPathTokens = pathTokens.map(token => {
if (token[0] === ':' && options?.param && options.param[token.slice(1)]) {
return options.param[token.slice(1)];
}
return token;
});

const queryPairs = Object.entries(options?.query ?? []).map(
([key, value]) => `${key}=${value}`,
);

const apiPath = replacedPathTokens.join('/');
const query = queryPairs.length === 0 ? '' : '?' + queryPairs.join('&');

return apiPath + query;
}

0 comments on commit 4d51a6b

Please sign in to comment.