@fabriciogferreira/schema-to-query-string 0.1.0 → 0.1.1

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.
@@ -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,159 @@
1
+ //* Libraries imports
2
+ import { it, expect } from "bun:test";
3
+ import z from "zod/v4";
4
+ import fastCartesian from "fast-cartesian";
5
+ import { PowerSet } from 'js-combinatorics';
6
+ //* Utils imports
7
+ import { schemaToQueryString } from "../src";
8
+ const partialObjects = [
9
+ [
10
+ {
11
+ rootOne: z.string(),
12
+ },
13
+ "fields[root]=rootOne"
14
+ ],
15
+ [
16
+ {
17
+ rootTwo: z.string(),
18
+ },
19
+ "fields[root]=rootTwo"
20
+ ],
21
+ [
22
+ {
23
+ rootObject: z.object({
24
+ subOne: z.number(),
25
+ }),
26
+ },
27
+ "fields[rootObject]=subOne&include=rootObject"
28
+ ],
29
+ [
30
+ {
31
+ rootObject: z.object({
32
+ subTwo: z.number(),
33
+ }),
34
+ },
35
+ "fields[rootObject]=subTwo&include=rootObject"
36
+ ],
37
+ [
38
+ {
39
+ rootNullableObject: z.object({
40
+ subOne: z.number(),
41
+ }).nullable(),
42
+ },
43
+ "fields[rootNullableObject]=subOne&include=rootNullableObject"
44
+ ],
45
+ [
46
+ {
47
+ rootNullableObject: z.object({
48
+ subTwo: z.number(),
49
+ }).nullable(),
50
+ },
51
+ "fields[rootNullableObject]=subTwo&include=rootNullableObject"
52
+ ],
53
+ [
54
+ {
55
+ rootArrayObject: z.object({
56
+ subOne: z.number(),
57
+ }).array(),
58
+ },
59
+ "fields[rootArrayObject]=subOne&include=rootArrayObject"
60
+ ],
61
+ [
62
+ {
63
+ rootArrayObject: z.object({
64
+ subTwo: z.number(),
65
+ }).array(),
66
+ },
67
+ "fields[rootArrayObject]=subTwo&include=rootArrayObject"
68
+ ],
69
+ ];
70
+ function unwrapObject(schema) {
71
+ if (schema instanceof z.ZodObject) {
72
+ return schema;
73
+ }
74
+ if (schema instanceof z.ZodNullable) {
75
+ const inner = schema.unwrap();
76
+ return inner instanceof z.ZodObject ? inner : null;
77
+ }
78
+ if (schema instanceof z.ZodArray) {
79
+ const inner = schema.unwrap();
80
+ return inner instanceof z.ZodObject ? inner : null;
81
+ }
82
+ return null;
83
+ }
84
+ function isNullable(schema) {
85
+ return schema instanceof z.ZodNullable;
86
+ }
87
+ function deepExtendZod(base, extension) {
88
+ const newShape = { ...base.shape };
89
+ for (const key in extension) {
90
+ const baseField = newShape[key];
91
+ const extField = extension[key];
92
+ const baseObj = baseField ? unwrapObject(baseField) : null;
93
+ const extObj = unwrapObject(extField);
94
+ if (baseObj && extObj) {
95
+ const merged = deepExtendZod(baseObj, extObj.shape);
96
+ const shouldBeNullable = isNullable(baseField) || isNullable(extField);
97
+ newShape[key] = shouldBeNullable
98
+ ? merged.nullable()
99
+ : merged;
100
+ }
101
+ else {
102
+ newShape[key] = extField;
103
+ }
104
+ }
105
+ return z.object(newShape);
106
+ }
107
+ const schemaCombinations = [...new PowerSet(partialObjects)];
108
+ const schemaCases = schemaCombinations.map((objects) => {
109
+ let schema = z.object({});
110
+ let fields = {};
111
+ let includes = new Set([]);
112
+ let expectedQueryString = '';
113
+ objects.forEach(([object, queryString]) => {
114
+ //@ts-expect-error
115
+ schema = deepExtendZod(schema, object);
116
+ const params = queryString.split("&");
117
+ params.forEach((param) => {
118
+ const [key, values] = param.split("=");
119
+ if (key.startsWith("include")) {
120
+ values.split(',').map(value => includes.add(value));
121
+ }
122
+ if (key.startsWith("fields")) {
123
+ if (!(key in fields))
124
+ fields[key] = new Set([]);
125
+ values.split(',').map(value => fields[key].add(value));
126
+ }
127
+ });
128
+ });
129
+ let includeValues = Array.from(includes).join(',');
130
+ let includeQueryString = includeValues ? `include=${Array.from(includes).join(',')}` : '';
131
+ let filtersQueryString = Object.entries(fields).map(([key, values]) => `${key}=${Array.from(values).join(',')}`).join('&');
132
+ expectedQueryString = '?' + [filtersQueryString, includeQueryString].filter(Boolean).join('&');
133
+ return [schema, expectedQueryString];
134
+ });
135
+ const objectNameCases = fastCartesian([
136
+ // ["fields"],
137
+ ["fields", "alternativeKeyForFields"],
138
+ // [undefined, "", "fields", "alternativeKeyForFields"],
139
+ // ["include"],
140
+ ["include", "alternativeKeyForInclude"],
141
+ // [undefined, "", "include", "alternativeKeyForInclude"],
142
+ ]);
143
+ const combinations = fastCartesian([
144
+ objectNameCases,
145
+ schemaCases
146
+ ]);
147
+ const cases = combinations.map(([config, [schema, expectedQueryString]]) => {
148
+ const fieldKey = config[0] === "" ? "vazio" : config[0] === undefined ? "undefined" : config[0];
149
+ const includeKey = config[1] === "" ? "vazio" : config[1] === undefined ? "undefined" : config[1];
150
+ return [`${fieldKey},${includeKey}`, expectedQueryString, fieldKey, includeKey, schema];
151
+ });
152
+ it.each(cases)("when config is %s and should %s", (_, expected, fieldKey, includeKey, schema) => {
153
+ expected = expected.replace("include", includeKey);
154
+ expected = expected.replaceAll("fields", fieldKey);
155
+ const received = schemaToQueryString(schema, "root", includeKey, fieldKey);
156
+ // console.log("======================================================================================================")
157
+ // console.log(received, expected)
158
+ expect(received).toBe(expected);
159
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fabriciogferreira/schema-to-query-string",
3
3
  "description": "function for convert schema to Spatie Laravel Query Builder",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "private": false,
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",