controlresell 0.0.1 → 0.0.3
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/dist/helpers/json.d.ts +2 -0
- package/dist/helpers/json.js +20 -0
- package/dist/helpers/legacy.d.ts +1 -0
- package/dist/helpers/legacy.js +46 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/models/items/itemFieldValue.d.ts +12 -0
- package/dist/models/items/itemFieldValue.js +8 -0
- package/package.json +4 -3
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.safeJsonParse = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Safe parse JSON:
|
|
6
|
+
* - it checks for null or undefined
|
|
7
|
+
* - it checks that the value returned is corresponding to the schema
|
|
8
|
+
* - it catch any json parsing error (and returns undefined if there is one)
|
|
9
|
+
*/
|
|
10
|
+
const safeJsonParse = (json, schema) => {
|
|
11
|
+
if (json === null || json === undefined)
|
|
12
|
+
return undefined;
|
|
13
|
+
try {
|
|
14
|
+
return schema.parse(JSON.parse(json));
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
exports.safeJsonParse = safeJsonParse;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const zod_1 = require("zod");
|
|
4
|
+
// Function to convert snake_case to camelCase
|
|
5
|
+
const toCamelCase = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
6
|
+
// Function to convert camelCase to snake_case
|
|
7
|
+
const toSnakeCase = (str) => str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
8
|
+
// Preprocessing function to handle input
|
|
9
|
+
const snakeToCamel = (schema) => zod_1.z.preprocess((data) => {
|
|
10
|
+
if (typeof data !== "object" || data === null)
|
|
11
|
+
return data;
|
|
12
|
+
const converted = Object.fromEntries(Object.entries(data).map(([key, value]) => [toCamelCase(key), value]));
|
|
13
|
+
return { ...data, ...converted }; // Preserve both keys
|
|
14
|
+
}, schema);
|
|
15
|
+
// Post-processing function to add snake_case keys back
|
|
16
|
+
const addSnakeCaseKeys = (schema) => schema.transform((data) => {
|
|
17
|
+
if (typeof data !== "object" || data === null)
|
|
18
|
+
return data;
|
|
19
|
+
const converted = Object.fromEntries(Object.entries(data).map(([key, value]) => [toSnakeCase(key), value]));
|
|
20
|
+
return { ...data, ...converted }; // Preserve both keys
|
|
21
|
+
});
|
|
22
|
+
// Define your schema
|
|
23
|
+
const schema = addSnakeCaseKeys(snakeToCamel(zod_1.z.object({
|
|
24
|
+
firstName: zod_1.z.string(),
|
|
25
|
+
lastName: zod_1.z.string(),
|
|
26
|
+
age: zod_1.z.number(),
|
|
27
|
+
})));
|
|
28
|
+
// Example input with snake_case
|
|
29
|
+
const input = {
|
|
30
|
+
first_name: "John",
|
|
31
|
+
last_name: "Doe",
|
|
32
|
+
age: 30,
|
|
33
|
+
};
|
|
34
|
+
// Parsing input
|
|
35
|
+
const parsed = schema.parse(input);
|
|
36
|
+
console.log(parsed);
|
|
37
|
+
/*
|
|
38
|
+
Output:
|
|
39
|
+
{
|
|
40
|
+
firstName: 'John',
|
|
41
|
+
lastName: 'Doe',
|
|
42
|
+
age: 30,
|
|
43
|
+
first_name: 'John',
|
|
44
|
+
last_name: 'Doe'
|
|
45
|
+
}
|
|
46
|
+
*/
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -17,6 +17,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
17
17
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
__exportStar(require("./helpers/json"), exports);
|
|
20
21
|
__exportStar(require("./models/items/item"), exports);
|
|
21
22
|
__exportStar(require("./models/items/itemField"), exports);
|
|
23
|
+
__exportStar(require("./models/items/itemFieldValue"), exports);
|
|
22
24
|
__exportStar(require("./models/users/user"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const ItemFieldValuePayloadSchema: z.ZodObject<{
|
|
3
|
+
field_id: z.ZodNumber;
|
|
4
|
+
value: z.ZodString;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
|
+
value: string;
|
|
7
|
+
field_id: number;
|
|
8
|
+
}, {
|
|
9
|
+
value: string;
|
|
10
|
+
field_id: number;
|
|
11
|
+
}>;
|
|
12
|
+
export type ItemFieldValuePayload = z.infer<typeof ItemFieldValuePayloadSchema>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ItemFieldValuePayloadSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
exports.ItemFieldValuePayloadSchema = zod_1.z.object({
|
|
6
|
+
field_id: zod_1.z.number(),
|
|
7
|
+
value: zod_1.z.string(),
|
|
8
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "controlresell",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"generate-index": "barrelsby --delete --directory src",
|
|
12
12
|
"build": "npm run generate-index && tsc",
|
|
13
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
13
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
14
|
+
"lint": "eslint ."
|
|
14
15
|
},
|
|
15
16
|
"keywords": [],
|
|
16
17
|
"author": "",
|
|
@@ -18,7 +19,7 @@
|
|
|
18
19
|
"description": "",
|
|
19
20
|
"devDependencies": {
|
|
20
21
|
"@eslint/js": "^9.21.0",
|
|
21
|
-
"@guimauvedigital/eslint-plugin-safety": "^1.0.
|
|
22
|
+
"@guimauvedigital/eslint-plugin-safety": "^1.0.9",
|
|
22
23
|
"barrelsby": "^2.8.1",
|
|
23
24
|
"eslint": "^9.21.0",
|
|
24
25
|
"globals": "^16.0.0",
|