Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thejmazz/raml2fetch
generate schema safe fetch requests from raml
https://github.com/thejmazz/raml2fetch
Last synced: 19 days ago
JSON representation
generate schema safe fetch requests from raml
- Host: GitHub
- URL: https://github.com/thejmazz/raml2fetch
- Owner: thejmazz
- Created: 2016-06-26T21:07:24.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-06-27T14:02:06.000Z (over 8 years ago)
- Last Synced: 2024-11-07T08:51:33.765Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# raml2fetch
![fetch](https://cloud.githubusercontent.com/assets/1270998/16381488/df1afb3c-3c4a-11e6-9aa2-218e11cb56e5.gif)
raml2fetch is an attempt *to make fetch happen*.
Given a [raml document](https://github.com/thejmazz/raml2fetch/blob/master/test/todos/todos.raml):
```yaml
#%RAML 1.0
title: todostypes:
Todo:
properties:
content: string
completed: boolean
id: number
example:
content: "Make lunch"
completed: true
id: 2/todos:
post:
body:
application/json:
properties:
content: string
example:
content: "Buy eggs"
responses:
200:
body:
application/json:
type: Todo
```You provide it to raml2fetch, with an optional overwriting `baseURL`:
```js
const api = raml2fetch(path.resolve(__dirname, 'todos.raml'), BASE_URL)
```Then `api` has an object for each route, with functions on each method. It will verify the body schema matches the RAML spec, check if the response code has an entry in RAML, and verify if the response body matches the its schema as well. Each of these can be configured by turning on *ignore-x* flags.
```js
// Will reject b/c no body provided
api['/todos'].post()// Will reject b/c schema types do not match
api['/todos'].post({ content: 42} )// Will work. Also checks response schema types
api['/todos'].post({ content: 'Buy eggs' })// Will reject b/c no need for body with GET
api['/todos/all'].get({ foo: 'bar' })// Will work. Verifies response schema is as expected
api['/todos/all'].get()
```## Why
- keep API and front end in sync - changes in URI parameters or body schemas will have an immediate effect and notify front end developer of errors
- autogenerate an API for a given RAML document## Todos
- URI parameters
- options## License
MIT [http://jmazz.mit-license.org/](http://jmazz.mit-license.org/)