@spoosh/plugin-qs 0.1.0-beta.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/LICENSE +21 -0
- package/README.md +105 -0
- package/dist/index.d.mts +60 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +73 -0
- package/dist/index.mjs +40 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Spoosh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @spoosh/plugin-qs
|
|
2
|
+
|
|
3
|
+
Query string serialization plugin for Spoosh with nested object support.
|
|
4
|
+
|
|
5
|
+
**[Documentation](https://spoosh.dev/docs/plugins/qs)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @spoosh/plugin-qs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { qsPlugin } from "@spoosh/plugin-qs";
|
|
17
|
+
|
|
18
|
+
const plugins = [
|
|
19
|
+
qsPlugin({ arrayFormat: "brackets" }), // default
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
// Query object
|
|
23
|
+
const query = {
|
|
24
|
+
pagination: { limit: 10, offset: 0 },
|
|
25
|
+
filters: { status: "active", tags: ["a", "b"] },
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
useRead((api) => api.items.$get({ query }));
|
|
29
|
+
// Result: pagination[limit]=10&pagination[offset]=0&filters[status]=active&filters[tags][]=a&filters[tags][]=b
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
- ✅ Nested object serialization with bracket notation
|
|
35
|
+
- ✅ Multiple array formats (brackets, indices, repeat, comma)
|
|
36
|
+
- ✅ Dot notation support for nested objects
|
|
37
|
+
- ✅ Automatic null value skipping
|
|
38
|
+
- ✅ Per-request configuration override
|
|
39
|
+
- ✅ Powered by battle-tested `qs` package
|
|
40
|
+
|
|
41
|
+
## Plugin Config
|
|
42
|
+
|
|
43
|
+
| Option | Type | Default | Description |
|
|
44
|
+
| ------------- | ------------------------------------------------ | ------------ | ------------------------------------ |
|
|
45
|
+
| `arrayFormat` | `"brackets" \| "indices" \| "repeat" \| "comma"` | `"brackets"` | How to serialize arrays |
|
|
46
|
+
| `allowDots` | `boolean` | `false` | Use dot notation instead of brackets |
|
|
47
|
+
| `skipNulls` | `boolean` | `true` | Skip null values in serialization |
|
|
48
|
+
| `options` | `IStringifyOptions` | `{}` | Additional qs stringify options |
|
|
49
|
+
|
|
50
|
+
## Per-Request Options
|
|
51
|
+
|
|
52
|
+
Override plugin defaults for specific requests:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
// Use comma-separated arrays for this request
|
|
56
|
+
useRead((api) => api.items.$get({ query }), { arrayFormat: "comma" });
|
|
57
|
+
|
|
58
|
+
// Use dot notation for nested objects
|
|
59
|
+
useRead((api) => api.search.$get({ query }), { allowDots: true });
|
|
60
|
+
|
|
61
|
+
// Include null values for this request
|
|
62
|
+
useRead((api) => api.data.$get({ query }), { skipNulls: false });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Array Formats
|
|
66
|
+
|
|
67
|
+
### brackets (default)
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
{ tags: ["a", "b"] }
|
|
71
|
+
// tags[]=a&tags[]=b
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### indices
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
{ tags: ["a", "b"] }
|
|
78
|
+
// tags[0]=a&tags[1]=b
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### repeat
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
{ tags: ["a", "b"] }
|
|
85
|
+
// tags=a&tags=b
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### comma
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
{ tags: ["a", "b"] }
|
|
92
|
+
// tags=a,b
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Dot Notation
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// allowDots: false (default)
|
|
99
|
+
{ filters: { status: "active" } }
|
|
100
|
+
// filters[status]=active
|
|
101
|
+
|
|
102
|
+
// allowDots: true
|
|
103
|
+
{ filters: { status: "active" } }
|
|
104
|
+
// filters.status=active
|
|
105
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { IStringifyOptions } from 'qs';
|
|
2
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
3
|
+
|
|
4
|
+
interface QsPluginConfig {
|
|
5
|
+
/** Array format for serialization. Defaults to "brackets". */
|
|
6
|
+
arrayFormat?: "brackets" | "indices" | "repeat" | "comma";
|
|
7
|
+
/** Use dot notation instead of brackets. Defaults to false. */
|
|
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">;
|
|
13
|
+
}
|
|
14
|
+
interface QsReadOptions {
|
|
15
|
+
/** Array format for serialization. Overrides plugin default. */
|
|
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;
|
|
21
|
+
}
|
|
22
|
+
interface QsWriteOptions {
|
|
23
|
+
/** Array format for serialization. Overrides plugin default. */
|
|
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;
|
|
29
|
+
}
|
|
30
|
+
type QsInfiniteReadOptions = QsReadOptions;
|
|
31
|
+
type QsReadResult = object;
|
|
32
|
+
type QsWriteResult = object;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Enables nested object serialization in query parameters using bracket notation.
|
|
36
|
+
*
|
|
37
|
+
* Transforms nested objects like `{ pagination: { limit: 10 } }` into
|
|
38
|
+
* `pagination[limit]=10` format.
|
|
39
|
+
*
|
|
40
|
+
* @param config - Plugin configuration
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://spoosh.dev/docs/plugins/qs | QS Plugin Documentation}
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const plugins = [qsPlugin({ arrayFormat: "brackets" })];
|
|
47
|
+
*
|
|
48
|
+
* // Query: { filters: { status: "active", tags: ["a", "b"] } }
|
|
49
|
+
* // Result: filters[status]=active&filters[tags][]=a&filters[tags][]=b
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function qsPlugin(config?: QsPluginConfig): SpooshPlugin<{
|
|
53
|
+
readOptions: QsReadOptions;
|
|
54
|
+
writeOptions: QsWriteOptions;
|
|
55
|
+
infiniteReadOptions: QsInfiniteReadOptions;
|
|
56
|
+
readResult: QsReadResult;
|
|
57
|
+
writeResult: QsWriteResult;
|
|
58
|
+
}>;
|
|
59
|
+
|
|
60
|
+
export { type QsInfiniteReadOptions, type QsPluginConfig, type QsReadOptions, type QsReadResult, type QsWriteOptions, type QsWriteResult, qsPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { IStringifyOptions } from 'qs';
|
|
2
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
3
|
+
|
|
4
|
+
interface QsPluginConfig {
|
|
5
|
+
/** Array format for serialization. Defaults to "brackets". */
|
|
6
|
+
arrayFormat?: "brackets" | "indices" | "repeat" | "comma";
|
|
7
|
+
/** Use dot notation instead of brackets. Defaults to false. */
|
|
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">;
|
|
13
|
+
}
|
|
14
|
+
interface QsReadOptions {
|
|
15
|
+
/** Array format for serialization. Overrides plugin default. */
|
|
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;
|
|
21
|
+
}
|
|
22
|
+
interface QsWriteOptions {
|
|
23
|
+
/** Array format for serialization. Overrides plugin default. */
|
|
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;
|
|
29
|
+
}
|
|
30
|
+
type QsInfiniteReadOptions = QsReadOptions;
|
|
31
|
+
type QsReadResult = object;
|
|
32
|
+
type QsWriteResult = object;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Enables nested object serialization in query parameters using bracket notation.
|
|
36
|
+
*
|
|
37
|
+
* Transforms nested objects like `{ pagination: { limit: 10 } }` into
|
|
38
|
+
* `pagination[limit]=10` format.
|
|
39
|
+
*
|
|
40
|
+
* @param config - Plugin configuration
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://spoosh.dev/docs/plugins/qs | QS Plugin Documentation}
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const plugins = [qsPlugin({ arrayFormat: "brackets" })];
|
|
47
|
+
*
|
|
48
|
+
* // Query: { filters: { status: "active", tags: ["a", "b"] } }
|
|
49
|
+
* // Result: filters[status]=active&filters[tags][]=a&filters[tags][]=b
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function qsPlugin(config?: QsPluginConfig): SpooshPlugin<{
|
|
53
|
+
readOptions: QsReadOptions;
|
|
54
|
+
writeOptions: QsWriteOptions;
|
|
55
|
+
infiniteReadOptions: QsInfiniteReadOptions;
|
|
56
|
+
readResult: QsReadResult;
|
|
57
|
+
writeResult: QsWriteResult;
|
|
58
|
+
}>;
|
|
59
|
+
|
|
60
|
+
export { type QsInfiniteReadOptions, type QsPluginConfig, type QsReadOptions, type QsReadResult, type QsWriteOptions, type QsWriteResult, qsPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
qsPlugin: () => qsPlugin
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/plugin.ts
|
|
38
|
+
var import_qs = __toESM(require("qs"));
|
|
39
|
+
function qsPlugin(config = {}) {
|
|
40
|
+
const {
|
|
41
|
+
arrayFormat: defaultArrayFormat = "brackets",
|
|
42
|
+
allowDots: defaultAllowDots = false,
|
|
43
|
+
skipNulls: defaultSkipNulls = true,
|
|
44
|
+
options: additionalOptions = {}
|
|
45
|
+
} = config;
|
|
46
|
+
return {
|
|
47
|
+
name: "spoosh:qs",
|
|
48
|
+
operations: ["read", "write", "infiniteRead"],
|
|
49
|
+
middleware: async (context, next) => {
|
|
50
|
+
const query = context.requestOptions.query;
|
|
51
|
+
if (!query || Object.keys(query).length === 0) {
|
|
52
|
+
return next();
|
|
53
|
+
}
|
|
54
|
+
const pluginOptions = context.pluginOptions;
|
|
55
|
+
const arrayFormat = pluginOptions?.arrayFormat ?? defaultArrayFormat;
|
|
56
|
+
const allowDots = pluginOptions?.allowDots ?? defaultAllowDots;
|
|
57
|
+
const skipNulls = pluginOptions?.skipNulls ?? defaultSkipNulls;
|
|
58
|
+
const stringified = import_qs.default.stringify(query, {
|
|
59
|
+
...additionalOptions,
|
|
60
|
+
arrayFormat,
|
|
61
|
+
allowDots,
|
|
62
|
+
skipNulls,
|
|
63
|
+
encode: false
|
|
64
|
+
});
|
|
65
|
+
const flatQuery = import_qs.default.parse(stringified, { depth: 0 });
|
|
66
|
+
context.requestOptions = {
|
|
67
|
+
...context.requestOptions,
|
|
68
|
+
query: flatQuery
|
|
69
|
+
};
|
|
70
|
+
return next();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
import qs from "qs";
|
|
3
|
+
function qsPlugin(config = {}) {
|
|
4
|
+
const {
|
|
5
|
+
arrayFormat: defaultArrayFormat = "brackets",
|
|
6
|
+
allowDots: defaultAllowDots = false,
|
|
7
|
+
skipNulls: defaultSkipNulls = true,
|
|
8
|
+
options: additionalOptions = {}
|
|
9
|
+
} = config;
|
|
10
|
+
return {
|
|
11
|
+
name: "spoosh:qs",
|
|
12
|
+
operations: ["read", "write", "infiniteRead"],
|
|
13
|
+
middleware: async (context, next) => {
|
|
14
|
+
const query = context.requestOptions.query;
|
|
15
|
+
if (!query || Object.keys(query).length === 0) {
|
|
16
|
+
return next();
|
|
17
|
+
}
|
|
18
|
+
const pluginOptions = context.pluginOptions;
|
|
19
|
+
const arrayFormat = pluginOptions?.arrayFormat ?? defaultArrayFormat;
|
|
20
|
+
const allowDots = pluginOptions?.allowDots ?? defaultAllowDots;
|
|
21
|
+
const skipNulls = pluginOptions?.skipNulls ?? defaultSkipNulls;
|
|
22
|
+
const stringified = qs.stringify(query, {
|
|
23
|
+
...additionalOptions,
|
|
24
|
+
arrayFormat,
|
|
25
|
+
allowDots,
|
|
26
|
+
skipNulls,
|
|
27
|
+
encode: false
|
|
28
|
+
});
|
|
29
|
+
const flatQuery = qs.parse(stringified, { depth: 0 });
|
|
30
|
+
context.requestOptions = {
|
|
31
|
+
...context.requestOptions,
|
|
32
|
+
query: flatQuery
|
|
33
|
+
};
|
|
34
|
+
return next();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
qsPlugin
|
|
40
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spoosh/plugin-qs",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Query string serialization plugin for Spoosh with nested object support",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nxnom/spoosh.git",
|
|
9
|
+
"directory": "packages/plugin-qs"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/nxnom/spoosh/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://spoosh.dev/docs/plugins/qs",
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"spoosh",
|
|
20
|
+
"plugin",
|
|
21
|
+
"query-string",
|
|
22
|
+
"qs",
|
|
23
|
+
"nested-objects",
|
|
24
|
+
"api-client"
|
|
25
|
+
],
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.mjs",
|
|
33
|
+
"require": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"qs": "^6.13.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@spoosh/core": ">=0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/qs": "^6.9.17",
|
|
44
|
+
"@spoosh/core": "0.1.0-beta.0",
|
|
45
|
+
"@spoosh/test-utils": "0.1.0-beta.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"dev": "tsup --watch",
|
|
49
|
+
"build": "tsup",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
51
|
+
"lint": "eslint src --max-warnings 0",
|
|
52
|
+
"format": "prettier --write 'src/**/*.ts'"
|
|
53
|
+
}
|
|
54
|
+
}
|