open-path
v1.0.6
Published
Url path parser for OpenAPI.
Downloads
11
Readme
open-path
Url path parser for OpenAPI.
Install
$ npm i open-path
Usage
var Parse = require('open-path');
var parse = new Parse('/user/{category}/today/{tag}/detail');
// or
// var parse = new Parse('/user/:category/today/:tag/detail');
console.log(parse.path);
// prints: /user/{category}/today/{tag}/detail
// match
parse.match(ctx.path); // returns params if matched or null
// build
parse.build({
category: 'tech',
tag: 'mobile'
});
// returns: /user/tech/today/mobile/detail
Parameters
The path argument is used to define parameters.
Named Parameters
Named parameters are defined by prefixing a colon to the parameter name (:foo) or surround with braces ({foo}). By default, the parameter will match until the following path segment.
Unnamed Parameters
Unnamed parameters are defined by regexp and naming by index.
Parameter Modifiers
Optional
Parameters can be suffixed with a question mark (?) to make the parameter optional.
var parse = new Parse('/:foo/:bar?');
Zero or more
Parameters can be suffixed with an asterisk (*) to denote a zero or more parameter matches. The prefix is taken into account for each match.
var parse = new Parse('/:foo*');
One or more
Parameters can be suffixed with a plus sign (+) to denote a one or more parameter matches. The prefix is taken into account for each match.
var parse = new Parse('/:foo+');
Path
Path can be suffixed with an asterisk (*) or plus sign (+) to allow more path matches.
Zero or more
var parse = new Parse('/api*');
One or more
var parse = new Parse('/api+');