@bidirekt/typescript-sdk-cli 0.1.1 → 0.3.0
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/README.md +73 -41
- package/dist/command.js +14 -13
- package/dist/model/contract.js +7 -1
- package/dist/model/declarations-to-contract.js +97 -37
- package/dist/model/description.js +16 -0
- package/dist/model/protocols/graphql/block-to-references.js +8 -0
- package/dist/model/protocols/graphql/blocks-to-merged.js +77 -0
- package/dist/model/protocols/graphql/contract.js +1 -0
- package/dist/model/protocols/graphql/field-extra.js +46 -0
- package/dist/model/protocols/graphql/returns-to-schema.js +77 -0
- package/dist/model/protocols/rest/contract.js +1 -0
- package/dist/model/{resources-to-merged.js → protocols/rest/resources-to-merged.js} +15 -10
- package/dist/model/protocols/rest/resources-to-references.js +10 -0
- package/dist/protocols.js +49 -0
- package/dist/reader/comment-tags.js +77 -37
- package/dist/reader/literal-options.js +17 -2
- package/dist/reader/program.js +1 -1
- package/dist/reader/project-to-contract.js +51 -23
- package/dist/reader/protocols/graphql/decorators-to-blocks.js +401 -0
- package/dist/reader/protocols/graphql/protocol-input-to-blocks.js +11 -0
- package/dist/reader/protocols/graphql/tags-to-blocks.js +239 -0
- package/dist/reader/{handler-methods.js → protocols/rest/handler-methods.js} +36 -26
- package/dist/reader/protocols/rest/protocol-input-to-resources.js +11 -0
- package/dist/reader/protocols/rest/tag-handlers.js +152 -0
- package/dist/reader/schema-classes.js +193 -31
- package/dist/reader/schema-context.js +10 -5
- package/dist/reader/sdk-references.js +149 -39
- package/dist/reader/tag-handlers.js +36 -146
- package/dist/reader/tag-schemas.js +93 -43
- package/dist/reader/type-text-to-schema-node.js +4 -1
- package/dist/reader/type-to-schema-node.js +60 -10
- package/dist/writer/contract-to-yaml.js +38 -59
- package/dist/writer/protocols/graphql/block-to-yaml.js +31 -0
- package/dist/writer/protocols/graphql/field-extra-to-yaml.js +13 -0
- package/dist/writer/protocols/rest/resources-to-yaml.js +36 -0
- package/package.json +2 -2
- /package/dist/model/{schema-names.js → protocols/rest/schema-names.js} +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Document, Pair, Scalar, YAMLMap, YAMLSeq } from 'yaml';
|
|
2
2
|
const header = ' Generated by @bidirekt/typescript-sdk. Do not edit.';
|
|
3
|
-
export function contractToYaml(contract) {
|
|
3
|
+
export function contractToYaml(contract, protocols) {
|
|
4
4
|
const root = new YAMLMap();
|
|
5
|
-
if (contract.provides.
|
|
6
|
-
root.add(new Pair('provides',
|
|
5
|
+
if (contract.provides.size > 0) {
|
|
6
|
+
root.add(new Pair('provides', blocksToYaml(contract.provides, protocols)));
|
|
7
7
|
}
|
|
8
|
-
if (contract.consumes.
|
|
9
|
-
root.add(new Pair('consumes', consumedToYaml(contract.consumes)));
|
|
8
|
+
if (contract.consumes.size > 0) {
|
|
9
|
+
root.add(new Pair('consumes', consumedToYaml(contract.consumes, protocols)));
|
|
10
10
|
}
|
|
11
11
|
if (contract.schemas.length > 0) {
|
|
12
12
|
root.add(new Pair('schemas', schemasToYaml(contract.schemas)));
|
|
@@ -19,89 +19,55 @@ export function contractToYaml(contract) {
|
|
|
19
19
|
nullStr: '~',
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
|
-
function compareBytes(a, b) {
|
|
22
|
+
export function compareBytes(a, b) {
|
|
23
23
|
return Buffer.compare(Buffer.from(a), Buffer.from(b));
|
|
24
24
|
}
|
|
25
|
-
function sortedByKey(groups) {
|
|
25
|
+
export function sortedByKey(groups) {
|
|
26
26
|
return [...groups].sort(([a], [b]) => compareBytes(a, b));
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
const rest = new YAMLMap();
|
|
30
|
-
rest.add(new Pair('rest', resourcesToYaml(resources)));
|
|
31
|
-
return rest;
|
|
32
|
-
}
|
|
33
|
-
function consumedToYaml(resources) {
|
|
28
|
+
function consumedToYaml(consumes, protocols) {
|
|
34
29
|
const providers = new YAMLMap();
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
providers.add(new Pair(name, providedToYaml(own)));
|
|
30
|
+
for (const [name, blocks] of sortedByKey(consumes)) {
|
|
31
|
+
providers.add(new Pair(name, blocksToYaml(blocks, protocols)));
|
|
38
32
|
}
|
|
39
33
|
return providers;
|
|
40
34
|
}
|
|
41
|
-
function
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
for (const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
.
|
|
48
|
-
method: resource.method.toLowerCase(),
|
|
49
|
-
resource,
|
|
50
|
-
}))
|
|
51
|
-
.sort((a, b) => compareBytes(a.method, b.method));
|
|
52
|
-
for (const { method, resource } of atPath) {
|
|
53
|
-
methods.add(new Pair(method, resourceToYaml(resource)));
|
|
35
|
+
function blocksToYaml(blocks, protocols) {
|
|
36
|
+
const byProtocol = new YAMLMap();
|
|
37
|
+
const sorted = [...protocols].sort((a, b) => compareBytes(a.name, b.name));
|
|
38
|
+
for (const protocol of sorted) {
|
|
39
|
+
if (blocks.has(protocol.name)) {
|
|
40
|
+
const block = protocol.toYaml(blocks.get(protocol.name));
|
|
41
|
+
byProtocol.add(new Pair(protocol.name, block));
|
|
54
42
|
}
|
|
55
|
-
endpoints.add(new Pair(path, methods));
|
|
56
|
-
}
|
|
57
|
-
return endpoints;
|
|
58
|
-
}
|
|
59
|
-
function resourceToYaml(resource) {
|
|
60
|
-
const interaction = new YAMLMap();
|
|
61
|
-
if (resource.request) {
|
|
62
|
-
interaction.add(new Pair('request', bodyToYaml(resource.request)));
|
|
63
43
|
}
|
|
64
|
-
|
|
65
|
-
const byStatus = [...resource.responses].sort((a, b) => a.status - b.status);
|
|
66
|
-
for (const response of byStatus) {
|
|
67
|
-
responses.add(new Pair(response.status, bodyToYaml(response)));
|
|
68
|
-
}
|
|
69
|
-
interaction.add(new Pair('responses', responses));
|
|
70
|
-
return interaction;
|
|
71
|
-
}
|
|
72
|
-
function bodyToYaml(body) {
|
|
73
|
-
return new Scalar(body.schema);
|
|
44
|
+
return byProtocol;
|
|
74
45
|
}
|
|
75
46
|
function schemasToYaml(schemas) {
|
|
76
47
|
const named = new YAMLMap();
|
|
77
48
|
const sorted = [...schemas].sort((a, b) => compareBytes(a.name, b.name));
|
|
78
49
|
for (const schema of sorted) {
|
|
79
|
-
named.add(new Pair(schema.name, nodeToYaml(schema.node)));
|
|
50
|
+
named.add(new Pair(schema.name, nodeToYaml(schema.node, schema.vocabulary)));
|
|
80
51
|
}
|
|
81
52
|
return named;
|
|
82
53
|
}
|
|
83
|
-
function nodeToYaml(node) {
|
|
54
|
+
function nodeToYaml(node, vocabulary) {
|
|
84
55
|
const fields = new YAMLMap();
|
|
85
56
|
switch (node.type) {
|
|
86
57
|
case 'ref':
|
|
87
58
|
fields.add(new Pair('ref', node.schema));
|
|
88
59
|
break;
|
|
89
|
-
case 'object':
|
|
60
|
+
case 'object':
|
|
90
61
|
fields.add(new Pair('type', node.type));
|
|
91
|
-
|
|
92
|
-
for (const property of node.properties) {
|
|
93
|
-
properties.add(new Pair(property.name, nodeToYaml(property.node)));
|
|
94
|
-
}
|
|
95
|
-
fields.add(new Pair('properties', properties));
|
|
62
|
+
fields.add(new Pair('properties', propertiesToYaml(node.properties)));
|
|
96
63
|
break;
|
|
97
|
-
}
|
|
98
64
|
case 'array':
|
|
99
65
|
fields.add(new Pair('type', node.type));
|
|
100
|
-
fields.add(new Pair('items', nodeToYaml(node.items)));
|
|
66
|
+
fields.add(new Pair('items', nodeToYaml(node.items, vocabulary)));
|
|
101
67
|
break;
|
|
102
68
|
case 'map':
|
|
103
69
|
fields.add(new Pair('type', node.type));
|
|
104
|
-
fields.add(new Pair('values', nodeToYaml(node.values)));
|
|
70
|
+
fields.add(new Pair('values', nodeToYaml(node.values, vocabulary)));
|
|
105
71
|
break;
|
|
106
72
|
case 'enum':
|
|
107
73
|
fields.add(new Pair('type', node.type));
|
|
@@ -109,9 +75,12 @@ function nodeToYaml(node) {
|
|
|
109
75
|
break;
|
|
110
76
|
case 'union': {
|
|
111
77
|
fields.add(new Pair('type', node.type));
|
|
78
|
+
if (node.properties !== undefined) {
|
|
79
|
+
fields.add(new Pair('properties', propertiesToYaml(node.properties)));
|
|
80
|
+
}
|
|
112
81
|
const members = new YAMLMap();
|
|
113
82
|
for (const member of node.members) {
|
|
114
|
-
members.add(new Pair(member.key, nodeToYaml(member.node)));
|
|
83
|
+
members.add(new Pair(member.key, nodeToYaml(member.node, vocabulary)));
|
|
115
84
|
}
|
|
116
85
|
fields.add(new Pair('members', members));
|
|
117
86
|
break;
|
|
@@ -119,6 +88,9 @@ function nodeToYaml(node) {
|
|
|
119
88
|
default:
|
|
120
89
|
fields.add(new Pair('type', node.type));
|
|
121
90
|
}
|
|
91
|
+
if (node.extra !== undefined) {
|
|
92
|
+
vocabulary?.toYaml(node, fields);
|
|
93
|
+
}
|
|
122
94
|
if (node.optional) {
|
|
123
95
|
fields.add(new Pair('optional', true));
|
|
124
96
|
}
|
|
@@ -130,6 +102,13 @@ function nodeToYaml(node) {
|
|
|
130
102
|
}
|
|
131
103
|
return fields;
|
|
132
104
|
}
|
|
105
|
+
function propertiesToYaml(properties) {
|
|
106
|
+
const map = new YAMLMap();
|
|
107
|
+
for (const property of properties) {
|
|
108
|
+
map.add(new Pair(property.name, nodeToYaml(property.node, property.vocabulary)));
|
|
109
|
+
}
|
|
110
|
+
return map;
|
|
111
|
+
}
|
|
133
112
|
function enumMembersToYaml(members) {
|
|
134
113
|
const sequence = new YAMLSeq();
|
|
135
114
|
sequence.flow = true;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Pair, YAMLMap } from 'yaml';
|
|
2
|
+
import { compareBytes } from "../../contract-to-yaml.js";
|
|
3
|
+
const operationKinds = ['query', 'mutation'];
|
|
4
|
+
export function blockToYaml(block) {
|
|
5
|
+
const graphql = new YAMLMap();
|
|
6
|
+
if (block.graph !== undefined) {
|
|
7
|
+
graphql.add(new Pair('graph', block.graph.name));
|
|
8
|
+
}
|
|
9
|
+
const byKind = Map.groupBy(block.operations, ({ kind }) => kind);
|
|
10
|
+
for (const kind of operationKinds) {
|
|
11
|
+
const operations = byKind.get(kind);
|
|
12
|
+
if (operations === undefined) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const fields = new YAMLMap();
|
|
16
|
+
const sorted = [...operations].sort((a, b) => compareBytes(a.name, b.name));
|
|
17
|
+
for (const operation of sorted) {
|
|
18
|
+
fields.add(new Pair(operation.name, operationToYaml(operation)));
|
|
19
|
+
}
|
|
20
|
+
graphql.add(new Pair(kind, fields));
|
|
21
|
+
}
|
|
22
|
+
return graphql;
|
|
23
|
+
}
|
|
24
|
+
function operationToYaml(operation) {
|
|
25
|
+
const field = new YAMLMap();
|
|
26
|
+
if (operation.arguments !== undefined) {
|
|
27
|
+
field.add(new Pair('arguments', operation.arguments.schema));
|
|
28
|
+
}
|
|
29
|
+
field.add(new Pair('returns', operation.returns.schema));
|
|
30
|
+
return field;
|
|
31
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Pair } from 'yaml';
|
|
2
|
+
import { nodeToArguments, nodeToScalar, } from "../../../model/protocols/graphql/field-extra.js";
|
|
3
|
+
export function fieldExtraToYaml(node, map) {
|
|
4
|
+
const scalar = nodeToScalar(node);
|
|
5
|
+
if (scalar !== undefined) {
|
|
6
|
+
map.set('type', 'scalar');
|
|
7
|
+
map.add(new Pair('name', scalar));
|
|
8
|
+
}
|
|
9
|
+
const schema = nodeToArguments(node);
|
|
10
|
+
if (schema !== undefined) {
|
|
11
|
+
map.add(new Pair('arguments', schema));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Pair, Scalar, YAMLMap } from 'yaml';
|
|
2
|
+
import { compareBytes, sortedByKey } from "../../contract-to-yaml.js";
|
|
3
|
+
export function resourcesToYaml(resources) {
|
|
4
|
+
const endpoints = new YAMLMap();
|
|
5
|
+
const byPath = Map.groupBy(resources, (resource) => resource.path);
|
|
6
|
+
for (const [path, own] of sortedByKey(byPath)) {
|
|
7
|
+
const methods = new YAMLMap();
|
|
8
|
+
const atPath = own
|
|
9
|
+
.map((resource) => ({
|
|
10
|
+
method: resource.method.toLowerCase(),
|
|
11
|
+
resource,
|
|
12
|
+
}))
|
|
13
|
+
.sort((a, b) => compareBytes(a.method, b.method));
|
|
14
|
+
for (const { method, resource } of atPath) {
|
|
15
|
+
methods.add(new Pair(method, resourceToYaml(resource)));
|
|
16
|
+
}
|
|
17
|
+
endpoints.add(new Pair(path, methods));
|
|
18
|
+
}
|
|
19
|
+
return endpoints;
|
|
20
|
+
}
|
|
21
|
+
function resourceToYaml(resource) {
|
|
22
|
+
const interaction = new YAMLMap();
|
|
23
|
+
if (resource.request) {
|
|
24
|
+
interaction.add(new Pair('request', bodyToYaml(resource.request)));
|
|
25
|
+
}
|
|
26
|
+
const responses = new YAMLMap();
|
|
27
|
+
const byStatus = [...resource.responses].sort((a, b) => a.status - b.status);
|
|
28
|
+
for (const response of byStatus) {
|
|
29
|
+
responses.add(new Pair(response.status, bodyToYaml(response)));
|
|
30
|
+
}
|
|
31
|
+
interaction.add(new Pair('responses', responses));
|
|
32
|
+
return interaction;
|
|
33
|
+
}
|
|
34
|
+
function bodyToYaml(body) {
|
|
35
|
+
return new Scalar(body.schema);
|
|
36
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bidirekt/typescript-sdk-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"prepublishOnly": "npm run build"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@bidirekt/typescript-sdk": "0.
|
|
28
|
+
"@bidirekt/typescript-sdk": "0.3.0",
|
|
29
29
|
"typescript": "~6.0.3",
|
|
30
30
|
"yaml": "^2.9.1"
|
|
31
31
|
}
|
|
File without changes
|