All Projects → greylocklabs → teapot

greylocklabs / teapot

Licence: MIT license
Utilities for working with HTTP status codes, errors, and more

Programming Languages

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

Projects that are alternatives of or similar to teapot

blog
✍️无他术,唯勤读书而多为之,自工。
Stars: ✭ 62 (+342.86%)
Mutual labels:  koa, http-server
koa-better-error-handler
A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
Stars: ✭ 51 (+264.29%)
Mutual labels:  koa, error-handling
Parrot Mocker Web
Simple web server to forward received requests to real servers or just mock
Stars: ✭ 194 (+1285.71%)
Mutual labels:  koa
Blog
若川的博客—学习源码整体架构系列8篇,前端面试高频源码,微信搜索「若川视野」关注我,长期交流学习~
Stars: ✭ 234 (+1571.43%)
Mutual labels:  koa
Graphql Demo
🎉Koa + GraphQL + Apollo-Server demo
Stars: ✭ 215 (+1435.71%)
Mutual labels:  koa
Koa2 Boilerplate
[Deprecated] Minimal koa v2 boilerplate. 🤣
Stars: ✭ 196 (+1300%)
Mutual labels:  koa
Openapi Backend
Build, Validate, Route, Authenticate and Mock using OpenAPI
Stars: ✭ 216 (+1442.86%)
Mutual labels:  koa
Koa Passport Mongoose Graphql
Koa 2 server with Passport + Mongoose + GraphQL
Stars: ✭ 190 (+1257.14%)
Mutual labels:  koa
Javascript Boilerplate
Node.js+Koa.js+PostgreSQL+React.js+Webpack+Mocha+Makefile, a starter kit for new apps
Stars: ✭ 253 (+1707.14%)
Mutual labels:  koa
Nuxt Juejin Project
仿掘金web网站,使用服务端渲染。主要技术:nuxt + koa + vuex + axios + element-ui 。
Stars: ✭ 209 (+1392.86%)
Mutual labels:  koa
Koa Websocket
Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.
Stars: ✭ 224 (+1500%)
Mutual labels:  koa
Nobibi
一款基于Next.js+mongo的轻量级开源社区(open community by Next.js & mongo)
Stars: ✭ 209 (+1392.86%)
Mutual labels:  koa
Maka
Maka.js是基于react的一个前端微服务开发框架(makajs.com)
Stars: ✭ 200 (+1328.57%)
Mutual labels:  koa
Roa
async web framework inspired by koajs, lightweight but powerful.
Stars: ✭ 221 (+1478.57%)
Mutual labels:  koa
Egg Core
A core Pluggable framework based on koa.
Stars: ✭ 194 (+1285.71%)
Mutual labels:  koa
Strapi Sdk Javascript
🔌 Official JavaScript SDK for APIs built with Strapi.
Stars: ✭ 247 (+1664.29%)
Mutual labels:  koa
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (+1242.86%)
Mutual labels:  koa
Umajs
Umajs,easy-to-use framework base on Typescript
Stars: ✭ 209 (+1392.86%)
Mutual labels:  koa
Koa Webpack Middleware
webpack dev&hot middleware for koa2
Stars: ✭ 215 (+1435.71%)
Mutual labels:  koa
ndaify-web
NDAify helps you keep your trade secrets under wraps 🔒
Stars: ✭ 33 (+135.71%)
Mutual labels:  koa

Teapot

Utilities for working with HTTP status codes, errors, and more.

npm version build status coverage status


Teapot is an HTTP utility library for JavaScript, which leverages the Node.js HTTP library. It provides the following:

  1. The ability to get an HTTP status code: teapot.status(404) and teapot.status('not found') would both return 404.
  2. Useful error classes to represent HTTP error codes:
    • HTTPError: Base class to represent an HTTP error
    • ClientError and ServerError: Classes to represent 4xx and 5xx errors
    • Classes for every unique HTTP error status code, ranging from NotImplementedError to PaymentRequiredError
  3. A function to generate one of the HTTP error classes from a status code: teapot.error(404) would return an instance of NotFoundError. Great when handling responses from third-party APIs, when you might not know what status codes to expect all the time.

TypeScript definitions are included as well.

Installation

With yarn:

$ yarn add node-teapot

With npm:

$ npm install node-teapot

Usage

Get a status code

There are a variety of ways to get a status code from a number or string message:

teapot.status.code(404); // 404
teapot.status.code('not implemented'); // 405

teapot.status.codes['BAD GATEWAY']; // 502

teapot.status.MOVED_PERMANENTLY; // 301

Get a canned status message

teapot.status[200]; // "OK"

Create an HTTP error

Teapot's errors are compatible with Koa and Express:

throw new teapot.InternalServerError('Oops! Something went wrong.');

Generate an error from a status code

teapot.error(500) // returns instance of InternalServerError
teapot.error(204) // throws error because 204 is not an error code

teapot.error(404, 'My custom message', { // custom message w/ misc. additional properties
   expose: true,
   data: {
      misc: 'blah',
   },
})

Contributing

See CONTRIBUTING.md for guidelines.

License

MIT License. See LICENSE file for details.

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].