@bidirekt/typescript-sdk-cli 0.1.1 → 0.2.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 CHANGED
@@ -555,7 +555,7 @@ With `emitDecoratorMetadata` on (NestJS, TypeORM), TypeScript keeps the imports
555
555
  | `@bidirekt response <status> [body]` | follows `provides` or `consumes`, once per status |
556
556
 
557
557
  - A handler's comment can also declare schemas, see [Schemas in a handler's comment](#schemas-in-a-handlers-comment).
558
- - A tag starts a line of a `/** */` comment, and its arguments are separated by spaces. Only a `description` value is quoted (`description="Shown on the card"`), and only a `union=` value is grouped in `{...}`, with optional spaces inside; quotes or braces anywhere else stop `generate`.
558
+ - A tag starts a line of a `/** */` comment, and its arguments are separated by spaces. Only a `description` value is quoted (`description="Shown on the card"`), and inside the quotes it holds only ASCII letters and digits, with single spaces between words. Only a `union=` value is grouped in `{...}`, with optional spaces inside; quotes or braces anywhere else stop `generate`.
559
559
  - A comment goes above the statement or declaration it annotates, and above its decorators. It declares at most one handler; tags of one element written in two comments, or `@bidirekt schema` on two blocks of one interface, stop `generate`.
560
560
  - A tag names a schema by its name in the contract, which is not always its name in the code. In any tag, `type=` also takes a schema name, alone, in a list or in a map (`HealthCheck`, `HealthCheck[]`, `map<HealthCheck>`).
561
561
  - In a path, write a `*` before another segment as `*\/` (`/pets/*\/photos`): a bare `*/` would end the comment. `generate` reads `*\/` as `*/`.
@@ -576,14 +576,14 @@ Without a request (no `request` option, no `@bidirekt request` tag), the resourc
576
576
 
577
577
  ### Fields
578
578
 
579
- | Decorator | Tag | What it declares |
580
- | ----------------------------------------------- | -------------------------------------------- | -------------------------------------------------------------------------------- |
581
- | `type: 'integer'` | `type=integer` | the whole type of the field, see [Types](#types) |
582
- | `name: 'birth_date'` | `name=birth_date` | the field's name in the contract; without it, the property's name |
583
- | `optional: true` | `optional=true` | whether the field can be absent; without it, from `?` or `undefined` in the type |
584
- | `nullable: true` | `nullable=true` | whether the field can be `null`; without it, from `null` in the type |
585
- | `description: 'Shown on the card'` | `description="Shown on the card"` | the field's description; only a declared one, a plain comment is not |
586
- | `union: { card: CardPayment, pix: PixPayment }` | `union={card: CardPayment, pix: PixPayment}` | the key of each member of a union of schemas |
579
+ | Decorator | Tag | What it declares |
580
+ | ----------------------------------------------- | -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
581
+ | `type: 'integer'` | `type=integer` | the whole type of the field, see [Types](#types) |
582
+ | `name: 'birth_date'` | `name=birth_date` | the field's name in the contract; without it, the property's name |
583
+ | `optional: true` | `optional=true` | whether the field can be absent; without it, from `?` or `undefined` in the type |
584
+ | `nullable: true` | `nullable=true` | whether the field can be `null`; without it, from `null` in the type |
585
+ | `description: 'Shown on the card'` | `description="Shown on the card"` | the field's description, ASCII letters and digits with single spaces between words; only a declared one, a plain comment is not |
586
+ | `union: { card: CardPayment, pix: PixPayment }` | `union={card: CardPayment, pix: PixPayment}` | the key of each member of a union of schemas |
587
587
 
588
588
  A `union` names each schema of the field's union once, by one key, and nothing else; a tag names each member by its name in the contract, never a list or a map. It applies to the union of the field, of its items or of its values (`Array<Card | Pix>`), and `null` and `undefined` in the type still make them `nullable` or `optional`.
589
589
 
@@ -0,0 +1,16 @@
1
+ const descriptionPattern = /^[A-Za-z0-9]+( [A-Za-z0-9]+)*$/;
2
+ export function descriptionToDiagnostic(description, location) {
3
+ if (description === '') {
4
+ return {
5
+ message: '"description" is empty: write a description or leave "description" out',
6
+ location,
7
+ };
8
+ }
9
+ if (descriptionPattern.test(description)) {
10
+ return undefined;
11
+ }
12
+ return {
13
+ message: `description ${JSON.stringify(description)} must be ASCII letters and digits, with single spaces between words: remove or replace the other characters`,
14
+ location,
15
+ };
16
+ }
@@ -1,4 +1,5 @@
1
1
  import ts from 'typescript';
2
+ import { descriptionToDiagnostic } from "../model/description.js";
2
3
  import { isDiagnostic } from "../model/diagnostic.js";
3
4
  import { nodeToLocation } from "./location.js";
4
5
  export function propertyNameToText(name) {
@@ -98,6 +99,14 @@ export function literalOptionToName(options, name) {
98
99
  }
99
100
  return literal.text;
100
101
  }
102
+ export function literalOptionToDescription(options) {
103
+ const literal = literalOptionToStringLiteral(options, 'description');
104
+ if (literal === undefined || isDiagnostic(literal)) {
105
+ return literal;
106
+ }
107
+ return (descriptionToDiagnostic(literal.text, nodeToLocation(literal)) ??
108
+ literal.text);
109
+ }
101
110
  export function literalOptionToBoolean(options, name) {
102
111
  const value = options.get(name);
103
112
  if (value === undefined) {
@@ -1,7 +1,7 @@
1
1
  import ts from 'typescript';
2
2
  import { isDiagnostic } from "../model/diagnostic.js";
3
3
  import { expressionToUnionMembers } from "./expression-to-schema-node.js";
4
- import { expressionToLiteralOptions, literalOptionToBoolean, literalOptionToName, literalOptionToString, literalOptionToStringLiteral, propertyNameToText, } from "./literal-options.js";
4
+ import { expressionToLiteralOptions, literalOptionToBoolean, literalOptionToDescription, literalOptionToName, literalOptionToStringLiteral, propertyNameToText, } from "./literal-options.js";
5
5
  import { nodeToLocation } from "./location.js";
6
6
  import { builtDeclarationToDiagnostic, isLibrarySymbol, isObjectDeclaration, javaScriptTagHints, memberToOwner, nodeToSymbol, symbolToClass, tagHints, unreadClassToDiagnostic, } from "./schema-context.js";
7
7
  import { typeTextToSchemaNode, typeTextUsage, } from "./type-text-to-schema-node.js";
@@ -440,7 +440,7 @@ function literalOptionsToFieldOptions(options, context) {
440
440
  if (isDiagnostic(name)) {
441
441
  return name;
442
442
  }
443
- const description = literalOptionToString(options, 'description');
443
+ const description = literalOptionToDescription(options);
444
444
  if (isDiagnostic(description)) {
445
445
  return description;
446
446
  }
@@ -1,4 +1,5 @@
1
1
  import ts from 'typescript';
2
+ import { descriptionToDiagnostic } from "../model/description.js";
2
3
  import { isDiagnostic } from "../model/diagnostic.js";
3
4
  import { argumentToOption, isHandlerTag } from "./comment-tags.js";
4
5
  import { namesToChoice } from "./literal-options.js";
@@ -234,12 +235,19 @@ export function argumentsToOptions(tagArguments, tag, keys) {
234
235
  options[key] = value === 'true';
235
236
  break;
236
237
  }
237
- case 'name':
238
- case 'description': {
238
+ case 'name': {
239
239
  if (value === '') {
240
240
  return usage;
241
241
  }
242
- options[key] = value;
242
+ options.name = value;
243
+ break;
244
+ }
245
+ case 'description': {
246
+ const invalid = descriptionToDiagnostic(value, tag.location);
247
+ if (invalid !== undefined) {
248
+ return invalid;
249
+ }
250
+ options.description = value;
243
251
  break;
244
252
  }
245
253
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bidirekt/typescript-sdk-cli",
3
- "version": "0.1.1",
3
+ "version": "0.2.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.1.1",
28
+ "@bidirekt/typescript-sdk": "0.2.0",
29
29
  "typescript": "~6.0.3",
30
30
  "yaml": "^2.9.1"
31
31
  }