Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/minjieliu/koa-query-pretty
Pretty query middleware for koa.
https://github.com/minjieliu/koa-query-pretty
koa koa-middleware koa-query koa-query-pretty koa2 query
Last synced: 22 days ago
JSON representation
Pretty query middleware for koa.
- Host: GitHub
- URL: https://github.com/minjieliu/koa-query-pretty
- Owner: MinJieLiu
- License: mit
- Created: 2017-06-28T09:35:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-07-03T12:53:29.000Z (over 7 years ago)
- Last Synced: 2024-10-01T02:21:37.241Z (about 1 month ago)
- Topics: koa, koa-middleware, koa-query, koa-query-pretty, koa2, query
- Language: JavaScript
- Homepage:
- Size: 49.8 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README-zh_CN.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-query-pretty
Pretty query koa 中间件.
[![npm](https://img.shields.io/npm/v/koa-query-pretty.svg?style=flat-square)](https://www.npmjs.com/package/koa-query-pretty)
[![Build Status](https://travis-ci.org/MinJieLiu/koa-query-pretty.svg?branch=master)](https://travis-ci.org/MinJieLiu/koa-query-pretty)
[![Coverage Status](https://coveralls.io/repos/github/MinJieLiu/koa-query-pretty/badge.svg?branch=master)](https://coveralls.io/github/MinJieLiu/koa-query-pretty?branch=master)
[![npm](https://img.shields.io/npm/dt/koa-query-pretty.svg?style=flat-square)](https://github.com/MinJieLiu/koa-query-pretty)### 说明
`koa` 默认使用 `queryString` 解析 `GET` 参数,但解析后的参数都是 `string` 类型。
`koa-query-pretty` 可以将类似于 `int`, `float`,` boolean`, `null`, `undefined` 转换为具体类型。
### 安装
```
yarn add koa-query-pretty
```支持 node.js v7.6+
### 使用
```js
const Koa = require('koa');
const queryPretty = require('koa-query-pretty');const app = new Koa();
app.use(queryPretty());
```### 效果
/home?id=1&name=jack&enable=true&money=2.5&hobby=1&hobby=2
结果
ctx.query:
```json
{
"id": 1,
"name": "jack",
"enable": true,
"money": 2.5,
"hobby": [
1,
2
]
}
```### 配置
#### override
覆盖 `ctx.query` 参数,默认 `true`,否则使用 `ctx.prettyQuery` 获取
```js
app.use(queryPretty({ override: false }));app.use(async (ctx, next) => {
console.log(ctx.prettyQuery);
await next();
});
```