Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spikef/open-path
Url path parser for OpenAPI.
https://github.com/spikef/open-path
Last synced: about 2 months ago
JSON representation
Url path parser for OpenAPI.
- Host: GitHub
- URL: https://github.com/spikef/open-path
- Owner: Spikef
- Created: 2018-01-15T10:28:45.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-12T08:49:51.000Z (almost 7 years ago)
- Last Synced: 2024-11-02T16:24:56.526Z (2 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# open-path
> Url path parser for OpenAPI.
## Install
```bash
$ npm i open-path
```## Usage
```javascript
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.
```javascript
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.
```javascript
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.
```javascript
var parse = new Parse('/:foo+');
```## Path
Path can be suffixed with an asterisk (*) or plus sign (+) to allow more path matches.
### Zero or more
```javascript
var parse = new Parse('/api*');
```### One or more
```javascript
var parse = new Parse('/api+');
```