All Projects → rawrmaan → Restyped

rawrmaan / Restyped

Licence: mit
End-to-end typing for REST APIs with TypeScript

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Restyped

Swagger Express Ts
Generate and serve swagger.json
Stars: ✭ 102 (-64.46%)
Mutual labels:  rest-api, rest, express
Rest Api Nodejs Mongodb
A boilerplate for REST API Development with Node.js, Express, and MongoDB
Stars: ✭ 672 (+134.15%)
Mutual labels:  rest-api, rest, express
Resource Router Middleware
🚴 Express REST resources as middleware mountable anywhere
Stars: ✭ 124 (-56.79%)
Mutual labels:  rest-api, rest, express
Axios Rest
A simple axios wrapper to make rest api call delightful
Stars: ✭ 41 (-85.71%)
Mutual labels:  rest-api, rest, axios
Node Express Mongoose Passport Jwt Rest Api Auth
Node, express, mongoose, passport and JWT REST API authentication example
Stars: ✭ 146 (-49.13%)
Mutual labels:  rest-api, rest, express
Nodejs Restful Api
How to create a RESTful CRUD API using Nodejs?
Stars: ✭ 285 (-0.7%)
Mutual labels:  rest, express
Spring Boot Postgresql Jpa Hibernate Rest Api Demo
Building RESTful APIs with Spring Boot, PostgreSQL, JPA and Hibernate
Stars: ✭ 209 (-27.18%)
Mutual labels:  rest-api, rest
Symfony Flex Backend
Symfony Flex REST API template project
Stars: ✭ 214 (-25.44%)
Mutual labels:  rest-api, rest
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+1101.39%)
Mutual labels:  rest-api, rest
Awot
Arduino web server library.
Stars: ✭ 200 (-30.31%)
Mutual labels:  rest-api, rest
Bookmarks.dev
Bookmarks and Code Snippets Manager for Developers & Co
Stars: ✭ 218 (-24.04%)
Mutual labels:  rest-api, rest
Express React Fullstack
Simple, Useful Full Stack Express and React Application
Stars: ✭ 286 (-0.35%)
Mutual labels:  express, axios
Stampede
🦕 Deno REST framework/eco-system
Stars: ✭ 205 (-28.57%)
Mutual labels:  rest-api, rest
Blogbackendproject
Backend code for my blogs, develop with Django Rest framework.
Stars: ✭ 204 (-28.92%)
Mutual labels:  rest-api, rest
Magic
Create your .Net Core/Angular/Database CRUD Web apps by simply clicking a button
Stars: ✭ 214 (-25.44%)
Mutual labels:  rest-api, rest
Jenkins Rest
Java client, built on top of jclouds, for working with Jenkins REST API
Stars: ✭ 201 (-29.97%)
Mutual labels:  rest-api, rest
Json Schema Tools
Packages for working with JSON Schema and JSON Hyper-Schema
Stars: ✭ 232 (-19.16%)
Mutual labels:  rest-api, rest
Prest
PostgreSQL ➕ REST, low-code, simplify and accelerate development, ⚡ instant, realtime, high-performance on any Postgres application, existing or new
Stars: ✭ 3,023 (+953.31%)
Mutual labels:  rest-api, rest
Fosrestbundle
This Bundle provides various tools to rapidly develop RESTful API's with Symfony
Stars: ✭ 2,683 (+834.84%)
Mutual labels:  rest-api, rest
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (-11.85%)
Mutual labels:  rest-api, rest

End-to-end typing for REST APIs with TypeScript

Motivation

Read the blog post

Benefits

  • End-to-end typing. Share request and response types between your client and server for ease of use and peace of mind.
  • Unopinionated. Works with any new or existing REST API.
  • Universal. Can support any server framework or REST client.
  • Lightweight Weightless. Client and server implementations add no runtime code--It's Just Types™.
  • Use existing syntax. Declare and call your routes the same way you always have.
  • Great for private APIs. Keep API clients across your organization in sync with the latest changes.
  • Great for public APIs. Create a RESTyped definition so TypeScript users can consume your API fully typed.
  • Easy to learn and use. Start using RESTyped in less than one minute per route.

How to use it

RESTyped is a specification (see below). You can use these server and client packages along with a RESTyped defintion file to declare and consume APIs in a type-safe manner:

You can help make RESTyped more useful by implementing support in your favorite server framework or HTTP client!

RESTyped requires TypeScript 2.4 or higher.

Specification

It's very easy to get started with RESTyped. Just follow a few steps to type your existing API or create a new typed API:

  • Your API should be defined in one interface, exported as {my_api_name}API from a file ending in .d.ts
  • Each route is a top level key in the interface. You should exclude any prefixes like /api/.
  • Each route can have up to one key per valid HTTP method:
    • GET, POST, PUT, PATCH, DELETE, HEAD or OPTIONS
  • Each HTTP method can have one or more of the following keys:
    • params: Route params in the URL (e.g. /users/:id would have id as a param)
    • query: Query string params, typically used in GET requests (e.g. req.query in express)
    • body: JSON body object (e.g. req.body in express or data object in an axios request)
    • response: The route's JSON response

Also see the spec implementation base defintions.

Example: my-social-api.d.ts

interface User {
  // Model inteface--could be imported from another file
  email: string
  name: string
  gender: 'Male' | 'Female' | 'Other'
}

export interface MySocialAPI {
  '/users': {
    // Route name (without prefix, if you have one)
    GET: {
      // Any valid HTTP method
      query: {
        // Query string params (e.g. /me?includeProfilePics=true)
        includeProfilePics?: boolean
      }
      response: User[] // JSON response
    }
  }

  '/user/:id/send-message': {
    POST: {
      params: {
        // Inline route params
        id: string
      }
      body: {
        // JSON request body
        message: string
      }
      response: {
        // JSON response
        success: boolean
      }
    }
  }
}

Full-Stack Example

1. Define your API

food-delivery-api.d.ts

export interface FoodDeliveryAPI {
  '/me/orders': {
    POST: {
      body: {
        foodItemIds: number[]
        address: string
        paymentMethod: 'card' | 'cash'
      }
      response: {
        success: boolean
        eta?: string
      }
    }
  }

  // ...other routes...
}

2. Declare the API via express

import RestypedRouter from 'restyped-express-async'
import { FoodDeliveryAPI } from './food-delivery-api'
import * as express from 'express'

const app = express()

const apiRouter = express.Router()
app.use('/api', apiRouter)

const router = RestypedRouter<FoodDeliveryAPI>(apiRouter)

router.post('/me/orders', async req => {
  // Will not compile if you attempt to access an invalid body property
  const {
    foodItemIds, // number[]
    address, // string
    paymentMethod // 'card' | 'cash'
  } = req.body

  const success = await OrderModel.order(foodItemIds, address, paymentMethod)

  // Will not compile if returned value is not of type {success: boolean}
  return { success }
})

3. Consume the API via axios

import axios from 'restyped-axios'
import { FoodDeliveryAPI } from './food-delivery-api'

const api = axios.create<FoodDeliveryAPI>({
  baseURL: 'https://fooddelivery.com/api/'
})

async function order() {
  // Will not compile if you pass incorrectly typed body params
  const res = await api.post('/me/orders', {
    foodItemIds: [142, 788],
    address: '1601 Market St, Phiadelphia, PA 19103',
    paymentMethod: 'cash'
  })

  // TypeScript knows that res.data is of type {success: boolean, eta?: string}
  const { success, eta } = res.data
}

What RESTyped isn't

  • A replacement for API docs. A RESTyped spec will help you get the routes and types right, but doesn't provide any context or explanation of your API.

Popular APIs to try out

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].