@fabriciogferreira/schema-to-query-string 0.1.4 → 0.1.8

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,19 @@
1
+ import z4 from "zod/v4";
2
+ export declare const schemaForTest: z4.ZodObject<{
3
+ rootOne: z4.ZodString;
4
+ rootTwo: z4.ZodString;
5
+ rootObject: z4.ZodObject<{
6
+ subOne: z4.ZodNumber;
7
+ subTwo: z4.ZodNumber;
8
+ }, z4.core.$strip>;
9
+ rootNullableObject: z4.ZodNullable<z4.ZodObject<{
10
+ subOne: z4.ZodNumber;
11
+ subTwo: z4.ZodNumber;
12
+ }, z4.core.$strip>>;
13
+ rootArrayObject: z4.ZodArray<z4.ZodObject<{
14
+ subOne: z4.ZodNumber;
15
+ subTwo: z4.ZodNumber;
16
+ }, z4.core.$strip>>;
17
+ }, z4.core.$strip>;
18
+ export declare const expectedStringForTest = "fields[root]=rootOne,rootTwo&fields[rootObject]=subOne,subTwo&fields[rootNullableObject]=subOne,subTwo&fields[rootArrayObject]=subOne,subTwo&include=rootObject,rootNullableObject,rootArrayObject";
19
+ export declare const expectedQueryStringForTest: string;
@@ -0,0 +1,19 @@
1
+ import z4 from "zod/v4";
2
+ export const schemaForTest = z4.object({
3
+ rootOne: z4.string(),
4
+ rootTwo: z4.string(),
5
+ rootObject: z4.object({
6
+ subOne: z4.number(),
7
+ subTwo: z4.number(),
8
+ }),
9
+ rootNullableObject: z4.object({
10
+ subOne: z4.number(),
11
+ subTwo: z4.number(),
12
+ }).nullable(),
13
+ rootArrayObject: z4.object({
14
+ subOne: z4.number(),
15
+ subTwo: z4.number(),
16
+ }).array(),
17
+ });
18
+ export const expectedStringForTest = "fields[root]=rootOne,rootTwo&fields[rootObject]=subOne,subTwo&fields[rootNullableObject]=subOne,subTwo&fields[rootArrayObject]=subOne,subTwo&include=rootObject,rootNullableObject,rootArrayObject";
19
+ export const expectedQueryStringForTest = "?" + expectedStringForTest;
@@ -0,0 +1,3 @@
1
+ export { schemaToQueryString } from './schema-to-query-string';
2
+ export { schemaForTest, expectedStringForTest, expectedQueryStringForTest } from './for-tests';
3
+ export type { schemaParam, rootResourceParam, includeParam, fieldsParam, SchemaToQueryStringConfig } from './types';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { schemaToQueryString } from './schema-to-query-string';
2
+ export { schemaForTest, expectedStringForTest, expectedQueryStringForTest } from './for-tests';
@@ -0,0 +1,5 @@
1
+ import { fieldsParam, includeParam, rootResourceParam, schemaParam } from "./types";
2
+ export declare const schemaToQueryString: (schema: schemaParam, rootResource: rootResourceParam, includeKey?: includeParam, fieldsKey?: fieldsParam) => {
3
+ string: string;
4
+ queryString: string;
5
+ };
@@ -43,5 +43,10 @@ export const schemaToQueryString = (schema, rootResource, includeKey = "include"
43
43
  if (includes.size) {
44
44
  queryParts.push(`${includeKey}=${Array.from(includes).join(",")}`);
45
45
  }
46
- return "?" + queryParts.join("&");
46
+ const string = queryParts.length ? queryParts.join("&") : "";
47
+ const queryString = string ? "?" + string : "";
48
+ return {
49
+ string,
50
+ queryString
51
+ };
47
52
  };
@@ -0,0 +1,11 @@
1
+ import { ZodObject } from "zod/v4";
2
+ export type schemaParam = ZodObject;
3
+ export type rootResourceParam = string;
4
+ export type includeParam = string;
5
+ export type fieldsParam = string;
6
+ export type SchemaToQueryStringConfig = {
7
+ schema: schemaParam;
8
+ rootResource: rootResourceParam;
9
+ includeKey?: includeParam;
10
+ fieldsKey?: fieldsParam;
11
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "@fabriciogferreira/schema-to-query-string",
3
- "version": "0.1.4",
3
+ "version": "0.1.8",
4
+ "description": "Function for convert schema to Spatie Laravel Query Builder",
4
5
  "type": "module",
5
6
  "license": "MIT",
6
- "author": "Fabrício G. Ferreira <fabriciof481@gmail.com>",
7
- "description": "Function for convert schema to Spatie Laravel Query Builder",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
8
15
  "files": [
9
- "src",
10
- "./index.ts"
16
+ "dist"
11
17
  ],
12
18
  "keywords": [
13
19
  "conversion",
@@ -23,13 +29,13 @@
23
29
  "test": "bun test",
24
30
  "prepublishOnly": "bun run build"
25
31
  },
32
+ "peerDependencies": {
33
+ "zod": "^4.3.5"
34
+ },
26
35
  "devDependencies": {
27
- "bun-types": "^1.3.5",
28
36
  "typescript": "^5.9.3",
37
+ "bun-types": "^1.3.5",
29
38
  "fast-cartesian": "^9.0.1",
30
39
  "js-combinatorics": "^2.1.2"
31
- },
32
- "dependencies": {
33
- "zod": "^4.3.5"
34
40
  }
35
41
  }
package/index.ts DELETED
@@ -1 +0,0 @@
1
- export { schemaToQueryString } from './src';
package/src/index.js DELETED
@@ -1 +0,0 @@
1
- export { schemaToQueryString } from './schema-to-query-string';
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export { schemaToQueryString } from './schema-to-query-string';
@@ -1,76 +0,0 @@
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
- };