How to use the url-template.parse function in url-template

To help you get started, we’ve selected a few url-template examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wikimedia / restbase / lib / proxyHandler.js View on Github external
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;
    }
}
github octokit / rest.js / lib / endpoint / index.js View on Github external
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
    }
github fourkitchens / saucier / lib / api.js View on Github external
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;
      });
github googleapis / google-api-nodejs-client / src / shared / src / apirequest.ts View on Github external
});

  // 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];
  });
github googleapis / google-api-nodejs-client / src / shared / src / apirequest.ts View on Github external
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
github googleapis / nodejs-googleapis-common / src / apirequest.ts View on Github external
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
github wikimedia / restbase / lib / proxyHandler.js View on Github external
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);
}
github bleupen / halacious / lib / plugin.js View on Github external
var resolveHref = function (href, ctx) {
            return _.isFunction(href) ? href(rep, ctx) : urlTemplate.parse(href)
                .expand(flattenContext(href, ctx));
        };
github ethanresnick / json-api / build / src / steps / do-query / do-post.js View on Github external
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);
                }
            }
        });
    }

url-template

A URI template implementation (RFC 6570 compliant)

BSD-3-Clause
Latest version published 1 year ago

Package Health Score

71 / 100
Full package analysis

Popular url-template functions