JSFiddle - React, Tailwind, and code Playground
by Luiza CICONE
JavaScript
inject = (restApi, context) => {
if (!restApi) {
throw new Error('inject: restApi must be defined');
}
if (!context) {
throw new Error('inject: context must be defined');
}
// Search for context properties to inject
const properties = restApi.match(/(:\w+)/g);
properties.forEach((property) => {
const contextVar = property.substring(1);
let contextValue = context[contextVar];
if (contextValue !== undefined) {
contextValue = encodeURIComponent(contextValue);
restApi = restApi.replace(property, contextValue);
} else {
throw new Error('inject: context.' + contextVar + ' expected but undefined');
}
});
return restApi;
}
console.er