Skip to content

🔌 hapijs-based library for serving up RESTyped routes

Notifications You must be signed in to change notification settings

jbpionnier/restyped-hapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RESTyped Hapi Wrapper

Hapi route wrappers for declaring type-safe APIs with RESTyped.

Inspired by restyped-express-async

📘 Usage

npm install restyped-hapi


It's just like normal hapi, except you'll need to provide a RESTyped API definition file for the API you want to use, and return a Promise with your response value in order to activate type-checking.

import RestypedRouter from 'restyped-hapi'
import { MyAPI } from './MyAPI' // <- Your API's RESTyped defintion
import * as Hapi from 'hapi'

const server = new Hapi.Server()
const route = RestypedRouter<GiphyAPI>(server)

// You'll get a compile error if you declare a route that doesn't exist in your API defintion.
route({
  method: 'POST',
  path: '/login',
  async handler(request) {
    // Error if you try to access body properties that don't exist in your API definition.`
    const { username, password, twoFactorPin } = request.payload
    //      ^ string  ^ string  ^ number
  
    const accessToken = await User.login(username, password, twoFactorPin)
  
    // Error if you don't return the response type defined in your API defintion.
    return accessToken
  },
})

About

🔌 hapijs-based library for serving up RESTyped routes

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published