raml2fetch is an attempt to make fetch happen.
Given a raml document:
#%RAML 1.0
title: todos
types:
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
:
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.
// 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()
- 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
- URI parameters
- options