All Projects → mmiszy → typesafe-hapi

mmiszy / typesafe-hapi

Licence: MIT license
Typechecking for HapiJS based on Joi schemas!

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to typesafe-hapi

hapi-moon
Hassle-free and production ready hapi.js Server boilerplate
Stars: ✭ 23 (+9.52%)
Mutual labels:  hapi, hapijs
joi-to-typescript
Convert Joi Schemas to TypeScript interfaces
Stars: ✭ 73 (+247.62%)
Mutual labels:  hapi, joi
wily
Build Node.js APIs from the command line (Dead Project 😵)
Stars: ✭ 14 (-33.33%)
Mutual labels:  hapi, joi
hapi-cli
CLI to build API with Hapi, mongodb and mongoose. Work with Hapi V17.
Stars: ✭ 36 (+71.43%)
Mutual labels:  hapi, hapijs
felicity
Javascript object constructors and sample data based on Joi schema.
Stars: ✭ 107 (+409.52%)
Mutual labels:  hapi, joi
good-sentry
Sentry broadcasting for good process monitor
Stars: ✭ 15 (-28.57%)
Mutual labels:  hapi, hapijs
Crashlyzer
Crash viewer web application for displaying the crashes reported by rn-crash-reporter components from React Native mobile applications.
Stars: ✭ 59 (+180.95%)
Mutual labels:  hapi, hapijs
hapi-good-winston
A good reporter to send and log events with winston
Stars: ✭ 21 (+0%)
Mutual labels:  hapi, hapijs
nodejs-tutorials-hapi
Examples for the hapi tutorial series within the Future Studio University
Stars: ✭ 70 (+233.33%)
Mutual labels:  hapi, hapijs
sapper-authentication-demo
A demonstration of Auth with Sapper + JWT + Server Side Rendering + RBAC
Stars: ✭ 102 (+385.71%)
Mutual labels:  hapi, hapijs
hapi-plugin-mysql
Hapi plugin for MySQL
Stars: ✭ 17 (-19.05%)
Mutual labels:  hapi, hapijs
hapi-now-auth
Hapi token auth for bearer and jwt
Stars: ✭ 43 (+104.76%)
Mutual labels:  hapi, hapijs
noire-server
Hapi Boilerplate
Stars: ✭ 20 (-4.76%)
Mutual labels:  hapi
react-nes
React components for nes
Stars: ✭ 32 (+52.38%)
Mutual labels:  hapijs
BiB
📚 Library Management Application for Elementary Schools
Stars: ✭ 38 (+80.95%)
Mutual labels:  hapijs
hapio
A simple bridge plugin between HapiJS and SocketIO
Stars: ✭ 21 (+0%)
Mutual labels:  hapijs
hapi-mongo-models
📦 A hapi plugin for `mongo-models`
Stars: ✭ 101 (+380.95%)
Mutual labels:  hapi
typesafe-joi
A fork of joi that produces typed validation results in TypeScript
Stars: ✭ 75 (+257.14%)
Mutual labels:  joi
generator-hapi-plus
Hapi REST API generator with PostgreSql, MySql, Mongo plugins, automatic Swagger docs, authentication with JWT, logging, Docker etc
Stars: ✭ 21 (+0%)
Mutual labels:  hapi
hapi-doorkeeper
User authentication for web servers
Stars: ✭ 14 (-33.33%)
Mutual labels:  hapi

typesafe-hapi

typesafe-hapi is a fork of hapi which aims to improve typesafety. More precisely, this is a fork of @types/hapi__hapi because it has just redefined the essential APIs of hapi.

typesafe-hapi currently matches the API of hapi 18.3.x. It was tested with TypeScript 3.4.5.

How it works

typesafe-hapi uses typesafe-joi to infer correct type from Joi schemas automatically. It uses possesed type information to typecheck:

  • request.query
  • request.payload
  • request.params
  • response

Example

const payloadSchema = Joi.object({
  user: Joi.object({
    name: Joi.string().required(),
    email: Joi.string().required(),
  }).required(),
}).required();

const querySchema = Joi.object({
  search: Joi.string().optional().allow('', null),
}).required();

const paramsSchema = Joi.object({
  id: Joi.number().required()
}).required();

const responseSchema = Joi.object({
  id: Joi.number().required(),
  name: Joi.string().required(),
  email: Joi.string().required(),
  search: Joi.string().optional().allow(null),
}).required();

server.route({
  method: 'POST',
  path: '/:id',
  options: {
    validate: {
      payload: payloadSchema,
      query: querySchema,
      params: paramsSchema,
    },
    response: {
      schema: responseSchema,
    },
  },
  handler(request) {
    // type of `payload` is automatically inferred based on `options.validate.payload` schema
    const payload = request.payload; // { user: { name: string; email: string; }; } 
    const query = request.query; // { search?: string | null | undefined; }
    const params = request.params; // { id: number; } 

    // return type is also typechecked based on `options.response.schema`
    return {
      id: params.id,
      name: payload.user.name, // string
      email: payload.user.email, // string
      search: query.search, // string | null | undefined
    };
  },
});

Neat, huh? See more examples in index.test-d.ts.

Usage

Import and use hapi from typesafe-hapi:

import * as Hapi from 'typesafe-hapi'

In order to avoid any compatibility issues, and to be able to use existing packages and plugins easily, you should create an alias for typesafe-hapi and rename it to just hapi. In your tsconfig.json:

{
  "compilerOptions": {
    // … other options
    "paths": {
      "hapi": ["node_modules/typesafe-hapi"],
      "joi": ["node_modules/typesafe-joi"]
    }
  }
}
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].