Created
March 18, 2022 03:25
-
-
Save chanphiromsok/3225cd7b969aa94266ab6191d53c8d40 to your computer and use it in GitHub Desktop.
utils
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
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