@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,14 +1,17 @@
|
|
|
1
1
|
import { declarationsToContract } from "../model/declarations-to-contract.js";
|
|
2
2
|
import { programToTaggedComments } from "./comment-tags.js";
|
|
3
|
-
import { sdkReferencesToResources } from "./handler-methods.js";
|
|
4
3
|
import { programToSyntaxErrors, projectToProgram } from "./program.js";
|
|
5
4
|
import { sdkReferencesToSchemas } from "./schema-classes.js";
|
|
6
5
|
import { programToSchemaContext } from "./schema-context.js";
|
|
7
6
|
import { programToSdkReferences } from "./sdk-references.js";
|
|
8
|
-
import {
|
|
7
|
+
import { taggedCommentsToHandlerComments } from "./tag-handlers.js";
|
|
9
8
|
import { taggedCommentsToSchemaTags } from "./tag-schemas.js";
|
|
10
|
-
export function projectToContract(project, cwd) {
|
|
11
|
-
const contract = {
|
|
9
|
+
export function projectToContract(project, cwd, protocols) {
|
|
10
|
+
const contract = {
|
|
11
|
+
provides: new Map(),
|
|
12
|
+
consumes: new Map(),
|
|
13
|
+
schemas: [],
|
|
14
|
+
};
|
|
12
15
|
const projectProgram = projectToProgram(project);
|
|
13
16
|
if ('message' in projectProgram) {
|
|
14
17
|
return {
|
|
@@ -21,41 +24,66 @@ export function projectToContract(project, cwd) {
|
|
|
21
24
|
if (syntaxErrors.length > 0) {
|
|
22
25
|
return { contract, errors: syntaxErrors, warnings: [] };
|
|
23
26
|
}
|
|
24
|
-
const references = programToSdkReferences(projectProgram);
|
|
25
|
-
const tagged = programToTaggedComments(projectProgram);
|
|
27
|
+
const references = programToSdkReferences(projectProgram, protocols);
|
|
28
|
+
const tagged = programToTaggedComments(projectProgram, protocols);
|
|
26
29
|
const context = programToSchemaContext(projectProgram.program, cwd);
|
|
27
|
-
const schemaTags = taggedCommentsToSchemaTags(tagged.comments, context);
|
|
28
|
-
const schemas = sdkReferencesToSchemas(references, schemaTags, context);
|
|
29
|
-
const
|
|
30
|
-
const
|
|
30
|
+
const schemaTags = taggedCommentsToSchemaTags(tagged.comments, context, protocols);
|
|
31
|
+
const schemas = sdkReferencesToSchemas(references, schemaTags, context, protocols);
|
|
32
|
+
const decorators = references.filter(isProtocolDecorator);
|
|
33
|
+
const handlers = taggedCommentsToHandlerComments(tagged.comments, decorators, protocols);
|
|
34
|
+
const reads = protocols.map((protocol) => ({
|
|
35
|
+
name: protocol.name,
|
|
36
|
+
vocabulary: protocol.vocabulary,
|
|
37
|
+
read: protocol.read({
|
|
38
|
+
decorators: decorators.filter((decorator) => decorator.protocol === protocol.name),
|
|
39
|
+
comments: handlers.comments.get(protocol.name) ?? [],
|
|
40
|
+
context,
|
|
41
|
+
}),
|
|
42
|
+
}));
|
|
31
43
|
const merged = declarationsToContract({
|
|
32
|
-
|
|
33
|
-
consumes: [...resources.consumes, ...tagResources.consumes],
|
|
44
|
+
blocks: new Map(reads.map(({ name, read }) => [name, read.blocks])),
|
|
34
45
|
schemas: [
|
|
35
46
|
...schemas.schemas,
|
|
36
|
-
...
|
|
37
|
-
...
|
|
47
|
+
...reads.flatMap(({ vocabulary, read }) => read.derived.map((schema) => withVocabulary(schema, vocabulary))),
|
|
48
|
+
...handlers.schemas,
|
|
38
49
|
],
|
|
39
|
-
});
|
|
50
|
+
}, protocols);
|
|
40
51
|
return {
|
|
41
52
|
contract: merged.contract,
|
|
42
53
|
errors: [
|
|
43
|
-
...
|
|
54
|
+
...referencesToDiagnostics(references, 'unregistered'),
|
|
55
|
+
...referencesToDiagnostics(references, 'unexported'),
|
|
56
|
+
...new Set([
|
|
57
|
+
...schemas.errors,
|
|
58
|
+
...reads.flatMap(({ read }) => read.errors),
|
|
59
|
+
]),
|
|
44
60
|
...tagged.errors,
|
|
45
61
|
...schemaTags.errors,
|
|
46
|
-
...
|
|
62
|
+
...handlers.errors,
|
|
47
63
|
...merged.errors,
|
|
48
64
|
],
|
|
49
65
|
warnings: [
|
|
50
|
-
...references
|
|
51
|
-
if (reference.use !== 'misplaced') {
|
|
52
|
-
return [];
|
|
53
|
-
}
|
|
54
|
-
return [{ message: reference.message, location: reference.location }];
|
|
55
|
-
}),
|
|
66
|
+
...referencesToDiagnostics(references, 'misplaced'),
|
|
56
67
|
...schemas.warnings,
|
|
57
68
|
...tagged.warnings,
|
|
58
69
|
...schemaTags.warnings,
|
|
59
70
|
],
|
|
60
71
|
};
|
|
61
72
|
}
|
|
73
|
+
function withVocabulary(schema, vocabulary) {
|
|
74
|
+
if (vocabulary === undefined) {
|
|
75
|
+
return schema;
|
|
76
|
+
}
|
|
77
|
+
return { ...schema, vocabulary };
|
|
78
|
+
}
|
|
79
|
+
function isProtocolDecorator(reference) {
|
|
80
|
+
return reference.use === 'decorator' && reference.protocol !== undefined;
|
|
81
|
+
}
|
|
82
|
+
function referencesToDiagnostics(references, use) {
|
|
83
|
+
return references.flatMap((reference) => {
|
|
84
|
+
if (reference.use === 'decorator' || reference.use !== use) {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
return [{ message: reference.message, location: reference.location }];
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { isDiagnostic } from "../../../model/diagnostic.js";
|
|
3
|
+
import { returnsToSchema } from "../../../model/protocols/graphql/returns-to-schema.js";
|
|
4
|
+
import { expressionToSchemaRef, expressionToUnionMembers, } from "../../expression-to-schema-node.js";
|
|
5
|
+
import { expressionToLiteralOptions, literalOptionToBoolean, literalOptionToName, literalOptionToString, } from "../../literal-options.js";
|
|
6
|
+
import { nodeToLocation } from "../../location.js";
|
|
7
|
+
import { fieldTarget } from "../../sdk-references.js";
|
|
8
|
+
import { textToPrimitive } from "../../type-text-to-schema-node.js";
|
|
9
|
+
export const graphqlDecoratorTargets = {
|
|
10
|
+
BidirektGraphqlConsumer: {
|
|
11
|
+
isTarget: ts.isMethodDeclaration,
|
|
12
|
+
description: 'a method',
|
|
13
|
+
},
|
|
14
|
+
BidirektGraphqlField: fieldTarget,
|
|
15
|
+
BidirektGraph: {
|
|
16
|
+
isTarget: ts.isClassDeclaration,
|
|
17
|
+
description: 'a class declaration',
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
const consumerOptions = [
|
|
21
|
+
'provider',
|
|
22
|
+
'query',
|
|
23
|
+
'mutation',
|
|
24
|
+
'arguments',
|
|
25
|
+
'returns',
|
|
26
|
+
];
|
|
27
|
+
const listKeys = ['list', 'nullable', 'nullableItems', 'name'];
|
|
28
|
+
const nullableKeys = ['nullable', 'name'];
|
|
29
|
+
const scalarKeys = ['scalar', 'name'];
|
|
30
|
+
const enumKeys = ['enum', 'name'];
|
|
31
|
+
const unionKeys = ['union', 'name'];
|
|
32
|
+
const wrapperPlaces = {
|
|
33
|
+
nullable: '"nullable"',
|
|
34
|
+
list: 'the list',
|
|
35
|
+
};
|
|
36
|
+
export function sdkDecoratorsToBlocks(decorators, context) {
|
|
37
|
+
const read = {
|
|
38
|
+
blocks: [],
|
|
39
|
+
derived: [],
|
|
40
|
+
errors: [],
|
|
41
|
+
};
|
|
42
|
+
for (const decorator of decorators) {
|
|
43
|
+
if (decorator.name === 'BidirektGraphqlConsumer') {
|
|
44
|
+
consumerToRead(decorator, context, read);
|
|
45
|
+
}
|
|
46
|
+
else if (decorator.name === 'BidirektGraph') {
|
|
47
|
+
graphToRead(decorator, read);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return read;
|
|
51
|
+
}
|
|
52
|
+
function consumerToRead({ name, call }, context, read) {
|
|
53
|
+
const what = `@${name} options`;
|
|
54
|
+
const options = expressionToLiteralOptions(call.arguments[0], what, consumerOptions);
|
|
55
|
+
if (isDiagnostic(options)) {
|
|
56
|
+
read.errors.push(options);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const location = nodeToLocation(call);
|
|
60
|
+
const errors = [];
|
|
61
|
+
const withoutDiagnostic = (value) => {
|
|
62
|
+
if (isDiagnostic(value)) {
|
|
63
|
+
errors.push(value);
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
};
|
|
68
|
+
const provider = withoutDiagnostic(literalOptionToString(options, 'provider') ?? {
|
|
69
|
+
message: `${what} must declare "provider"`,
|
|
70
|
+
location,
|
|
71
|
+
});
|
|
72
|
+
const operation = withoutDiagnostic(optionsToOperation(options, what, call));
|
|
73
|
+
const argumentsExpression = options.get('arguments');
|
|
74
|
+
let argumentsUse;
|
|
75
|
+
if (argumentsExpression !== undefined) {
|
|
76
|
+
argumentsUse = withoutDiagnostic(expressionToSchemaUse(argumentsExpression, context));
|
|
77
|
+
}
|
|
78
|
+
const returnsExpression = options.get('returns');
|
|
79
|
+
let returns;
|
|
80
|
+
if (returnsExpression === undefined) {
|
|
81
|
+
errors.push({ message: `${what} must declare "returns"`, location });
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
returns = withoutDiagnostic(expressionToReturns(returnsExpression, context));
|
|
85
|
+
}
|
|
86
|
+
read.errors.push(...errors);
|
|
87
|
+
if (provider === undefined ||
|
|
88
|
+
operation === undefined ||
|
|
89
|
+
returnsExpression === undefined ||
|
|
90
|
+
returns === undefined ||
|
|
91
|
+
errors.length > 0) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
consumedToRead({
|
|
95
|
+
provider,
|
|
96
|
+
...operation,
|
|
97
|
+
arguments: argumentsUse,
|
|
98
|
+
returns,
|
|
99
|
+
returnsLocation: nodeToLocation(returnsExpression),
|
|
100
|
+
location,
|
|
101
|
+
}, read);
|
|
102
|
+
}
|
|
103
|
+
export function consumedToRead({ provider, kind, name, arguments: argumentsUse, returns, returnsLocation, location, }, read) {
|
|
104
|
+
const { schema, derived } = returnsToSchema(returns, returnsLocation);
|
|
105
|
+
read.derived.push(...derived);
|
|
106
|
+
const operation = {
|
|
107
|
+
kind,
|
|
108
|
+
name,
|
|
109
|
+
returns: { schema, location: returnsLocation },
|
|
110
|
+
location,
|
|
111
|
+
};
|
|
112
|
+
if (argumentsUse !== undefined) {
|
|
113
|
+
operation.arguments = argumentsUse;
|
|
114
|
+
}
|
|
115
|
+
read.blocks.push({ provider, block: { operations: [operation] } });
|
|
116
|
+
}
|
|
117
|
+
function optionsToOperation(options, what, call) {
|
|
118
|
+
const query = literalOptionToString(options, 'query');
|
|
119
|
+
const mutation = literalOptionToString(options, 'mutation');
|
|
120
|
+
if (isDiagnostic(query)) {
|
|
121
|
+
return query;
|
|
122
|
+
}
|
|
123
|
+
if (isDiagnostic(mutation)) {
|
|
124
|
+
return mutation;
|
|
125
|
+
}
|
|
126
|
+
if (query !== undefined && mutation !== undefined) {
|
|
127
|
+
return {
|
|
128
|
+
message: `${what} declare both "query" and "mutation": write one`,
|
|
129
|
+
location: nodeToLocation(call),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (query !== undefined) {
|
|
133
|
+
return { kind: 'query', name: query };
|
|
134
|
+
}
|
|
135
|
+
if (mutation !== undefined) {
|
|
136
|
+
return { kind: 'mutation', name: mutation };
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
message: `${what} must declare "query" or "mutation"`,
|
|
140
|
+
location: nodeToLocation(call),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function graphToRead({ name, call }, read) {
|
|
144
|
+
const [graph] = call.arguments;
|
|
145
|
+
const location = nodeToLocation(call);
|
|
146
|
+
if (call.arguments.length !== 1 || !ts.isStringLiteralLike(graph)) {
|
|
147
|
+
read.errors.push({
|
|
148
|
+
message: `@${name} needs the graph name as a string literal written in place: write @${name}('<name>')`,
|
|
149
|
+
location,
|
|
150
|
+
});
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (graph.text === '') {
|
|
154
|
+
read.errors.push({
|
|
155
|
+
message: `the graph name of @${name} is empty: write @${name}('<name>')`,
|
|
156
|
+
location,
|
|
157
|
+
});
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
read.blocks.push({
|
|
161
|
+
block: { operations: [], graph: { name: graph.text, location } },
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
function expressionToSchemaUse(expression, context) {
|
|
165
|
+
const ref = expressionToSchemaRef(expression, context);
|
|
166
|
+
if (isDiagnostic(ref)) {
|
|
167
|
+
return ref;
|
|
168
|
+
}
|
|
169
|
+
return { schema: ref.schema, location: nodeToLocation(expression) };
|
|
170
|
+
}
|
|
171
|
+
function expressionToReturns(expression, context) {
|
|
172
|
+
if (ts.isStringLiteralLike(expression) && expression.text.endsWith('[]')) {
|
|
173
|
+
const type = textToReturnedPrimitive(expression.text.slice(0, -'[]'.length));
|
|
174
|
+
if (type === undefined) {
|
|
175
|
+
return unreadableReturns(expression);
|
|
176
|
+
}
|
|
177
|
+
return { form: 'list', type, nullable: false, nullableItems: false };
|
|
178
|
+
}
|
|
179
|
+
if (ts.isArrayLiteralExpression(expression)) {
|
|
180
|
+
const [items] = expression.elements;
|
|
181
|
+
if (expression.elements.length !== 1 || ts.isSpreadElement(items)) {
|
|
182
|
+
return unreadableReturns(expression);
|
|
183
|
+
}
|
|
184
|
+
const type = expressionToReturnedType(items, context, 'list');
|
|
185
|
+
if (isDiagnostic(type)) {
|
|
186
|
+
return type;
|
|
187
|
+
}
|
|
188
|
+
return { form: 'list', type, nullable: false, nullableItems: false };
|
|
189
|
+
}
|
|
190
|
+
if (ts.isObjectLiteralExpression(expression)) {
|
|
191
|
+
const options = expressionToLiteralOptions(expression, '"returns"');
|
|
192
|
+
if (isDiagnostic(options)) {
|
|
193
|
+
return options;
|
|
194
|
+
}
|
|
195
|
+
if (options.has('list')) {
|
|
196
|
+
return optionsToList(options, expression, context);
|
|
197
|
+
}
|
|
198
|
+
if (options.has('nullable')) {
|
|
199
|
+
return optionsToNullable(options, expression, context);
|
|
200
|
+
}
|
|
201
|
+
if (options.has('scalar') && hasOnlyKeys(options, scalarKeys)) {
|
|
202
|
+
return optionsToScalarReturns(options, expression);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const type = expressionToReturnedType(expression, context);
|
|
206
|
+
if (isDiagnostic(type)) {
|
|
207
|
+
return type;
|
|
208
|
+
}
|
|
209
|
+
return { form: 'type', type };
|
|
210
|
+
}
|
|
211
|
+
function optionsToList(options, expression, context) {
|
|
212
|
+
const items = options.get('list');
|
|
213
|
+
if (items === undefined || !hasOnlyKeys(options, listKeys)) {
|
|
214
|
+
return unreadableReturns(expression);
|
|
215
|
+
}
|
|
216
|
+
const type = expressionToReturnedType(items, context, 'list');
|
|
217
|
+
if (isDiagnostic(type)) {
|
|
218
|
+
return type;
|
|
219
|
+
}
|
|
220
|
+
const nullable = literalOptionToBoolean(options, 'nullable');
|
|
221
|
+
if (isDiagnostic(nullable)) {
|
|
222
|
+
return nullable;
|
|
223
|
+
}
|
|
224
|
+
const nullableItems = literalOptionToBoolean(options, 'nullableItems');
|
|
225
|
+
if (isDiagnostic(nullableItems)) {
|
|
226
|
+
return nullableItems;
|
|
227
|
+
}
|
|
228
|
+
const name = literalOptionToName(options, 'name');
|
|
229
|
+
if (isDiagnostic(name)) {
|
|
230
|
+
return name;
|
|
231
|
+
}
|
|
232
|
+
const returns = {
|
|
233
|
+
form: 'list',
|
|
234
|
+
type,
|
|
235
|
+
nullable: nullable ?? false,
|
|
236
|
+
nullableItems: nullableItems ?? false,
|
|
237
|
+
};
|
|
238
|
+
if (name !== undefined) {
|
|
239
|
+
returns.name = name;
|
|
240
|
+
}
|
|
241
|
+
return returns;
|
|
242
|
+
}
|
|
243
|
+
function optionsToNullable(options, expression, context) {
|
|
244
|
+
const nullable = options.get('nullable');
|
|
245
|
+
if (nullable === undefined || !hasOnlyKeys(options, nullableKeys)) {
|
|
246
|
+
return unreadableReturns(expression);
|
|
247
|
+
}
|
|
248
|
+
const type = expressionToReturnedType(nullable, context, 'nullable');
|
|
249
|
+
if (isDiagnostic(type)) {
|
|
250
|
+
return type;
|
|
251
|
+
}
|
|
252
|
+
const name = literalOptionToName(options, 'name');
|
|
253
|
+
if (isDiagnostic(name)) {
|
|
254
|
+
return name;
|
|
255
|
+
}
|
|
256
|
+
const returns = { form: 'nullable', type };
|
|
257
|
+
if (name !== undefined) {
|
|
258
|
+
returns.name = name;
|
|
259
|
+
}
|
|
260
|
+
return returns;
|
|
261
|
+
}
|
|
262
|
+
function optionsToScalarReturns(options, expression) {
|
|
263
|
+
const type = optionsToScalar(options, expression);
|
|
264
|
+
if (isDiagnostic(type)) {
|
|
265
|
+
return type;
|
|
266
|
+
}
|
|
267
|
+
const name = literalOptionToName(options, 'name');
|
|
268
|
+
if (isDiagnostic(name)) {
|
|
269
|
+
return name;
|
|
270
|
+
}
|
|
271
|
+
const returns = { form: 'type', type };
|
|
272
|
+
if (name !== undefined) {
|
|
273
|
+
returns.name = name;
|
|
274
|
+
}
|
|
275
|
+
return returns;
|
|
276
|
+
}
|
|
277
|
+
function expressionToReturnedType(expression, context, wrapper) {
|
|
278
|
+
if (ts.isIdentifier(expression) ||
|
|
279
|
+
ts.isPropertyAccessExpression(expression)) {
|
|
280
|
+
const ref = expressionToSchemaRef(expression, context);
|
|
281
|
+
if (isDiagnostic(ref)) {
|
|
282
|
+
return ref;
|
|
283
|
+
}
|
|
284
|
+
return { kind: 'schema', schema: ref.schema };
|
|
285
|
+
}
|
|
286
|
+
if (ts.isStringLiteralLike(expression)) {
|
|
287
|
+
return (textToReturnedPrimitive(expression.text) ?? unreadableReturns(expression));
|
|
288
|
+
}
|
|
289
|
+
if (!ts.isObjectLiteralExpression(expression)) {
|
|
290
|
+
return unreadableReturns(expression);
|
|
291
|
+
}
|
|
292
|
+
const options = expressionToLiteralOptions(expression, '"returns"');
|
|
293
|
+
if (isDiagnostic(options)) {
|
|
294
|
+
return options;
|
|
295
|
+
}
|
|
296
|
+
if (options.has('scalar') && hasOnlyKeys(options, scalarKeys)) {
|
|
297
|
+
return optionsToScalar(options, expression, wrapper);
|
|
298
|
+
}
|
|
299
|
+
if (options.has('enum') && hasOnlyKeys(options, enumKeys)) {
|
|
300
|
+
return optionsToEnum(options, expression);
|
|
301
|
+
}
|
|
302
|
+
if (options.has('union') && hasOnlyKeys(options, unionKeys)) {
|
|
303
|
+
return optionsToUnion(options, expression, context);
|
|
304
|
+
}
|
|
305
|
+
return unreadableReturns(expression);
|
|
306
|
+
}
|
|
307
|
+
function optionsToScalar(options, expression, wrapper) {
|
|
308
|
+
const scalar = literalOptionToName(options, 'scalar');
|
|
309
|
+
if (isDiagnostic(scalar)) {
|
|
310
|
+
return scalar;
|
|
311
|
+
}
|
|
312
|
+
if (scalar === undefined) {
|
|
313
|
+
return unreadableReturns(expression);
|
|
314
|
+
}
|
|
315
|
+
if (wrapper === undefined || !options.has('name')) {
|
|
316
|
+
return { kind: 'scalar', scalar };
|
|
317
|
+
}
|
|
318
|
+
const name = literalOptionToName(options, 'name');
|
|
319
|
+
if (isDiagnostic(name)) {
|
|
320
|
+
return name;
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
message: `a scalar's "name" goes on ${wrapperPlaces[wrapper]}: write { ${wrapper}: { scalar: '${scalar}' }, name: '${name}' }`,
|
|
324
|
+
location: nodeToLocation(expression),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function optionsToEnum(options, expression) {
|
|
328
|
+
const written = options.get('enum') ?? expression;
|
|
329
|
+
const members = expressionToStringLiterals(written);
|
|
330
|
+
if (members === undefined) {
|
|
331
|
+
return {
|
|
332
|
+
message: '"enum" must be an array of string literals written in place',
|
|
333
|
+
location: nodeToLocation(written),
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
const name = literalOptionToName(options, 'name');
|
|
337
|
+
if (isDiagnostic(name)) {
|
|
338
|
+
return name;
|
|
339
|
+
}
|
|
340
|
+
if (name === undefined) {
|
|
341
|
+
return {
|
|
342
|
+
message: `an enum in "returns" needs a name: write { enum: [...], name: '<name>' }`,
|
|
343
|
+
location: nodeToLocation(expression),
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
kind: 'schema',
|
|
348
|
+
schema: name,
|
|
349
|
+
declared: { type: 'enum', members },
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function optionsToUnion(options, expression, context) {
|
|
353
|
+
const members = expressionToUnionMembers(options.get('union'), '"union"', context);
|
|
354
|
+
if (isDiagnostic(members)) {
|
|
355
|
+
return members;
|
|
356
|
+
}
|
|
357
|
+
const name = literalOptionToName(options, 'name');
|
|
358
|
+
if (isDiagnostic(name)) {
|
|
359
|
+
return name;
|
|
360
|
+
}
|
|
361
|
+
if (name === undefined) {
|
|
362
|
+
return {
|
|
363
|
+
message: `a union in "returns" needs a name: write { union: {...}, name: '<name>' }`,
|
|
364
|
+
location: nodeToLocation(expression),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
return {
|
|
368
|
+
kind: 'schema',
|
|
369
|
+
schema: name,
|
|
370
|
+
declared: { type: 'union', members },
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
function expressionToStringLiterals(expression) {
|
|
374
|
+
if (!ts.isArrayLiteralExpression(expression)) {
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
const members = [];
|
|
378
|
+
for (const element of expression.elements) {
|
|
379
|
+
if (!ts.isStringLiteralLike(element)) {
|
|
380
|
+
return undefined;
|
|
381
|
+
}
|
|
382
|
+
members.push(element.text);
|
|
383
|
+
}
|
|
384
|
+
return members;
|
|
385
|
+
}
|
|
386
|
+
function textToReturnedPrimitive(text) {
|
|
387
|
+
const type = textToPrimitive(text);
|
|
388
|
+
if (type === undefined) {
|
|
389
|
+
return undefined;
|
|
390
|
+
}
|
|
391
|
+
return { kind: 'primitive', type };
|
|
392
|
+
}
|
|
393
|
+
function hasOnlyKeys(options, keys) {
|
|
394
|
+
return [...options.keys()].every((key) => keys.includes(key));
|
|
395
|
+
}
|
|
396
|
+
function unreadableReturns(expression) {
|
|
397
|
+
return {
|
|
398
|
+
message: `cannot read "${expression.getText()}" as returns: write a @BidirektSchema class, a primitive such as 'boolean', [Schema], { list: Schema }, { nullable: Schema }, { scalar: '<name>' }, { enum: [...], name: '<name>' } or { union: {...}, name: '<name>' }`,
|
|
399
|
+
location: nodeToLocation(expression),
|
|
400
|
+
};
|
|
401
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { sdkDecoratorsToBlocks } from "./decorators-to-blocks.js";
|
|
2
|
+
import { taggedCommentsToBlocks } from "./tags-to-blocks.js";
|
|
3
|
+
export function protocolInputToBlocks(input) {
|
|
4
|
+
const decorated = sdkDecoratorsToBlocks(input.decorators, input.context);
|
|
5
|
+
const tagged = taggedCommentsToBlocks(input.comments);
|
|
6
|
+
return {
|
|
7
|
+
blocks: [...decorated.blocks, ...tagged.blocks],
|
|
8
|
+
derived: [...decorated.derived, ...tagged.derived],
|
|
9
|
+
errors: [...decorated.errors, ...tagged.errors],
|
|
10
|
+
};
|
|
11
|
+
}
|