Skip to content

thejmazz/raml2fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

raml2fetch

fetch

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()

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/

About

generate schema safe fetch requests from raml

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published