@cgarciagarcia/react-query-builder 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +3 -3
  2. package/dist/actions/filter.js +44 -0
  3. package/dist/actions/include.js +23 -0
  4. package/{src/actions/index.ts → dist/actions/index.js} +0 -2
  5. package/dist/actions/sort.js +26 -0
  6. package/dist/dist/actions/filter.js +47 -0
  7. package/dist/dist/actions/include.js +26 -0
  8. package/dist/dist/actions/index.js +3 -0
  9. package/dist/dist/actions/sort.js +29 -0
  10. package/dist/dist/dist/actions/filter.js +47 -0
  11. package/dist/dist/dist/actions/include.js +26 -0
  12. package/dist/dist/dist/actions/index.js +3 -0
  13. package/dist/dist/dist/actions/sort.js +29 -0
  14. package/dist/dist/dist/useQueryBuilder.js +86 -0
  15. package/dist/dist/dist/utils/alias.js +4 -0
  16. package/dist/dist/dist/utils/build.js +30 -0
  17. package/dist/dist/dist/utils/state.js +1 -0
  18. package/dist/dist/index.js +3 -0
  19. package/dist/dist/useQueryBuilder.js +86 -0
  20. package/dist/dist/utils/alias.js +4 -0
  21. package/dist/dist/utils/build.js +30 -0
  22. package/dist/dist/utils/index.js +3 -0
  23. package/dist/dist/utils/state.js +1 -0
  24. package/dist/index.js +3 -0
  25. package/dist/tsconfig.tsbuildinfo +1 -0
  26. package/dist/useQueryBuilder.js +81 -0
  27. package/dist/utils/alias.js +4 -0
  28. package/dist/utils/build.js +29 -0
  29. package/dist/utils/index.js +3 -0
  30. package/dist/utils/state.js +1 -0
  31. package/package.json +5 -2
  32. package/.eslintignore +0 -1
  33. package/.eslintrc.cjs +0 -56
  34. package/.nvmrc +0 -1
  35. package/.prettierignore +0 -1
  36. package/prettier.config.cjs +0 -28
  37. package/src/actions/filter.ts +0 -46
  38. package/src/actions/include.ts +0 -14
  39. package/src/actions/sort.ts +0 -14
  40. package/src/useQueryBuilder.ts +0 -126
  41. package/src/utils/alias.ts +0 -11
  42. package/src/utils/build.ts +0 -31
  43. package/src/utils/state.ts +0 -8
  44. package/tsconfig.json +0 -27
  45. /package/{src/index.ts → dist/dist/dist/index.js} +0 -0
  46. /package/{src/utils/index.ts → dist/dist/dist/utils/index.js} +0 -0
package/README.md CHANGED
@@ -18,15 +18,15 @@ using your favorite library for frontend `React.js` using a customize hook which
18
18
  Here is a simple example:
19
19
 
20
20
  ```js
21
- import {useQueryBuilder} from "@cgarciagarcia/react-qiery-builder";
21
+ import {useQueryBuilder} from "@cgarciagarcia/react-query-builder";
22
22
 
23
- const baseConfigIfYouWant = {
23
+ const baseConfig = {
24
24
  aliases: {
25
25
  "frontend_name": "backend_name",
26
26
  }
27
27
  }
28
28
 
29
- const builder = useQueryBuilder(baseConfigIfYouWant)
29
+ const builder = useQueryBuilder(baseConfig)
30
30
  .filters("age", 18)
31
31
  .sorts("created_at")// by default sorting asc
32
32
  .sorts("age", 'desc') // sorting desc
@@ -0,0 +1,44 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
13
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
14
+ if (ar || !(i in from)) {
15
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
16
+ ar[i] = from[i];
17
+ }
18
+ }
19
+ return to.concat(ar || Array.prototype.slice.call(from));
20
+ };
21
+ import { usingAlias, } from "@/utils";
22
+ export var filterAction = function (attribute, value, state) {
23
+ var _a;
24
+ var alias = usingAlias(state, attribute);
25
+ var prevFilter;
26
+ var allFilters = state.filters.reduce(function (filters, filter) {
27
+ if (filter.attribute === alias) {
28
+ prevFilter = filter;
29
+ return filters;
30
+ }
31
+ return __spreadArray(__spreadArray([], filters, true), [filter], false);
32
+ }, []);
33
+ var val = Array.isArray(value) ? value : [value];
34
+ var newState = __assign(__assign({}, state), { filters: __spreadArray(__spreadArray([], allFilters, true), [
35
+ {
36
+ attribute: attribute,
37
+ value: __spreadArray(__spreadArray([], ((_a = prevFilter === null || prevFilter === void 0 ? void 0 : prevFilter.value) !== null && _a !== void 0 ? _a : []), true), val, true),
38
+ },
39
+ ], false) });
40
+ return newState;
41
+ };
42
+ export var clearFilterAction = function (state) {
43
+ return __assign(__assign({}, state), { filters: [] });
44
+ };
@@ -0,0 +1,23 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
13
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
14
+ if (ar || !(i in from)) {
15
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
16
+ ar[i] = from[i];
17
+ }
18
+ }
19
+ return to.concat(ar || Array.prototype.slice.call(from));
20
+ };
21
+ export var includeAction = function (includes, state) {
22
+ return __assign(__assign({}, state), { includes: __spreadArray(__spreadArray([], state.includes, true), [includes], false) });
23
+ };
@@ -1,5 +1,3 @@
1
1
  export * from "@/actions/sort";
2
2
  export * from "@/actions/filter";
3
3
  export * from "@/actions/include";
4
-
5
- export type Actions = "sort" | "filter" | "clear_filter" | "include";
@@ -0,0 +1,26 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
13
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
14
+ if (ar || !(i in from)) {
15
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
16
+ ar[i] = from[i];
17
+ }
18
+ }
19
+ return to.concat(ar || Array.prototype.slice.call(from));
20
+ };
21
+ import { usingAlias } from "@/index";
22
+ export var sortAction = function (sorts, state) {
23
+ var attribute = sorts[0], direction = sorts[1];
24
+ var attributeAliased = usingAlias(state, attribute);
25
+ return __assign(__assign({}, state), { sorts: __spreadArray(__spreadArray([], state.sorts, true), [[attributeAliased, direction]], false) });
26
+ };
@@ -0,0 +1,47 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2)
15
+ for (var i = 0, l = from.length, ar; i < l; i++) {
16
+ if (ar || !(i in from)) {
17
+ if (!ar)
18
+ ar = Array.prototype.slice.call(from, 0, i);
19
+ ar[i] = from[i];
20
+ }
21
+ }
22
+ return to.concat(ar || Array.prototype.slice.call(from));
23
+ };
24
+ import { usingAlias, } from "@/utils";
25
+ export var filterAction = function (attribute, value, state) {
26
+ var _a;
27
+ var alias = usingAlias(state, attribute);
28
+ var prevFilter;
29
+ var allFilters = state.filters.reduce(function (filters, filter) {
30
+ if (filter.attribute === alias) {
31
+ prevFilter = filter;
32
+ return filters;
33
+ }
34
+ return __spreadArray(__spreadArray([], filters, true), [filter], false);
35
+ }, []);
36
+ var val = Array.isArray(value) ? value : [value];
37
+ var newState = __assign(__assign({}, state), { filters: __spreadArray(__spreadArray([], allFilters, true), [
38
+ {
39
+ attribute: attribute,
40
+ value: __spreadArray(__spreadArray([], ((_a = prevFilter === null || prevFilter === void 0 ? void 0 : prevFilter.value) !== null && _a !== void 0 ? _a : []), true), val, true),
41
+ },
42
+ ], false) });
43
+ return newState;
44
+ };
45
+ export var clearFilterAction = function (state) {
46
+ return __assign(__assign({}, state), { filters: [] });
47
+ };
@@ -0,0 +1,26 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2)
15
+ for (var i = 0, l = from.length, ar; i < l; i++) {
16
+ if (ar || !(i in from)) {
17
+ if (!ar)
18
+ ar = Array.prototype.slice.call(from, 0, i);
19
+ ar[i] = from[i];
20
+ }
21
+ }
22
+ return to.concat(ar || Array.prototype.slice.call(from));
23
+ };
24
+ export var includeAction = function (includes, state) {
25
+ return __assign(__assign({}, state), { includes: __spreadArray(__spreadArray([], state.includes, true), [includes], false) });
26
+ };
@@ -0,0 +1,3 @@
1
+ export * from "@/actions/sort";
2
+ export * from "@/actions/filter";
3
+ export * from "@/actions/include";
@@ -0,0 +1,29 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2)
15
+ for (var i = 0, l = from.length, ar; i < l; i++) {
16
+ if (ar || !(i in from)) {
17
+ if (!ar)
18
+ ar = Array.prototype.slice.call(from, 0, i);
19
+ ar[i] = from[i];
20
+ }
21
+ }
22
+ return to.concat(ar || Array.prototype.slice.call(from));
23
+ };
24
+ import { usingAlias } from "@/index";
25
+ export var sortAction = function (sorts, state) {
26
+ var attribute = sorts[0], direction = sorts[1];
27
+ var attributeAliased = usingAlias(state, attribute);
28
+ return __assign(__assign({}, state), { sorts: __spreadArray(__spreadArray([], state.sorts, true), [[attributeAliased, direction]], false) });
29
+ };
@@ -0,0 +1,47 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2)
15
+ for (var i = 0, l = from.length, ar; i < l; i++) {
16
+ if (ar || !(i in from)) {
17
+ if (!ar)
18
+ ar = Array.prototype.slice.call(from, 0, i);
19
+ ar[i] = from[i];
20
+ }
21
+ }
22
+ return to.concat(ar || Array.prototype.slice.call(from));
23
+ };
24
+ import { usingAlias, } from "@/utils";
25
+ export var filterAction = function (attribute, value, state) {
26
+ var _a;
27
+ var alias = usingAlias(state, attribute);
28
+ var prevFilter;
29
+ var allFilters = state.filters.reduce(function (filters, filter) {
30
+ if (filter.attribute === alias) {
31
+ prevFilter = filter;
32
+ return filters;
33
+ }
34
+ return __spreadArray(__spreadArray([], filters, true), [filter], false);
35
+ }, []);
36
+ var val = Array.isArray(value) ? value : [value];
37
+ var newState = __assign(__assign({}, state), { filters: __spreadArray(__spreadArray([], allFilters, true), [
38
+ {
39
+ attribute: attribute,
40
+ value: __spreadArray(__spreadArray([], ((_a = prevFilter === null || prevFilter === void 0 ? void 0 : prevFilter.value) !== null && _a !== void 0 ? _a : []), true), val, true),
41
+ },
42
+ ], false) });
43
+ return newState;
44
+ };
45
+ export var clearFilterAction = function (state) {
46
+ return __assign(__assign({}, state), { filters: [] });
47
+ };
@@ -0,0 +1,26 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2)
15
+ for (var i = 0, l = from.length, ar; i < l; i++) {
16
+ if (ar || !(i in from)) {
17
+ if (!ar)
18
+ ar = Array.prototype.slice.call(from, 0, i);
19
+ ar[i] = from[i];
20
+ }
21
+ }
22
+ return to.concat(ar || Array.prototype.slice.call(from));
23
+ };
24
+ export var includeAction = function (includes, state) {
25
+ return __assign(__assign({}, state), { includes: __spreadArray(__spreadArray([], state.includes, true), [includes], false) });
26
+ };
@@ -0,0 +1,3 @@
1
+ export * from "@/actions/sort";
2
+ export * from "@/actions/filter";
3
+ export * from "@/actions/include";
@@ -0,0 +1,29 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2)
15
+ for (var i = 0, l = from.length, ar; i < l; i++) {
16
+ if (ar || !(i in from)) {
17
+ if (!ar)
18
+ ar = Array.prototype.slice.call(from, 0, i);
19
+ ar[i] = from[i];
20
+ }
21
+ }
22
+ return to.concat(ar || Array.prototype.slice.call(from));
23
+ };
24
+ import { usingAlias } from "@/index";
25
+ export var sortAction = function (sorts, state) {
26
+ var attribute = sorts[0], direction = sorts[1];
27
+ var attributeAliased = usingAlias(state, attribute);
28
+ return __assign(__assign({}, state), { sorts: __spreadArray(__spreadArray([], state.sorts, true), [[attributeAliased, direction]], false) });
29
+ };
@@ -0,0 +1,86 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ import { useReducer, useState } from "react";
14
+ import { build, clearFilterAction, filterAction, includeAction, sortAction, } from "@/index";
15
+ var reducer = function (state, action) {
16
+ switch (action === null || action === void 0 ? void 0 : action.type) {
17
+ case "filter": {
18
+ var filter = action.payload;
19
+ return filterAction(filter.attribute, filter.value, state);
20
+ }
21
+ case "clear_filter": {
22
+ return clearFilterAction(state);
23
+ }
24
+ case "include": {
25
+ var includes = action.payload;
26
+ return includeAction(includes, state);
27
+ }
28
+ case "sort": {
29
+ var sorts = action.payload;
30
+ return sortAction(sorts, state);
31
+ }
32
+ default: {
33
+ return __assign({}, state);
34
+ }
35
+ }
36
+ };
37
+ var initialState = function () {
38
+ return ({
39
+ aliases: {},
40
+ filters: [],
41
+ includes: [],
42
+ sorts: [],
43
+ });
44
+ };
45
+ export var useQueryBuilder = function (config) {
46
+ if (config === void 0) {
47
+ config = {};
48
+ }
49
+ var init = useState(function () { return initialState(); })[0];
50
+ var _a = useReducer(reducer, init, function (init) {
51
+ var _a;
52
+ return (__assign(__assign({}, init), { aliases: (_a = config === null || config === void 0 ? void 0 : config.aliases) !== null && _a !== void 0 ? _a : {} }));
53
+ }), state = _a[0], dispatch = _a[1];
54
+ var builder = {
55
+ filters: function (attribute, value) {
56
+ dispatch({
57
+ type: "filter",
58
+ payload: { attribute: attribute, value: value },
59
+ });
60
+ return builder;
61
+ },
62
+ build: function () { return build(state); },
63
+ clearFilters: function () {
64
+ dispatch({
65
+ type: "clear_filter",
66
+ payload: undefined,
67
+ });
68
+ return builder;
69
+ },
70
+ includes: function (includes) {
71
+ dispatch({
72
+ type: "include",
73
+ payload: includes,
74
+ });
75
+ return builder;
76
+ },
77
+ sorts: function (attribute, direction) {
78
+ dispatch({
79
+ type: "sort",
80
+ payload: [attribute, direction !== null && direction !== void 0 ? direction : "asc"],
81
+ });
82
+ return builder;
83
+ },
84
+ };
85
+ return builder;
86
+ };
@@ -0,0 +1,4 @@
1
+ export var usingAlias = function (state, key) {
2
+ var _a;
3
+ return (_a = state.aliases[key]) !== null && _a !== void 0 ? _a : key;
4
+ };
@@ -0,0 +1,30 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ export var build = function (state) {
14
+ var filters = state.filters.reduce(function (acc, filter) {
15
+ var _a;
16
+ return (__assign(__assign({}, acc), (_a = {}, _a["filters[".concat(filter.attribute, "]")] = filter.value.join(","), _a)));
17
+ }, {});
18
+ var sorts = state.sorts.reduce(function (acc, sort) {
19
+ var attribute = sort[0], dir = sort[1];
20
+ var direction = dir === "desc" ? "-" : "";
21
+ acc.push("".concat(direction).concat(attribute));
22
+ return acc;
23
+ }, []);
24
+ var urlSearchParams = new URLSearchParams(__assign({}, filters));
25
+ if (sorts.length > 0) {
26
+ urlSearchParams.append("sort", sorts.join(","));
27
+ }
28
+ var searchParamsString = urlSearchParams.toString();
29
+ return searchParamsString ? "?" + urlSearchParams.toString() : "";
30
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from "@/utils";
2
+ export * from "@/actions";
3
+ export * from "@/useQueryBuilder";
@@ -0,0 +1,86 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ import { useReducer, useState } from "react";
14
+ import { build, clearFilterAction, filterAction, includeAction, sortAction, } from "@/index";
15
+ var reducer = function (state, action) {
16
+ switch (action === null || action === void 0 ? void 0 : action.type) {
17
+ case "filter": {
18
+ var filter = action.payload;
19
+ return filterAction(filter.attribute, filter.value, state);
20
+ }
21
+ case "clear_filter": {
22
+ return clearFilterAction(state);
23
+ }
24
+ case "include": {
25
+ var includes = action.payload;
26
+ return includeAction(includes, state);
27
+ }
28
+ case "sort": {
29
+ var sorts = action.payload;
30
+ return sortAction(sorts, state);
31
+ }
32
+ default: {
33
+ return __assign({}, state);
34
+ }
35
+ }
36
+ };
37
+ var initialState = function () {
38
+ return ({
39
+ aliases: {},
40
+ filters: [],
41
+ includes: [],
42
+ sorts: [],
43
+ });
44
+ };
45
+ export var useQueryBuilder = function (config) {
46
+ if (config === void 0) {
47
+ config = {};
48
+ }
49
+ var init = useState(function () { return initialState(); })[0];
50
+ var _a = useReducer(reducer, init, function (init) {
51
+ var _a;
52
+ return (__assign(__assign({}, init), { aliases: (_a = config === null || config === void 0 ? void 0 : config.aliases) !== null && _a !== void 0 ? _a : {} }));
53
+ }), state = _a[0], dispatch = _a[1];
54
+ var builder = {
55
+ filters: function (attribute, value) {
56
+ dispatch({
57
+ type: "filter",
58
+ payload: { attribute: attribute, value: value },
59
+ });
60
+ return builder;
61
+ },
62
+ build: function () { return build(state); },
63
+ clearFilters: function () {
64
+ dispatch({
65
+ type: "clear_filter",
66
+ payload: undefined,
67
+ });
68
+ return builder;
69
+ },
70
+ includes: function (includes) {
71
+ dispatch({
72
+ type: "include",
73
+ payload: includes,
74
+ });
75
+ return builder;
76
+ },
77
+ sorts: function (attribute, direction) {
78
+ dispatch({
79
+ type: "sort",
80
+ payload: [attribute, direction !== null && direction !== void 0 ? direction : "asc"],
81
+ });
82
+ return builder;
83
+ },
84
+ };
85
+ return builder;
86
+ };
@@ -0,0 +1,4 @@
1
+ export var usingAlias = function (state, key) {
2
+ var _a;
3
+ return (_a = state.aliases[key]) !== null && _a !== void 0 ? _a : key;
4
+ };
@@ -0,0 +1,30 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s)
6
+ if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ export var build = function (state) {
14
+ var filters = state.filters.reduce(function (acc, filter) {
15
+ var _a;
16
+ return (__assign(__assign({}, acc), (_a = {}, _a["filters[".concat(filter.attribute, "]")] = filter.value.join(","), _a)));
17
+ }, {});
18
+ var sorts = state.sorts.reduce(function (acc, sort) {
19
+ var attribute = sort[0], dir = sort[1];
20
+ var direction = dir === "desc" ? "-" : "";
21
+ acc.push("".concat(direction).concat(attribute));
22
+ return acc;
23
+ }, []);
24
+ var urlSearchParams = new URLSearchParams(__assign({}, filters));
25
+ if (sorts.length > 0) {
26
+ urlSearchParams.append("sort", sorts.join(","));
27
+ }
28
+ var searchParamsString = urlSearchParams.toString();
29
+ return searchParamsString ? "?" + urlSearchParams.toString() : "";
30
+ };
@@ -0,0 +1,3 @@
1
+ export * from "@/utils/state";
2
+ export * from "@/utils/alias";
3
+ export * from "@/utils/build";
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "@/utils";
2
+ export * from "@/actions";
3
+ export * from "@/useQueryBuilder";
@@ -0,0 +1 @@
1
+ {"program":{"fileNames":["../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es5.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2016.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2017.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2018.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2019.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2021.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2022.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2023.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.esnext.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.core.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.generator.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.promise.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2016.intl.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2017.date.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2017.object.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2017.string.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2017.intl.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2018.intl.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2018.promise.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2019.array.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2019.object.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2019.string.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2019.intl.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.date.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.promise.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.string.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.intl.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2020.number.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2021.promise.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2021.string.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2021.weakref.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2021.intl.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2022.array.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2022.error.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2022.intl.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2022.object.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2022.sharedmemory.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2022.string.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2022.regexp.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2023.array.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2023.collection.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.esnext.collection.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.esnext.intl.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.esnext.disposable.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.esnext.promise.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.esnext.decorators.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.esnext.object.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.decorators.d.ts","../../personal-wallet/node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.decorators.legacy.d.ts","../node_modules/@types/react/global.d.ts","../node_modules/csstype/index.d.ts","../node_modules/@types/prop-types/index.d.ts","../node_modules/@types/react/index.d.ts","../node_modules/@types/react/jsx-runtime.d.ts","../src/actions/sort.ts","../src/actions/filter.ts","../src/actions/include.ts","../src/actions/index.ts","../src/usequerybuilder.ts","../src/index.ts","../src/utils/state.ts","../src/utils/alias.ts","../src/utils/build.ts","../src/utils/index.ts","./index.js","./usequerybuilder.js","./actions/filter.js","./actions/include.js","./actions/index.js","./actions/sort.js","./dist/index.js","./dist/usequerybuilder.js","./dist/actions/filter.js","./dist/actions/include.js","./dist/actions/index.js","./dist/actions/sort.js","./dist/utils/alias.js","./dist/utils/build.js","./dist/utils/index.js","./dist/utils/state.js","./utils/alias.js","./utils/build.js","./utils/index.js","./utils/state.js","../node_modules/@types/eslint/helpers.d.ts","../node_modules/@types/estree/index.d.ts","../node_modules/@types/json-schema/index.d.ts","../node_modules/@types/eslint/index.d.ts","../node_modules/@types/json5/index.d.ts","../node_modules/@types/semver/classes/semver.d.ts","../node_modules/@types/semver/functions/parse.d.ts","../node_modules/@types/semver/functions/valid.d.ts","../node_modules/@types/semver/functions/clean.d.ts","../node_modules/@types/semver/functions/inc.d.ts","../node_modules/@types/semver/functions/diff.d.ts","../node_modules/@types/semver/functions/major.d.ts","../node_modules/@types/semver/functions/minor.d.ts","../node_modules/@types/semver/functions/patch.d.ts","../node_modules/@types/semver/functions/prerelease.d.ts","../node_modules/@types/semver/functions/compare.d.ts","../node_modules/@types/semver/functions/rcompare.d.ts","../node_modules/@types/semver/functions/compare-loose.d.ts","../node_modules/@types/semver/functions/compare-build.d.ts","../node_modules/@types/semver/functions/sort.d.ts","../node_modules/@types/semver/functions/rsort.d.ts","../node_modules/@types/semver/functions/gt.d.ts","../node_modules/@types/semver/functions/lt.d.ts","../node_modules/@types/semver/functions/eq.d.ts","../node_modules/@types/semver/functions/neq.d.ts","../node_modules/@types/semver/functions/gte.d.ts","../node_modules/@types/semver/functions/lte.d.ts","../node_modules/@types/semver/functions/cmp.d.ts","../node_modules/@types/semver/functions/coerce.d.ts","../node_modules/@types/semver/classes/comparator.d.ts","../node_modules/@types/semver/classes/range.d.ts","../node_modules/@types/semver/functions/satisfies.d.ts","../node_modules/@types/semver/ranges/max-satisfying.d.ts","../node_modules/@types/semver/ranges/min-satisfying.d.ts","../node_modules/@types/semver/ranges/to-comparators.d.ts","../node_modules/@types/semver/ranges/min-version.d.ts","../node_modules/@types/semver/ranges/valid.d.ts","../node_modules/@types/semver/ranges/outside.d.ts","../node_modules/@types/semver/ranges/gtr.d.ts","../node_modules/@types/semver/ranges/ltr.d.ts","../node_modules/@types/semver/ranges/intersects.d.ts","../node_modules/@types/semver/ranges/simplify.d.ts","../node_modules/@types/semver/ranges/subset.d.ts","../node_modules/@types/semver/internals/identifiers.d.ts","../node_modules/@types/semver/index.d.ts"],"fileInfos":[{"version":"824cb491a40f7e8fdeb56f1df5edf91b23f3e3ee6b4cde84d4a99be32338faee","affectsGlobalScope":true},"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","3facaf05f0c5fc569c5649dd359892c98a85557e3e0c847964caeb67076f4d75","9a68c0c07ae2fa71b44384a839b7b8d81662a236d4b9ac30916718f7510b1b2d","5e1c4c362065a6b95ff952c0eab010f04dcd2c3494e813b493ecfd4fcb9fc0d8","68d73b4a11549f9c0b7d352d10e91e5dca8faa3322bfb77b661839c42b1ddec7","5efce4fc3c29ea84e8928f97adec086e3dc876365e0982cc8479a07954a3efd4","feecb1be483ed332fad555aff858affd90a48ab19ba7272ee084704eb7167569","5514e54f17d6d74ecefedc73c504eadffdeda79c7ea205cf9febead32d45c4bc","1c0cdb8dc619bc549c3e5020643e7cf7ae7940058e8c7e5aefa5871b6d86f44b","886e50ef125efb7878f744e86908884c0133e7a6d9d80013f421b0cd8fb2af94",{"version":"138fb588d26538783b78d1e3b2c2cc12d55840b97bf5e08bca7f7a174fbe2f17","affectsGlobalScope":true},{"version":"dc2df20b1bcdc8c2d34af4926e2c3ab15ffe1160a63e58b7e09833f616efff44","affectsGlobalScope":true},{"version":"4443e68b35f3332f753eacc66a04ac1d2053b8b035a0e0ac1d455392b5e243b3","affectsGlobalScope":true},{"version":"bc47685641087c015972a3f072480889f0d6c65515f12bd85222f49a98952ed7","affectsGlobalScope":true},{"version":"0dc1e7ceda9b8b9b455c3a2d67b0412feab00bd2f66656cd8850e8831b08b537","affectsGlobalScope":true},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true},{"version":"4ff2a353abf8a80ee399af572debb8faab2d33ad38c4b4474cff7f26e7653b8d","affectsGlobalScope":true},{"version":"93495ff27b8746f55d19fcbcdbaccc99fd95f19d057aed1bd2c0cafe1335fbf0","affectsGlobalScope":true},{"version":"6fc23bb8c3965964be8c597310a2878b53a0306edb71d4b5a4dfe760186bcc01","affectsGlobalScope":true},{"version":"ea011c76963fb15ef1cdd7ce6a6808b46322c527de2077b6cfdf23ae6f5f9ec7","affectsGlobalScope":true},{"version":"38f0219c9e23c915ef9790ab1d680440d95419ad264816fa15009a8851e79119","affectsGlobalScope":true},{"version":"bb42a7797d996412ecdc5b2787720de477103a0b2e53058569069a0e2bae6c7e","affectsGlobalScope":true},{"version":"4738f2420687fd85629c9efb470793bb753709c2379e5f85bc1815d875ceadcd","affectsGlobalScope":true},{"version":"2f11ff796926e0832f9ae148008138ad583bd181899ab7dd768a2666700b1893","affectsGlobalScope":true},{"version":"4de680d5bb41c17f7f68e0419412ca23c98d5749dcaaea1896172f06435891fc","affectsGlobalScope":true},{"version":"9fc46429fbe091ac5ad2608c657201eb68b6f1b8341bd6d670047d32ed0a88fa","affectsGlobalScope":true},{"version":"61c37c1de663cf4171e1192466e52c7a382afa58da01b1dc75058f032ddf0839","affectsGlobalScope":true},{"version":"b541a838a13f9234aba650a825393ffc2292dc0fc87681a5d81ef0c96d281e7a","affectsGlobalScope":true},{"version":"b20fe0eca9a4e405f1a5ae24a2b3290b37cf7f21eba6cbe4fc3fab979237d4f3","affectsGlobalScope":true},{"version":"811ec78f7fefcabbda4bfa93b3eb67d9ae166ef95f9bff989d964061cbf81a0c","affectsGlobalScope":true},{"version":"717937616a17072082152a2ef351cb51f98802fb4b2fdabd32399843875974ca","affectsGlobalScope":true},{"version":"d7e7d9b7b50e5f22c915b525acc5a49a7a6584cf8f62d0569e557c5cfc4b2ac2","affectsGlobalScope":true},{"version":"71c37f4c9543f31dfced6c7840e068c5a5aacb7b89111a4364b1d5276b852557","affectsGlobalScope":true},{"version":"576711e016cf4f1804676043e6a0a5414252560eb57de9faceee34d79798c850","affectsGlobalScope":true},{"version":"89c1b1281ba7b8a96efc676b11b264de7a8374c5ea1e6617f11880a13fc56dc6","affectsGlobalScope":true},{"version":"49ed889be54031e1044af0ad2c603d627b8bda8b50c1a68435fe85583901d072","affectsGlobalScope":true},{"version":"e93d098658ce4f0c8a0779e6cab91d0259efb88a318137f686ad76f8410ca270","affectsGlobalScope":true},{"version":"063600664504610fe3e99b717a1223f8b1900087fab0b4cad1496a114744f8df","affectsGlobalScope":true},{"version":"934019d7e3c81950f9a8426d093458b65d5aff2c7c1511233c0fd5b941e608ab","affectsGlobalScope":true},{"version":"bf14a426dbbf1022d11bd08d6b8e709a2e9d246f0c6c1032f3b2edb9a902adbe","affectsGlobalScope":true},{"version":"5e07ed3809d48205d5b985642a59f2eba47c402374a7cf8006b686f79efadcbd","affectsGlobalScope":true},{"version":"2b72d528b2e2fe3c57889ca7baef5e13a56c957b946906d03767c642f386bbc3","affectsGlobalScope":true},{"version":"8073890e29d2f46fdbc19b8d6d2eb9ea58db9a2052f8640af20baff9afbc8640","affectsGlobalScope":true},{"version":"368af93f74c9c932edd84c58883e736c9e3d53cec1fe24c0b0ff451f529ceab1","affectsGlobalScope":true},{"version":"af3dd424cf267428f30ccfc376f47a2c0114546b55c44d8c0f1d57d841e28d74","affectsGlobalScope":true},{"version":"995c005ab91a498455ea8dfb63aa9f83fa2ea793c3d8aa344be4a1678d06d399","affectsGlobalScope":true},{"version":"51e547984877a62227042850456de71a5c45e7fe86b7c975c6e68896c86fa23b","affectsGlobalScope":true},{"version":"956d27abdea9652e8368ce029bb1e0b9174e9678a273529f426df4b3d90abd60","affectsGlobalScope":true},{"version":"4fa6ed14e98aa80b91f61b9805c653ee82af3502dc21c9da5268d3857772ca05","affectsGlobalScope":true},{"version":"e6633e05da3ff36e6da2ec170d0d03ccf33de50ca4dc6f5aeecb572cedd162fb","affectsGlobalScope":true},{"version":"d8670852241d4c6e03f2b89d67497a4bbefe29ecaa5a444e2c11a9b05e6fccc6","affectsGlobalScope":true},{"version":"8444af78980e3b20b49324f4a16ba35024fef3ee069a0eb67616ea6ca821c47a","affectsGlobalScope":true},{"version":"caccc56c72713969e1cfe5c3d44e5bab151544d9d2b373d7dbe5a1e4166652be","affectsGlobalScope":true},{"version":"3287d9d085fbd618c3971944b65b4be57859f5415f495b33a6adc994edd2f004","affectsGlobalScope":true},{"version":"50d53ccd31f6667aff66e3d62adf948879a3a16f05d89882d1188084ee415bbc","affectsGlobalScope":true},{"version":"08a58483392df5fcc1db57d782e87734f77ae9eab42516028acbfe46f29a3ef7","affectsGlobalScope":true},{"version":"436aaf437562f276ec2ddbee2f2cdedac7664c1e4c1d2c36839ddd582eeb3d0a","affectsGlobalScope":true},{"version":"b1cb28af0c891c8c96b2d6b7be76bd394fddcfdb4709a20ba05a7c1605eea0f9","affectsGlobalScope":true},{"version":"13f6e6380c78e15e140243dc4be2fa546c287c6d61f4729bc2dd7cf449605471","affectsGlobalScope":true},{"version":"15b98a533864d324e5f57cd3cfc0579b231df58c1c0f6063ea0fcb13c3c74ff9","affectsGlobalScope":true},{"version":"ac77cb3e8c6d3565793eb90a8373ee8033146315a3dbead3bde8db5eaf5e5ec6","affectsGlobalScope":true},{"version":"d4b1d2c51d058fc21ec2629fff7a76249dec2e36e12960ea056e3ef89174080f","affectsGlobalScope":true},{"version":"2fef54945a13095fdb9b84f705f2b5994597640c46afeb2ce78352fab4cb3279","affectsGlobalScope":true},{"version":"33358442698bb565130f52ba79bfd3d4d484ac85fe33f3cb1759c54d18201393","affectsGlobalScope":true},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true},{"version":"36a2e4c9a67439aca5f91bb304611d5ae6e20d420503e96c230cf8fcdc948d94","affectsGlobalScope":true},"8a8eb4ebffd85e589a1cc7c178e291626c359543403d58c9cd22b81fab5b1fb9","247a952efd811d780e5630f8cfd76f495196f5fa74f6f0fee39ac8ba4a3c9800",{"version":"8ca4709dbd22a34bcc1ebf93e1877645bdb02ebd3f3d9a211a299a8db2ee4ba1","affectsGlobalScope":true},"42c169fb8c2d42f4f668c624a9a11e719d5d07dacbebb63cbcf7ef365b0a75b3","4001cb03b235a742281b2750827bfb7086ec0472c8681a8b1b4747c45f5b1523","c8ce20c95f4c581e26b929f44802e2fb43493d0fec9f9bc4ce961a66086b35f0","185cef62784cabc03bec476bcf55237197aabac68b330b6e1ca56800d6dc8abb","0e37ae5ef3edcd6115a587951d25014cc5bc3f953fc929b134d92c8cef302098","42c330263fcf91f809202fef1374eea97da206086b1f342d0d2a87bbd77848c5","9b94524fe81d65aa037b251de4e8b9d79ab232192b5d5423be98c2ab33a00dd3","81ecc1ad9d04099aa08500207e223a5a88dc81417557a282670106299e95689e","f3e0da52b93dd65a91c9e3d0cc99bc8b95a0e0af06eb2f250f990b6665e4bd16","a26b4e8139cad65cd09a54aed2158063f54edccdf655d4737e129c98fa01977c","b8cebadea3a810df4a3088661f8b3b167324f6adc4d4d322a147e0bbd776b319","9b94524fe81d65aa037b251de4e8b9d79ab232192b5d5423be98c2ab33a00dd3",{"version":"196b68f796f36652092a6b5dd33d4718fc2e84c34b6d991fcaa453c04d23177b","signature":"82e3a6c9d3e419fe1615394ce1c957b9eebe79372dac0743f6224827fc8515b9"},{"version":"abfc9c6d2fa90fa34aa04514809a22467d33be8a16cd67a14d80c9f4d2803f4c","signature":"87509250ff1e1172fc1779dda405a31c0aba43fd1b47066d3b94604a2a167712"},{"version":"66e718fa1b782acb0e10b785460cadaba0352d2517a48bef345f94f5d34fb07b","signature":"bef9a910eb35d25f156e93a1b55b42b968a4013e32baf2eb882b811aff8c66e5"},"dd0eac172db9df20f09754786bfab7312e8486e8c71d40a2b2da47dcc07024f6",{"version":"3e8273c3a35e4638df54c920afd327f4fe6e1045d4163fd18d6e33ca4080c7b1","signature":"110dde780d788b1e27fe4be26b1917ea72bb182f71bd5a70982d56bc2aa5cbe0"},"9b94524fe81d65aa037b251de4e8b9d79ab232192b5d5423be98c2ab33a00dd3",{"version":"916659b8304613a9c4f10c820413b3c5688944ebe4b84bd6f60ae8d5eebd6272","signature":"82e3a6c9d3e419fe1615394ce1c957b9eebe79372dac0743f6224827fc8515b9"},{"version":"a886a29e0a03dd43fa185b8d6180d5b0fe62cdfedd92fe5609675b4061738c41","signature":"87509250ff1e1172fc1779dda405a31c0aba43fd1b47066d3b94604a2a167712"},{"version":"31ddbd1c39049d3e639c6bfda003a56c746a9d2d79ebf40c94eb7a9bb7dc3b52","signature":"bef9a910eb35d25f156e93a1b55b42b968a4013e32baf2eb882b811aff8c66e5"},"dd0eac172db9df20f09754786bfab7312e8486e8c71d40a2b2da47dcc07024f6",{"version":"4aa16197ad9abb7e54bd182825c415130559b830464381dce23f21b0ff95db10","signature":"110dde780d788b1e27fe4be26b1917ea72bb182f71bd5a70982d56bc2aa5cbe0"},{"version":"edae8f0c93020c613c62d0eb7b8befcf4230aa2b155f15d96216c3b2b8648ff5","signature":"fb6b322fdcea8fb0d10ff3d992a9ac954a4638cdc817b6c121287f5019fa9f6b"},{"version":"fdafd1d40dadcf13826a8088f7ef9089df883cd3e9d98ef9f10accaace831590","signature":"76146d8cb3adcd7d1b6343257426bceb99037e29efac1ac1ec45c309db43eabe"},"b8cebadea3a810df4a3088661f8b3b167324f6adc4d4d322a147e0bbd776b319","8e609bb71c20b858c77f0e9f90bb1319db8477b13f9f965f1a1e18524bf50881",{"version":"edae8f0c93020c613c62d0eb7b8befcf4230aa2b155f15d96216c3b2b8648ff5","signature":"fb6b322fdcea8fb0d10ff3d992a9ac954a4638cdc817b6c121287f5019fa9f6b"},{"version":"00e86344bcd239314bcb29b3d255347fab8a7bdd0210cbfa19ee3061ce799524","signature":"76146d8cb3adcd7d1b6343257426bceb99037e29efac1ac1ec45c309db43eabe"},"b8cebadea3a810df4a3088661f8b3b167324f6adc4d4d322a147e0bbd776b319","8e609bb71c20b858c77f0e9f90bb1319db8477b13f9f965f1a1e18524bf50881",{"version":"64d4b35c5456adf258d2cf56c341e203a073253f229ef3208fc0d5020253b241","affectsGlobalScope":true},"ee7d8894904b465b072be0d2e4b45cf6b887cdba16a467645c4e200982ece7ea","f3d8c757e148ad968f0d98697987db363070abada5f503da3c06aefd9d4248c1","7852500a7dc3f9cb6b73d619f6e0249119211ea662fd5e16c59ee5aba3deeb80","96d14f21b7652903852eef49379d04dbda28c16ed36468f8c9fa08f7c14c9538","cf3d384d082b933d987c4e2fe7bfb8710adfd9dc8155190056ed6695a25a559e","9871b7ee672bc16c78833bdab3052615834b08375cb144e4d2cba74473f4a589","c863198dae89420f3c552b5a03da6ed6d0acfa3807a64772b895db624b0de707","8b03a5e327d7db67112ebbc93b4f744133eda2c1743dbb0a990c61a8007823ef","86c73f2ee1752bac8eeeece234fd05dfcf0637a4fbd8032e4f5f43102faa8eec","42fad1f540271e35ca37cecda12c4ce2eef27f0f5cf0f8dd761d723c744d3159","ff3743a5de32bee10906aff63d1de726f6a7fd6ee2da4b8229054dfa69de2c34","83acd370f7f84f203e71ebba33ba61b7f1291ca027d7f9a662c6307d74e4ac22","1445cec898f90bdd18b2949b9590b3c012f5b7e1804e6e329fb0fe053946d5ec","0e5318ec2275d8da858b541920d9306650ae6ac8012f0e872fe66eb50321a669","cf530297c3fb3a92ec9591dd4fa229d58b5981e45fe6702a0bd2bea53a5e59be","c1f6f7d08d42148ddfe164d36d7aba91f467dbcb3caa715966ff95f55048b3a4","f4e9bf9103191ef3b3612d3ec0044ca4044ca5be27711fe648ada06fad4bcc85","0c1ee27b8f6a00097c2d6d91a21ee4d096ab52c1e28350f6362542b55380059a","7677d5b0db9e020d3017720f853ba18f415219fb3a9597343b1b1012cfd699f7","bc1c6bc119c1784b1a2be6d9c47addec0d83ef0d52c8fbe1f14a51b4dfffc675","52cf2ce99c2a23de70225e252e9822a22b4e0adb82643ab0b710858810e00bf1","770625067bb27a20b9826255a8d47b6b5b0a2d3dfcbd21f89904c731f671ba77","d1ed6765f4d7906a05968fb5cd6d1db8afa14dbe512a4884e8ea5c0f5e142c80","799c0f1b07c092626cf1efd71d459997635911bb5f7fc1196efe449bba87e965","2a184e4462b9914a30b1b5c41cf80c6d3428f17b20d3afb711fff3f0644001fd","9eabde32a3aa5d80de34af2c2206cdc3ee094c6504a8d0c2d6d20c7c179503cc","397c8051b6cfcb48aa22656f0faca2553c5f56187262135162ee79d2b2f6c966","a8ead142e0c87dcd5dc130eba1f8eeed506b08952d905c47621dc2f583b1bff9","a02f10ea5f73130efca046429254a4e3c06b5475baecc8f7b99a0014731be8b3","c2576a4083232b0e2d9bd06875dd43d371dee2e090325a9eac0133fd5650c1cb","4c9a0564bb317349de6a24eb4efea8bb79898fa72ad63a1809165f5bd42970dd","f40ac11d8859092d20f953aae14ba967282c3bb056431a37fced1866ec7a2681","cc11e9e79d4746cc59e0e17473a59d6f104692fd0eeea1bdb2e206eabed83b03","b444a410d34fb5e98aa5ee2b381362044f4884652e8bc8a11c8fe14bbd85518e","c35808c1f5e16d2c571aa65067e3cb95afeff843b259ecfa2fc107a9519b5392","14d5dc055143e941c8743c6a21fa459f961cbc3deedf1bfe47b11587ca4b3ef5","a3ad4e1fc542751005267d50a6298e6765928c0c3a8dce1572f2ba6ca518661c","f237e7c97a3a89f4591afd49ecb3bd8d14f51a1c4adc8fcae3430febedff5eb6","3ffdfbec93b7aed71082af62b8c3e0cc71261cc68d796665faa1e91604fbae8f","662201f943ed45b1ad600d03a90dffe20841e725203ced8b708c91fcd7f9379a","c9ef74c64ed051ea5b958621e7fb853fe3b56e8787c1587aefc6ea988b3c7e79","2462ccfac5f3375794b861abaa81da380f1bbd9401de59ffa43119a0b644253d","34baf65cfee92f110d6653322e2120c2d368ee64b3c7981dff08ed105c4f19b0","7d8ddf0f021c53099e34ee831a06c394d50371816caa98684812f089b4c6b3d4"],"root":[[73,102]],"options":{"allowJs":true,"esModuleInterop":true,"jsx":4,"module":99,"noUncheckedIndexedAccess":true,"noUnusedLocals":true,"noUnusedParameters":true,"outDir":"./","skipLibCheck":true,"strict":true},"fileIdsList":[[72,82],[72],[72,73,74,75],[72,78],[72,76,77,82],[71,72,78],[72,79,80,81],[103,104,105],[68,69,70],[71],[108,147],[108,132,147],[147],[108],[108,133,147],[108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146],[133,147],[72,79]],"referencedMap":[[85,1],[86,2],[87,3],[88,4],[91,1],[92,2],[93,3],[94,4],[89,5],[90,6],[95,2],[96,2],[97,7],[98,2],[83,5],[84,6],[99,2],[100,2],[101,7],[102,2],[106,8],[71,9],[72,10],[132,11],[133,12],[108,13],[111,13],[130,11],[131,11],[121,11],[120,14],[118,11],[113,11],[126,11],[124,11],[128,11],[112,11],[125,11],[129,11],[114,11],[115,11],[127,11],[109,11],[116,11],[117,11],[119,11],[123,11],[134,15],[122,11],[110,11],[147,16],[141,15],[143,17],[142,15],[135,15],[136,15],[138,15],[140,15],[144,17],[145,17],[137,17],[139,17],[74,1],[75,4],[76,3],[73,4],[78,5],[77,6],[80,4],[81,18],[82,7],[79,4]],"exportedModulesMap":[[106,8],[71,9],[72,10],[132,11],[133,12],[108,13],[111,13],[130,11],[131,11],[121,11],[120,14],[118,11],[113,11],[126,11],[124,11],[128,11],[112,11],[125,11],[129,11],[114,11],[115,11],[127,11],[109,11],[116,11],[117,11],[119,11],[123,11],[134,15],[122,11],[110,11],[147,16],[141,15],[143,17],[142,15],[135,15],[136,15],[138,15],[140,15],[144,17],[145,17],[137,17],[139,17],[74,1],[75,4],[76,3],[73,4],[78,5],[77,6],[80,4],[81,18],[82,7],[79,4]],"semanticDiagnosticsPerFile":[66,67,13,12,2,14,15,16,17,18,19,20,21,3,22,4,23,27,24,25,26,28,29,30,5,31,32,33,34,6,38,35,36,37,39,7,40,45,46,41,42,43,44,8,50,47,48,49,51,9,52,53,54,57,55,56,58,59,10,1,60,11,64,62,61,65,63,85,86,87,88,83,84,99,100,101,102,103,106,104,105,107,70,68,71,72,132,133,108,111,130,131,121,120,118,113,126,124,128,112,125,129,114,115,127,109,116,117,119,123,134,122,110,147,146,141,143,142,135,136,138,140,144,145,137,139,69,74,75,76,73,78,77,80,[81,[{"file":"../src/utils/build.ts","start":525,"length":15,"messageText":"Cannot find name 'URLSearchParams'. Did you mean 'urlSearchParams'?","category":1,"code":2552,"relatedInformation":[{"file":"../src/utils/build.ts","start":503,"length":15,"messageText":"'urlSearchParams' is declared here.","category":3,"code":2728}]}]],82,79]},"version":"5.4.5"}
@@ -0,0 +1,81 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { useReducer, useState } from "react";
13
+ import { build, clearFilterAction, filterAction, includeAction, sortAction, } from "@/index";
14
+ var reducer = function (state, action) {
15
+ switch (action === null || action === void 0 ? void 0 : action.type) {
16
+ case "filter": {
17
+ var filter = action.payload;
18
+ return filterAction(filter.attribute, filter.value, state);
19
+ }
20
+ case "clear_filter": {
21
+ return clearFilterAction(state);
22
+ }
23
+ case "include": {
24
+ var includes = action.payload;
25
+ return includeAction(includes, state);
26
+ }
27
+ case "sort": {
28
+ var sorts = action.payload;
29
+ return sortAction(sorts, state);
30
+ }
31
+ default: {
32
+ return __assign({}, state);
33
+ }
34
+ }
35
+ };
36
+ var initialState = function () { return ({
37
+ aliases: {},
38
+ filters: [],
39
+ includes: [],
40
+ sorts: [],
41
+ }); };
42
+ export var useQueryBuilder = function (config) {
43
+ if (config === void 0) { config = {}; }
44
+ var init = useState(function () { return initialState(); })[0];
45
+ var _a = useReducer(reducer, init, function (init) {
46
+ var _a;
47
+ return (__assign(__assign({}, init), { aliases: (_a = config === null || config === void 0 ? void 0 : config.aliases) !== null && _a !== void 0 ? _a : {} }));
48
+ }), state = _a[0], dispatch = _a[1];
49
+ var builder = {
50
+ filters: function (attribute, value) {
51
+ dispatch({
52
+ type: "filter",
53
+ payload: { attribute: attribute, value: value },
54
+ });
55
+ return builder;
56
+ },
57
+ build: function () { return build(state); },
58
+ clearFilters: function () {
59
+ dispatch({
60
+ type: "clear_filter",
61
+ payload: undefined,
62
+ });
63
+ return builder;
64
+ },
65
+ includes: function (includes) {
66
+ dispatch({
67
+ type: "include",
68
+ payload: includes,
69
+ });
70
+ return builder;
71
+ },
72
+ sorts: function (attribute, direction) {
73
+ dispatch({
74
+ type: "sort",
75
+ payload: [attribute, direction !== null && direction !== void 0 ? direction : "asc"],
76
+ });
77
+ return builder;
78
+ },
79
+ };
80
+ return builder;
81
+ };
@@ -0,0 +1,4 @@
1
+ export var usingAlias = function (state, key) {
2
+ var _a;
3
+ return (_a = state.aliases[key]) !== null && _a !== void 0 ? _a : key;
4
+ };
@@ -0,0 +1,29 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ export var build = function (state) {
13
+ var filters = state.filters.reduce(function (acc, filter) {
14
+ var _a;
15
+ return (__assign(__assign({}, acc), (_a = {}, _a["filters[".concat(filter.attribute, "]")] = filter.value.join(","), _a)));
16
+ }, {});
17
+ var sorts = state.sorts.reduce(function (acc, sort) {
18
+ var attribute = sort[0], dir = sort[1];
19
+ var direction = dir === "desc" ? "-" : "";
20
+ acc.push("".concat(direction).concat(attribute));
21
+ return acc;
22
+ }, []);
23
+ var urlSearchParams = new URLSearchParams(__assign({}, filters));
24
+ if (sorts.length > 0) {
25
+ urlSearchParams.append("sort", sorts.join(","));
26
+ }
27
+ var searchParamsString = urlSearchParams.toString();
28
+ return searchParamsString ? "?" + urlSearchParams.toString() : "";
29
+ };
@@ -0,0 +1,3 @@
1
+ export * from "@/utils/state";
2
+ export * from "@/utils/alias";
3
+ export * from "@/utils/build";
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@cgarciagarcia/react-query-builder",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
7
10
  "scripts": {
8
- "build": "",
11
+ "build": "tsc",
9
12
  "test": "echo \"Error: no test specified\" && exit 1"
10
13
  },
11
14
  "repository": {
package/.eslintignore DELETED
@@ -1 +0,0 @@
1
- dist/
package/.eslintrc.cjs DELETED
@@ -1,56 +0,0 @@
1
- /** @type {import('eslint').Linter.Config} */
2
- const config = {
3
- extends: [
4
- 'eslint:recommended',
5
- 'plugin:@typescript-eslint/recommended-type-checked',
6
- 'plugin:@typescript-eslint/stylistic-type-checked',
7
- 'plugin:jsx-a11y/recommended',
8
- 'plugin:react-hooks/recommended',
9
- 'plugin:react/recommended',
10
- 'prettier'
11
- ],
12
- env: {
13
- es2022: true,
14
- browser: true,
15
- commonjs: true
16
- },
17
- parser: '@typescript-eslint/parser',
18
- parserOptions: {
19
- project: true
20
- },
21
- plugins: ['@typescript-eslint', 'import', 'unused-imports'],
22
- rules: {
23
- 'react/prop-types': 'off',
24
- '@typescript-eslint/unbound-method': 'off',
25
- '@typescript-eslint/no-unused-vars': [
26
- 'error',
27
- {
28
- argsIgnorePattern: '^_',
29
- varsIgnorePattern: '^_'
30
- }
31
- ],
32
- 'unused-imports/no-unused-imports': 'error',
33
- '@typescript-eslint/consistent-type-imports': [
34
- 'warn',
35
- {prefer: 'type-imports', fixStyle: 'separate-type-imports'}
36
- ],
37
- '@typescript-eslint/no-misused-promises': [
38
- 2,
39
- {checksVoidReturn: {attributes: false}}
40
- ],
41
- 'import/consistent-type-specifier-style': ['error', 'prefer-inline'],
42
- 'import/no-duplicates': 'error',
43
- 'object-curly-spacing': ["warn", "always"]
44
-
45
- },
46
- ignorePatterns: ['.eslintrc.cjs', '.config.js', '.config.cjs', '.config.ts'],
47
- reportUnusedDisableDirectives: true,
48
- globals: {
49
- React: 'writable'
50
- },
51
- settings: {
52
- react: {version: 'detect'}
53
- }
54
- }
55
-
56
- module.exports = config
package/.nvmrc DELETED
@@ -1 +0,0 @@
1
- v20.14.0
package/.prettierignore DELETED
@@ -1 +0,0 @@
1
- node_modules/
@@ -1,28 +0,0 @@
1
- /** @typedef {import('@ianvs/prettier-plugin-sort-imports').PluginConfig} SortImportsConfig */
2
- /** @typedef {import('prettier').Config} PrettierConfig */
3
- /** @typedef {{ tailwindConfig: string }} TailwindConfig */
4
-
5
- /** @type { PrettierConfig | SortImportsConfig | TailwindConfig } */
6
- const config = {
7
- arrowParens: 'always',
8
- printWidth: 80,
9
- singleQuote: false,
10
- jsxSingleQuote: false,
11
- semi: true,
12
- trailingComma: 'all',
13
- tabWidth: 2,
14
- plugins: [
15
- '@ianvs/prettier-plugin-sort-imports',
16
- ],
17
- importOrder: [
18
- '^(react/(.*)$)|^(react$)',
19
- '<THIRD_PARTY_MODULES>',
20
- '^[../]',
21
- '^[./]'
22
- ],
23
- importOrderParserPlugins: ['typescript', 'jsx', 'decorators-legacy'],
24
- importOrderTypeScriptVersion: '5.0.0',
25
- bracketSpacing: true
26
- }
27
-
28
- module.exports = config
@@ -1,46 +0,0 @@
1
- import { type Alias, type GlobalState, usingAlias, } from "@/utils";
2
-
3
- export type FilterValue = (string | number)[] | string | number;
4
-
5
- export interface Filter {
6
- attribute: string;
7
- value: (string | number)[];
8
- }
9
-
10
- export const filterAction = <Al extends Alias<object>, Attr extends string>(
11
- attribute: Attr,
12
- value: FilterValue,
13
- state: GlobalState<Al>,
14
- ) => {
15
- const alias = usingAlias(state, attribute);
16
- let prevFilter: Filter | undefined;
17
-
18
- const allFilters = state.filters.reduce((filters, filter) => {
19
- if (filter.attribute === alias) {
20
- prevFilter = filter;
21
- return filters;
22
- }
23
- return [...filters, filter];
24
- }, [] as Filter[]);
25
-
26
- const val = Array.isArray(value) ? value : [value];
27
-
28
- const newState: GlobalState<Al> = {
29
- ...state,
30
- filters: [
31
- ...allFilters,
32
- {
33
- attribute: attribute,
34
- value: [...(prevFilter?.value ?? []), ...val],
35
- },
36
- ],
37
- };
38
- return newState
39
- };
40
-
41
- export const clearFilterAction = <Al>(state: GlobalState<Al>) => {
42
- return {
43
- ...state,
44
- filters: [],
45
- };
46
- };
@@ -1,14 +0,0 @@
1
- import { type GlobalState } from "@/index";
2
-
3
- export type Include = string
4
- export type Includes = string[];
5
-
6
- export const includeAction = <T>(
7
- includes: Include,
8
- state: GlobalState<T>,
9
- ): GlobalState<T> => {
10
- return {
11
- ...state,
12
- includes: [...state.includes, includes],
13
- } satisfies GlobalState<T>;
14
- };
@@ -1,14 +0,0 @@
1
- import { type GlobalState, usingAlias } from "@/index";
2
-
3
- export type Sort = [string, "asc" | "desc"];
4
-
5
- export type Sorts = Sort[];
6
-
7
- export const sortAction = <T>(sorts: Sort, state: GlobalState<T>) => {
8
- const [attribute, direction] = sorts;
9
- const attributeAliased = usingAlias(state, attribute);
10
- return {
11
- ...state,
12
- sorts: [...state.sorts, [attributeAliased, direction]],
13
- } satisfies GlobalState<T>;
14
- };
@@ -1,126 +0,0 @@
1
- import { useReducer, useState } from "react";
2
-
3
- import {
4
- type Actions,
5
- type Alias,
6
- build,
7
- clearFilterAction,
8
- type Filter,
9
- filterAction,
10
- type FilterValue,
11
- type GlobalState,
12
- type Include,
13
- includeAction,
14
- type Sort,
15
- sortAction,
16
- } from "@/index";
17
-
18
-
19
- interface Action {
20
- type: Actions;
21
- payload: unknown;
22
-
23
- }
24
-
25
- const reducer = <Aliases extends Record<string, string>>(
26
- state: GlobalState<Aliases>,
27
- action: Action,
28
- ) => {
29
- switch (action?.type) {
30
- case "filter": {
31
- const filter = action.payload as Filter;
32
- return filterAction(filter.attribute, filter.value, state);
33
- }
34
- case "clear_filter": {
35
- return clearFilterAction(state);
36
- }
37
- case "include": {
38
- const includes = action.payload as Include;
39
- return includeAction(includes, state);
40
- }
41
- case "sort": {
42
- const sorts = action.payload as Sort;
43
- return sortAction(sorts, state);
44
- }
45
- default: {
46
- return { ...state };
47
- }
48
- }
49
-
50
- };
51
-
52
- const initialState = <T>(): GlobalState<T> => ({
53
- aliases: {} as Alias<T>,
54
- filters: [],
55
- includes: [],
56
- sorts: [],
57
- });
58
-
59
- export interface QueryBuilder<AliasType = NonNullable<unknown>> {
60
- filters: (
61
- attribute: keyof AliasType | string,
62
- value: FilterValue,
63
- ) => QueryBuilder<AliasType>;
64
- build: () => string;
65
- clearFilters: () => QueryBuilder<AliasType>;
66
- includes: (includes: Include) => QueryBuilder<AliasType>;
67
- sorts: (
68
- attribute: string,
69
- direction?: "asc" | "desc",
70
- ) => QueryBuilder<AliasType>;
71
- }
72
-
73
- type ExtensibleQueryProps<T> = Pick<
74
- QueryBuilder<T>,
75
- "filters" | "includes" | "sorts"
76
- >;
77
-
78
- interface QueryBuilderProps<AliasType> extends ExtensibleQueryProps<AliasType> {
79
- aliases?: AliasType;
80
- }
81
-
82
- export const useQueryBuilder: <Aliases extends Record<string, string>>(
83
- config?: QueryBuilderProps<Aliases>,
84
- ) => QueryBuilder<Aliases> = <Aliases extends Record<string, string>>(
85
- config = {} as QueryBuilderProps<Aliases>,
86
- ) => {
87
- const [init] = useState(() => initialState<Aliases>());
88
- const [state, dispatch] = useReducer(reducer, init, (init) => ({
89
- ...init,
90
- aliases: config?.aliases ?? ({} as Aliases),
91
- }));
92
-
93
- const builder: QueryBuilder<Aliases> = {
94
- filters: (attribute, value) => {
95
- dispatch({
96
- type: "filter",
97
- payload: { attribute, value },
98
- });
99
- return builder;
100
- },
101
- build: () => build(state),
102
- clearFilters: () => {
103
- dispatch({
104
- type: "clear_filter",
105
- payload: undefined,
106
- });
107
- return builder;
108
- },
109
- includes: (includes) => {
110
- dispatch({
111
- type: "include",
112
- payload: includes,
113
- });
114
- return builder;
115
- },
116
- sorts: (attribute, direction) => {
117
- dispatch({
118
- type: "sort",
119
- payload: [attribute, direction ?? "asc"],
120
- });
121
- return builder;
122
- },
123
- };
124
-
125
- return builder;
126
- };
@@ -1,11 +0,0 @@
1
- import { type GlobalState } from "@/index";
2
-
3
- export const usingAlias = <Al, K>(state: GlobalState<Al>, key: K) => {
4
- return (state.aliases[key as unknown as keyof Al] as string) ?? key;
5
- };
6
-
7
-
8
- export type Alias<Prop> = {
9
- [key in keyof Prop]: Prop[key] extends string ? string : never;
10
- };
11
-
@@ -1,31 +0,0 @@
1
- import { type GlobalState } from "@/utils/state";
2
-
3
-
4
- export const build = <T>(state: GlobalState<T>): string => {
5
- const filters = state.filters.reduce(
6
- (acc, filter) => ({
7
- ...acc,
8
- [`filters[${filter.attribute}]`]: filter.value.join(","),
9
- }),
10
- {},
11
- );
12
-
13
- const sorts = state.sorts.reduce((acc, sort) => {
14
- const [attribute, dir] = sort;
15
- const direction = dir === "desc" ? "-" : "";
16
- acc.push(`${direction}${attribute}`);
17
- return acc;
18
- }, [] as string[]);
19
-
20
- const urlSearchParams = new URLSearchParams({
21
- ...filters,
22
- });
23
-
24
- if (sorts.length > 0) {
25
- urlSearchParams.append("sort", sorts.join(","));
26
- }
27
-
28
- const searchParamsString = urlSearchParams.toString();
29
-
30
- return searchParamsString ? "?" + urlSearchParams.toString() : "";
31
- };
@@ -1,8 +0,0 @@
1
- import { type Alias, type Filter, type Includes, type Sorts } from "@/index";
2
-
3
- export interface GlobalState<Al> {
4
- aliases: Alias<Al>;
5
- filters: Filter[];
6
- includes: Includes;
7
- sorts: Sorts;
8
- }
package/tsconfig.json DELETED
@@ -1,27 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "allowJs": true,
4
- "baseUrl": "./src",
5
- "esModuleInterop": true,
6
- "forceConsistentCasingInFileNames": true,
7
- "incremental": true,
8
- "isolatedModules": true,
9
- "jsx": "react-jsx",
10
- "outDir": "./dist",
11
- "noUnusedParameters": true,
12
- "noUnusedLocals": true,
13
- "noUncheckedIndexedAccess": true,
14
- "paths": {
15
- "@/*": [
16
- "./*"
17
- ]
18
- },
19
- "resolveJsonModule": true,
20
- "skipLibCheck": true,
21
- "strict": true
22
- },
23
- "type": "module",
24
- "exclude": [
25
- "./*.config.cjs"
26
- ]
27
- }
File without changes