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

resolve-url.macro

v1.1.1

Published

Resolve your URLs during build

Downloads

192

Readme

resolve-url.macro

Resolve your URLs during build

Build Status Greenkeeper badge

This macro allows you to resolve the URLs from the back end (e.g. a Django app) in the client-side JS code. Thanks to that the URLs can be verified at the build time, and only used URLs will appear in the code.

Getting started

First of all, you'll need Babel.

Then, install babel-plugin-macros together with this package:

yarn add -D resolve-url.macro babel-plugin-macros
# or
npm install -D resolve-url.macro babel-plugin-macros

Finally, add a configuration file in the root of your project, called babel-plugin-macros.config.js:

module.exports = {
  resolve: {
    urlsPath: 'path/to/urls',
  },
};

This is it for the configuration, but what's inside the URLs file?

The URLs file

This file should contain a list of available URLs. Each URL should consist of:

  • a name: e.g. "login", "get-me-that-data",
  • a template: e.g. "/login", "/items/${id}"

As for the format, it can be either a JS file:

module.exports = [
  {
    name: 'no-params',
    template: 'params/zero/',
  },
  {
    name: 'three-params',
    template: 'params/three/${first}-${second}/${third}/',
  },
];

or a JSON file:

[
  {
    "name": "no-params",
    "template": "params/zero/"
  },
  {
    "name": "three-params",
    "template": "params/three/${first}-${second}/${third}/"
  }
]

The parts of the URL wrapped in ${} will be recognized as URL parameters.

Such a file will most probably be produced by another tool (coming soon!).

Using in the code

As with other macros built on babel-plugin-macros, you'll need to import the macro. After that just use it as a function:

import resolveUrl from 'resolve-url.macro';

axios.post(resolveUrl('three-params', 2 + 2, -1, 'quick maths'), data);

Under the hood the URL will be inlined using the configuration and will turn into something like this:

axios.post(`params/three/${2 + 2}-${-1}-${'quick maths'}`, data);

The number of parameters has to match the predefined URL template. Notice that the expressions are preserved and will be computed at runtime.

Passing query parameters

A common pattern while working with URLs is the need to pass query parameters. You can do that easily with the mecro too, using the withQuery property:

import resolveUrl from 'resolve-url.macro';

axios.post(resolveUrl.withQuery('search', { sortBy: 'id' }), data);

The last argument will be used to build the query string.

All properties of the query object will be properly escaped:

// query object
{ redirect_url: 'https://example.com/foobar' }

// result
'?redirect_url=https%3A%2F%2Fexample.com%2Ffoobar'

Null and undefined values will be ignored:

// query object
{ foo: null, bar: 42 }

// result
'?bar=42'

Using the object parameter

It's also possible to pass an object as a parameter to the resolveUrl function. In that case property names have to match parameters from the URL template:

import resolveUrl from 'resolve-url.macro';

axios.get(resolveUrl('get-object', { userId, objectId }));

In such a situation the object has to be the last argument.

Info for contributors

Everyone is welcome to contribute to the package - just be nice.

After cloning the repo you'll have a few commands useful in development:

  • lint - just lints (using ESLint and Prettier)
  • lint:fix - lints and fixes what's possible; use it especially for autoformatting
  • test - tests the code using Jest
  • test:update - test and updates the snapshots if they've changed

License

Copyright (c) 2018 Rafał Ruciński. Licensed under the MIT license.