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

enda

v1.0.0

Published

A simple utility for managing API endpoint constants.

Downloads

2

Readme

Enda provides an interface for composing and formatting API endpoint URLs. It helps avoid potential typos by letting you maintain URL parts in a single place.

import { Enda } from 'enda';

const enda = new Enda({
  base: 'https://api.myapi.com',
  parts: {
    projects: 'projects',
    tasks: 'tasks',
    versions: {
      v1: 'v1',
      v2: 'v2'
    },
    params: {
      projectId: '{projectId}',
      taskId: '{taskId}'
    }
  }
});

const PROJECT_TASK = enda.make(({ join, parts }) =>
  join(
    parts.versions.v1,
    parts.projects,
    parts.params.projectId,
    parts.tasks,
    parts.params.taskId
  )
);

const getProjectTask = (projectId, taskId) => {
  const url = PROJECT_TASK.format({ projectId, taskId }); // e.g. https://api.myapi.com/projects/23/tasks/2
  return makeRequest(url);
};

Installation

Via npm:

npm install enda

Usage

new Enda([options])

  • options: EndaOptions
    • base?: string Base URL - Default '/'
    • parts?: {[k: string]: string} URL parts
    • parameterMatcher?: RegExp Pattern used to match path parameters - Default /{([^}]+)}/g
    • strictParameterMatching?: boolean Whether an error will be thrown if a replacement for a path parameter matched by the parameterMatcher expression is not found when the format method is called - Default true

The base URL does not need to end with a / character. If not already present, this will be appended to base when joining paths.

Path parameters are matched based on the parameterMatcher Enda option. By default this is /{([^}]+)}/g. The method will use the matched contents of the first, outermost match group to look up a corresponding value in the path parameters object. If no match group is present, the full match will be used.

An error will be thrown if a match is found in the URL but no corresponding property is found in the path parameters object passed when formatting. You can disable this behavior by setting strictParameterMatching to false in the Enda options.

enda.make

Constructing an Enda object will give you access to a make method that can be used to create endpoint URLs based on the options provided.

make is an overloaded method and can take its arguments in a number of forms:

enda.make(): Endpoint

Calling make with no arguments will simply give you back an Endpoint instance whose underlying URL is the base URL.

import { Enda } from 'enda';

const enda = new Enda({ base: 'https://api.myapi.com' });

const base: Endpoint = enda.make();
base.url(); // https://api.myapi.com

enda.make(...parts: string[]): Endpoint

Calling make with any number of string arguments will give you back an Endpoint instance whose underlying URL is the base URL joined with the provided parts. Parts are joined using the / character, so there's no need to include this in each part string. enda will ignore any leading / characters.

import { Enda } from 'enda';

const enda = new Enda();

const project: Endpoint = enda.make('projects', '{projectId}');
project.url(); // /foo/bar/baz

// Leading / characters will be ignored...
const tasks: Endpoint = enda.make('projects', '/{projectId}', '//tasks');
tasks.url(); // /projects/{projectId}/tasks

enda.make(maker: MakerFunction): Endpoint

If a function is provided as the first argument to make it will be called with an object containing a join helper function and the parts provided when constructing the Enda instance. If no parts were provided, then parts will be undefined.

import { Enda } from 'enda';

const enda = new Enda({
  parts: {
    projects: 'projects',
    projectId: '{projectId}',
    tasks: 'tasks'
  }
});

const tasks: Endpoint = enda.make(({ join, parts }) =>
  join(parts.projects, parts.projectId, parts.tasks)
);
tasks.url(); // /foo/{projectId}/tasks

// You don't have to use the join helper function. As long as
// your function returns a string, you're set. You will need
// manually join using the / character though!
const task: Endpoint = enda.make(
  ({ parts: { projects, projectId, tasks } }) =>
    `${projects}/${projectId}/${tasks}/{taskId}`
);
task.url(); // /foo/{projectId}/tasks/{taskId}

Endpoint methods

The Endpoint instance returned from make provides two additional methods.

endpoint.url(): string

Get the endpoint URL as a string.

import { Enda } from 'enda';

const enda = new Enda({
  base: 'https://api.myapi.com'
});

const projects: Endpoint = enda.make('projects');

projects.url(); // https://api.myapi.com/projects

endpoint.format(pathParameters: PathParameters): string

  • pathParameters: PathParameters Object mapping path parameters in the endpoint URL to their corresponding replacement values. Values can be either type string or number.

Format path parameters in the endpoint URL. The formatted URL will be returned as a string.

import { Enda } from 'enda';

const enda = new Enda({
  parts: {
    projects: 'projects',
    projectId: '{projectId}',
    tasks: 'tasks',
    taskId: '{taskId}'
  }
});

const task: Endpoint = enda.make(({ join, parts }) =>
  join(parts.projects, parts.projectId, parts.tasks, parts.taskId)
);

task.format({ projectId: 23, taskId: '2' }); // /projects/23/tasks/2