@cgarciagarcia/react-query-builder 1.0.0 → 1.1.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.
- package/.eslintignore +1 -0
- package/README.md +42 -1
- package/package.json +5 -4
- package/prettier.config.cjs +0 -2
- package/src/actions/filter.ts +3 -1
- package/src/actions/include.ts +4 -3
- package/src/useQueryBuilder.ts +3 -3
- package/tsconfig.json +6 -14
- package/bun.lockb +0 -0
- package/index.ts +0 -6
package/.eslintignore
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dist/
|
package/README.md
CHANGED
|
@@ -1 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
<p><b>JavaScript/TypeScript Query Builder</b> provides a way to build a query string compatible with <a href="https://github.com/spatie/laravel-query-builder">spatie/laravel-query-builder</a>.</p>
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
You can install package using yarn (or npm):
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
yarn add @cgarciagarcia/react-qiery-builder
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
This package was thought offer an easy way to interact with backend integration `spatie/laravel-query-builder`
|
|
14
|
+
using your favorite library for frontend `React.js` using a customize hook which you could interact.
|
|
15
|
+
|
|
16
|
+
### All Example
|
|
17
|
+
|
|
18
|
+
Here is a simple example:
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import {useQueryBuilder} from "@cgarciagarcia/react-qiery-builder";
|
|
22
|
+
|
|
23
|
+
const baseConfigIfYouWant = {
|
|
24
|
+
aliases: {
|
|
25
|
+
"frontend_name": "backend_name",
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const builder = useQueryBuilder(baseConfigIfYouWant)
|
|
30
|
+
.filters("age", 18)
|
|
31
|
+
.sorts("created_at")// by default sorting asc
|
|
32
|
+
.sorts("age", 'desc') // sorting desc
|
|
33
|
+
.includes("posts", "comments")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
console.log(theQuery.build());
|
|
37
|
+
// /users?filter[age]=18,
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cgarciagarcia/react-query-builder",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"scripts": {
|
|
8
|
+
"build": "",
|
|
7
9
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
10
|
},
|
|
9
11
|
"repository": {
|
|
@@ -27,8 +29,7 @@
|
|
|
27
29
|
"eslint-plugin-react": "^7.33.2",
|
|
28
30
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
29
31
|
"eslint-plugin-unused-imports": "^3.0.0",
|
|
30
|
-
"prettier": "^3.0.3"
|
|
31
|
-
"@types/bun": "latest"
|
|
32
|
+
"prettier": "^3.0.3"
|
|
32
33
|
},
|
|
33
34
|
"keywords": [
|
|
34
35
|
"react",
|
package/prettier.config.cjs
CHANGED
package/src/actions/filter.ts
CHANGED
|
@@ -24,7 +24,8 @@ export const filterAction = <Al extends Alias<object>, Attr extends string>(
|
|
|
24
24
|
}, [] as Filter[]);
|
|
25
25
|
|
|
26
26
|
const val = Array.isArray(value) ? value : [value];
|
|
27
|
-
|
|
27
|
+
|
|
28
|
+
const newState: GlobalState<Al> = {
|
|
28
29
|
...state,
|
|
29
30
|
filters: [
|
|
30
31
|
...allFilters,
|
|
@@ -34,6 +35,7 @@ export const filterAction = <Al extends Alias<object>, Attr extends string>(
|
|
|
34
35
|
},
|
|
35
36
|
],
|
|
36
37
|
};
|
|
38
|
+
return newState
|
|
37
39
|
};
|
|
38
40
|
|
|
39
41
|
export const clearFilterAction = <Al>(state: GlobalState<Al>) => {
|
package/src/actions/include.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { type GlobalState } from "@/index";
|
|
2
2
|
|
|
3
|
+
export type Include = string
|
|
3
4
|
export type Includes = string[];
|
|
4
5
|
|
|
5
6
|
export const includeAction = <T>(
|
|
6
|
-
includes:
|
|
7
|
+
includes: Include,
|
|
7
8
|
state: GlobalState<T>,
|
|
8
9
|
): GlobalState<T> => {
|
|
9
10
|
return {
|
|
10
11
|
...state,
|
|
11
|
-
includes: includes,
|
|
12
|
-
}
|
|
12
|
+
includes: [...state.includes, includes],
|
|
13
|
+
} satisfies GlobalState<T>;
|
|
13
14
|
};
|
package/src/useQueryBuilder.ts
CHANGED
|
@@ -9,8 +9,8 @@ import {
|
|
|
9
9
|
filterAction,
|
|
10
10
|
type FilterValue,
|
|
11
11
|
type GlobalState,
|
|
12
|
+
type Include,
|
|
12
13
|
includeAction,
|
|
13
|
-
type Includes,
|
|
14
14
|
type Sort,
|
|
15
15
|
sortAction,
|
|
16
16
|
} from "@/index";
|
|
@@ -35,7 +35,7 @@ const reducer = <Aliases extends Record<string, string>>(
|
|
|
35
35
|
return clearFilterAction(state);
|
|
36
36
|
}
|
|
37
37
|
case "include": {
|
|
38
|
-
const includes = action.payload as
|
|
38
|
+
const includes = action.payload as Include;
|
|
39
39
|
return includeAction(includes, state);
|
|
40
40
|
}
|
|
41
41
|
case "sort": {
|
|
@@ -63,7 +63,7 @@ export interface QueryBuilder<AliasType = NonNullable<unknown>> {
|
|
|
63
63
|
) => QueryBuilder<AliasType>;
|
|
64
64
|
build: () => string;
|
|
65
65
|
clearFilters: () => QueryBuilder<AliasType>;
|
|
66
|
-
includes: (includes:
|
|
66
|
+
includes: (includes: Include) => QueryBuilder<AliasType>;
|
|
67
67
|
sorts: (
|
|
68
68
|
attribute: string,
|
|
69
69
|
direction?: "asc" | "desc",
|
package/tsconfig.json
CHANGED
|
@@ -7,29 +7,21 @@
|
|
|
7
7
|
"incremental": true,
|
|
8
8
|
"isolatedModules": true,
|
|
9
9
|
"jsx": "react-jsx",
|
|
10
|
-
"
|
|
11
|
-
"esnext"
|
|
12
|
-
],
|
|
13
|
-
"module": "esnext",
|
|
14
|
-
"moduleResolution": "node",
|
|
15
|
-
"noEmit": true,
|
|
10
|
+
"outDir": "./dist",
|
|
16
11
|
"noUnusedParameters": true,
|
|
17
12
|
"noUnusedLocals": true,
|
|
18
13
|
"noUncheckedIndexedAccess": true,
|
|
19
14
|
"paths": {
|
|
20
15
|
"@/*": [
|
|
21
|
-
"
|
|
16
|
+
"./*"
|
|
22
17
|
]
|
|
23
18
|
},
|
|
24
19
|
"resolveJsonModule": true,
|
|
25
20
|
"skipLibCheck": true,
|
|
26
|
-
"strict": true
|
|
27
|
-
"target": "ESNext"
|
|
21
|
+
"strict": true
|
|
28
22
|
},
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
".
|
|
32
|
-
"./prettier.config.cjs",
|
|
33
|
-
"./index.ts"
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exclude": [
|
|
25
|
+
"./*.config.cjs"
|
|
34
26
|
]
|
|
35
27
|
}
|
package/bun.lockb
DELETED
|
Binary file
|