🌐 Enda
A simple utility for managing API endpoint constants
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.
; const 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; const getProjectTask = { const url = PROJECT_TASK; // e.g. https://api.myapi.com/projects/23/tasks/2 return ;};
Installation
Via npm:
npm install enda
Usage
new Enda([options])
options: EndaOptions
base?: string
Base URL - Default'/'
parts?: {[k: string]: string}
URL partsparameterMatcher?: 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 theparameterMatcher
expression is not found when theformat
method is called - Defaulttrue
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.
; const enda = base: 'https://api.myapi.com' ; const base: Endpoint = enda;base; // 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.
; const enda = ; const project: Endpoint = enda;project; // /foo/bar/baz // Leading / characters will be ignored...const tasks: Endpoint = enda;tasks; // /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
.
; const enda = parts: projects: 'projects' projectId: '{projectId}' tasks: 'tasks' ; const tasks: Endpoint = enda;tasks; // /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;task; // /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.
; const enda = base: 'https://api.myapi.com'; const projects: Endpoint = enda; projects; // 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 typestring
ornumber
.
Format path parameters in the endpoint URL. The formatted URL will be returned as a string.
; const enda = parts: projects: 'projects' projectId: '{projectId}' tasks: 'tasks' taskId: '{taskId}' ; const task: Endpoint = enda; task; // /projects/23/tasks/2