@fabriciogferreira/schema-to-query-string 0.1.0 → 0.1.2
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/package.json +15 -7
- package/src/index.js +1 -0
- package/src/index.ts +1 -0
- package/src/schema-to-query-string.js +47 -0
- package/src/schema-to-query-string.ts +76 -0
package/package.json
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabriciogferreira/schema-to-query-string",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"types": "dist/index.d.ts",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Fabrício G. Ferreira <fabriciof481@gmail.com>",
|
|
7
|
+
"description": "Function for convert schema to Spatie Laravel Query Builder",
|
|
9
8
|
"files": [
|
|
10
|
-
"
|
|
9
|
+
"src"
|
|
11
10
|
],
|
|
11
|
+
"keywords": [
|
|
12
|
+
"conversion",
|
|
13
|
+
"schema",
|
|
14
|
+
"query string"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/fabriciogferreira/schema-to-query-string"
|
|
19
|
+
},
|
|
12
20
|
"scripts": {
|
|
13
21
|
"build": "tsc",
|
|
14
22
|
"test": "bun test",
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { schemaToQueryString } from './schema-to-query-string';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { schemaToQueryString } from './schema-to-query-string';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//* Libraries imports
|
|
2
|
+
import { ZodArray, ZodNullable, ZodObject } from "zod/v4";
|
|
3
|
+
export const schemaToQueryString = (schema, rootResource, includeKey = "include", fieldsKey = "fields") => {
|
|
4
|
+
const fields = {};
|
|
5
|
+
const includes = new Set();
|
|
6
|
+
fields[rootResource] = [];
|
|
7
|
+
const walk = (currentSchema, resourcePath) => {
|
|
8
|
+
const currentFieldsKey = resourcePath ?? rootResource;
|
|
9
|
+
if (!fields[currentFieldsKey]) {
|
|
10
|
+
fields[currentFieldsKey] = [];
|
|
11
|
+
}
|
|
12
|
+
for (const key in currentSchema.shape) {
|
|
13
|
+
let rawField = currentSchema.shape[key];
|
|
14
|
+
if (rawField instanceof ZodNullable) {
|
|
15
|
+
rawField = rawField.unwrap();
|
|
16
|
+
}
|
|
17
|
+
if (rawField instanceof ZodObject || rawField instanceof ZodArray) {
|
|
18
|
+
let nextPath = resourcePath
|
|
19
|
+
? `${resourcePath}.${key}`
|
|
20
|
+
: key;
|
|
21
|
+
includes.add(nextPath);
|
|
22
|
+
// ARRAY
|
|
23
|
+
if (rawField instanceof ZodArray) {
|
|
24
|
+
rawField = rawField.unwrap();
|
|
25
|
+
}
|
|
26
|
+
// OBJETO
|
|
27
|
+
if (rawField instanceof ZodObject) {
|
|
28
|
+
walk(rawField, nextPath);
|
|
29
|
+
}
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
// CAMPO SIMPLES
|
|
33
|
+
fields[currentFieldsKey].push(key);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
walk(schema, null);
|
|
37
|
+
const queryParts = [];
|
|
38
|
+
for (const resource in fields) {
|
|
39
|
+
if (fields[resource].length > 0) {
|
|
40
|
+
queryParts.push(`${fieldsKey}[${resource}]=${fields[resource].join(",")}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (includes.size) {
|
|
44
|
+
queryParts.push(`${includeKey}=${Array.from(includes).join(",")}`);
|
|
45
|
+
}
|
|
46
|
+
return "?" + queryParts.join("&");
|
|
47
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//* Libraries imports
|
|
2
|
+
import { ZodArray, ZodNullable, ZodObject } from "zod/v4";
|
|
3
|
+
|
|
4
|
+
export const schemaToQueryString = (
|
|
5
|
+
schema: ZodObject,
|
|
6
|
+
rootResource: string,
|
|
7
|
+
includeKey: string = "include",
|
|
8
|
+
fieldsKey: string = "fields"
|
|
9
|
+
) => {
|
|
10
|
+
const fields: Record<string, string[]> = {};
|
|
11
|
+
const includes = new Set<string>();
|
|
12
|
+
|
|
13
|
+
fields[rootResource] = [];
|
|
14
|
+
|
|
15
|
+
const walk = (
|
|
16
|
+
currentSchema: ZodObject,
|
|
17
|
+
resourcePath: string | null
|
|
18
|
+
) => {
|
|
19
|
+
const currentFieldsKey = resourcePath ?? rootResource;
|
|
20
|
+
|
|
21
|
+
if (!fields[currentFieldsKey]) {
|
|
22
|
+
fields[currentFieldsKey] = [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
for (const key in currentSchema.shape) {
|
|
26
|
+
let rawField = currentSchema.shape[key];
|
|
27
|
+
|
|
28
|
+
if (rawField instanceof ZodNullable) {
|
|
29
|
+
rawField = rawField.unwrap()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (rawField instanceof ZodObject || rawField instanceof ZodArray) {
|
|
33
|
+
let nextPath = resourcePath
|
|
34
|
+
? `${resourcePath}.${key}`
|
|
35
|
+
: key;
|
|
36
|
+
|
|
37
|
+
includes.add(nextPath);
|
|
38
|
+
|
|
39
|
+
// ARRAY
|
|
40
|
+
if (rawField instanceof ZodArray) {
|
|
41
|
+
rawField = rawField.unwrap()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// OBJETO
|
|
45
|
+
if (rawField instanceof ZodObject) {
|
|
46
|
+
walk(rawField, nextPath);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// CAMPO SIMPLES
|
|
53
|
+
fields[currentFieldsKey].push(key);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
walk(schema, null);
|
|
58
|
+
|
|
59
|
+
const queryParts: string[] = [];
|
|
60
|
+
|
|
61
|
+
for (const resource in fields) {
|
|
62
|
+
if (fields[resource].length > 0) {
|
|
63
|
+
queryParts.push(
|
|
64
|
+
`${fieldsKey}[${resource}]=${fields[resource].join(",")}`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (includes.size) {
|
|
70
|
+
queryParts.push(
|
|
71
|
+
`${includeKey}=${Array.from(includes).join(",")}`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return "?" + queryParts.join("&");
|
|
76
|
+
};
|