https://github.com/zcong1993/koa-helpers
a set of helper functions for koa
https://github.com/zcong1993/koa-helpers
Last synced: 8 months ago
JSON representation
a set of helper functions for koa
- Host: GitHub
- URL: https://github.com/zcong1993/koa-helpers
- Owner: zcong1993
- License: mit
- Created: 2019-01-26T16:04:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-07-04T20:55:41.000Z (over 4 years ago)
- Last Synced: 2025-02-18T05:48:24.860Z (8 months ago)
- Language: TypeScript
- Homepage:
- Size: 377 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-helpers
[](https://npmjs.com/package/@zcong/koa-helpers) [](https://npmjs.com/package/@zcong/koa-helpers) [](https://circleci.com/gh/zcong1993/koa-helpers/tree/master) [](https://codecov.io/gh/zcong1993/koa-helpers)
> helpers set for koa
## Usage
### [error](./src/error)
```js
const { ApiError, simpleErrorHandler } = require('@zcong/koa-helpers')koa.use(simpleErrorHandler())
koa.use(ctx => {
throw new ApiError()
})// will get
// {
// "code": "INTERNAL_ERROR",
// "message: "INTERNAL_ERROR",
// "messages": []
// }
```### [password](./src/password)
```js
const { hash, compare } = require('@zcong/koa-helpers')const run = async () => {
const pass = 'password'
const hash = await hash(pass)
console.log(await compare(pass, hash)) // true
}
```### [transform](./src/transform)
```js
const { transform } = require('@zcong/koa-helpers')const post = {
'_id': 'uuid'
title: 'test',
content: 'test',
author: {
'_id': 'uuid',
name: 'user',
password: 'hashedpassword'
}
}console.log(transform(post, {
omit: ['author._id', 'author.password'],
mapping: {
'_id': 'id'
}
}))// output
// {
// id: 'uuid'
// title: 'test',
// content: 'test',
// author: {
// name: 'user'
// }
// }
```### [validator](./src/validator)
```js
const Joi = require('joi')
const { validate } = require('@zcong/koa-helpers')const schema = Joi.object().keys({
name: Joi.string()
.alphanum()
.min(6)
.required()
})const run = async () => {
await validate({ name: 'test' }, schema) // throw new ApiError('INVALID_INPUT', 'INVALID_INPUT', 400, ['"name" length must be at least 6 characters long'])
}
```## License
MIT © zcong1993