wasm-ast-types 0.2.5 → 0.3.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.
- package/main/messages.js +10 -181
- package/main/messages.spec.js +1 -1
- package/main/react-query.js +5 -3
- package/main/react-query.spec.js +2 -2
- package/main/recoil.spec.js +1 -1
- package/main/{utils.js → utils/babel.js} +31 -1
- package/main/{utils.spec.js → utils/babel.spec.js} +20 -20
- package/main/utils/index.js +18 -0
- package/main/utils/types.js +252 -0
- package/main/wasm.js +22 -193
- package/main/wasm.sg721.spec.js +30 -0
- package/main/wasm.spec.js +18 -13
- package/main/wasm.vectis.spec.js +53 -0
- package/module/messages.js +4 -164
- package/module/messages.spec.js +1 -1
- package/module/react-query.js +2 -1
- package/module/react-query.spec.js +2 -2
- package/module/recoil.spec.js +1 -1
- package/module/{utils.js → utils/babel.js} +19 -0
- package/module/{utils.spec.js → utils/babel.spec.js} +2 -2
- package/module/utils/index.js +1 -0
- package/module/utils/types.js +200 -0
- package/module/wasm.js +6 -157
- package/module/wasm.sg721.spec.js +24 -0
- package/module/wasm.spec.js +17 -13
- package/module/wasm.vectis.spec.js +43 -0
- package/package.json +2 -2
@@ -0,0 +1,53 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
var _generator = _interopRequireDefault(require("@babel/generator"));
|
6
|
+
|
7
|
+
var _cosmos_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/cosmos_msg_for__empty.json"));
|
8
|
+
|
9
|
+
var _execute_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/execute_msg_for__empty.json"));
|
10
|
+
|
11
|
+
var _can_execute_relay_response = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/can_execute_relay_response.json"));
|
12
|
+
|
13
|
+
var _info_response = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/info_response.json"));
|
14
|
+
|
15
|
+
var _relay_transaction = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/relay_transaction.json"));
|
16
|
+
|
17
|
+
var _wasm = require("./wasm");
|
18
|
+
|
19
|
+
var expectCode = function expectCode(ast) {
|
20
|
+
expect((0, _generator["default"])(ast).code).toMatchSnapshot();
|
21
|
+
};
|
22
|
+
|
23
|
+
var printCode = function printCode(ast) {
|
24
|
+
console.log((0, _generator["default"])(ast).code);
|
25
|
+
};
|
26
|
+
|
27
|
+
it('cosmos_msg_for__empty', function () {
|
28
|
+
expectCode((0, _wasm.createTypeInterface)(_cosmos_msg_for__empty["default"]));
|
29
|
+
});
|
30
|
+
it('execute_msg_for__empty', function () {
|
31
|
+
expectCode((0, _wasm.createTypeInterface)(_execute_msg_for__empty["default"]));
|
32
|
+
});
|
33
|
+
it('can_execute_relay_response', function () {
|
34
|
+
expectCode((0, _wasm.createTypeInterface)(_can_execute_relay_response["default"]));
|
35
|
+
});
|
36
|
+
it('info_response', function () {
|
37
|
+
expectCode((0, _wasm.createTypeInterface)(_info_response["default"]));
|
38
|
+
});
|
39
|
+
it('relay_transaction', function () {
|
40
|
+
expectCode((0, _wasm.createTypeInterface)(_relay_transaction["default"]));
|
41
|
+
});
|
42
|
+
it('query classes', function () {
|
43
|
+
expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _cosmos_msg_for__empty["default"]));
|
44
|
+
});
|
45
|
+
it('query classes', function () {
|
46
|
+
expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _execute_msg_for__empty["default"]));
|
47
|
+
});
|
48
|
+
it('execute classes array types', function () {
|
49
|
+
expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg_for__empty["default"]));
|
50
|
+
});
|
51
|
+
it('execute interfaces no extends', function () {
|
52
|
+
expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _execute_msg_for__empty["default"]));
|
53
|
+
});
|
package/module/messages.js
CHANGED
@@ -1,134 +1,10 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { camel } from 'case';
|
3
3
|
import { bindMethod, typedIdentifier, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils';
|
4
|
+
import { createTypedObjectParams } from './utils/types';
|
5
|
+
import { identifier, tsTypeOperator } from './utils/babel';
|
4
6
|
|
5
|
-
const
|
6
|
-
switch ($ref) {
|
7
|
-
case '#/definitions/Binary':
|
8
|
-
return t.tsTypeReference(t.identifier('Binary'));
|
9
|
-
|
10
|
-
default:
|
11
|
-
if ($ref.startsWith('#/definitions/')) {
|
12
|
-
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
|
13
|
-
}
|
14
|
-
|
15
|
-
throw new Error('what is $ref: ' + $ref);
|
16
|
-
}
|
17
|
-
};
|
18
|
-
|
19
|
-
const getArrayTypeFromRef = $ref => {
|
20
|
-
return t.tsArrayType(getTypeFromRef($ref));
|
21
|
-
};
|
22
|
-
|
23
|
-
const getArrayTypeFromType = type => {
|
24
|
-
return t.tsArrayType(getType(type));
|
25
|
-
}; // MARKED AS NOT DRY
|
26
|
-
|
27
|
-
|
28
|
-
const identifier = (name, typeAnnotation, optional = false) => {
|
29
|
-
const type = t.identifier(name);
|
30
|
-
type.typeAnnotation = typeAnnotation;
|
31
|
-
type.optional = optional;
|
32
|
-
return type;
|
33
|
-
};
|
34
|
-
|
35
|
-
const getType = type => {
|
36
|
-
switch (type) {
|
37
|
-
case 'string':
|
38
|
-
return t.tsStringKeyword();
|
39
|
-
|
40
|
-
case 'boolean':
|
41
|
-
return t.tSBooleanKeyword();
|
42
|
-
|
43
|
-
case 'integer':
|
44
|
-
return t.tsNumberKeyword();
|
45
|
-
|
46
|
-
default:
|
47
|
-
throw new Error('what is type: ' + type);
|
48
|
-
}
|
49
|
-
}; // MARKED AS NOT DRY
|
50
|
-
|
51
|
-
|
52
|
-
const getPropertyType = (schema, prop) => {
|
53
|
-
const props = schema.properties ?? {};
|
54
|
-
let info = props[prop];
|
55
|
-
let type = null;
|
56
|
-
let optional = schema.required?.includes(prop);
|
57
|
-
|
58
|
-
if (info.allOf && info.allOf.length === 1) {
|
59
|
-
info = info.allOf[0];
|
60
|
-
}
|
61
|
-
|
62
|
-
if (typeof info.$ref === 'string') {
|
63
|
-
type = getTypeFromRef(info.$ref);
|
64
|
-
}
|
65
|
-
|
66
|
-
if (Array.isArray(info.anyOf)) {
|
67
|
-
// assuming 2nd is null, but let's check to ensure
|
68
|
-
if (info.anyOf.length !== 2) {
|
69
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
70
|
-
}
|
71
|
-
|
72
|
-
const [nullableType, nullType] = info.anyOf;
|
73
|
-
|
74
|
-
if (nullType?.type !== 'null') {
|
75
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
76
|
-
}
|
77
|
-
|
78
|
-
type = getTypeFromRef(nullableType?.$ref);
|
79
|
-
optional = true;
|
80
|
-
}
|
81
|
-
|
82
|
-
if (typeof info.type === 'string') {
|
83
|
-
if (info.type === 'array') {
|
84
|
-
if (info.items.$ref) {
|
85
|
-
type = getArrayTypeFromRef(info.items.$ref);
|
86
|
-
} else {
|
87
|
-
type = getArrayTypeFromType(info.items.type);
|
88
|
-
}
|
89
|
-
} else {
|
90
|
-
type = getType(info.type);
|
91
|
-
}
|
92
|
-
}
|
93
|
-
|
94
|
-
if (Array.isArray(info.type)) {
|
95
|
-
// assuming 2nd is null, but let's check to ensure
|
96
|
-
if (info.type.length !== 2) {
|
97
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
98
|
-
}
|
99
|
-
|
100
|
-
const [nullableType, nullType] = info.type;
|
101
|
-
|
102
|
-
if (nullType !== 'null') {
|
103
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
104
|
-
}
|
105
|
-
|
106
|
-
type = getType(nullableType);
|
107
|
-
optional = true;
|
108
|
-
}
|
109
|
-
|
110
|
-
if (!type) {
|
111
|
-
throw new Error('cannot find type for ' + JSON.stringify(info));
|
112
|
-
}
|
113
|
-
|
114
|
-
if (schema.required?.includes(prop)) {
|
115
|
-
optional = false;
|
116
|
-
}
|
117
|
-
|
118
|
-
return {
|
119
|
-
type,
|
120
|
-
optional
|
121
|
-
};
|
122
|
-
};
|
123
|
-
|
124
|
-
const tsTypeOperator = (typeAnnotation, operator) => {
|
125
|
-
const obj = t.tsTypeOperator(typeAnnotation);
|
126
|
-
obj.operator = operator;
|
127
|
-
return obj;
|
128
|
-
}; // MARKED AS NOT DRY
|
129
|
-
|
130
|
-
|
131
|
-
const createWasmExecMethod = jsonschema => {
|
7
|
+
const createWasmExecMethodPartial = jsonschema => {
|
132
8
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
133
9
|
const methodName = camel(underscoreName);
|
134
10
|
const properties = jsonschema.properties[underscoreName].properties ?? {};
|
@@ -169,7 +45,7 @@ export const createFromPartialClass = (className, implementsClassName, execMsg)
|
|
169
45
|
const propertyNames = getMessageProperties(execMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
|
170
46
|
const bindings = propertyNames.map(camel).map(bindMethod);
|
171
47
|
const methods = getMessageProperties(execMsg).map(schema => {
|
172
|
-
return
|
48
|
+
return createWasmExecMethodPartial(schema);
|
173
49
|
});
|
174
50
|
const blockStmt = [];
|
175
51
|
[].push.apply(blockStmt, [t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('sender')), t.identifier('sender'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))), ...bindings]);
|
@@ -190,42 +66,6 @@ export const createFromPartialInterface = (className, execMsg) => {
|
|
190
66
|
t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
|
191
67
|
}; // MARKED AS NOT DRY
|
192
68
|
|
193
|
-
const propertySignature = (name, typeAnnotation, optional = false) => {
|
194
|
-
// prop.leadingComments = [{
|
195
|
-
// type: 'Comment',
|
196
|
-
// value: ' Data on the token itself'
|
197
|
-
// }];
|
198
|
-
// prop.leadingComments = [{
|
199
|
-
// type: 'CommentBlock',
|
200
|
-
// value: '* Data on the token itself'
|
201
|
-
// }];
|
202
|
-
return {
|
203
|
-
type: 'TSPropertySignature',
|
204
|
-
key: t.identifier(name),
|
205
|
-
typeAnnotation,
|
206
|
-
optional
|
207
|
-
};
|
208
|
-
}; // MARKED AS NOT DRY
|
209
|
-
|
210
|
-
|
211
|
-
const createTypedObjectParams = (jsonschema, camelize = true) => {
|
212
|
-
const keys = Object.keys(jsonschema.properties ?? {});
|
213
|
-
if (!keys.length) return;
|
214
|
-
const typedParams = keys.map(prop => {
|
215
|
-
const {
|
216
|
-
type,
|
217
|
-
optional
|
218
|
-
} = getPropertyType(jsonschema, prop);
|
219
|
-
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
|
220
|
-
});
|
221
|
-
const params = keys.map(prop => {
|
222
|
-
return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
|
223
|
-
});
|
224
|
-
const obj = t.objectPattern([...params]);
|
225
|
-
obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
|
226
|
-
return obj;
|
227
|
-
};
|
228
|
-
|
229
69
|
const createPropertyFunctionWithObjectParamsForPartial = (methodName, responseType, jsonschema) => {
|
230
70
|
const obj = createTypedObjectParams(jsonschema);
|
231
71
|
const fixedParams = [// identifier('fee', t.tsTypeAnnotation(
|
package/module/messages.spec.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import generate from '@babel/generator';
|
2
|
-
import execute_msg from '
|
2
|
+
import execute_msg from './../../../__fixtures__/basic/execute_msg_for__empty.json';
|
3
3
|
import { createFromPartialClass, createFromPartialInterface } from './messages';
|
4
4
|
|
5
5
|
const expectCode = ast => {
|
package/module/react-query.js
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { camel, pascal } from 'case';
|
3
3
|
import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils';
|
4
|
-
import { propertySignature
|
4
|
+
import { propertySignature } from './utils/babel';
|
5
|
+
import { getPropertyType } from './utils/types';
|
5
6
|
export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => {
|
6
7
|
return getMessageProperties(queryMsg).reduce((m, schema) => {
|
7
8
|
const underscoreName = Object.keys(schema.properties)[0];
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import generate from '@babel/generator';
|
2
2
|
import * as t from '@babel/types';
|
3
|
-
import query_msg from '
|
3
|
+
import query_msg from './../../../__fixtures__/basic/query_msg.json';
|
4
4
|
import { createReactQueryHooks } from './react-query';
|
5
5
|
|
6
6
|
const expectCode = ast => {
|
@@ -21,6 +21,6 @@ const printCode = ast => {
|
|
21
21
|
|
22
22
|
|
23
23
|
it('createReactQueryHooks', () => {
|
24
|
-
|
24
|
+
expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
|
25
25
|
expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
|
26
26
|
});
|
package/module/recoil.spec.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import generate from '@babel/generator';
|
2
2
|
import * as t from '@babel/types';
|
3
|
-
import query_msg from '
|
3
|
+
import query_msg from './../../../__fixtures__/basic/query_msg.json';
|
4
4
|
import { createRecoilSelector, createRecoilSelectors, createRecoilQueryClient } from './recoil';
|
5
5
|
|
6
6
|
const expectCode = ast => {
|
@@ -1,5 +1,24 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { snake } from "case";
|
3
|
+
export const propertySignature = (name, typeAnnotation, optional = false) => {
|
4
|
+
return {
|
5
|
+
type: 'TSPropertySignature',
|
6
|
+
key: t.identifier(name),
|
7
|
+
typeAnnotation,
|
8
|
+
optional
|
9
|
+
};
|
10
|
+
};
|
11
|
+
export const identifier = (name, typeAnnotation, optional = false) => {
|
12
|
+
const type = t.identifier(name);
|
13
|
+
type.typeAnnotation = typeAnnotation;
|
14
|
+
type.optional = optional;
|
15
|
+
return type;
|
16
|
+
};
|
17
|
+
export const tsTypeOperator = (typeAnnotation, operator) => {
|
18
|
+
const obj = t.tsTypeOperator(typeAnnotation);
|
19
|
+
obj.operator = operator;
|
20
|
+
return obj;
|
21
|
+
};
|
3
22
|
export const getMessageProperties = msg => {
|
4
23
|
if (msg.anyOf) return msg.anyOf;
|
5
24
|
if (msg.oneOf) return msg.oneOf;
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { importStmt } from './
|
1
|
+
import { importStmt } from './babel';
|
2
2
|
import generate from '@babel/generator';
|
3
3
|
import * as t from '@babel/types';
|
4
|
-
import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './
|
4
|
+
import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './babel';
|
5
5
|
|
6
6
|
const expectCode = ast => {
|
7
7
|
expect(generate(ast).code).toMatchSnapshot();
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './babel';
|
@@ -0,0 +1,200 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { camel } from 'case';
|
3
|
+
import { propertySignature } from './babel';
|
4
|
+
export const forEmptyNameFix = name => {
|
5
|
+
if (name.endsWith('For_Empty')) {
|
6
|
+
return name.replace(/For_Empty$/, '_for_Empty');
|
7
|
+
}
|
8
|
+
|
9
|
+
return name;
|
10
|
+
};
|
11
|
+
|
12
|
+
const getTypeFromRef = $ref => {
|
13
|
+
switch ($ref) {
|
14
|
+
case '#/definitions/Binary':
|
15
|
+
return t.tsTypeReference(t.identifier('Binary'));
|
16
|
+
|
17
|
+
default:
|
18
|
+
if ($ref?.startsWith('#/definitions/')) {
|
19
|
+
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
|
20
|
+
}
|
21
|
+
|
22
|
+
throw new Error('what is $ref: ' + $ref);
|
23
|
+
}
|
24
|
+
};
|
25
|
+
|
26
|
+
const getArrayTypeFromRef = $ref => {
|
27
|
+
return t.tsArrayType(getTypeFromRef($ref));
|
28
|
+
};
|
29
|
+
|
30
|
+
const getArrayTypeFromType = type => {
|
31
|
+
return t.tsArrayType(getType(type));
|
32
|
+
};
|
33
|
+
|
34
|
+
export const getType = type => {
|
35
|
+
switch (type) {
|
36
|
+
case 'string':
|
37
|
+
return t.tsStringKeyword();
|
38
|
+
|
39
|
+
case 'boolean':
|
40
|
+
return t.tSBooleanKeyword();
|
41
|
+
|
42
|
+
case 'integer':
|
43
|
+
return t.tsNumberKeyword();
|
44
|
+
// case 'object':
|
45
|
+
// return t.tsObjectKeyword();
|
46
|
+
|
47
|
+
default:
|
48
|
+
throw new Error('contact maintainers [unknown type]: ' + type);
|
49
|
+
}
|
50
|
+
};
|
51
|
+
export const getPropertyType = (schema, prop) => {
|
52
|
+
const props = schema.properties ?? {};
|
53
|
+
let info = props[prop];
|
54
|
+
let type = null;
|
55
|
+
let optional = !schema.required?.includes(prop);
|
56
|
+
|
57
|
+
if (info.allOf && info.allOf.length === 1) {
|
58
|
+
info = info.allOf[0];
|
59
|
+
}
|
60
|
+
|
61
|
+
if (typeof info.$ref === 'string') {
|
62
|
+
type = getTypeFromRef(info.$ref);
|
63
|
+
}
|
64
|
+
|
65
|
+
if (Array.isArray(info.anyOf)) {
|
66
|
+
// assuming 2nd is null, but let's check to ensure
|
67
|
+
if (info.anyOf.length !== 2) {
|
68
|
+
throw new Error('case not handled by transpiler. contact maintainers.');
|
69
|
+
}
|
70
|
+
|
71
|
+
const [nullableType, nullType] = info.anyOf;
|
72
|
+
|
73
|
+
if (nullType?.type !== 'null') {
|
74
|
+
throw new Error('[nullableType.type]: case not handled by transpiler. contact maintainers.');
|
75
|
+
}
|
76
|
+
|
77
|
+
if (!nullableType?.$ref) {
|
78
|
+
if (nullableType.title) {
|
79
|
+
type = t.tsTypeReference(t.identifier(nullableType.title));
|
80
|
+
} else {
|
81
|
+
throw new Error('[nullableType.title] case not handled by transpiler. contact maintainers.');
|
82
|
+
}
|
83
|
+
} else {
|
84
|
+
type = getTypeFromRef(nullableType?.$ref);
|
85
|
+
}
|
86
|
+
|
87
|
+
optional = true;
|
88
|
+
}
|
89
|
+
|
90
|
+
if (typeof info.type === 'string') {
|
91
|
+
if (info.type === 'array') {
|
92
|
+
if (info.items.$ref) {
|
93
|
+
type = getArrayTypeFromRef(info.items.$ref);
|
94
|
+
} else if (info.items.title) {
|
95
|
+
type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
|
96
|
+
} else if (info.items.type) {
|
97
|
+
type = getArrayTypeFromType(info.items.type);
|
98
|
+
} else {
|
99
|
+
throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
|
100
|
+
}
|
101
|
+
} else {
|
102
|
+
type = getType(info.type);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
if (Array.isArray(info.type)) {
|
107
|
+
// assuming 2nd is null, but let's check to ensure
|
108
|
+
if (info.type.length !== 2) {
|
109
|
+
throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2));
|
110
|
+
}
|
111
|
+
|
112
|
+
const [nullableType, nullType] = info.type;
|
113
|
+
|
114
|
+
if (nullType !== 'null') {
|
115
|
+
throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2));
|
116
|
+
}
|
117
|
+
|
118
|
+
if (nullableType === 'array') {
|
119
|
+
type = t.tsArrayType(getType(info.items.type));
|
120
|
+
} else {
|
121
|
+
type = getType(nullableType);
|
122
|
+
}
|
123
|
+
|
124
|
+
optional = true;
|
125
|
+
}
|
126
|
+
|
127
|
+
if (!type) {
|
128
|
+
throw new Error('cannot find type for ' + JSON.stringify(info));
|
129
|
+
}
|
130
|
+
|
131
|
+
if (schema.required?.includes(prop)) {
|
132
|
+
optional = false;
|
133
|
+
}
|
134
|
+
|
135
|
+
return {
|
136
|
+
type,
|
137
|
+
optional
|
138
|
+
};
|
139
|
+
};
|
140
|
+
export const createTypedObjectParams = (jsonschema, camelize = true) => {
|
141
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
142
|
+
if (!keys.length) return;
|
143
|
+
const typedParams = keys.map(prop => {
|
144
|
+
if (jsonschema.properties[prop].type === 'object') {
|
145
|
+
if (jsonschema.properties[prop].title) {
|
146
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(jsonschema.properties[prop].title)))));
|
147
|
+
} else {
|
148
|
+
throw new Error('createTypedObjectParams() contact maintainer');
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
if (Array.isArray(jsonschema.properties[prop].allOf)) {
|
153
|
+
const isOptional = !jsonschema.required?.includes(prop);
|
154
|
+
const unionTypes = jsonschema.properties[prop].allOf.map(el => {
|
155
|
+
if (el.title) return el.title;
|
156
|
+
return el.type;
|
157
|
+
});
|
158
|
+
const uniqUnionTypes = [...new Set(unionTypes)];
|
159
|
+
|
160
|
+
if (uniqUnionTypes.length === 1) {
|
161
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(uniqUnionTypes[0])))), isOptional);
|
162
|
+
} else {
|
163
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(forEmptyNameFix(typ)))))), isOptional);
|
164
|
+
}
|
165
|
+
} else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
|
166
|
+
const oneOf = JSON.stringify(jsonschema.properties[prop].oneOf, null, 2);
|
167
|
+
const isOptional = !jsonschema.required?.includes(prop);
|
168
|
+
const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
|
169
|
+
if (el.title) return el.title;
|
170
|
+
return el.type;
|
171
|
+
});
|
172
|
+
const uniqUnionTypes = [...new Set(unionTypes)];
|
173
|
+
|
174
|
+
if (uniqUnionTypes.length === 1) {
|
175
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(uniqUnionTypes[0])))), isOptional);
|
176
|
+
} else {
|
177
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(forEmptyNameFix(typ)))))), isOptional);
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
try {
|
182
|
+
getPropertyType(jsonschema, prop);
|
183
|
+
} catch (e) {
|
184
|
+
console.log(e);
|
185
|
+
console.log(jsonschema, prop);
|
186
|
+
}
|
187
|
+
|
188
|
+
const {
|
189
|
+
type,
|
190
|
+
optional
|
191
|
+
} = getPropertyType(jsonschema, prop);
|
192
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
|
193
|
+
});
|
194
|
+
const params = keys.map(prop => {
|
195
|
+
return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
|
196
|
+
});
|
197
|
+
const obj = t.objectPattern([...params]);
|
198
|
+
obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
|
199
|
+
return obj;
|
200
|
+
};
|