https://github.com/jmjuanes/url-api
Format and generate the urls for an API service
https://github.com/jmjuanes/url-api
api nodejs url url-api
Last synced: about 2 months ago
JSON representation
Format and generate the urls for an API service
- Host: GitHub
- URL: https://github.com/jmjuanes/url-api
- Owner: jmjuanes
- License: mit
- Created: 2017-04-19T09:15:12.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-03T11:02:01.000Z (about 9 years ago)
- Last Synced: 2023-12-25T20:04:38.096Z (over 2 years ago)
- Topics: api, nodejs, url, url-api
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/url-api
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# url-api
[](https://www.npmjs.com/package/url-api)
[](https://www.npmjs.com/package/url-api)
[](https://github.com/jmjuanes/url-api)
> Format and generate the urls for an API service
This package will format all the string patterns used in frameworks like [express](http://expressjs.com/en/guide/routing.html) into full urls like [this](https://api.github.com).
## Usage
```javascript
//Import dependencies
var url_api = require('url-api');
//Url object
var url_obj = { protocol: 'https', hostname: 'mywebsite.com' };
//Api urls
var api = { projects: '/projects/:project', home: '/', users: '/users/:user' };
//Simple api
url_api(url_obj, api);
// {
// projects: 'https://mywebsite.com/projects/{project}',
// home: 'https://mywebsite.com/',
// users: 'https://mywebsite.com/users/{user}'
// }
//With options
url_api(url_obj, api, { prefix: '{{', suffix: '}}' });
// {
// projects: 'https://mywebsite.com/projects/{{project}}',
// home: 'https://mywebsite.com/',
// users: 'https://mywebsite.com/users/{{user}}'
// }
```
## API
### url_api(url_obj, api[, opt])
Returns a new object with all the pathnames formatted. This method accepts the following arguments:
- `url_obj`: a [url object](https://nodejs.org/api/url.html#url_url_strings_and_url_objects) with the API host information.
- `api`: an object with all the paths strings of your RESTful API service.
- `opt` (optionally): an object with the options:
- `find`: a regular expression to find the pattern parameters. Default is set to find the parameters starting with `:`.
- `prefix`: the formatted pattern parameter prefix. Default is `{`.
- `suffix`: the formatted pattern parameter suffix. Default is `}`.
## License
[MIT LICENSE](./LICENSE) © Josemi Juanes.