npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oniyi-http-plugin-format-url-template

v1.1.0

Published

Plugin for injecting template values into request href and/or query string

Downloads

12

Readme

oniyi-http-plugin-format-url-template

Plugin for injecting template values into request href and/or query string

Install

$ npm install --save oniyi-http-plugin-format-url-template

Usage

const OniyiHttpClient = require('oniyi-http-client');
const oniyiHttpPluginUrlTemplate = require('oniyi-http-plugin-format-url-template');

const options = {
  requestPhases: ['initial','format-url-template', 'final'],
};

const pluginOptions = {
  valuesMap: {
    authType: {
      oauth: 'myAuthType', // this line overrides default 'myAuthType' type name, and 'myAuthType' will be injected into url if requested
    }
  },
  applyToQueryString: true,
};

const plugin = oniyiHttpPluginUrlTemplate(pluginOptions);
const phaseMapOptions = {
  requestPhaseMap: {
    'format-url-template': 'url-template',
  },
  responsePhaseMap: {
    final: 'end',
  },
};

const httpClient = OniyiHttpClient
  .create(options)                // create custom http client with defined phases lists
  .use(plugin, phaseMapOptions);  // mount a plugin

These are the default plugin options, they can be overridden as shown above (merged deeply)

const defaultPluginOptions = {
  applyToUrl: true,
  applyToQueryString: false,
  valuesMap: {
    authType: {
      oauth: 'oauth',
      basic: 'basic',
      saml: 'form',
      cookie: 'form',
    },
  },
};

This plugin relies on logic implemented in oniyi-http-client, which has extensive documentation on how phase lists work and what conventions must be followed when implementing a plugin.

Conventions

It is important to follow couple of conventions when defining a request uri:

-- convention 1 --

const requestOptions = {
  auth: {},
  headers: {},
  method: '',
  qs: {},
  uri: 'my/custom/{ authType }/path',
}

{ authType } can be placed anywhere within uri/url. Valid template string:

  1. {authType}
  2. {authType }
  3. { authType}
  4. { authType }

{authType} does not have to be used within every single http request in service. The logic behind this plugin is to only format template Strings if any was found. Otherwise it will return parsed uri without any changes.

-- convention 2 --

const requestOptions = {
  auth: {},
  headers: {},
  method: '',
  qs: {},
  uri: 'my/custom/{ authType }/path/{ pathID }',
  pathID: 'mockId123456',
}

You are able to add multiple templates into uri ('pathID',...), as long as you define their values within requestOptions.

-- convention 3 --

Format url template options can be applied on two levels:

  1. When initializing plugin:
const pluginOptions = {
  valuesMap: {},
  applyToUrl: false,
};

httpClient.use(oniyiHttpPluginUrlTemplate(pluginOptions));
  1. When configuring http request:
const requestOptions = {
  qs: {},
  uri: 'my/custom/path/without/template',
  plugins: {
    formatUrlTemplate: { // camelized plugin name
      applyToUrl: false,
    },
  },
}

Set applyToUrl to false if there is no need to apply this plugin on an uri.

Set applyToQueryString to true if formatting query string ( 'qs' ) is required.

Setup under 1. overwrites the default formatUrlTempalte options, while setup under 2. overwrites initial pluginOptions setup

const requestOptions = {
  auth: {},
  headers: {},
  qs: {
    pageSize: '{ pageSize }',
  },
  plugins: {
    formatUrlTemplate: {
      applyToQueryString: true,
    },
  },
  uri: 'my/custom/path',
  pageSize: '15',
}

-- convention 4 --

It is recommended to use this plugin in combination with oniyi-http-plugin-credentials, since it resolves authType which is being used by this plugin.

Otherwise, authType need to be added manually into requestOptions.

const requestOptions = {
  authType: 'basic',
    uri: 'my/custom/{ authType }/path',
  //...
  }

-- convention 5 --

Please provide uri parameter as a String or Url object:

const uriAsString = 'my/custom/authType }/path';
const parsedUri = {
  auth: null,
  hash: null,
  host:'host.com',
  hostname: 'host.com',
  href:'host.com/my/custom/{ authType/path',
  path:'/my/custom/path',
  pathname:'/my/custom/path',
  port: null,
  protocol:'https:',
  query: null,
  search: null,
  slashes: true,
}

If conventions 1 && 2 have not been implemented properly, response uri will look like this:

// uri as string request
{
  uri: 'my/custom/authType }/path',
}
// uri as parsed object
{
  uri: {
    auth: null,
    hash: null,
    host:'host.com',
    hostname: 'host.com',
    href:'host.com/my/custom/%7B%20authType/path',
    path:'/my/custom/%7B%20authType/path',
    pathname:'/my/custom/%7B%20authType/path',
    port: null,
    protocol:'https:',
    query: null,
    search: null,
    slashes: true,
  },
}

The reason why uriAsString response is not encoded as parsedUri is because plugin takes href prop from parsedUri, uses url.parse(href); which encodes all special characters that are used in the href path.

This way plugin is notifying service that something went wrong while setting up, most likely because of a typo.

authType can be added with falsy value as default, and in return it will be removed completely from an uri.

const requestOptions = {
  authType: '', // or 0, null, undefined, false, NaN
  uri: 'my/custom/{ authType }/path',
  //...
  };
  //...

const httpResponse = {
  uri: 'my/custom/path',
}

Similar goes for qs parameter, response will look like this:

{
  qs: {
    pageSize: '{ psTemplate }',
  }
}

The reason why qs response is not encoded as uri is because plugin uses url.parse(uri); which encodes all special characters that are used in the uri string.

This way it should be relatively simple injecting any parameter into service uri / qs path, in order to load the same service with different auth providers (ibm-connections-cloud, microsoft...) by using custom template mapping.