1 | # @36node/query-normalizr
|
2 |
|
3 | [![version][0]][1] [![downloads][2]][3]
|
4 |
|
5 | query-normalizr 的作用: 将 url 中的 query 规则化成方便 sdk 和 service 层调用的数据格式。
|
6 |
|
7 | ## Install
|
8 |
|
9 | ```bash
|
10 | yarn add @36node/query-normalizr
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | import { queryNormalizr } from "@36node/query-normalizr";
|
17 |
|
18 | // koa app
|
19 | app.use(normalizr(queryNormalizr));
|
20 | ```
|
21 |
|
22 | ## API
|
23 |
|
24 | ### middleware
|
25 |
|
26 | ```js
|
27 | normalizr(options);
|
28 |
|
29 | // return koa middleware
|
30 | ```
|
31 |
|
32 | ### normalize
|
33 |
|
34 | ```js
|
35 | import { normalize } from "@36node/query-normalizr";
|
36 | import qs from "query-string";
|
37 |
|
38 | const queryStr =
|
39 | " _expand=department&_group=type&_limit=10&_offset=0&_populate=user&_select=name&_select=age&_sort=updatedAt&_sort=-createdAt&age_gt=10&age_lt=20&level_gte=10&level_lte=20&plate_like=沪A&plate_like=沪B&tag_ne=pretty&title_like=hello&type=test1&type=test2";
|
40 |
|
41 | normalize(qs.parse(queryStr));
|
42 |
|
43 | /*
|
44 | return {
|
45 | limit: 10,
|
46 | offset: 0,
|
47 | sort: ["updatedAt", "-createdAt"],
|
48 | populate: "user",
|
49 | select: ["name", "age"],
|
50 | group: "type",
|
51 | filter: {
|
52 | age: {
|
53 | $gt: "10",
|
54 | $lt: "20",
|
55 | },
|
56 | level: {
|
57 | $gte: "10",
|
58 | $lte: "20",
|
59 | },
|
60 | plate: {
|
61 | $regex: [/沪A/i, /沪B/i],
|
62 | },
|
63 | tag: {
|
64 | $ne: "pretty",
|
65 | },
|
66 | title: {
|
67 | $regex: /hello/i,
|
68 | },
|
69 | type: ["test1", "test2"]
|
70 | },
|
71 | _expand: "department",
|
72 | };
|
73 | */
|
74 | ```
|
75 |
|
76 | ### denormalize
|
77 |
|
78 | ```js
|
79 | import { denormalize } from "@36node/query-normalizr";
|
80 | import qs from "query-string";
|
81 |
|
82 | const queryObj = {
|
83 | limit: 10,
|
84 | offset: 0,
|
85 | sort: ["updatedAt", "-createdAt"],
|
86 | populate: "user",
|
87 | select: ["name", "age"],
|
88 | group: "type",
|
89 | filter: {
|
90 | age: {
|
91 | $gt: "10",
|
92 | $lt: "20",
|
93 | },
|
94 | level: {
|
95 | $gte: "10",
|
96 | $lte: "20",
|
97 | },
|
98 | plate: {
|
99 | $regex: [/沪A/i, /沪B/i],
|
100 | },
|
101 | tag: {
|
102 | $ne: "pretty",
|
103 | },
|
104 | title: {
|
105 | $regex: /hello/i,
|
106 | },
|
107 | type: ["test1", "test2"],
|
108 | },
|
109 | _expand: "department",
|
110 | };
|
111 |
|
112 | qs.stringfy(denormalize(queryObj));
|
113 |
|
114 | // return " _expand=department&_group=type&_limit=10&_offset=0&_populate=user&_select=name&_select=age&_sort=updatedAt&_sort=-createdAt&age_gt=10&age_lt=20&assignees=%2A&followers=none&level_gte=10&level_lte=20&plate_like=%E6%B2%AAA&plate_like=%E6%B2%AAB&q=hello&tag_ne=pretty&title_like=hello&type=test1&type=test2"
|
115 | ```
|
116 |
|
117 | ## What is query normalizr
|
118 |
|
119 | ![image](https://user-images.githubusercontent.com/4343458/53739979-0c2d6f00-3ece-11e9-9c32-9516ecea9c25.png)
|
120 |
|
121 | ### Query in route (QIR)
|
122 |
|
123 | reference in [url.md](../../docs/url.md)
|
124 |
|
125 | ## Contributing
|
126 |
|
127 | 1. Fork it!
|
128 | 2. Create your feature branch: `git checkout -b my-new-feature`
|
129 | 3. Commit your changes: `git commit -am 'Add some feature'`
|
130 | 4. Push to the branch: `git push origin my-new-feature`
|
131 | 5. Submit a pull request :D
|
132 |
|
133 | ## Author
|
134 |
|
135 | **query-normalizr** © [36node](https://github.com/36node), Released under the [MIT](./LICENSE) License.
|
136 |
|
137 | Authored and maintained by 36node with help from contributors ([list](https://github.com/36node/query-normalizr/contributors)).
|
138 |
|
139 | > [github.com/zzswang](https://github.com/zzswang) · GitHub [@36node](https://github.com/36node) · Twitter [@y](https://twitter.com/y)
|
140 |
|
141 | [0]: https://img.shields.io/npm/v/@36node/query-normalizr.svg?style=flat
|
142 | [1]: https://npmjs.com/package/@36node/query-normalizr
|
143 | [2]: https://img.shields.io/npm/dm/@36node/query-normalizr.svg?style=flat
|
144 | [3]: https://npmjs.com/package/@36node/query-normalizr
|