@spoosh/plugin-qs 0.1.3 → 0.2.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/README.md +8 -7
- package/dist/index.d.mts +12 -28
- package/dist/index.d.ts +12 -28
- package/dist/index.js +9 -14
- package/dist/index.mjs +9 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ const query = {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
useRead((api) => api.items.$get({ query }));
|
|
29
|
-
// Result: pagination[limit]=10&pagination[offset]=0&filters[status]=active&filters[tags][]=a
|
|
29
|
+
// Result: pagination[limit]=10&pagination[offset]=0&filters[status]=active&filters[tags][]=a,b
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
## Features
|
|
@@ -40,12 +40,13 @@ useRead((api) => api.items.$get({ query }));
|
|
|
40
40
|
|
|
41
41
|
## Plugin Config
|
|
42
42
|
|
|
43
|
+
Accepts all [`qs` stringify options](https://github.com/ljharb/qs#stringifying). Common options:
|
|
44
|
+
|
|
43
45
|
| Option | Type | Default | Description |
|
|
44
46
|
| ------------- | ------------------------------------------------ | ------------ | ------------------------------------ |
|
|
45
47
|
| `arrayFormat` | `"brackets" \| "indices" \| "repeat" \| "comma"` | `"brackets"` | How to serialize arrays |
|
|
46
48
|
| `allowDots` | `boolean` | `false` | Use dot notation instead of brackets |
|
|
47
49
|
| `skipNulls` | `boolean` | `true` | Skip null values in serialization |
|
|
48
|
-
| `options` | `IStringifyOptions` | `{}` | Additional qs stringify options |
|
|
49
50
|
|
|
50
51
|
## Per-Request Options
|
|
51
52
|
|
|
@@ -53,13 +54,13 @@ Override plugin defaults for specific requests:
|
|
|
53
54
|
|
|
54
55
|
```typescript
|
|
55
56
|
// Use comma-separated arrays for this request
|
|
56
|
-
useRead((api) => api.items.$get({ query }), { arrayFormat: "comma" });
|
|
57
|
+
useRead((api) => api.items.$get({ query }), { qs: { arrayFormat: "comma" } });
|
|
57
58
|
|
|
58
59
|
// Use dot notation for nested objects
|
|
59
|
-
useRead((api) => api.search.$get({ query }), { allowDots: true });
|
|
60
|
+
useRead((api) => api.search.$get({ query }), { qs: { allowDots: true } });
|
|
60
61
|
|
|
61
62
|
// Include null values for this request
|
|
62
|
-
useRead((api) => api.data.$get({ query }), { skipNulls: false });
|
|
63
|
+
useRead((api) => api.data.$get({ query }), { qs: { skipNulls: false } });
|
|
63
64
|
```
|
|
64
65
|
|
|
65
66
|
## Array Formats
|
|
@@ -70,7 +71,7 @@ useRead((api) => api.data.$get({ query }), { skipNulls: false });
|
|
|
70
71
|
{
|
|
71
72
|
tags: ["a", "b"];
|
|
72
73
|
}
|
|
73
|
-
// tags[]=a
|
|
74
|
+
// tags[]=a,b
|
|
74
75
|
```
|
|
75
76
|
|
|
76
77
|
### indices
|
|
@@ -88,7 +89,7 @@ useRead((api) => api.data.$get({ query }), { skipNulls: false });
|
|
|
88
89
|
{
|
|
89
90
|
tags: ["a", "b"];
|
|
90
91
|
}
|
|
91
|
-
// tags=a
|
|
92
|
+
// tags=a,b
|
|
92
93
|
```
|
|
93
94
|
|
|
94
95
|
### comma
|
package/dist/index.d.mts
CHANGED
|
@@ -1,33 +1,17 @@
|
|
|
1
1
|
import { IStringifyOptions } from 'qs';
|
|
2
2
|
import { SpooshPlugin } from '@spoosh/core';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
allowDots?: boolean;
|
|
9
|
-
/** Skip null values in serialization. Defaults to true. */
|
|
10
|
-
skipNulls?: boolean;
|
|
11
|
-
/** Additional qs stringify options. */
|
|
12
|
-
options?: Omit<IStringifyOptions, "arrayFormat" | "allowDots" | "skipNulls">;
|
|
4
|
+
type QsPluginConfig = IStringifyOptions;
|
|
5
|
+
type QsOptions = IStringifyOptions;
|
|
6
|
+
interface QsReadHookOptions {
|
|
7
|
+
qs?: QsOptions;
|
|
13
8
|
}
|
|
14
|
-
interface
|
|
15
|
-
|
|
16
|
-
arrayFormat?: "brackets" | "indices" | "repeat" | "comma";
|
|
17
|
-
/** Use dot notation instead of brackets. Overrides plugin default. */
|
|
18
|
-
allowDots?: boolean;
|
|
19
|
-
/** Skip null values in serialization. Overrides plugin default. */
|
|
20
|
-
skipNulls?: boolean;
|
|
9
|
+
interface QsWriteHookOptions {
|
|
10
|
+
qs?: QsOptions;
|
|
21
11
|
}
|
|
22
|
-
interface
|
|
23
|
-
|
|
24
|
-
arrayFormat?: "brackets" | "indices" | "repeat" | "comma";
|
|
25
|
-
/** Use dot notation instead of brackets. Overrides plugin default. */
|
|
26
|
-
allowDots?: boolean;
|
|
27
|
-
/** Skip null values in serialization. Overrides plugin default. */
|
|
28
|
-
skipNulls?: boolean;
|
|
12
|
+
interface QsInfiniteReadHookOptions {
|
|
13
|
+
qs?: QsOptions;
|
|
29
14
|
}
|
|
30
|
-
type QsInfiniteReadOptions = QsReadOptions;
|
|
31
15
|
type QsReadResult = object;
|
|
32
16
|
type QsWriteResult = object;
|
|
33
17
|
|
|
@@ -56,11 +40,11 @@ type QsWriteResult = object;
|
|
|
56
40
|
* ```
|
|
57
41
|
*/
|
|
58
42
|
declare function qsPlugin(config?: QsPluginConfig): SpooshPlugin<{
|
|
59
|
-
readOptions:
|
|
60
|
-
writeOptions:
|
|
61
|
-
infiniteReadOptions:
|
|
43
|
+
readOptions: QsReadHookOptions;
|
|
44
|
+
writeOptions: QsWriteHookOptions;
|
|
45
|
+
infiniteReadOptions: QsInfiniteReadHookOptions;
|
|
62
46
|
readResult: QsReadResult;
|
|
63
47
|
writeResult: QsWriteResult;
|
|
64
48
|
}>;
|
|
65
49
|
|
|
66
|
-
export { type
|
|
50
|
+
export { type QsInfiniteReadHookOptions, type QsOptions, type QsPluginConfig, type QsReadHookOptions, type QsReadResult, type QsWriteHookOptions, type QsWriteResult, qsPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,33 +1,17 @@
|
|
|
1
1
|
import { IStringifyOptions } from 'qs';
|
|
2
2
|
import { SpooshPlugin } from '@spoosh/core';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
allowDots?: boolean;
|
|
9
|
-
/** Skip null values in serialization. Defaults to true. */
|
|
10
|
-
skipNulls?: boolean;
|
|
11
|
-
/** Additional qs stringify options. */
|
|
12
|
-
options?: Omit<IStringifyOptions, "arrayFormat" | "allowDots" | "skipNulls">;
|
|
4
|
+
type QsPluginConfig = IStringifyOptions;
|
|
5
|
+
type QsOptions = IStringifyOptions;
|
|
6
|
+
interface QsReadHookOptions {
|
|
7
|
+
qs?: QsOptions;
|
|
13
8
|
}
|
|
14
|
-
interface
|
|
15
|
-
|
|
16
|
-
arrayFormat?: "brackets" | "indices" | "repeat" | "comma";
|
|
17
|
-
/** Use dot notation instead of brackets. Overrides plugin default. */
|
|
18
|
-
allowDots?: boolean;
|
|
19
|
-
/** Skip null values in serialization. Overrides plugin default. */
|
|
20
|
-
skipNulls?: boolean;
|
|
9
|
+
interface QsWriteHookOptions {
|
|
10
|
+
qs?: QsOptions;
|
|
21
11
|
}
|
|
22
|
-
interface
|
|
23
|
-
|
|
24
|
-
arrayFormat?: "brackets" | "indices" | "repeat" | "comma";
|
|
25
|
-
/** Use dot notation instead of brackets. Overrides plugin default. */
|
|
26
|
-
allowDots?: boolean;
|
|
27
|
-
/** Skip null values in serialization. Overrides plugin default. */
|
|
28
|
-
skipNulls?: boolean;
|
|
12
|
+
interface QsInfiniteReadHookOptions {
|
|
13
|
+
qs?: QsOptions;
|
|
29
14
|
}
|
|
30
|
-
type QsInfiniteReadOptions = QsReadOptions;
|
|
31
15
|
type QsReadResult = object;
|
|
32
16
|
type QsWriteResult = object;
|
|
33
17
|
|
|
@@ -56,11 +40,11 @@ type QsWriteResult = object;
|
|
|
56
40
|
* ```
|
|
57
41
|
*/
|
|
58
42
|
declare function qsPlugin(config?: QsPluginConfig): SpooshPlugin<{
|
|
59
|
-
readOptions:
|
|
60
|
-
writeOptions:
|
|
61
|
-
infiniteReadOptions:
|
|
43
|
+
readOptions: QsReadHookOptions;
|
|
44
|
+
writeOptions: QsWriteHookOptions;
|
|
45
|
+
infiniteReadOptions: QsInfiniteReadHookOptions;
|
|
62
46
|
readResult: QsReadResult;
|
|
63
47
|
writeResult: QsWriteResult;
|
|
64
48
|
}>;
|
|
65
49
|
|
|
66
|
-
export { type
|
|
50
|
+
export { type QsInfiniteReadHookOptions, type QsOptions, type QsPluginConfig, type QsReadHookOptions, type QsReadResult, type QsWriteHookOptions, type QsWriteResult, qsPlugin };
|
package/dist/index.js
CHANGED
|
@@ -36,13 +36,12 @@ module.exports = __toCommonJS(src_exports);
|
|
|
36
36
|
|
|
37
37
|
// src/plugin.ts
|
|
38
38
|
var import_qs = __toESM(require("qs"));
|
|
39
|
+
var DEFAULT_OPTIONS = {
|
|
40
|
+
arrayFormat: "brackets",
|
|
41
|
+
allowDots: false,
|
|
42
|
+
skipNulls: true
|
|
43
|
+
};
|
|
39
44
|
function qsPlugin(config = {}) {
|
|
40
|
-
const {
|
|
41
|
-
arrayFormat: defaultArrayFormat = "brackets",
|
|
42
|
-
allowDots: defaultAllowDots = false,
|
|
43
|
-
skipNulls: defaultSkipNulls = true,
|
|
44
|
-
options: additionalOptions = {}
|
|
45
|
-
} = config;
|
|
46
45
|
return {
|
|
47
46
|
name: "spoosh:qs",
|
|
48
47
|
operations: ["read", "write", "infiniteRead"],
|
|
@@ -51,15 +50,11 @@ function qsPlugin(config = {}) {
|
|
|
51
50
|
if (!query || Object.keys(query).length === 0) {
|
|
52
51
|
return next();
|
|
53
52
|
}
|
|
54
|
-
const pluginOptions = context.pluginOptions;
|
|
55
|
-
const arrayFormat = pluginOptions?.arrayFormat ?? defaultArrayFormat;
|
|
56
|
-
const allowDots = pluginOptions?.allowDots ?? defaultAllowDots;
|
|
57
|
-
const skipNulls = pluginOptions?.skipNulls ?? defaultSkipNulls;
|
|
53
|
+
const pluginOptions = context.pluginOptions?.qs;
|
|
58
54
|
const stringified = import_qs.default.stringify(query, {
|
|
59
|
-
...
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
skipNulls,
|
|
55
|
+
...DEFAULT_OPTIONS,
|
|
56
|
+
...config,
|
|
57
|
+
...pluginOptions,
|
|
63
58
|
encode: false
|
|
64
59
|
});
|
|
65
60
|
const flatQuery = import_qs.default.parse(stringified, { depth: 0 });
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
// src/plugin.ts
|
|
2
2
|
import qs from "qs";
|
|
3
|
+
var DEFAULT_OPTIONS = {
|
|
4
|
+
arrayFormat: "brackets",
|
|
5
|
+
allowDots: false,
|
|
6
|
+
skipNulls: true
|
|
7
|
+
};
|
|
3
8
|
function qsPlugin(config = {}) {
|
|
4
|
-
const {
|
|
5
|
-
arrayFormat: defaultArrayFormat = "brackets",
|
|
6
|
-
allowDots: defaultAllowDots = false,
|
|
7
|
-
skipNulls: defaultSkipNulls = true,
|
|
8
|
-
options: additionalOptions = {}
|
|
9
|
-
} = config;
|
|
10
9
|
return {
|
|
11
10
|
name: "spoosh:qs",
|
|
12
11
|
operations: ["read", "write", "infiniteRead"],
|
|
@@ -15,15 +14,11 @@ function qsPlugin(config = {}) {
|
|
|
15
14
|
if (!query || Object.keys(query).length === 0) {
|
|
16
15
|
return next();
|
|
17
16
|
}
|
|
18
|
-
const pluginOptions = context.pluginOptions;
|
|
19
|
-
const arrayFormat = pluginOptions?.arrayFormat ?? defaultArrayFormat;
|
|
20
|
-
const allowDots = pluginOptions?.allowDots ?? defaultAllowDots;
|
|
21
|
-
const skipNulls = pluginOptions?.skipNulls ?? defaultSkipNulls;
|
|
17
|
+
const pluginOptions = context.pluginOptions?.qs;
|
|
22
18
|
const stringified = qs.stringify(query, {
|
|
23
|
-
...
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
skipNulls,
|
|
19
|
+
...DEFAULT_OPTIONS,
|
|
20
|
+
...config,
|
|
21
|
+
...pluginOptions,
|
|
27
22
|
encode: false
|
|
28
23
|
});
|
|
29
24
|
const flatQuery = qs.parse(stringified, { depth: 0 });
|