Skip to content

Instantly share code, notes, and snippets.

@chanphiromsok
Created March 18, 2022 03:25
Show Gist options
  • Save chanphiromsok/3225cd7b969aa94266ab6191d53c8d40 to your computer and use it in GitHub Desktop.
Save chanphiromsok/3225cd7b969aa94266ab6191d53c8d40 to your computer and use it in GitHub Desktop.
utils
import _ from 'lodash';
type T = {
option?: any;
cases?: 'snakeCase' | 'camelCase';
url: string;
};
const dynamicUrl = ({ option, url, cases }: T) => {
let endPoint = 'http://localhost:3000' + url;
Object.keys(option).forEach((key, index) => {
let transformKey =
cases === 'camelCase'
? _.camelCase(key)
: cases === 'snakeCase'
? _.snakeCase(key)
: key;
if (!!option[key as keyof typeof option]) {
endPoint =
index === 0
? endPoint.concat(
`?${transformKey}=${option[key as keyof typeof option]}`
)
: endPoint.concat(
`&${transformKey}=${option[key as keyof typeof option]}`
);
}
});
return endPoint;
};
console.log(
dynamicUrl({
url: '/api/v1/customer/wallet',
cases: 'snakeCase',
option: { filter: 'ITEM', page: 1, perPage: 20 },
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment