Last active
October 6, 2016 13:06
-
-
Save sotaan/c29facfc7f2f2c2bb2a5e47909d842aa to your computer and use it in GitHub Desktop.
generate URL of any RESTfull API Endpoint
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
/** | |
* Components used to build the final URL | |
* @type {Object} | |
*/ | |
const urlComponents = { | |
url: "http://myapi.mycompany.com/", | |
prefix: "api", | |
cart_uri: "/carts", | |
cart_active_uri: "/active", | |
cart_clean_uri: "/clean", | |
suborder_uri: "/suborders", | |
option_uri: "/options", | |
auth_uri: "/auth", | |
auth_login_uri: "/login", | |
auth_signup_uri: "/signup", | |
auth_recovery_uri: "/recovery", | |
purchase_tunnel_uri: "/purchase_tunnel", | |
purchase_tunnel_steps_uri: "/steps" | |
} | |
/** | |
* Logic applied for a single Entity | |
* @param {string} entity [description] | |
* @param {string} action [description] | |
* @param {Object} params [description] | |
* @return {string} [description] | |
*/ | |
let handleEntity = (entity, action, params) => { | |
let url = "", | |
entityParameter = params.hasOwnProperty(entity+'_id')? params[entity+'_id'] : false | |
url += urlComponents[entity+'_uri'] | |
if (entityParameter) { | |
url += '/'+entityParameter | |
} | |
//url += (action)? urlComponents[entity+'_'+action+'_'+'uri'] : '' | |
if (action) url = url.concat(urlComponents[entity+'_'+action+'_'+'uri']) | |
return url | |
} | |
/** | |
* Deduce any RESTfull API endpoint from one or more Entity (and eventually an action) | |
* then return the full URL | |
* @param {Array / string} entity | |
* @param {Object} [params={}] optional | |
* @param {string} [action=null] optional | |
* @return {string} full URL | |
*/ | |
//let generateURL = (entity, params = {}, action = null) => { | |
//let generateURL = (entity, action = null, params = {} ) => { | |
let generateURL = (entity, ...args) => { | |
let url = urlComponents.url + urlComponents.prefix, | |
[params = {}, action = null] = (typeof args[0] === 'string')? [args[1], args[0]] : [args[0], args[1]] | |
// e.g. for this case: `/carts/{cart}/suborders/{suborder}/options` | |
// is gotten from this call: `generateURL(["cart", "suborder", "option"], { | |
// 'cart_id': xx, suborder_id: xxx} )` | |
if (Array.isArray(entity)) { | |
let lastEntity = entity[entity.length - 1], | |
idx = lastEntity+'_'+action+'_'+'uri' | |
lastAction = urlComponents.hasOwnProperty(idx)? urlComponents[idx] : false | |
url += entity.reduce((acc, curr) => acc + handleEntity(curr, null, params), "") | |
if (lastAction) url = url.concat(lastAction) | |
} | |
// simple case with one entity & an action (eventually) | |
else if (urlComponents[entity+'_uri']) { | |
url += handleEntity(entity, action, params) | |
} | |
return url | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment