@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
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { isDiagnostic } from "../../../model/diagnostic.js";
|
|
2
|
+
import { isHandlerTag, tagToText } from "../../comment-tags.js";
|
|
3
|
+
import { valueToUnionMembers } from "../../tag-schemas.js";
|
|
4
|
+
import { isSchemaName, textToPrimitive, } from "../../type-text-to-schema-node.js";
|
|
5
|
+
import { consumedToRead } from "./decorators-to-blocks.js";
|
|
6
|
+
const consumesUsage = '@bidirekt graphql consumes <provider> query|mutation <name>';
|
|
7
|
+
const operationKinds = ['query', 'mutation'];
|
|
8
|
+
const returnsPattern = /^(union=\{[^{}]*\}|[^\s?[\]]+)(\?)?(?:(\[\])(\?)?)?(?: as ([^\s?[\]]+))?$/;
|
|
9
|
+
const returnsUsage = '"<type>", "<type>?", "<type>[]", "<type>[]?", "<type>?[]" or "<type>?[]?", each with an optional "as <name>", where <type> is a schema, a primitive, scalar:<name>, enum:<A>|<B> or union={<key>: <schema>, ...}';
|
|
10
|
+
export function taggedCommentsToBlocks(comments) {
|
|
11
|
+
const read = {
|
|
12
|
+
blocks: [],
|
|
13
|
+
derived: [],
|
|
14
|
+
errors: [],
|
|
15
|
+
};
|
|
16
|
+
for (const { tags } of comments) {
|
|
17
|
+
for (const tag of tags.filter(({ keyword }) => keyword === 'graph')) {
|
|
18
|
+
graphTagToRead(tag, read);
|
|
19
|
+
}
|
|
20
|
+
const handler = tags.find(isHandlerTag);
|
|
21
|
+
const uses = tags.filter(({ keyword }) => keyword === 'arguments' || keyword === 'returns');
|
|
22
|
+
if (handler === undefined) {
|
|
23
|
+
read.errors.push(...uses.map((tag) => ({
|
|
24
|
+
message: `"@bidirekt ${tag.keyword}" needs a "@bidirekt graphql consumes" tag in the same comment`,
|
|
25
|
+
location: tag.location,
|
|
26
|
+
})));
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
tagsToOperation(handler, uses, read);
|
|
30
|
+
}
|
|
31
|
+
return read;
|
|
32
|
+
}
|
|
33
|
+
function graphTagToRead(tag, read) {
|
|
34
|
+
if (tag.error !== undefined) {
|
|
35
|
+
read.errors.push(tag.error);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const [name] = tag.arguments;
|
|
39
|
+
if (tag.arguments.length !== 1) {
|
|
40
|
+
read.errors.push({
|
|
41
|
+
message: `cannot read "${tagToText(tag)}": write "@bidirekt graph <name>"`,
|
|
42
|
+
location: tag.location,
|
|
43
|
+
});
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
read.blocks.push({
|
|
47
|
+
block: { operations: [], graph: { name, location: tag.location } },
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function tagsToOperation(handler, uses, read) {
|
|
51
|
+
const errors = [];
|
|
52
|
+
const consumed = handlerToConsumed(handler);
|
|
53
|
+
if (isDiagnostic(consumed)) {
|
|
54
|
+
errors.push(consumed);
|
|
55
|
+
}
|
|
56
|
+
let argumentsUse;
|
|
57
|
+
let returnsTag;
|
|
58
|
+
for (const tag of uses) {
|
|
59
|
+
if (tag.error !== undefined) {
|
|
60
|
+
errors.push(tag.error);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (uses.slice(0, uses.indexOf(tag)).some(isSameKeyword(tag))) {
|
|
64
|
+
errors.push({
|
|
65
|
+
message: `"@bidirekt ${tag.keyword}" is declared more than once in this comment: keep one`,
|
|
66
|
+
location: tag.location,
|
|
67
|
+
});
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (tag.keyword === 'returns') {
|
|
71
|
+
returnsTag = tag;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const use = tagToArguments(tag);
|
|
75
|
+
if (isDiagnostic(use)) {
|
|
76
|
+
errors.push(use);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
argumentsUse = use;
|
|
80
|
+
}
|
|
81
|
+
let returns = {
|
|
82
|
+
message: `"${tagToText(handler)}" needs a "@bidirekt returns <type>" tag in the same comment`,
|
|
83
|
+
location: handler.location,
|
|
84
|
+
};
|
|
85
|
+
if (returnsTag !== undefined) {
|
|
86
|
+
returns = tagToReturns(returnsTag);
|
|
87
|
+
}
|
|
88
|
+
if (isDiagnostic(returns)) {
|
|
89
|
+
errors.push(returns);
|
|
90
|
+
}
|
|
91
|
+
read.errors.push(...errors);
|
|
92
|
+
if (isDiagnostic(consumed) ||
|
|
93
|
+
isDiagnostic(returns) ||
|
|
94
|
+
returnsTag === undefined ||
|
|
95
|
+
errors.length > 0) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
consumedToRead({
|
|
99
|
+
...consumed,
|
|
100
|
+
arguments: argumentsUse,
|
|
101
|
+
returns,
|
|
102
|
+
returnsLocation: returnsTag.location,
|
|
103
|
+
location: handler.location,
|
|
104
|
+
}, read);
|
|
105
|
+
}
|
|
106
|
+
function isSameKeyword(tag) {
|
|
107
|
+
return (other) => other.keyword === tag.keyword;
|
|
108
|
+
}
|
|
109
|
+
function handlerToConsumed(handler) {
|
|
110
|
+
if (handler.error !== undefined) {
|
|
111
|
+
return handler.error;
|
|
112
|
+
}
|
|
113
|
+
const [form, provider, kind, name] = handler.arguments;
|
|
114
|
+
const operationKind = operationKinds.find((known) => known === kind);
|
|
115
|
+
if (form !== 'consumes' ||
|
|
116
|
+
handler.arguments.length !== 4 ||
|
|
117
|
+
operationKind === undefined) {
|
|
118
|
+
return {
|
|
119
|
+
message: `cannot read "${tagToText(handler)}": write "${consumesUsage}"`,
|
|
120
|
+
location: handler.location,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return { provider, kind: operationKind, name };
|
|
124
|
+
}
|
|
125
|
+
function tagToArguments(tag) {
|
|
126
|
+
const [schema] = tag.arguments;
|
|
127
|
+
if (tag.arguments.length !== 1 || !isSchemaName(schema)) {
|
|
128
|
+
return {
|
|
129
|
+
message: `cannot read "${tagToText(tag)}": write "@bidirekt arguments <schema>"`,
|
|
130
|
+
location: tag.location,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return { schema, location: tag.location };
|
|
134
|
+
}
|
|
135
|
+
function tagToReturns(tag) {
|
|
136
|
+
const text = tag.arguments.join(' ');
|
|
137
|
+
if (text === '') {
|
|
138
|
+
return {
|
|
139
|
+
message: `cannot read "${tagToText(tag)}": write "@bidirekt returns <type>"`,
|
|
140
|
+
location: tag.location,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const match = returnsPattern.exec(text);
|
|
144
|
+
if (match === null) {
|
|
145
|
+
return unreadableReturns(text, tag.location);
|
|
146
|
+
}
|
|
147
|
+
const [, written, typeMark, listMark, listMarkAfter] = match;
|
|
148
|
+
const typeNullable = typeMark === '?';
|
|
149
|
+
const isList = listMark === '[]';
|
|
150
|
+
const name = / as (\S+)$/.exec(text)?.[1];
|
|
151
|
+
const type = textToReturnedType(written, name, text, tag.location);
|
|
152
|
+
if (isDiagnostic(type)) {
|
|
153
|
+
return type;
|
|
154
|
+
}
|
|
155
|
+
const namesType = type.kind === 'schema' && type.declared !== undefined;
|
|
156
|
+
if (type.kind === 'schema' &&
|
|
157
|
+
!namesType &&
|
|
158
|
+
name !== undefined &&
|
|
159
|
+
!isList &&
|
|
160
|
+
!typeNullable) {
|
|
161
|
+
return {
|
|
162
|
+
message: `cannot read "${text}" as returns: a schema is returned by its own name, write "${written}"`,
|
|
163
|
+
location: tag.location,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
let returns = { form: 'type', type };
|
|
167
|
+
if (isList) {
|
|
168
|
+
returns = {
|
|
169
|
+
form: 'list',
|
|
170
|
+
type,
|
|
171
|
+
nullable: listMarkAfter === '?',
|
|
172
|
+
nullableItems: typeNullable,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
else if (typeNullable) {
|
|
176
|
+
returns = { form: 'nullable', type };
|
|
177
|
+
}
|
|
178
|
+
if (name !== undefined && !namesType) {
|
|
179
|
+
returns.name = name;
|
|
180
|
+
}
|
|
181
|
+
return returns;
|
|
182
|
+
}
|
|
183
|
+
function textToReturnedType(written, name, text, location) {
|
|
184
|
+
const primitive = textToPrimitive(written);
|
|
185
|
+
if (primitive !== undefined) {
|
|
186
|
+
return { kind: 'primitive', type: primitive };
|
|
187
|
+
}
|
|
188
|
+
if (written.startsWith('scalar:')) {
|
|
189
|
+
const scalar = written.slice('scalar:'.length);
|
|
190
|
+
if (!isSchemaName(scalar)) {
|
|
191
|
+
return unreadableReturns(text, location);
|
|
192
|
+
}
|
|
193
|
+
return { kind: 'scalar', scalar };
|
|
194
|
+
}
|
|
195
|
+
if (written.startsWith('enum:')) {
|
|
196
|
+
const members = written.slice('enum:'.length).split('|');
|
|
197
|
+
if (members.some((member) => member === '')) {
|
|
198
|
+
return unreadableReturns(text, location);
|
|
199
|
+
}
|
|
200
|
+
if (name === undefined) {
|
|
201
|
+
return {
|
|
202
|
+
message: `cannot read "${text}" as returns: an enum needs a name, write "${text} as <name>"`,
|
|
203
|
+
location,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
kind: 'schema',
|
|
208
|
+
schema: name,
|
|
209
|
+
declared: { type: 'enum', members },
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
if (written.startsWith('union=')) {
|
|
213
|
+
const members = valueToUnionMembers(written.slice('union='.length));
|
|
214
|
+
if (members === undefined) {
|
|
215
|
+
return unreadableReturns(text, location);
|
|
216
|
+
}
|
|
217
|
+
if (name === undefined) {
|
|
218
|
+
return {
|
|
219
|
+
message: `cannot read "${text}" as returns: a union needs a name, write "${text} as <name>"`,
|
|
220
|
+
location,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
kind: 'schema',
|
|
225
|
+
schema: name,
|
|
226
|
+
declared: { type: 'union', members },
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
if (!isSchemaName(written)) {
|
|
230
|
+
return unreadableReturns(text, location);
|
|
231
|
+
}
|
|
232
|
+
return { kind: 'schema', schema: written };
|
|
233
|
+
}
|
|
234
|
+
function unreadableReturns(text, location) {
|
|
235
|
+
return {
|
|
236
|
+
message: `cannot read "${text}" as returns: write ${returnsUsage}`,
|
|
237
|
+
location,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
@@ -1,28 +1,38 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
-
import { isDiagnostic } from "
|
|
3
|
-
import { listToNamedSchema } from "
|
|
4
|
-
import { expressionToSchemaRef } from "
|
|
5
|
-
import { computedKeyToValue, expressionToLiteralOptions, literalOptionToName, literalOptionToString, } from "
|
|
6
|
-
import { nodeToLocation } from "
|
|
2
|
+
import { isDiagnostic } from "../../../model/diagnostic.js";
|
|
3
|
+
import { listToNamedSchema } from "../../../model/protocols/rest/schema-names.js";
|
|
4
|
+
import { expressionToSchemaRef } from "../../expression-to-schema-node.js";
|
|
5
|
+
import { computedKeyToValue, expressionToLiteralOptions, literalOptionToName, literalOptionToString, } from "../../literal-options.js";
|
|
6
|
+
import { nodeToLocation } from "../../location.js";
|
|
7
|
+
export const restDecoratorTargets = {
|
|
8
|
+
BidirektRestProvider: {
|
|
9
|
+
isTarget: ts.isMethodDeclaration,
|
|
10
|
+
description: 'a method',
|
|
11
|
+
},
|
|
12
|
+
BidirektRestConsumer: {
|
|
13
|
+
isTarget: ts.isMethodDeclaration,
|
|
14
|
+
description: 'a method',
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
export const restRenamedExports = {
|
|
18
|
+
BidirektProvider: 'BidirektRestProvider',
|
|
19
|
+
BidirektConsumer: 'BidirektRestConsumer',
|
|
20
|
+
BidirektProviderOptions: 'BidirektRestProviderOptions',
|
|
21
|
+
BidirektConsumerOptions: 'BidirektRestConsumerOptions',
|
|
22
|
+
BidirektHttpMethod: 'BidirektRestHttpMethod',
|
|
23
|
+
};
|
|
7
24
|
const listKeys = ['list', 'name'];
|
|
8
25
|
const handlerOptions = {
|
|
9
|
-
|
|
10
|
-
|
|
26
|
+
BidirektRestProvider: ['method', 'path', 'request', 'responses'],
|
|
27
|
+
BidirektRestConsumer: ['provider', 'method', 'path', 'request', 'responses'],
|
|
11
28
|
};
|
|
12
|
-
export function
|
|
13
|
-
return (reference.use === 'decorator' &&
|
|
14
|
-
(reference.name === 'BidirektProvider' ||
|
|
15
|
-
reference.name === 'BidirektConsumer'));
|
|
16
|
-
}
|
|
17
|
-
export function sdkReferencesToResources(references, context) {
|
|
29
|
+
export function sdkDecoratorsToResources(decorators, context) {
|
|
18
30
|
const read = {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
schemas: [],
|
|
31
|
+
blocks: [],
|
|
32
|
+
derived: [],
|
|
22
33
|
errors: [],
|
|
23
34
|
};
|
|
24
|
-
for (const
|
|
25
|
-
const { name, call } = reference;
|
|
35
|
+
for (const { name, call } of decorators) {
|
|
26
36
|
const what = `@${name}`;
|
|
27
37
|
const options = expressionToLiteralOptions(call.arguments[0], `${what} options`, handlerOptions[name]);
|
|
28
38
|
if (isDiagnostic(options)) {
|
|
@@ -38,7 +48,7 @@ export function sdkReferencesToResources(references, context) {
|
|
|
38
48
|
errors: [],
|
|
39
49
|
};
|
|
40
50
|
let provider;
|
|
41
|
-
if (name === '
|
|
51
|
+
if (name === 'BidirektRestConsumer') {
|
|
42
52
|
provider = withoutDiagnostic(requiredString(handler, 'provider'), handler);
|
|
43
53
|
}
|
|
44
54
|
const declared = handlerToDeclared(handler);
|
|
@@ -50,17 +60,17 @@ export function sdkReferencesToResources(references, context) {
|
|
|
50
60
|
}
|
|
51
61
|
return read;
|
|
52
62
|
}
|
|
53
|
-
export function declaredToRead({ provider, request, ...
|
|
54
|
-
read.
|
|
55
|
-
const resource =
|
|
63
|
+
export function declaredToRead({ provider, request, ...declared }, derived, read) {
|
|
64
|
+
read.derived.push(...derived);
|
|
65
|
+
const resource = declared;
|
|
56
66
|
if (request !== undefined) {
|
|
57
67
|
resource.request = request;
|
|
58
68
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
69
|
+
const sideBlock = { block: [resource] };
|
|
70
|
+
if (provider !== undefined) {
|
|
71
|
+
sideBlock.provider = provider;
|
|
62
72
|
}
|
|
63
|
-
read.
|
|
73
|
+
read.blocks.push(sideBlock);
|
|
64
74
|
}
|
|
65
75
|
function handlerToDeclared(handler) {
|
|
66
76
|
const method = withoutDiagnostic(handlerToMethod(handler), handler);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { sdkDecoratorsToResources } from "./handler-methods.js";
|
|
2
|
+
import { taggedCommentsToResources } from "./tag-handlers.js";
|
|
3
|
+
export function protocolInputToResources(input) {
|
|
4
|
+
const decorated = sdkDecoratorsToResources(input.decorators, input.context);
|
|
5
|
+
const tagged = taggedCommentsToResources(input.comments);
|
|
6
|
+
return {
|
|
7
|
+
blocks: [...decorated.blocks, ...tagged.blocks],
|
|
8
|
+
derived: [...decorated.derived, ...tagged.derived],
|
|
9
|
+
errors: [...decorated.errors, ...tagged.errors],
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { isDiagnostic } from "../../../model/diagnostic.js";
|
|
2
|
+
import { listToNamedSchema } from "../../../model/protocols/rest/schema-names.js";
|
|
3
|
+
import { isHandlerTag, tagToText } from "../../comment-tags.js";
|
|
4
|
+
import { declaredToRead, methodToUppercaseDiagnostic, statusToNumberDiagnostic, } from "./handler-methods.js";
|
|
5
|
+
export const restRenamedTags = {
|
|
6
|
+
provides: 'rest provides',
|
|
7
|
+
consumes: 'rest consumes',
|
|
8
|
+
};
|
|
9
|
+
const endpointForms = {
|
|
10
|
+
provides: { usage: '@bidirekt rest provides <METHOD> <path>', arguments: 2 },
|
|
11
|
+
consumes: {
|
|
12
|
+
usage: '@bidirekt rest consumes <provider> <METHOD> <path>',
|
|
13
|
+
arguments: 3,
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
const bodyPattern = /^([^\s[\]]+)(?:\[\](?: as [^\s[\]]+)?)?$/;
|
|
17
|
+
export function taggedCommentsToResources(comments) {
|
|
18
|
+
const read = {
|
|
19
|
+
blocks: [],
|
|
20
|
+
derived: [],
|
|
21
|
+
errors: [],
|
|
22
|
+
};
|
|
23
|
+
for (const { tags } of comments) {
|
|
24
|
+
const handler = tags.find(isHandlerTag);
|
|
25
|
+
const bodies = tags.filter((tag) => !isHandlerTag(tag));
|
|
26
|
+
if (handler === undefined) {
|
|
27
|
+
read.errors.push(...bodies.map((tag) => ({
|
|
28
|
+
message: `"@bidirekt ${tag.keyword}" needs a "@bidirekt rest provides" or "@bidirekt rest consumes" tag in the same comment`,
|
|
29
|
+
location: tag.location,
|
|
30
|
+
})));
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
tagsToResource(handler, bodies, read);
|
|
34
|
+
}
|
|
35
|
+
return read;
|
|
36
|
+
}
|
|
37
|
+
function tagsToResource(handler, bodies, read) {
|
|
38
|
+
const errors = [];
|
|
39
|
+
const derived = [];
|
|
40
|
+
const endpoint = handlerToEndpoint(handler);
|
|
41
|
+
if (isDiagnostic(endpoint)) {
|
|
42
|
+
errors.push(endpoint);
|
|
43
|
+
}
|
|
44
|
+
let request;
|
|
45
|
+
const responses = [];
|
|
46
|
+
for (const tag of bodies) {
|
|
47
|
+
if (tag.error !== undefined) {
|
|
48
|
+
errors.push(tag.error);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (tag.keyword === 'request') {
|
|
52
|
+
if (request !== undefined) {
|
|
53
|
+
errors.push({
|
|
54
|
+
message: `"@bidirekt request" is declared more than once in this comment: keep one`,
|
|
55
|
+
location: tag.location,
|
|
56
|
+
});
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const body = argumentsToBody(tag.arguments, tag.location, derived);
|
|
60
|
+
if (isDiagnostic(body)) {
|
|
61
|
+
errors.push(body);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
request = body;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const response = tagToResponse(tag, responses, derived);
|
|
68
|
+
if (isDiagnostic(response)) {
|
|
69
|
+
errors.push(response);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
responses.push(response);
|
|
73
|
+
}
|
|
74
|
+
read.errors.push(...errors);
|
|
75
|
+
if (isDiagnostic(endpoint) || errors.length > 0) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
declaredToRead({ ...endpoint, request, responses, location: handler.location }, derived, read);
|
|
79
|
+
}
|
|
80
|
+
function handlerToEndpoint(handler) {
|
|
81
|
+
if (handler.error !== undefined) {
|
|
82
|
+
return handler.error;
|
|
83
|
+
}
|
|
84
|
+
const [form, ...endpointArguments] = handler.arguments;
|
|
85
|
+
if (form !== 'provides' && form !== 'consumes') {
|
|
86
|
+
return {
|
|
87
|
+
message: `cannot read "${tagToText(handler)}": write "${endpointForms.provides.usage}" or "${endpointForms.consumes.usage}"`,
|
|
88
|
+
location: handler.location,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const { usage, arguments: count } = endpointForms[form];
|
|
92
|
+
if (endpointArguments.length !== count) {
|
|
93
|
+
return {
|
|
94
|
+
message: `cannot read "${tagToText(handler)}": write "${usage}"`,
|
|
95
|
+
location: handler.location,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const [method, path] = endpointArguments.slice(-2);
|
|
99
|
+
const uppercase = methodToUppercaseDiagnostic(method, handler.location);
|
|
100
|
+
if (uppercase !== undefined) {
|
|
101
|
+
return uppercase;
|
|
102
|
+
}
|
|
103
|
+
const endpoint = { method, path };
|
|
104
|
+
if (form === 'consumes') {
|
|
105
|
+
endpoint.provider = endpointArguments[0];
|
|
106
|
+
}
|
|
107
|
+
return endpoint;
|
|
108
|
+
}
|
|
109
|
+
function tagToResponse(tag, declared, derived) {
|
|
110
|
+
if (tag.arguments.length === 0) {
|
|
111
|
+
return {
|
|
112
|
+
message: `cannot read "${tagToText(tag)}": write "@bidirekt response <status> [schema]"`,
|
|
113
|
+
location: tag.location,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const [status, ...body] = tag.arguments;
|
|
117
|
+
const number = statusToNumberDiagnostic(status, tag.location);
|
|
118
|
+
if (number !== undefined) {
|
|
119
|
+
return number;
|
|
120
|
+
}
|
|
121
|
+
if (declared.some((response) => response.status === Number(status))) {
|
|
122
|
+
return {
|
|
123
|
+
message: `response status "${status}" is declared more than once in this comment: keep one`,
|
|
124
|
+
location: tag.location,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const read = argumentsToBody(body, tag.location, derived);
|
|
128
|
+
if (isDiagnostic(read)) {
|
|
129
|
+
return read;
|
|
130
|
+
}
|
|
131
|
+
return { ...read, status: Number(status) };
|
|
132
|
+
}
|
|
133
|
+
function argumentsToBody(body, location, derived) {
|
|
134
|
+
if (body.length === 0) {
|
|
135
|
+
return { schema: null, location };
|
|
136
|
+
}
|
|
137
|
+
const text = body.join(' ');
|
|
138
|
+
const match = bodyPattern.exec(text);
|
|
139
|
+
if (match === null) {
|
|
140
|
+
return {
|
|
141
|
+
message: `cannot read "${text}" as a body: write "<schema>", "<schema>[]" or "<schema>[] as <name>"`,
|
|
142
|
+
location,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const [, schema] = match;
|
|
146
|
+
if (!text.includes('[]')) {
|
|
147
|
+
return { schema, location };
|
|
148
|
+
}
|
|
149
|
+
const list = listToNamedSchema(schema, / as (\S+)$/.exec(text)?.[1], location);
|
|
150
|
+
derived.push(list);
|
|
151
|
+
return { schema: list.name, location };
|
|
152
|
+
}
|