@makehq/forman-schema 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,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toJSONSchema = toJSONSchema;
4
+ exports.toFormanSchema = toFormanSchema;
5
+ const FORMAN_PRIMITIVE_TYPE_MAP = {
6
+ text: 'string',
7
+ number: 'number',
8
+ boolean: 'boolean',
9
+ date: 'string',
10
+ json: 'string',
11
+ };
12
+ const JSON_PRIMITIVE_TYPE_MAP = {
13
+ string: 'text',
14
+ number: 'number',
15
+ boolean: 'boolean',
16
+ };
17
+ function noEmpty(text) {
18
+ if (!text)
19
+ return undefined;
20
+ return text;
21
+ }
22
+ function toJSONSchema(field) {
23
+ switch (field.type) {
24
+ case 'collection':
25
+ const required = [];
26
+ const properties = (Array.isArray(field.spec) ? field.spec : []).reduce((object, subField) => {
27
+ if (!subField.name)
28
+ return object;
29
+ if (subField.required)
30
+ required.push(subField.name);
31
+ return Object.defineProperty(object, subField.name, {
32
+ enumerable: true,
33
+ value: toJSONSchema(subField),
34
+ });
35
+ }, {});
36
+ return {
37
+ type: 'object',
38
+ description: noEmpty(field.help),
39
+ properties,
40
+ required,
41
+ };
42
+ case 'array':
43
+ return {
44
+ type: 'array',
45
+ description: noEmpty(field.help),
46
+ items: field.spec &&
47
+ toJSONSchema(Array.isArray(field.spec)
48
+ ? {
49
+ type: 'collection',
50
+ spec: field.spec,
51
+ }
52
+ : field.spec),
53
+ };
54
+ case 'select':
55
+ return {
56
+ type: 'string',
57
+ description: noEmpty(field.help),
58
+ enum: (field.options || []).map(option => option.value),
59
+ };
60
+ default:
61
+ return {
62
+ type: FORMAN_PRIMITIVE_TYPE_MAP[field.type],
63
+ default: field.default != '' && field.default != null ? field.default : undefined,
64
+ description: noEmpty(field.help),
65
+ };
66
+ }
67
+ }
68
+ function toFormanSchema(field) {
69
+ switch (field.type) {
70
+ case 'object':
71
+ const spec = field.properties
72
+ ? Object.entries(field.properties).map(([name, property]) => {
73
+ const subField = toFormanSchema(property);
74
+ subField.name = name;
75
+ subField.required = field.required?.includes(name) || false;
76
+ return subField;
77
+ })
78
+ : [];
79
+ return {
80
+ type: 'collection',
81
+ help: field.description,
82
+ spec,
83
+ };
84
+ case 'array':
85
+ return {
86
+ type: 'array',
87
+ help: field.description,
88
+ spec: field.items &&
89
+ (field.items?.type === 'object' && field.items.properties
90
+ ? Object.entries(field.items.properties).map(([name, property]) => {
91
+ const subField = toFormanSchema(property);
92
+ subField.name = name;
93
+ subField.required = field.items?.required?.includes(name) || false;
94
+ return subField;
95
+ })
96
+ : toFormanSchema(field.items)),
97
+ };
98
+ case 'string':
99
+ if (field.enum) {
100
+ return {
101
+ type: 'select',
102
+ help: field.description,
103
+ options: field.enum.map(value => ({ value })),
104
+ };
105
+ }
106
+ return {
107
+ type: 'text',
108
+ help: field.description,
109
+ default: field.default,
110
+ };
111
+ default:
112
+ return {
113
+ type: JSON_PRIMITIVE_TYPE_MAP[field.type],
114
+ help: field.description,
115
+ default: field.default,
116
+ };
117
+ }
118
+ }
@@ -0,0 +1,22 @@
1
+ export type FormanSchemaField = {
2
+ name?: string;
3
+ type: string;
4
+ required?: boolean;
5
+ default?: string | number | boolean | null;
6
+ options?: {
7
+ value: string;
8
+ }[];
9
+ help?: string;
10
+ spec?: FormanSchemaField[] | FormanSchemaField;
11
+ };
12
+ export type JSONSchemaField = {
13
+ type: string;
14
+ description?: string;
15
+ default?: string | number | boolean | null;
16
+ enum?: string[];
17
+ properties?: Record<string, JSONSchemaField>;
18
+ items?: JSONSchemaField;
19
+ required?: string[];
20
+ };
21
+ export declare function toJSONSchema(field: FormanSchemaField): JSONSchemaField;
22
+ export declare function toFormanSchema(field: JSONSchemaField): FormanSchemaField;
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@makehq/forman-schema",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Foman Schema Tools",
5
5
  "license": "MIT",
6
6
  "author": "Make",
7
7
  "repository": "github:integromat/forman-schema",
8
8
  "homepage": "https://www.make.com",
9
9
  "type": "module",
10
- "module": "./dist/index.js",
11
- "types": "./dist/index.d.ts",
10
+ "main": "./dist/cjs/index.js",
11
+ "module": "./dist/esm/index.js",
12
+ "types": "./dist/esm/index.d.ts",
12
13
  "exports": {
13
14
  ".": {
14
- "import": "./dist/index.js",
15
- "types": "./dist/index.d.ts"
15
+ "import": "./dist/esm/index.js",
16
+ "require": "./dist/cjs/index.js",
17
+ "types": "./dist/esm/index.d.ts"
16
18
  }
17
19
  },
18
20
  "engines": {
@@ -23,12 +25,16 @@
23
25
  ],
24
26
  "scripts": {
25
27
  "test": "jest --runInBand --forceExit --verbose false",
26
- "build": "tsc"
28
+ "build": "npm run build:esm && npm run build:cjs",
29
+ "build:esm": "tsc -p tsconfig.esm.json",
30
+ "build:cjs": "tsc -p tsconfig.cjs.json",
31
+ "format": "npx prettier . --write"
27
32
  },
28
33
  "devDependencies": {
29
34
  "@jest/globals": "^29.7.0",
30
35
  "@types/node": "^22.13.10",
31
36
  "jest": "^29.7.0",
37
+ "prettier": "^3.5.3",
32
38
  "ts-jest": "^29.2.6",
33
39
  "ts-node": "^10.9.2",
34
40
  "typescript": "^5.8.2"
File without changes
File without changes