Skip to content

Instantly share code, notes, and snippets.

@rawrmaan
Last active December 14, 2017 22:14
Show Gist options
  • Save rawrmaan/fe52e044c4e7f066f23e53088d1c3374 to your computer and use it in GitHub Desktop.
Save rawrmaan/fe52e044c4e7f066f23e53088d1c3374 to your computer and use it in GitHub Desktop.
RESTyped route defintion
import RestypedRouter from 'restyped-express-async'
import {MyAPI} from './MyAPI' // <- Your API's RESTyped defintion
import * as express from 'express'
const app = express()
const router = RestypedRouter<MyAPI>(app)
// TS compile error if you declare a route that doesn't exist in your API defintion
router.post('/login', async req => {
// Error if you try to access non-existent body properties
const {username, password, twoFactorPin} = req.body
// ^ string ^ string ^ number
const accessToken = await User.login(username, password, twoFactorPin)
// Error if you don't return the specified response type
return accessToken
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment