Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var traverse = function(path) {
// A simple dotted path reference
path.split(/\./).forEach(function(pathBit) {
// Let errors bubble up, assuming this is all inside of a promise
context = context[pathBit];
});
return context;
};
if (/^\$/.test(variable)) {
return traverse(variable.slice(1));
} else if (/^\{[^}]+\}$/.test(variable)) {
var groups = /^\{([^}]+)\}$/.exec(variable);
return traverse(groups[1]);
} else if (/^\//.test(variable)) {
// An URL template
var urlTemplate = template.parse(variable);
return urlTemplate.expand(context);
} else {
// Treat it as a string literal
return variable;
}
}
options = defaultsDeep({}, options, DEFAULTS)
let method = options.method.toLowerCase()
let baseUrl = options.baseUrl
let url = options.url
let body = options.body
let headers = options.headers
let remainingOptions = omit(options, ['method', 'baseUrl', 'url', 'headers'])
// replace :varname with {varname} to make it RFC 6570 compatible
url = url.replace(/:([a-z]\w+)/g, '{+$1}')
// extract variable names from URL to calculate remaining variables later
const urlVariableNames = extractUrlVariableNames(url)
url = urlTemplate.parse(url).expand(remainingOptions)
if (!/^http/.test(url)) {
url = (baseUrl) + url
}
const requestOptions = remainingOptions.request
remainingOptions = omit(remainingOptions, intersection(Object.keys(options), urlVariableNames).concat(NON_PARAMETERS))
if (method === 'get' || method === 'head') {
url = addQueryParameters(url, remainingOptions)
} else {
if ('input' in remainingOptions) {
deprecate('"input" option has been renamed to "data"')
remainingOptions.data = remainingOptions.input
delete remainingOptions.input
}
options.resource = options.resource.map(function (item) {
// Prepend query string with ? or &.
if (query) {
query = item.indexOf('?') === -1 ? '?' + query : '&' + query;
}
return urlTemplate.parse(item).expand(urlParams) + query;
});
});
// Check for missing required parameters in the API request
const missingParams = getMissingParams(params, parameters.requiredParams);
if (missingParams) {
// Some params are missing - stop further operations and inform the
// developer which required params are not included in the request
throw new Error('Missing required parameters: ' + missingParams.join(', '));
}
// Parse urls
if (options.url) {
options.url = urlTemplate.parse(options.url).expand(params);
}
if (parameters.mediaUrl) {
parameters.mediaUrl = urlTemplate.parse(parameters.mediaUrl).expand(params);
}
// When forming the querystring, override the serializer so that array
// values are serialized like this:
// myParams: ['one', 'two'] ---> 'myParams=one&myParams=two'
// This serializer also encodes spaces in the querystring as `%20`,
// whereas the default serializer in axios encodes to a `+`.
options.paramsSerializer = (params) => {
return qs.stringify(params, {arrayFormat: 'repeat'});
};
// delete path parameters from the params object so they do not end up in
// query
parameters.pathParams.forEach(param => {
delete params[param];
});
params[newKey] = params[key];
delete params[key];
}
});
// Check for missing required parameters in the API request
const missingParams = getMissingParams(params, parameters.requiredParams);
if (missingParams) {
// Some params are missing - stop further operations and inform the
// developer which required params are not included in the request
throw new Error('Missing required parameters: ' + missingParams.join(', '));
}
// Parse urls
if (options.url) {
options.url = urlTemplate.parse(options.url).expand(params);
}
if (parameters.mediaUrl) {
parameters.mediaUrl = urlTemplate.parse(parameters.mediaUrl).expand(params);
}
// When forming the querystring, override the serializer so that array
// values are serialized like this:
// myParams: ['one', 'two'] ---> 'myParams=one&myParams=two'
// This serializer also encodes spaces in the querystring as `%20`,
// whereas the default serializer in axios encodes to a `+`.
options.paramsSerializer = (params) => {
return qs.stringify(params, {arrayFormat: 'repeat'});
};
// delete path parameters from the params object so they do not end up in
// query
params[newKey] = params[key];
delete params[key];
}
});
// Check for missing required parameters in the API request
const missingParams = getMissingParams(params, parameters.requiredParams);
if (missingParams) {
// Some params are missing - stop further operations and inform the
// developer which required params are not included in the request
throw new Error('Missing required parameters: ' + missingParams.join(', '));
}
// Parse urls
if (options.url) {
options.url = urlTemplate.parse(options.url).expand(params);
}
if (parameters.mediaUrl) {
parameters.mediaUrl = urlTemplate.parse(parameters.mediaUrl).expand(params);
}
// When forming the querystring, override the serializer so that array
// values are serialized like this:
// myParams: ['one', 'two'] ---> 'myParams=one&myParams=two'
// This serializer also encodes spaces in the querystring as `%20`,
// whereas the default serializer in gaxios encodes to a `+`.
options.paramsSerializer = params => {
return qs.stringify(params, {arrayFormat: 'repeat'});
};
// delete path parameters from the params object so they do not end up in
// query
function expandUrl(url, context) {
var slugs = url.split('/').filter(function(str){return (/\S/).test(str);});
var newUrl = {};
for (var i = 0; i < slugs.length; i++) {
var slug = slugs[i];
var expanded = expandVar(slug, context);
if (/^\{[^}]+\}$/.test(slug)) {
var groups = /^\{([^}]+)\}$/.exec(slug);
newUrl[groups[1]] = expanded;
} else {
newUrl[slug] = expanded;
}
}
return template.parse(url).expand(newUrl);
}
var resolveHref = function (href, ctx) {
return _.isFunction(href) ? href(rep, ctx) : urlTemplate.parse(href)
.expand(flattenContext(href, ctx));
};
return adapter.create(type, primary).then((created) => {
responseContext.primary = created;
responseContext.status = 201;
if (created instanceof Resource_1.default) {
const templates = registry.urlTemplates(created.type);
const template = templates && templates.self;
if (template) {
const templateData = Object.assign({ "id": created.id }, created.attrs);
responseContext.headers.location = templating.parse(template).expand(templateData);
}
}
});
}