@bidirekt/typescript-sdk-cli 0.1.0 → 0.1.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/README.md +320 -2363
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -24,28 +24,67 @@ Their types need TypeScript 5.0 or later.
|
|
|
24
24
|
|
|
25
25
|
## Quickstart
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
A field without an annotation stays out of the contract. Its type comes from TypeScript; declare `type` when TypeScript can't say it: a primitive (`string`, `integer`, `float`, `number`, `boolean`), `T[]` or `map<T>`, nested up to three levels in a decorator and to any depth in a tag (`integer[][]`, `map<float[]>`).
|
|
27
|
+
`petstore_ts` provides `GET /pets/*` and `POST /pets`, and consumes `GET /ratings/*` from `petstore_reviews`. There are two ways to annotate it, and both write the same contract: decorators, for code with classes, or `@bidirekt` tags in `/** */` comments, for interfaces, function handlers and plain JavaScript. One service can use both, never both on the same element. A property without an annotation, like `internalNotes`, stays out of the contract.
|
|
30
28
|
|
|
31
29
|
### With decorators
|
|
32
30
|
|
|
33
|
-
|
|
31
|
+
`@BidirektSchema` marks a schema and `@BidirektField` each field of the contract. A field's type comes from TypeScript; declare a `type` when TypeScript can't say it, as `integer` or `float` for a `number`:
|
|
34
32
|
|
|
35
33
|
```ts
|
|
36
34
|
// pet.ts
|
|
37
35
|
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
38
36
|
|
|
37
|
+
export enum PetStatus {
|
|
38
|
+
Available = 'available',
|
|
39
|
+
Sold = 'sold',
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@BidirektSchema()
|
|
43
|
+
export class Owner {
|
|
44
|
+
@BidirektField()
|
|
45
|
+
name!: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@BidirektSchema()
|
|
49
|
+
export class Shelter {
|
|
50
|
+
@BidirektField()
|
|
51
|
+
city!: string
|
|
52
|
+
}
|
|
53
|
+
|
|
39
54
|
@BidirektSchema()
|
|
40
55
|
export class Pet {
|
|
41
56
|
@BidirektField({ type: 'integer' })
|
|
42
|
-
|
|
57
|
+
petId!: number
|
|
43
58
|
|
|
44
59
|
@BidirektField()
|
|
45
60
|
name!: string
|
|
46
61
|
|
|
47
62
|
@BidirektField()
|
|
48
|
-
|
|
63
|
+
status!: PetStatus
|
|
64
|
+
|
|
65
|
+
@BidirektField()
|
|
66
|
+
size!: 'small' | 'large'
|
|
67
|
+
|
|
68
|
+
@BidirektField({ type: 'float' })
|
|
69
|
+
weight!: number
|
|
70
|
+
|
|
71
|
+
@BidirektField({ type: 'string', description: 'ISO 8601 date' })
|
|
72
|
+
birthDate!: Date
|
|
73
|
+
|
|
74
|
+
@BidirektField()
|
|
75
|
+
nickname?: string
|
|
76
|
+
|
|
77
|
+
@BidirektField()
|
|
78
|
+
owner!: Owner | null
|
|
79
|
+
|
|
80
|
+
@BidirektField({ union: { owner: Owner, shelter: Shelter } })
|
|
81
|
+
origin!: Owner | Shelter
|
|
82
|
+
|
|
83
|
+
@BidirektField()
|
|
84
|
+
tags!: string[]
|
|
85
|
+
|
|
86
|
+
@BidirektField()
|
|
87
|
+
vaccines!: Record<string, string>
|
|
49
88
|
|
|
50
89
|
internalNotes!: string
|
|
51
90
|
}
|
|
@@ -55,26 +94,15 @@ export class NewPet {
|
|
|
55
94
|
@BidirektField()
|
|
56
95
|
name!: string
|
|
57
96
|
}
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
// charge.ts
|
|
62
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
63
97
|
|
|
64
98
|
@BidirektSchema()
|
|
65
|
-
export class
|
|
99
|
+
export class Rating {
|
|
66
100
|
@BidirektField({ type: 'integer' })
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
@BidirektSchema()
|
|
71
|
-
export class Charge {
|
|
72
|
-
@BidirektField()
|
|
73
|
-
id!: string
|
|
101
|
+
stars!: number
|
|
74
102
|
}
|
|
75
103
|
```
|
|
76
104
|
|
|
77
|
-
|
|
105
|
+
`@BidirektProvider` on a method declares a resource it provides (NestJS here). A body is a schema class, or `null` for no body:
|
|
78
106
|
|
|
79
107
|
```ts
|
|
80
108
|
// pets.controller.ts
|
|
@@ -90,24 +118,18 @@ import {
|
|
|
90
118
|
import { BidirektProvider } from '@bidirekt/typescript-sdk'
|
|
91
119
|
import { NewPet, Pet } from './pet.js'
|
|
92
120
|
|
|
93
|
-
const pets: Array<Pet> = []
|
|
94
|
-
|
|
95
121
|
@Controller('pets')
|
|
96
122
|
export class PetsController {
|
|
97
|
-
|
|
98
|
-
@BidirektProvider({ method: 'GET', path: '/pets', responses: { 200: [Pet] } })
|
|
99
|
-
list(): Array<Pet> {
|
|
100
|
-
return pets
|
|
101
|
-
}
|
|
123
|
+
private readonly pets = new Map<number, Pet>()
|
|
102
124
|
|
|
103
|
-
@Get(':
|
|
125
|
+
@Get(':petId')
|
|
104
126
|
@BidirektProvider({
|
|
105
127
|
method: 'GET',
|
|
106
128
|
path: '/pets/*',
|
|
107
129
|
responses: { 200: Pet, 404: null },
|
|
108
130
|
})
|
|
109
|
-
find(@Param('
|
|
110
|
-
const pet = pets.
|
|
131
|
+
find(@Param('petId') petId: string): Pet {
|
|
132
|
+
const pet = this.pets.get(Number(petId))
|
|
111
133
|
if (pet === undefined) {
|
|
112
134
|
throw new NotFoundException()
|
|
113
135
|
}
|
|
@@ -121,67 +143,95 @@ export class PetsController {
|
|
|
121
143
|
request: NewPet,
|
|
122
144
|
responses: { 201: Pet, 400: null },
|
|
123
145
|
})
|
|
124
|
-
create(@Body() body: NewPet
|
|
125
|
-
if (typeof body
|
|
146
|
+
create(@Body() body: NewPet): Pet {
|
|
147
|
+
if (typeof body.name !== 'string') {
|
|
126
148
|
throw new BadRequestException()
|
|
127
149
|
}
|
|
128
|
-
const pet = new Pet()
|
|
129
|
-
|
|
130
|
-
pet.name = body.name
|
|
131
|
-
pet.birthDate = null
|
|
132
|
-
pets.push(pet)
|
|
150
|
+
const pet = Object.assign(new Pet(), { petId: this.pets.size + 1, ...body })
|
|
151
|
+
this.pets.set(pet.petId, pet)
|
|
133
152
|
return pet
|
|
134
153
|
}
|
|
135
154
|
}
|
|
136
155
|
```
|
|
137
156
|
|
|
138
|
-
|
|
157
|
+
`@BidirektConsumer` declares a resource it consumes, and names the provider:
|
|
139
158
|
|
|
140
159
|
```ts
|
|
141
|
-
//
|
|
160
|
+
// ratings.client.ts
|
|
142
161
|
import { BidirektConsumer } from '@bidirekt/typescript-sdk'
|
|
143
|
-
import {
|
|
162
|
+
import { Rating } from './pet.js'
|
|
144
163
|
|
|
145
|
-
export class
|
|
164
|
+
export class RatingsClient {
|
|
146
165
|
@BidirektConsumer({
|
|
147
|
-
provider: '
|
|
148
|
-
method: '
|
|
149
|
-
path: '/
|
|
150
|
-
|
|
151
|
-
responses: { 201: Charge, 402: null },
|
|
166
|
+
provider: 'petstore_reviews',
|
|
167
|
+
method: 'GET',
|
|
168
|
+
path: '/ratings/*',
|
|
169
|
+
responses: { 200: Rating, 404: null },
|
|
152
170
|
})
|
|
153
|
-
async
|
|
154
|
-
const response = await fetch(
|
|
155
|
-
|
|
156
|
-
headers: { 'content-type': 'application/json' },
|
|
157
|
-
body: JSON.stringify(body),
|
|
158
|
-
})
|
|
159
|
-
if (response.status === 402) {
|
|
160
|
-
throw new Error('payment required')
|
|
161
|
-
}
|
|
162
|
-
return (await response.json()) as Charge
|
|
171
|
+
async find(petId: number): Promise<Rating | null> {
|
|
172
|
+
const response = await fetch(`http://petstore-reviews/ratings/${petId}`)
|
|
173
|
+
return response.status === 404 ? null : ((await response.json()) as Rating)
|
|
163
174
|
}
|
|
164
175
|
}
|
|
165
176
|
```
|
|
166
177
|
|
|
167
|
-
With `emitDecoratorMetadata` on (NestJS, TypeORM), TypeScript keeps the imports of decorated classes at runtime, so classes that reference each other can fail at startup with `Cannot access 'Pet' before initialization`, and each decorated field emits its type as metadata that other libraries act on (class-transformer's implicit conversion, for example, turns `'10'` into `10`). Annotate such classes with tags instead.
|
|
168
|
-
|
|
169
178
|
### With tags
|
|
170
179
|
|
|
171
|
-
|
|
180
|
+
`@bidirekt schema` above an `interface`, an object `type` or a class marks a schema, and `@bidirekt field` each field; `field` takes the same options as the decorator, as `key=value`. `@bidirekt provides <METHOD> <path>` or `@bidirekt consumes <provider> <METHOD> <path>` above a handler declares its resource, then `@bidirekt request [schema]` and one `@bidirekt response <status> [schema]` per status, where nothing after the status means no body:
|
|
172
181
|
|
|
173
182
|
```ts
|
|
174
|
-
//
|
|
183
|
+
// pet.ts
|
|
184
|
+
export enum PetStatus {
|
|
185
|
+
Available = 'available',
|
|
186
|
+
Sold = 'sold',
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** @bidirekt schema */
|
|
190
|
+
export interface Owner {
|
|
191
|
+
/** @bidirekt field */
|
|
192
|
+
name: string
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** @bidirekt schema */
|
|
196
|
+
export interface Shelter {
|
|
197
|
+
/** @bidirekt field */
|
|
198
|
+
city: string
|
|
199
|
+
}
|
|
200
|
+
|
|
175
201
|
/** @bidirekt schema */
|
|
176
202
|
export interface Pet {
|
|
177
203
|
/** @bidirekt field type=integer */
|
|
178
|
-
|
|
204
|
+
petId: number
|
|
179
205
|
|
|
180
206
|
/** @bidirekt field */
|
|
181
207
|
name: string
|
|
182
208
|
|
|
183
209
|
/** @bidirekt field */
|
|
184
|
-
|
|
210
|
+
status: PetStatus
|
|
211
|
+
|
|
212
|
+
/** @bidirekt field */
|
|
213
|
+
size: 'small' | 'large'
|
|
214
|
+
|
|
215
|
+
/** @bidirekt field type=float */
|
|
216
|
+
weight: number
|
|
217
|
+
|
|
218
|
+
/** @bidirekt field type=string description="ISO 8601 date" */
|
|
219
|
+
birthDate: Date
|
|
220
|
+
|
|
221
|
+
/** @bidirekt field */
|
|
222
|
+
nickname?: string
|
|
223
|
+
|
|
224
|
+
/** @bidirekt field */
|
|
225
|
+
owner: Owner | null
|
|
226
|
+
|
|
227
|
+
/** @bidirekt field union={owner: Owner, shelter: Shelter} */
|
|
228
|
+
origin: Owner | Shelter
|
|
229
|
+
|
|
230
|
+
/** @bidirekt field */
|
|
231
|
+
tags: string[]
|
|
232
|
+
|
|
233
|
+
/** @bidirekt field */
|
|
234
|
+
vaccines: Record<string, string>
|
|
185
235
|
|
|
186
236
|
internalNotes: string
|
|
187
237
|
}
|
|
@@ -193,46 +243,28 @@ export interface NewPet {
|
|
|
193
243
|
}
|
|
194
244
|
|
|
195
245
|
/** @bidirekt schema */
|
|
196
|
-
export interface
|
|
246
|
+
export interface Rating {
|
|
197
247
|
/** @bidirekt field type=integer */
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/** @bidirekt schema */
|
|
202
|
-
export interface Charge {
|
|
203
|
-
/** @bidirekt field */
|
|
204
|
-
id: string
|
|
248
|
+
stars: number
|
|
205
249
|
}
|
|
206
250
|
```
|
|
207
251
|
|
|
208
|
-
In plain JavaScript, a schema is a class with the same tags, and a field's type comes from its JSDoc `@type` or from `type=`, never from its initial value, see [JavaScript](#javascript).
|
|
209
|
-
|
|
210
|
-
Declare what a function handler provides with `@bidirekt provides <METHOD> <path>` in the comment above it (Express, for example), then its `@bidirekt request [schema]` and each `@bidirekt response <status> [schema]`. A tag names a schema by its name in the contract; `Pet[]` is a list (named `PetList`) and `Pet[] as PetCollection` a list with its own name:
|
|
211
|
-
|
|
212
252
|
```ts
|
|
213
253
|
// pets.ts
|
|
214
254
|
import express from 'express'
|
|
215
|
-
import type { NewPet, Pet } from './
|
|
255
|
+
import type { NewPet, Pet } from './pet.ts'
|
|
216
256
|
|
|
217
|
-
const pets
|
|
257
|
+
const pets = new Map<string, Pet>()
|
|
218
258
|
|
|
219
259
|
export const router = express.Router()
|
|
220
260
|
|
|
221
|
-
/**
|
|
222
|
-
* @bidirekt provides GET /pets
|
|
223
|
-
* @bidirekt response 200 Pet[]
|
|
224
|
-
*/
|
|
225
|
-
router.get('/pets', (_req, res) => {
|
|
226
|
-
res.json(pets)
|
|
227
|
-
})
|
|
228
|
-
|
|
229
261
|
/**
|
|
230
262
|
* @bidirekt provides GET /pets/*
|
|
231
263
|
* @bidirekt response 200 Pet
|
|
232
264
|
* @bidirekt response 404
|
|
233
265
|
*/
|
|
234
|
-
router.get('/pets/:
|
|
235
|
-
const pet = pets.
|
|
266
|
+
router.get('/pets/:petId', (req, res) => {
|
|
267
|
+
const pet = pets.get(req.params.petId)
|
|
236
268
|
if (pet === undefined) {
|
|
237
269
|
res.sendStatus(404)
|
|
238
270
|
return
|
|
@@ -247,48 +279,31 @@ router.get('/pets/:id', (req, res) => {
|
|
|
247
279
|
* @bidirekt response 400
|
|
248
280
|
*/
|
|
249
281
|
router.post('/pets', (req, res) => {
|
|
250
|
-
const body = req.body as NewPet
|
|
251
|
-
if (typeof body
|
|
282
|
+
const body = req.body as Partial<NewPet>
|
|
283
|
+
if (typeof body.name !== 'string') {
|
|
252
284
|
res.sendStatus(400)
|
|
253
285
|
return
|
|
254
286
|
}
|
|
255
|
-
|
|
256
|
-
id: pets.length + 1,
|
|
257
|
-
name: body.name,
|
|
258
|
-
birthDate: null,
|
|
259
|
-
internalNotes: '',
|
|
260
|
-
}
|
|
261
|
-
pets.push(pet)
|
|
262
|
-
res.status(201).json(pet)
|
|
287
|
+
res.status(201).json({ petId: pets.size + 1, name: body.name })
|
|
263
288
|
})
|
|
264
289
|
```
|
|
265
290
|
|
|
266
|
-
And what a function consumes with `@bidirekt consumes <provider> <METHOD> <path>`:
|
|
267
|
-
|
|
268
291
|
```ts
|
|
269
|
-
//
|
|
270
|
-
import type {
|
|
292
|
+
// ratings.ts
|
|
293
|
+
import type { Rating } from './pet.ts'
|
|
271
294
|
|
|
272
295
|
/**
|
|
273
|
-
* @bidirekt consumes
|
|
274
|
-
* @bidirekt
|
|
275
|
-
* @bidirekt response
|
|
276
|
-
* @bidirekt response 402
|
|
296
|
+
* @bidirekt consumes petstore_reviews GET /ratings/*
|
|
297
|
+
* @bidirekt response 200 Rating
|
|
298
|
+
* @bidirekt response 404
|
|
277
299
|
*/
|
|
278
|
-
export async function
|
|
279
|
-
const response = await fetch(
|
|
280
|
-
|
|
281
|
-
headers: { 'content-type': 'application/json' },
|
|
282
|
-
body: JSON.stringify(body),
|
|
283
|
-
})
|
|
284
|
-
if (response.status === 402) {
|
|
285
|
-
throw new Error('payment required')
|
|
286
|
-
}
|
|
287
|
-
return (await response.json()) as Charge
|
|
300
|
+
export async function findRating(petId: number): Promise<Rating | null> {
|
|
301
|
+
const response = await fetch(`http://petstore-reviews/ratings/${petId}`)
|
|
302
|
+
return response.status === 404 ? null : ((await response.json()) as Rating)
|
|
288
303
|
}
|
|
289
304
|
```
|
|
290
305
|
|
|
291
|
-
###
|
|
306
|
+
### The contract
|
|
292
307
|
|
|
293
308
|
Run `generate` in the project folder:
|
|
294
309
|
|
|
@@ -296,16 +311,14 @@ Run `generate` in the project folder:
|
|
|
296
311
|
npx @bidirekt/typescript-sdk-cli generate
|
|
297
312
|
```
|
|
298
313
|
|
|
299
|
-
Either example writes the same `contracts/generated.yaml`:
|
|
314
|
+
Either example writes the same `contracts/generated.yaml`. `status`, a string `enum`, becomes a `PetStatus` enum schema, `size` an inline enum, `owner` a `ref` to the `Owner` schema, and `origin` a union keyed by `union`:
|
|
300
315
|
|
|
316
|
+
<!-- prettier-ignore -->
|
|
301
317
|
```yaml
|
|
302
318
|
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
303
319
|
provides:
|
|
304
320
|
rest:
|
|
305
321
|
/pets:
|
|
306
|
-
get:
|
|
307
|
-
responses:
|
|
308
|
-
200: PetList
|
|
309
322
|
post:
|
|
310
323
|
request: NewPet
|
|
311
324
|
responses:
|
|
@@ -317,26 +330,20 @@ provides:
|
|
|
317
330
|
200: Pet
|
|
318
331
|
404: ~
|
|
319
332
|
consumes:
|
|
320
|
-
|
|
333
|
+
petstore_reviews:
|
|
321
334
|
rest:
|
|
322
|
-
/
|
|
323
|
-
|
|
324
|
-
request: NewCharge
|
|
335
|
+
/ratings/*:
|
|
336
|
+
get:
|
|
325
337
|
responses:
|
|
326
|
-
|
|
327
|
-
|
|
338
|
+
200: Rating
|
|
339
|
+
404: ~
|
|
328
340
|
schemas:
|
|
329
|
-
|
|
341
|
+
NewPet:
|
|
330
342
|
type: object
|
|
331
343
|
properties:
|
|
332
|
-
|
|
344
|
+
name:
|
|
333
345
|
type: string
|
|
334
|
-
|
|
335
|
-
type: object
|
|
336
|
-
properties:
|
|
337
|
-
amount:
|
|
338
|
-
type: integer
|
|
339
|
-
NewPet:
|
|
346
|
+
Owner:
|
|
340
347
|
type: object
|
|
341
348
|
properties:
|
|
342
349
|
name:
|
|
@@ -344,22 +351,59 @@ schemas:
|
|
|
344
351
|
Pet:
|
|
345
352
|
type: object
|
|
346
353
|
properties:
|
|
347
|
-
|
|
354
|
+
petId:
|
|
348
355
|
type: integer
|
|
349
356
|
name:
|
|
350
357
|
type: string
|
|
358
|
+
status:
|
|
359
|
+
ref: PetStatus
|
|
360
|
+
size:
|
|
361
|
+
type: enum
|
|
362
|
+
members: ["small", "large"]
|
|
363
|
+
weight:
|
|
364
|
+
type: float
|
|
351
365
|
birthDate:
|
|
352
366
|
type: string
|
|
367
|
+
description: ISO 8601 date
|
|
368
|
+
nickname:
|
|
369
|
+
type: string
|
|
370
|
+
optional: true
|
|
371
|
+
owner:
|
|
372
|
+
ref: Owner
|
|
353
373
|
nullable: true
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
374
|
+
origin:
|
|
375
|
+
type: union
|
|
376
|
+
members:
|
|
377
|
+
owner:
|
|
378
|
+
ref: Owner
|
|
379
|
+
shelter:
|
|
380
|
+
ref: Shelter
|
|
381
|
+
tags:
|
|
382
|
+
type: array
|
|
383
|
+
items:
|
|
384
|
+
type: string
|
|
385
|
+
vaccines:
|
|
386
|
+
type: map
|
|
387
|
+
values:
|
|
388
|
+
type: string
|
|
389
|
+
PetStatus:
|
|
390
|
+
type: enum
|
|
391
|
+
members: ["available", "sold"]
|
|
392
|
+
Rating:
|
|
393
|
+
type: object
|
|
394
|
+
properties:
|
|
395
|
+
stars:
|
|
396
|
+
type: integer
|
|
397
|
+
Shelter:
|
|
398
|
+
type: object
|
|
399
|
+
properties:
|
|
400
|
+
city:
|
|
401
|
+
type: string
|
|
358
402
|
```
|
|
359
403
|
|
|
360
|
-
|
|
404
|
+
## Schemas in a handler's comment
|
|
361
405
|
|
|
362
|
-
A handler that builds its body by hand, with no type to read, declares the schema in its own comment: `@bidirekt schema <Name>` opens a schema and each following `@bidirekt field <name> type=...` is a field of it, until the next `schema` or the end of the comment. `type=` is required there
|
|
406
|
+
A handler that builds its body by hand, with no type to read, declares the schema in its own comment: `@bidirekt schema <Name>` opens a schema and each following `@bidirekt field <name> type=...` is a field of it, until the next `schema` or the end of the comment. `type=` is required there. These schemas belong to the whole contract, so other handlers and fields reference them by name:
|
|
363
407
|
|
|
364
408
|
```ts
|
|
365
409
|
// health.ts
|
|
@@ -373,25 +417,17 @@ export const router = express.Router()
|
|
|
373
417
|
*
|
|
374
418
|
* @bidirekt schema Health
|
|
375
419
|
* @bidirekt field status type=string
|
|
376
|
-
* @bidirekt field uptime type=integer
|
|
377
420
|
* @bidirekt field checks type=HealthCheck[]
|
|
378
421
|
*
|
|
379
422
|
* @bidirekt schema HealthCheck
|
|
380
423
|
* @bidirekt field name type=string
|
|
381
|
-
* @bidirekt field ok type=boolean
|
|
382
424
|
* @bidirekt field error type=string nullable=true
|
|
383
425
|
*/
|
|
384
426
|
router.get('/health', (_req, res) => {
|
|
385
|
-
res.json({
|
|
386
|
-
status: 'ok',
|
|
387
|
-
uptime: Math.floor(process.uptime()),
|
|
388
|
-
checks: [{ name: 'database', ok: true, error: null }],
|
|
389
|
-
})
|
|
427
|
+
res.json({ status: 'ok', checks: [{ name: 'database', error: null }] })
|
|
390
428
|
})
|
|
391
429
|
```
|
|
392
430
|
|
|
393
|
-
`generate` writes:
|
|
394
|
-
|
|
395
431
|
```yaml
|
|
396
432
|
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
397
433
|
provides:
|
|
@@ -406,8 +442,6 @@ schemas:
|
|
|
406
442
|
properties:
|
|
407
443
|
status:
|
|
408
444
|
type: string
|
|
409
|
-
uptime:
|
|
410
|
-
type: integer
|
|
411
445
|
checks:
|
|
412
446
|
type: array
|
|
413
447
|
items:
|
|
@@ -417,2275 +451,198 @@ schemas:
|
|
|
417
451
|
properties:
|
|
418
452
|
name:
|
|
419
453
|
type: string
|
|
420
|
-
ok:
|
|
421
|
-
type: boolean
|
|
422
454
|
error:
|
|
423
455
|
type: string
|
|
424
456
|
nullable: true
|
|
425
457
|
```
|
|
426
458
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
```ts
|
|
430
|
-
// health.ts
|
|
431
|
-
/**
|
|
432
|
-
* @bidirekt provides GET /health
|
|
433
|
-
* @bidirekt response 200 Health
|
|
434
|
-
*
|
|
435
|
-
* @bidirekt schema Health
|
|
436
|
-
*/
|
|
437
|
-
export function health() {}
|
|
438
|
-
```
|
|
439
|
-
|
|
440
|
-
```text
|
|
441
|
-
health.ts:6 schema "Health" has no field: annotate at least one field with "@bidirekt field"
|
|
442
|
-
```
|
|
443
|
-
|
|
444
|
-
## Reference
|
|
445
|
-
|
|
446
|
-
Every example below is a whole project: `generate` run on it alone writes the YAML shown, or prints the lines shown and exits with 1. Where a feature has both forms, the decorator example and the tag example write the same YAML. The TypeScript examples compile with `strict` and `experimentalDecorators`, except the one that shows a syntax error.
|
|
459
|
+
## JavaScript
|
|
447
460
|
|
|
448
|
-
|
|
461
|
+
In JavaScript, a schema is a class with tags, and the handler tags are the same as in TypeScript. A field's type comes from `type=` or from its JSDoc `@type`, never from its initial value; `@type` reads a `@typedef`, `import('./owner.js').Owner`, `?T` (`T | null`), `!T` (`T`) and `Object.<string, T>` (a map):
|
|
449
462
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
| Decorator | Tag | What it declares |
|
|
455
|
-
| ----------------------------------------------- | -------------------------------------------- | -------------------------------------------------------------------------------- |
|
|
456
|
-
| `type: 'integer'` | `type=integer` | the whole type of the field, see [Declared types](#declared-types) |
|
|
457
|
-
| `name: 'birth_date'` | `name=birth_date` | the field's name in the contract; without it, the property's name |
|
|
458
|
-
| `optional: true` | `optional=true` | whether the field can be absent; without it, from `?` or `undefined` in the type |
|
|
459
|
-
| `nullable: true` | `nullable=true` | whether the field can be `null`; without it, from `null` in the type |
|
|
460
|
-
| `description: 'Shown on the card'` | `description="Shown on the card"` | the field's description; only a declared one, a plain comment is not |
|
|
461
|
-
| `union: { card: CardPayment, pix: PixPayment }` | `union={card: CardPayment, pix: PixPayment}` | the key of each member of a union of schemas, see [Unions](#unions) |
|
|
462
|
-
|
|
463
|
-
Decorator options are written in place: an object literal of string literals, `true`, `false` and, in `union`, classes, never a variable. Tag arguments are separated by spaces, and only a `description` value is quoted. A property without an annotation stays out of the contract.
|
|
464
|
-
|
|
465
|
-
```ts
|
|
466
|
-
// pet.ts
|
|
467
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
468
|
-
|
|
469
|
-
@BidirektSchema()
|
|
470
|
-
export class Pet {
|
|
471
|
-
@BidirektField({ name: 'birth_date', description: 'Day the pet was born' })
|
|
472
|
-
birthDate!: string
|
|
473
|
-
|
|
474
|
-
@BidirektField({ optional: true, nullable: true })
|
|
475
|
-
nickname!: string
|
|
476
|
-
|
|
477
|
-
@BidirektField({ optional: false, nullable: false })
|
|
478
|
-
breed?: string | null
|
|
479
|
-
|
|
480
|
-
internalNotes!: string
|
|
481
|
-
}
|
|
482
|
-
```
|
|
463
|
+
```js
|
|
464
|
+
// pet.js
|
|
465
|
+
/** @typedef {'small' | 'large'} Size */
|
|
483
466
|
|
|
484
|
-
```ts
|
|
485
|
-
// pet.ts
|
|
486
467
|
/** @bidirekt schema */
|
|
487
|
-
export
|
|
488
|
-
/** @bidirekt field
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
/** @bidirekt field optional=true nullable=true */
|
|
492
|
-
nickname: string
|
|
468
|
+
export class Pet {
|
|
469
|
+
/** @bidirekt field type=integer */
|
|
470
|
+
petId
|
|
493
471
|
|
|
494
|
-
/** @bidirekt field
|
|
495
|
-
|
|
472
|
+
/** @bidirekt field
|
|
473
|
+
* @type {?string} */
|
|
474
|
+
nickname
|
|
496
475
|
|
|
497
|
-
|
|
476
|
+
/** @bidirekt field
|
|
477
|
+
* @type {Size} */
|
|
478
|
+
size
|
|
498
479
|
}
|
|
499
480
|
```
|
|
500
481
|
|
|
482
|
+
<!-- prettier-ignore -->
|
|
501
483
|
```yaml
|
|
502
484
|
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
503
485
|
schemas:
|
|
504
486
|
Pet:
|
|
505
487
|
type: object
|
|
506
488
|
properties:
|
|
507
|
-
|
|
508
|
-
type:
|
|
509
|
-
description: Day the pet was born
|
|
489
|
+
petId:
|
|
490
|
+
type: integer
|
|
510
491
|
nickname:
|
|
511
492
|
type: string
|
|
512
|
-
optional: true
|
|
513
493
|
nullable: true
|
|
514
|
-
|
|
515
|
-
type:
|
|
494
|
+
size:
|
|
495
|
+
type: enum
|
|
496
|
+
members: ["small", "large"]
|
|
516
497
|
```
|
|
517
498
|
|
|
518
|
-
|
|
499
|
+
`generate` reads the `.js` files of a project with a `jsconfig.json` or with no config at all; with a `tsconfig.json`, only when it sets `allowJs`. A hand-written `.d.ts` beside a `.js` is read too, but only its `@bidirekt schema` and `@bidirekt field` on an interface or an object type, and a name the `.js` also exports is read from the `.js` only. A `@typedef` is never a schema, and `String`, `Number` or `Boolean` stop `generate`: write the lowercase type.
|
|
519
500
|
|
|
520
|
-
|
|
521
|
-
// pet.ts
|
|
522
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
501
|
+
## The `generate` command
|
|
523
502
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
503
|
+
```sh
|
|
504
|
+
npx @bidirekt/typescript-sdk-cli generate
|
|
505
|
+
npx @bidirekt/typescript-sdk-cli generate --check
|
|
506
|
+
```
|
|
528
507
|
|
|
529
|
-
|
|
530
|
-
static species = 'dog'
|
|
531
|
-
}
|
|
508
|
+
`generate` reads the `tsconfig.json` in the current folder, or the `jsconfig.json` when there is none, or the `.ts`, `.mts`, `.cts`, `.tsx`, `.js`, `.mjs`, `.cjs` and `.jsx` files in it when there is neither, leaving out `node_modules` and, at any depth, `dist`, `build`, `out` and `coverage`. It writes `contracts/generated.yaml` in the project's folder, whole, on every run. It reads the source code and never runs your app.
|
|
532
509
|
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
name!: string
|
|
510
|
+
- `--project <tsconfig>` reads another project, and writes to its folder.
|
|
511
|
+
- `--out <file>` writes to another file.
|
|
512
|
+
- `--check` writes nothing and exits with 1 when the file on disk differs from what `generate` would write (`"contracts/generated.yaml" is out of date: run generate without --check`). Run it in CI.
|
|
537
513
|
|
|
538
|
-
|
|
539
|
-
static brand = 'acme'
|
|
540
|
-
}
|
|
541
|
-
```
|
|
514
|
+
Each error is printed as `file:line message`; on an error nothing is written and the exit code is 1. A warning is printed as `file:line warning: message` and doesn't change the exit code.
|
|
542
515
|
|
|
543
|
-
|
|
544
|
-
pet.ts:9 warning: @BidirektField is on a static property, so it is not read: put it on a property
|
|
545
|
-
pet.ts:18 warning: "@bidirekt field" is above a static property, so it is not read: put it above a property
|
|
546
|
-
```
|
|
516
|
+
`contracts/generated.yaml` is the service's only contract, and you never edit it by hand: the next `generate` writes over it. Regenerate it whenever an annotation or an annotated type changes, commit it with the code, and ship it like any contract file:
|
|
547
517
|
|
|
548
|
-
```
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
type: string
|
|
556
|
-
Toy:
|
|
557
|
-
type: object
|
|
558
|
-
properties:
|
|
559
|
-
name:
|
|
560
|
-
type: string
|
|
518
|
+
```sh
|
|
519
|
+
npx @bidirekt/typescript-sdk-cli generate
|
|
520
|
+
bidirekt validate contracts/generated.yaml --participant petstore_ts --environment production
|
|
521
|
+
bidirekt publish contracts/generated.yaml --participant petstore_ts --version 1.4.0
|
|
522
|
+
bidirekt can-i-deploy petstore_ts --version 1.4.0 --environment production
|
|
523
|
+
# deploy
|
|
524
|
+
bidirekt record-deployment petstore_ts --version 1.4.0 --environment production
|
|
561
525
|
```
|
|
562
526
|
|
|
563
|
-
|
|
527
|
+
When `can-i-deploy` reports a break, fix the annotation or the type in the code and generate again.
|
|
564
528
|
|
|
565
|
-
A
|
|
529
|
+
A package of your workspace, which npm links into `node_modules`, is read as your code when its `types` or `exports` point at its source. A class, interface or type reached through its built `.d.ts` stops `generate`.
|
|
566
530
|
|
|
567
|
-
|
|
568
|
-
| ------------------------------------------------------------------------------ | --------------------------------------------------------- |
|
|
569
|
-
| `string`, `boolean`, `number` | `string`, `boolean`, `number` |
|
|
570
|
-
| `T \| null` | `nullable: true` |
|
|
571
|
-
| `x?: T`, `T \| undefined` | `optional: true` |
|
|
572
|
-
| `'a' \| 'b'`, through aliases too, `Status \| 'unknown'`, `(typeof X)[number]` | `type: enum`, inline |
|
|
573
|
-
| a string `enum` | `ref` to an `enum` schema named after it, with its values |
|
|
574
|
-
| one member of a string `enum`, `PetStatus.Available` | `type: enum`, inline, with that member |
|
|
575
|
-
| a numeric `enum` | `integer` |
|
|
576
|
-
| `T[]`, `Array<T>`, `readonly T[]` | `type: array` |
|
|
577
|
-
| `Record<string, T>`, `{ [key: string]: T }` | `type: map` |
|
|
578
|
-
| `(T \| null)[]`, `Record<string, T \| null>` | `nullable: true` on the items or values |
|
|
579
|
-
| a schema: class, interface or object type | `ref` |
|
|
580
|
-
| a union of schemas | `type: union`, with the keys from `union` |
|
|
581
|
-
|
|
582
|
-
`integer` and `float` come only from a [declared type](#declared-types). A string enum or a literal union imported from a dependency translates the same way.
|
|
583
|
-
|
|
584
|
-
A type alias is followed to its type, a generic one too, while a generic class stops `generate`, see [Types `generate` can't translate](#types-generate-cant-translate):
|
|
585
|
-
|
|
586
|
-
```ts
|
|
587
|
-
// pet.ts
|
|
588
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
589
|
-
|
|
590
|
-
type Nullable<T> = T | null
|
|
591
|
-
|
|
592
|
-
@BidirektSchema()
|
|
593
|
-
export class Pet {
|
|
594
|
-
@BidirektField()
|
|
595
|
-
nickname!: Nullable<string>
|
|
596
|
-
}
|
|
597
|
-
```
|
|
598
|
-
|
|
599
|
-
```ts
|
|
600
|
-
// pet.ts
|
|
601
|
-
type Nullable<T> = T | null
|
|
602
|
-
|
|
603
|
-
/** @bidirekt schema */
|
|
604
|
-
export interface Pet {
|
|
605
|
-
/** @bidirekt field */
|
|
606
|
-
nickname: Nullable<string>
|
|
607
|
-
}
|
|
608
|
-
```
|
|
609
|
-
|
|
610
|
-
```yaml
|
|
611
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
612
|
-
schemas:
|
|
613
|
-
Pet:
|
|
614
|
-
type: object
|
|
615
|
-
properties:
|
|
616
|
-
nickname:
|
|
617
|
-
type: string
|
|
618
|
-
nullable: true
|
|
619
|
-
```
|
|
620
|
-
|
|
621
|
-
#### Primitives, `null` and `undefined`
|
|
622
|
-
|
|
623
|
-
```ts
|
|
624
|
-
// pet.ts
|
|
625
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
626
|
-
|
|
627
|
-
@BidirektSchema()
|
|
628
|
-
export class Pet {
|
|
629
|
-
@BidirektField()
|
|
630
|
-
name!: string
|
|
631
|
-
|
|
632
|
-
@BidirektField()
|
|
633
|
-
vaccinated!: boolean
|
|
634
|
-
|
|
635
|
-
@BidirektField()
|
|
636
|
-
weight!: number
|
|
637
|
-
|
|
638
|
-
@BidirektField()
|
|
639
|
-
birthDate!: string | null
|
|
640
|
-
|
|
641
|
-
@BidirektField()
|
|
642
|
-
nickname?: string
|
|
643
|
-
|
|
644
|
-
@BidirektField()
|
|
645
|
-
breed!: string | undefined
|
|
646
|
-
|
|
647
|
-
@BidirektField()
|
|
648
|
-
color?: string | null
|
|
649
|
-
}
|
|
650
|
-
```
|
|
651
|
-
|
|
652
|
-
```ts
|
|
653
|
-
// pet.ts
|
|
654
|
-
/** @bidirekt schema */
|
|
655
|
-
export interface Pet {
|
|
656
|
-
/** @bidirekt field */
|
|
657
|
-
name: string
|
|
658
|
-
|
|
659
|
-
/** @bidirekt field */
|
|
660
|
-
vaccinated: boolean
|
|
661
|
-
|
|
662
|
-
/** @bidirekt field */
|
|
663
|
-
weight: number
|
|
664
|
-
|
|
665
|
-
/** @bidirekt field */
|
|
666
|
-
birthDate: string | null
|
|
667
|
-
|
|
668
|
-
/** @bidirekt field */
|
|
669
|
-
nickname?: string
|
|
670
|
-
|
|
671
|
-
/** @bidirekt field */
|
|
672
|
-
breed: string | undefined
|
|
673
|
-
|
|
674
|
-
/** @bidirekt field */
|
|
675
|
-
color?: string | null
|
|
676
|
-
}
|
|
677
|
-
```
|
|
678
|
-
|
|
679
|
-
```yaml
|
|
680
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
681
|
-
schemas:
|
|
682
|
-
Pet:
|
|
683
|
-
type: object
|
|
684
|
-
properties:
|
|
685
|
-
name:
|
|
686
|
-
type: string
|
|
687
|
-
vaccinated:
|
|
688
|
-
type: boolean
|
|
689
|
-
weight:
|
|
690
|
-
type: number
|
|
691
|
-
birthDate:
|
|
692
|
-
type: string
|
|
693
|
-
nullable: true
|
|
694
|
-
nickname:
|
|
695
|
-
type: string
|
|
696
|
-
optional: true
|
|
697
|
-
breed:
|
|
698
|
-
type: string
|
|
699
|
-
optional: true
|
|
700
|
-
color:
|
|
701
|
-
type: string
|
|
702
|
-
optional: true
|
|
703
|
-
nullable: true
|
|
704
|
-
```
|
|
705
|
-
|
|
706
|
-
#### Enums
|
|
707
|
-
|
|
708
|
-
```ts
|
|
709
|
-
// pet.ts
|
|
710
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
711
|
-
|
|
712
|
-
export enum PetStatus {
|
|
713
|
-
Available = 'available',
|
|
714
|
-
OnHold = 'on_hold',
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
export enum Priority {
|
|
718
|
-
Low,
|
|
719
|
-
High,
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
const SIZES = ['small', 'large'] as const
|
|
723
|
-
|
|
724
|
-
type Color = 'black' | 'white'
|
|
725
|
-
|
|
726
|
-
@BidirektSchema()
|
|
727
|
-
export class Pet {
|
|
728
|
-
@BidirektField()
|
|
729
|
-
status!: PetStatus
|
|
730
|
-
|
|
731
|
-
@BidirektField()
|
|
732
|
-
reportedStatus!: PetStatus | 'unknown'
|
|
733
|
-
|
|
734
|
-
@BidirektField()
|
|
735
|
-
initialStatus!: PetStatus.Available
|
|
736
|
-
|
|
737
|
-
@BidirektField()
|
|
738
|
-
size!: (typeof SIZES)[number]
|
|
739
|
-
|
|
740
|
-
@BidirektField()
|
|
741
|
-
color!: Color | null
|
|
742
|
-
|
|
743
|
-
@BidirektField()
|
|
744
|
-
priority!: Priority
|
|
745
|
-
}
|
|
746
|
-
```
|
|
747
|
-
|
|
748
|
-
```ts
|
|
749
|
-
// pet.ts
|
|
750
|
-
export enum PetStatus {
|
|
751
|
-
Available = 'available',
|
|
752
|
-
OnHold = 'on_hold',
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
export enum Priority {
|
|
756
|
-
Low,
|
|
757
|
-
High,
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
const SIZES = ['small', 'large'] as const
|
|
761
|
-
|
|
762
|
-
type Color = 'black' | 'white'
|
|
763
|
-
|
|
764
|
-
/** @bidirekt schema */
|
|
765
|
-
export interface Pet {
|
|
766
|
-
/** @bidirekt field */
|
|
767
|
-
status: PetStatus
|
|
768
|
-
|
|
769
|
-
/** @bidirekt field */
|
|
770
|
-
reportedStatus: PetStatus | 'unknown'
|
|
771
|
-
|
|
772
|
-
/** @bidirekt field */
|
|
773
|
-
initialStatus: PetStatus.Available
|
|
774
|
-
|
|
775
|
-
/** @bidirekt field */
|
|
776
|
-
size: (typeof SIZES)[number]
|
|
777
|
-
|
|
778
|
-
/** @bidirekt field */
|
|
779
|
-
color: Color | null
|
|
780
|
-
|
|
781
|
-
/** @bidirekt field */
|
|
782
|
-
priority: Priority
|
|
783
|
-
}
|
|
784
|
-
```
|
|
785
|
-
|
|
786
|
-
<!-- prettier-ignore -->
|
|
787
|
-
```yaml
|
|
788
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
789
|
-
schemas:
|
|
790
|
-
Pet:
|
|
791
|
-
type: object
|
|
792
|
-
properties:
|
|
793
|
-
status:
|
|
794
|
-
ref: PetStatus
|
|
795
|
-
reportedStatus:
|
|
796
|
-
type: enum
|
|
797
|
-
members: ["available", "on_hold", "unknown"]
|
|
798
|
-
initialStatus:
|
|
799
|
-
type: enum
|
|
800
|
-
members: ["available"]
|
|
801
|
-
size:
|
|
802
|
-
type: enum
|
|
803
|
-
members: ["small", "large"]
|
|
804
|
-
color:
|
|
805
|
-
type: enum
|
|
806
|
-
members: ["black", "white"]
|
|
807
|
-
nullable: true
|
|
808
|
-
priority:
|
|
809
|
-
type: integer
|
|
810
|
-
PetStatus:
|
|
811
|
-
type: enum
|
|
812
|
-
members: ["available", "on_hold"]
|
|
813
|
-
```
|
|
814
|
-
|
|
815
|
-
To name an enum's schema, or to write it without a field that uses it, see [Enums](#enums-1) under Schemas.
|
|
816
|
-
|
|
817
|
-
#### Lists, maps and references
|
|
818
|
-
|
|
819
|
-
```ts
|
|
820
|
-
// pet.ts
|
|
821
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
822
|
-
|
|
823
|
-
@BidirektSchema()
|
|
824
|
-
export class Owner {
|
|
825
|
-
@BidirektField()
|
|
826
|
-
name!: string
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
@BidirektSchema()
|
|
830
|
-
export class Pet {
|
|
831
|
-
@BidirektField()
|
|
832
|
-
tags!: string[]
|
|
833
|
-
|
|
834
|
-
@BidirektField()
|
|
835
|
-
scores!: Array<number | null>
|
|
836
|
-
|
|
837
|
-
@BidirektField()
|
|
838
|
-
aliases!: readonly string[]
|
|
839
|
-
|
|
840
|
-
@BidirektField()
|
|
841
|
-
notes!: Record<string, string | null>
|
|
842
|
-
|
|
843
|
-
@BidirektField()
|
|
844
|
-
labels!: { [key: string]: string }
|
|
845
|
-
|
|
846
|
-
@BidirektField()
|
|
847
|
-
owner!: Owner
|
|
848
|
-
|
|
849
|
-
@BidirektField()
|
|
850
|
-
formerOwners!: Owner[]
|
|
851
|
-
|
|
852
|
-
@BidirektField()
|
|
853
|
-
ownersByCity!: Record<string, Owner>
|
|
854
|
-
}
|
|
855
|
-
```
|
|
856
|
-
|
|
857
|
-
```ts
|
|
858
|
-
// pet.ts
|
|
859
|
-
/** @bidirekt schema */
|
|
860
|
-
export interface Owner {
|
|
861
|
-
/** @bidirekt field */
|
|
862
|
-
name: string
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
/** @bidirekt schema */
|
|
866
|
-
export interface Pet {
|
|
867
|
-
/** @bidirekt field */
|
|
868
|
-
tags: string[]
|
|
869
|
-
|
|
870
|
-
/** @bidirekt field */
|
|
871
|
-
scores: Array<number | null>
|
|
872
|
-
|
|
873
|
-
/** @bidirekt field */
|
|
874
|
-
aliases: readonly string[]
|
|
875
|
-
|
|
876
|
-
/** @bidirekt field */
|
|
877
|
-
notes: Record<string, string | null>
|
|
878
|
-
|
|
879
|
-
/** @bidirekt field */
|
|
880
|
-
labels: { [key: string]: string }
|
|
881
|
-
|
|
882
|
-
/** @bidirekt field */
|
|
883
|
-
owner: Owner
|
|
884
|
-
|
|
885
|
-
/** @bidirekt field */
|
|
886
|
-
formerOwners: Owner[]
|
|
887
|
-
|
|
888
|
-
/** @bidirekt field */
|
|
889
|
-
ownersByCity: Record<string, Owner>
|
|
890
|
-
}
|
|
891
|
-
```
|
|
892
|
-
|
|
893
|
-
```yaml
|
|
894
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
895
|
-
schemas:
|
|
896
|
-
Owner:
|
|
897
|
-
type: object
|
|
898
|
-
properties:
|
|
899
|
-
name:
|
|
900
|
-
type: string
|
|
901
|
-
Pet:
|
|
902
|
-
type: object
|
|
903
|
-
properties:
|
|
904
|
-
tags:
|
|
905
|
-
type: array
|
|
906
|
-
items:
|
|
907
|
-
type: string
|
|
908
|
-
scores:
|
|
909
|
-
type: array
|
|
910
|
-
items:
|
|
911
|
-
type: number
|
|
912
|
-
nullable: true
|
|
913
|
-
aliases:
|
|
914
|
-
type: array
|
|
915
|
-
items:
|
|
916
|
-
type: string
|
|
917
|
-
notes:
|
|
918
|
-
type: map
|
|
919
|
-
values:
|
|
920
|
-
type: string
|
|
921
|
-
nullable: true
|
|
922
|
-
labels:
|
|
923
|
-
type: map
|
|
924
|
-
values:
|
|
925
|
-
type: string
|
|
926
|
-
owner:
|
|
927
|
-
ref: Owner
|
|
928
|
-
formerOwners:
|
|
929
|
-
type: array
|
|
930
|
-
items:
|
|
931
|
-
ref: Owner
|
|
932
|
-
ownersByCity:
|
|
933
|
-
type: map
|
|
934
|
-
values:
|
|
935
|
-
ref: Owner
|
|
936
|
-
```
|
|
937
|
-
|
|
938
|
-
A decorated class can reference a tagged interface, and the other way round.
|
|
939
|
-
|
|
940
|
-
#### Declared types
|
|
941
|
-
|
|
942
|
-
A declared `type` is the whole type of the field, whatever TypeScript says: a primitive (`string`, `integer`, `float`, `number`, `boolean`), `T[]` for a list of `T`, or `map<T>` for a map of `T` (its keys are always strings), nested up to three levels in a decorator and to any depth in a tag (`integer[][]`, `map<float[]>`). `optional` and `nullable` still come from the TypeScript type when not declared:
|
|
943
|
-
|
|
944
|
-
```ts
|
|
945
|
-
// pet.ts
|
|
946
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
947
|
-
|
|
948
|
-
@BidirektSchema()
|
|
949
|
-
export class Pet {
|
|
950
|
-
@BidirektField({ type: 'integer' })
|
|
951
|
-
id!: number
|
|
952
|
-
|
|
953
|
-
@BidirektField({ type: 'float' })
|
|
954
|
-
weight!: number
|
|
955
|
-
|
|
956
|
-
@BidirektField({ type: 'string' })
|
|
957
|
-
bornAt?: Date | null
|
|
958
|
-
|
|
959
|
-
@BidirektField({ type: 'integer[]' })
|
|
960
|
-
scores!: number[]
|
|
961
|
-
|
|
962
|
-
@BidirektField({ type: 'integer[][]' })
|
|
963
|
-
grid!: number[][]
|
|
964
|
-
|
|
965
|
-
@BidirektField({ type: 'map<float[]>' })
|
|
966
|
-
readings!: Record<string, number[]>
|
|
967
|
-
}
|
|
968
|
-
```
|
|
969
|
-
|
|
970
|
-
```ts
|
|
971
|
-
// pet.ts
|
|
972
|
-
/** @bidirekt schema */
|
|
973
|
-
export interface Pet {
|
|
974
|
-
/** @bidirekt field type=integer */
|
|
975
|
-
id: number
|
|
976
|
-
|
|
977
|
-
/** @bidirekt field type=float */
|
|
978
|
-
weight: number
|
|
979
|
-
|
|
980
|
-
/** @bidirekt field type=string */
|
|
981
|
-
bornAt?: Date | null
|
|
982
|
-
|
|
983
|
-
/** @bidirekt field type=integer[] */
|
|
984
|
-
scores: number[]
|
|
985
|
-
|
|
986
|
-
/** @bidirekt field type=integer[][] */
|
|
987
|
-
grid: number[][]
|
|
988
|
-
|
|
989
|
-
/** @bidirekt field type=map<float[]> */
|
|
990
|
-
readings: Record<string, number[]>
|
|
991
|
-
}
|
|
992
|
-
```
|
|
993
|
-
|
|
994
|
-
```yaml
|
|
995
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
996
|
-
schemas:
|
|
997
|
-
Pet:
|
|
998
|
-
type: object
|
|
999
|
-
properties:
|
|
1000
|
-
id:
|
|
1001
|
-
type: integer
|
|
1002
|
-
weight:
|
|
1003
|
-
type: float
|
|
1004
|
-
bornAt:
|
|
1005
|
-
type: string
|
|
1006
|
-
optional: true
|
|
1007
|
-
nullable: true
|
|
1008
|
-
scores:
|
|
1009
|
-
type: array
|
|
1010
|
-
items:
|
|
1011
|
-
type: integer
|
|
1012
|
-
grid:
|
|
1013
|
-
type: array
|
|
1014
|
-
items:
|
|
1015
|
-
type: array
|
|
1016
|
-
items:
|
|
1017
|
-
type: integer
|
|
1018
|
-
readings:
|
|
1019
|
-
type: map
|
|
1020
|
-
values:
|
|
1021
|
-
type: array
|
|
1022
|
-
items:
|
|
1023
|
-
type: float
|
|
1024
|
-
```
|
|
1025
|
-
|
|
1026
|
-
In a tag, `type=` also takes a schema by its name in the contract, alone, in a list or in a map. A decorator references schemas by their TypeScript type instead:
|
|
1027
|
-
|
|
1028
|
-
```js
|
|
1029
|
-
// shelter.js
|
|
1030
|
-
/** @bidirekt schema */
|
|
1031
|
-
export class Owner {
|
|
1032
|
-
/** @bidirekt field type=string */
|
|
1033
|
-
name
|
|
1034
|
-
}
|
|
1035
|
-
|
|
1036
|
-
/** @bidirekt schema */
|
|
1037
|
-
export class Shelter {
|
|
1038
|
-
/** @bidirekt field type=Owner */
|
|
1039
|
-
keeper
|
|
1040
|
-
|
|
1041
|
-
/** @bidirekt field type=Owner[] */
|
|
1042
|
-
volunteers
|
|
1043
|
-
|
|
1044
|
-
/** @bidirekt field type=map<Owner> */
|
|
1045
|
-
ownersByPet
|
|
1046
|
-
}
|
|
1047
|
-
```
|
|
1048
|
-
|
|
1049
|
-
```yaml
|
|
1050
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
1051
|
-
schemas:
|
|
1052
|
-
Owner:
|
|
1053
|
-
type: object
|
|
1054
|
-
properties:
|
|
1055
|
-
name:
|
|
1056
|
-
type: string
|
|
1057
|
-
Shelter:
|
|
1058
|
-
type: object
|
|
1059
|
-
properties:
|
|
1060
|
-
keeper:
|
|
1061
|
-
ref: Owner
|
|
1062
|
-
volunteers:
|
|
1063
|
-
type: array
|
|
1064
|
-
items:
|
|
1065
|
-
ref: Owner
|
|
1066
|
-
ownersByPet:
|
|
1067
|
-
type: map
|
|
1068
|
-
values:
|
|
1069
|
-
ref: Owner
|
|
1070
|
-
```
|
|
1071
|
-
|
|
1072
|
-
#### Types `generate` can't translate
|
|
1073
|
-
|
|
1074
|
-
A type with no single contract type stops `generate` at its line. Declare a `type`, or declare a schema and use it as the type:
|
|
1075
|
-
|
|
1076
|
-
```ts
|
|
1077
|
-
// pet.ts
|
|
1078
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1079
|
-
|
|
1080
|
-
export enum Level {
|
|
1081
|
-
Low = 'low',
|
|
1082
|
-
High = 1,
|
|
1083
|
-
}
|
|
1084
|
-
|
|
1085
|
-
export interface Address {
|
|
1086
|
-
street: string
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1089
|
-
export type Collar = {
|
|
1090
|
-
size: string
|
|
1091
|
-
}
|
|
1092
|
-
|
|
1093
|
-
export class Toy {
|
|
1094
|
-
name = ''
|
|
1095
|
-
}
|
|
1096
|
-
|
|
1097
|
-
export class Page<T> {
|
|
1098
|
-
items: T[] = []
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
@BidirektSchema()
|
|
1102
|
-
export class Pet {
|
|
1103
|
-
@BidirektField()
|
|
1104
|
-
code!: string | number
|
|
1105
|
-
|
|
1106
|
-
@BidirektField()
|
|
1107
|
-
rank!: 1 | 2
|
|
1108
|
-
|
|
1109
|
-
@BidirektField()
|
|
1110
|
-
level!: Level
|
|
1111
|
-
|
|
1112
|
-
@BidirektField()
|
|
1113
|
-
extra!: unknown
|
|
1114
|
-
|
|
1115
|
-
@BidirektField()
|
|
1116
|
-
data!: any
|
|
1117
|
-
|
|
1118
|
-
@BidirektField()
|
|
1119
|
-
bornAt!: Date
|
|
1120
|
-
|
|
1121
|
-
@BidirektField()
|
|
1122
|
-
location!: { street: string }
|
|
1123
|
-
|
|
1124
|
-
@BidirektField()
|
|
1125
|
-
address!: Address
|
|
1126
|
-
|
|
1127
|
-
@BidirektField()
|
|
1128
|
-
collar!: Collar
|
|
1129
|
-
|
|
1130
|
-
@BidirektField()
|
|
1131
|
-
toy!: Toy
|
|
1132
|
-
|
|
1133
|
-
@BidirektField()
|
|
1134
|
-
siblings!: Page<Pet>
|
|
1135
|
-
|
|
1136
|
-
@BidirektField()
|
|
1137
|
-
scores!: Record<number, string>
|
|
1138
|
-
|
|
1139
|
-
@BidirektField()
|
|
1140
|
-
nicknames!: Array<string | undefined>
|
|
1141
|
-
|
|
1142
|
-
@BidirektField()
|
|
1143
|
-
tag = 'new'
|
|
1144
|
-
}
|
|
1145
|
-
```
|
|
1146
|
-
|
|
1147
|
-
```text
|
|
1148
|
-
pet.ts:28 cannot translate "string | number" to a contract type: declare it with @BidirektField({ type })
|
|
1149
|
-
pet.ts:31 cannot translate numeric literal type "1 | 2": use string literals or declare it with @BidirektField({ type })
|
|
1150
|
-
pet.ts:34 cannot translate enum "Level" with string and numeric members: make all its members strings or all numbers
|
|
1151
|
-
pet.ts:37 cannot translate "unknown" to a contract type: declare it with @BidirektField({ type })
|
|
1152
|
-
pet.ts:40 cannot translate "any" to a contract type: declare it with @BidirektField({ type })
|
|
1153
|
-
pet.ts:43 cannot translate "Date" to a contract type: declare it with @BidirektField({ type })
|
|
1154
|
-
pet.ts:46 cannot translate inline object type "{ street: string }": declare a class with @BidirektSchema and use it as the type
|
|
1155
|
-
pet.ts:49 cannot translate interface "Address": add "@bidirekt schema" above it
|
|
1156
|
-
pet.ts:52 cannot translate object type "Collar": add "@bidirekt schema" above it
|
|
1157
|
-
pet.ts:55 cannot translate class "Toy": add @BidirektSchema to it
|
|
1158
|
-
pet.ts:58 cannot translate generic type "Page<Pet>": declare a non-generic class with @BidirektSchema and use it as the type
|
|
1159
|
-
pet.ts:61 cannot translate "Record<number, string>": declare its key as string
|
|
1160
|
-
pet.ts:64 cannot translate "string | undefined" to a contract type: declare it with @BidirektField({ type })
|
|
1161
|
-
pet.ts:67 cannot read the type of "tag": write its type or declare it with @BidirektField({ type })
|
|
1162
|
-
```
|
|
1163
|
-
|
|
1164
|
-
```ts
|
|
1165
|
-
// pet.ts
|
|
1166
|
-
export enum Level {
|
|
1167
|
-
Low = 'low',
|
|
1168
|
-
High = 1,
|
|
1169
|
-
}
|
|
1170
|
-
|
|
1171
|
-
export interface Address {
|
|
1172
|
-
street: string
|
|
1173
|
-
}
|
|
1174
|
-
|
|
1175
|
-
export type Collar = {
|
|
1176
|
-
size: string
|
|
1177
|
-
}
|
|
1178
|
-
|
|
1179
|
-
export class Toy {
|
|
1180
|
-
name = ''
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
|
-
export class Page<T> {
|
|
1184
|
-
items: T[] = []
|
|
1185
|
-
}
|
|
1186
|
-
|
|
1187
|
-
/** @bidirekt schema */
|
|
1188
|
-
export class Pet {
|
|
1189
|
-
/** @bidirekt field */
|
|
1190
|
-
code!: string | number
|
|
1191
|
-
|
|
1192
|
-
/** @bidirekt field */
|
|
1193
|
-
rank!: 1 | 2
|
|
1194
|
-
|
|
1195
|
-
/** @bidirekt field */
|
|
1196
|
-
level!: Level
|
|
1197
|
-
|
|
1198
|
-
/** @bidirekt field */
|
|
1199
|
-
extra!: unknown
|
|
1200
|
-
|
|
1201
|
-
/** @bidirekt field */
|
|
1202
|
-
data!: any
|
|
1203
|
-
|
|
1204
|
-
/** @bidirekt field */
|
|
1205
|
-
bornAt!: Date
|
|
1206
|
-
|
|
1207
|
-
/** @bidirekt field */
|
|
1208
|
-
location!: { street: string }
|
|
1209
|
-
|
|
1210
|
-
/** @bidirekt field */
|
|
1211
|
-
address!: Address
|
|
1212
|
-
|
|
1213
|
-
/** @bidirekt field */
|
|
1214
|
-
collar!: Collar
|
|
1215
|
-
|
|
1216
|
-
/** @bidirekt field */
|
|
1217
|
-
toy!: Toy
|
|
1218
|
-
|
|
1219
|
-
/** @bidirekt field */
|
|
1220
|
-
siblings!: Page<Pet>
|
|
1221
|
-
|
|
1222
|
-
/** @bidirekt field */
|
|
1223
|
-
scores!: Record<number, string>
|
|
1224
|
-
|
|
1225
|
-
/** @bidirekt field */
|
|
1226
|
-
nicknames!: Array<string | undefined>
|
|
1227
|
-
|
|
1228
|
-
/** @bidirekt field */
|
|
1229
|
-
tag = 'new'
|
|
1230
|
-
}
|
|
1231
|
-
```
|
|
1232
|
-
|
|
1233
|
-
```text
|
|
1234
|
-
pet.ts:26 cannot translate "string | number" to a contract type: declare it with "@bidirekt field type=<type>"
|
|
1235
|
-
pet.ts:29 cannot translate numeric literal type "1 | 2": use string literals or declare it with "@bidirekt field type=<type>"
|
|
1236
|
-
pet.ts:32 cannot translate enum "Level" with string and numeric members: make all its members strings or all numbers
|
|
1237
|
-
pet.ts:35 cannot translate "unknown" to a contract type: declare it with "@bidirekt field type=<type>"
|
|
1238
|
-
pet.ts:38 cannot translate "any" to a contract type: declare it with "@bidirekt field type=<type>"
|
|
1239
|
-
pet.ts:41 cannot translate "Date" to a contract type: declare it with "@bidirekt field type=<type>"
|
|
1240
|
-
pet.ts:44 cannot translate inline object type "{ street: string }": declare a class with "@bidirekt schema" and use it as the type
|
|
1241
|
-
pet.ts:47 cannot translate interface "Address": add "@bidirekt schema" above it
|
|
1242
|
-
pet.ts:50 cannot translate object type "Collar": add "@bidirekt schema" above it
|
|
1243
|
-
pet.ts:53 cannot translate class "Toy": add "@bidirekt schema" above it
|
|
1244
|
-
pet.ts:56 cannot translate generic type "Page<Pet>": declare a non-generic class with "@bidirekt schema" and use it as the type
|
|
1245
|
-
pet.ts:59 cannot translate "Record<number, string>": declare its key as string
|
|
1246
|
-
pet.ts:62 cannot translate "string | undefined" to a contract type: declare it with "@bidirekt field type=<type>"
|
|
1247
|
-
pet.ts:65 cannot read the type of "tag": write its type or declare it with "@bidirekt field type=<type>"
|
|
1248
|
-
```
|
|
1249
|
-
|
|
1250
|
-
A union of schemas without keys is an error too, see [Unions](#unions).
|
|
1251
|
-
|
|
1252
|
-
A class whose only declaration is a `.d.ts` outside `node_modules`, like the build output a workspace package's `types` point at, has lost its decorators, and stops `generate`:
|
|
1253
|
-
|
|
1254
|
-
```ts
|
|
1255
|
-
// packages/dto/dist/index.d.ts
|
|
1256
|
-
export declare class Owner {
|
|
1257
|
-
name: string
|
|
1258
|
-
}
|
|
1259
|
-
```
|
|
1260
|
-
|
|
1261
|
-
```ts
|
|
1262
|
-
// pet.ts
|
|
1263
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1264
|
-
import { Owner } from './packages/dto/dist/index.js'
|
|
1265
|
-
|
|
1266
|
-
@BidirektSchema()
|
|
1267
|
-
export class Pet {
|
|
1268
|
-
@BidirektField()
|
|
1269
|
-
owner!: Owner
|
|
1270
|
-
}
|
|
1271
|
-
```
|
|
1272
|
-
|
|
1273
|
-
```text
|
|
1274
|
-
pet.ts:8 class "Owner" comes from "packages/dto/dist/index.d.ts", which has no decorators: point the package's "types" or "exports" at its source
|
|
1275
|
-
```
|
|
1276
|
-
|
|
1277
|
-
`@bidirekt schema` and `@bidirekt field` in a hand-written `.d.ts` of the project are read, see [JavaScript](#javascript). A `.d.ts` with its `.js` beside it is build output when the project leaves it out or it sits in a `dist`, `build` or `out` folder, and a tagged class, interface or type from it stops `generate` at the line that uses it, an `extends` too:
|
|
1278
|
-
|
|
1279
|
-
```ts
|
|
1280
|
-
// dist/index.d.ts
|
|
1281
|
-
/** @bidirekt schema */
|
|
1282
|
-
export interface Entity {
|
|
1283
|
-
/** @bidirekt field */
|
|
1284
|
-
id: string
|
|
1285
|
-
}
|
|
1286
|
-
|
|
1287
|
-
/** @bidirekt schema */
|
|
1288
|
-
export type Owner = {
|
|
1289
|
-
/** @bidirekt field */
|
|
1290
|
-
name: string
|
|
1291
|
-
}
|
|
1292
|
-
```
|
|
1293
|
-
|
|
1294
|
-
```js
|
|
1295
|
-
// dist/index.js
|
|
1296
|
-
export {}
|
|
1297
|
-
```
|
|
1298
|
-
|
|
1299
|
-
```ts
|
|
1300
|
-
// pet.ts
|
|
1301
|
-
import type { Entity, Owner } from './dist/index.js'
|
|
1302
|
-
|
|
1303
|
-
/** @bidirekt schema */
|
|
1304
|
-
export interface Pet extends Entity {
|
|
1305
|
-
/** @bidirekt field */
|
|
1306
|
-
owner: Owner
|
|
1307
|
-
}
|
|
1308
|
-
```
|
|
1309
|
-
|
|
1310
|
-
```text
|
|
1311
|
-
pet.ts:5 interface "Entity" comes from "dist/index.d.ts", which is build output, so its "@bidirekt" tags are not read: point the package's "types" or "exports" at its source
|
|
1312
|
-
pet.ts:7 type "Owner" comes from "dist/index.d.ts", which is build output, so its "@bidirekt" tags are not read: point the package's "types" or "exports" at its source
|
|
1313
|
-
```
|
|
1314
|
-
|
|
1315
|
-
### Schemas
|
|
1316
|
-
|
|
1317
|
-
#### Names
|
|
1318
|
-
|
|
1319
|
-
A schema is named after its class, interface or object type; `name` gives it another name in the contract. A tag marks a class, in TypeScript or JavaScript, an interface or an object type:
|
|
1320
|
-
|
|
1321
|
-
```ts
|
|
1322
|
-
// pet.ts
|
|
1323
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1324
|
-
|
|
1325
|
-
@BidirektSchema({ name: 'PetResponse' })
|
|
1326
|
-
export class Pet {
|
|
1327
|
-
@BidirektField()
|
|
1328
|
-
name!: string
|
|
1329
|
-
}
|
|
1330
|
-
```
|
|
1331
|
-
|
|
1332
|
-
```ts
|
|
1333
|
-
// pet.ts
|
|
1334
|
-
/** @bidirekt schema name=PetResponse */
|
|
1335
|
-
export class Pet {
|
|
1336
|
-
/** @bidirekt field */
|
|
1337
|
-
name!: string
|
|
1338
|
-
}
|
|
1339
|
-
```
|
|
1340
|
-
|
|
1341
|
-
```ts
|
|
1342
|
-
// pet.ts
|
|
1343
|
-
/** @bidirekt schema name=PetResponse */
|
|
1344
|
-
export interface Pet {
|
|
1345
|
-
/** @bidirekt field */
|
|
1346
|
-
name: string
|
|
1347
|
-
}
|
|
1348
|
-
```
|
|
1349
|
-
|
|
1350
|
-
```ts
|
|
1351
|
-
// pet.ts
|
|
1352
|
-
/** @bidirekt schema name=PetResponse */
|
|
1353
|
-
export type Pet = {
|
|
1354
|
-
/** @bidirekt field */
|
|
1355
|
-
name: string
|
|
1356
|
-
}
|
|
1357
|
-
```
|
|
1358
|
-
|
|
1359
|
-
```yaml
|
|
1360
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
1361
|
-
schemas:
|
|
1362
|
-
PetResponse:
|
|
1363
|
-
type: object
|
|
1364
|
-
properties:
|
|
1365
|
-
name:
|
|
1366
|
-
type: string
|
|
1367
|
-
```
|
|
1368
|
-
|
|
1369
|
-
Every schema is written, even one no handler references. Two schemas with the same name in the contract stop `generate`; give one of them a `name`. An anonymous class (`export default class`) needs a `name`, and `Page<Pet>` stops `generate` even when `Page` is a schema.
|
|
1370
|
-
|
|
1371
|
-
A schema without a field stops `generate`, and so does a tag above an object type that is only an index signature: that is a map, used as a field's type:
|
|
1372
|
-
|
|
1373
|
-
```ts
|
|
1374
|
-
// pet.ts
|
|
1375
|
-
import { BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1376
|
-
|
|
1377
|
-
@BidirektSchema()
|
|
1378
|
-
export class Pet {
|
|
1379
|
-
name!: string
|
|
1380
|
-
}
|
|
1381
|
-
|
|
1382
|
-
/** @bidirekt schema */
|
|
1383
|
-
export interface Owner {
|
|
1384
|
-
name: string
|
|
1385
|
-
}
|
|
1386
|
-
|
|
1387
|
-
/** @bidirekt schema */
|
|
1388
|
-
export type Labels = { [key: string]: string }
|
|
1389
|
-
```
|
|
1390
|
-
|
|
1391
|
-
```text
|
|
1392
|
-
pet.ts:5 schema "Pet" has no field: annotate at least one field with @BidirektField
|
|
1393
|
-
pet.ts:10 schema "Owner" has no field: annotate at least one field with "@bidirekt field"
|
|
1394
|
-
pet.ts:14 "@bidirekt schema" is above type "Labels", which is a map, not an object: remove the tag and use "Labels" as a field's type
|
|
1395
|
-
```
|
|
1396
|
-
|
|
1397
|
-
#### Enums
|
|
1398
|
-
|
|
1399
|
-
A string enum used by a field is written as a schema named after it. `@bidirekt schema` above the enum gives it another name, and writes it even when no field uses it, so tags can reference it (`@bidirekt response 200 PetStatus`, `type=PetStatus`). An enum without string members can't be a schema, and `generate` warns:
|
|
1400
|
-
|
|
1401
|
-
```ts
|
|
1402
|
-
// status.ts
|
|
1403
|
-
/** @bidirekt schema name=PetStatus */
|
|
1404
|
-
export enum Status {
|
|
1405
|
-
Available = 'available',
|
|
1406
|
-
Sold = 'sold',
|
|
1407
|
-
}
|
|
1408
|
-
|
|
1409
|
-
/** @bidirekt schema */
|
|
1410
|
-
export enum Size {
|
|
1411
|
-
Small = 'small',
|
|
1412
|
-
Large = 'large',
|
|
1413
|
-
}
|
|
1414
|
-
|
|
1415
|
-
/** @bidirekt schema */
|
|
1416
|
-
export enum Priority {
|
|
1417
|
-
Low = 1,
|
|
1418
|
-
High = 2,
|
|
1419
|
-
}
|
|
1420
|
-
```
|
|
1421
|
-
|
|
1422
|
-
```text
|
|
1423
|
-
status.ts:14 warning: "@bidirekt schema" is above enum "Priority", which has no string members, so it is not read: make its members strings
|
|
1424
|
-
```
|
|
1425
|
-
|
|
1426
|
-
<!-- prettier-ignore -->
|
|
1427
|
-
```yaml
|
|
1428
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
1429
|
-
schemas:
|
|
1430
|
-
PetStatus:
|
|
1431
|
-
type: enum
|
|
1432
|
-
members: ["available", "sold"]
|
|
1433
|
-
Size:
|
|
1434
|
-
type: enum
|
|
1435
|
-
members: ["small", "large"]
|
|
1436
|
-
```
|
|
1437
|
-
|
|
1438
|
-
#### Inheritance
|
|
1439
|
-
|
|
1440
|
-
A schema class takes the annotated fields of its parent classes, whether or not they are schemas. A schema interface takes those of the interfaces it extends, and of all its blocks:
|
|
1441
|
-
|
|
1442
|
-
```ts
|
|
1443
|
-
// pet.ts
|
|
1444
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1445
|
-
|
|
1446
|
-
export class Entity {
|
|
1447
|
-
@BidirektField()
|
|
1448
|
-
id!: string
|
|
1449
|
-
}
|
|
1450
|
-
|
|
1451
|
-
@BidirektSchema()
|
|
1452
|
-
export class Pet extends Entity {
|
|
1453
|
-
@BidirektField()
|
|
1454
|
-
name!: string
|
|
1455
|
-
}
|
|
1456
|
-
|
|
1457
|
-
@BidirektSchema()
|
|
1458
|
-
export class Dog extends Pet {
|
|
1459
|
-
@BidirektField()
|
|
1460
|
-
breed!: string
|
|
1461
|
-
}
|
|
1462
|
-
```
|
|
1463
|
-
|
|
1464
|
-
```ts
|
|
1465
|
-
// pet.ts
|
|
1466
|
-
export class Entity {
|
|
1467
|
-
/** @bidirekt field */
|
|
1468
|
-
id!: string
|
|
1469
|
-
}
|
|
1470
|
-
|
|
1471
|
-
/** @bidirekt schema */
|
|
1472
|
-
export class Pet extends Entity {
|
|
1473
|
-
/** @bidirekt field */
|
|
1474
|
-
name!: string
|
|
1475
|
-
}
|
|
1476
|
-
|
|
1477
|
-
/** @bidirekt schema */
|
|
1478
|
-
export class Dog extends Pet {
|
|
1479
|
-
/** @bidirekt field */
|
|
1480
|
-
breed!: string
|
|
1481
|
-
}
|
|
1482
|
-
```
|
|
1483
|
-
|
|
1484
|
-
```ts
|
|
1485
|
-
// pet.ts
|
|
1486
|
-
export interface Entity {
|
|
1487
|
-
/** @bidirekt field */
|
|
1488
|
-
id: string
|
|
1489
|
-
}
|
|
1490
|
-
|
|
1491
|
-
/** @bidirekt schema */
|
|
1492
|
-
export interface Pet extends Entity {}
|
|
1493
|
-
|
|
1494
|
-
export interface Pet {
|
|
1495
|
-
/** @bidirekt field */
|
|
1496
|
-
name: string
|
|
1497
|
-
}
|
|
1498
|
-
|
|
1499
|
-
/** @bidirekt schema */
|
|
1500
|
-
export interface Dog extends Pet {
|
|
1501
|
-
/** @bidirekt field */
|
|
1502
|
-
breed: string
|
|
1503
|
-
}
|
|
1504
|
-
```
|
|
1505
|
-
|
|
1506
|
-
```yaml
|
|
1507
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
1508
|
-
schemas:
|
|
1509
|
-
Dog:
|
|
1510
|
-
type: object
|
|
1511
|
-
properties:
|
|
1512
|
-
id:
|
|
1513
|
-
type: string
|
|
1514
|
-
name:
|
|
1515
|
-
type: string
|
|
1516
|
-
breed:
|
|
1517
|
-
type: string
|
|
1518
|
-
Pet:
|
|
1519
|
-
type: object
|
|
1520
|
-
properties:
|
|
1521
|
-
id:
|
|
1522
|
-
type: string
|
|
1523
|
-
name:
|
|
1524
|
-
type: string
|
|
1525
|
-
```
|
|
1526
|
-
|
|
1527
|
-
A parent `generate` can't read, like a mixin or a utility type, stops it; declare the fields in the schema:
|
|
1528
|
-
|
|
1529
|
-
```ts
|
|
1530
|
-
// pet.ts
|
|
1531
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1532
|
-
|
|
1533
|
-
type Constructor = new (...args: any[]) => object
|
|
1534
|
-
|
|
1535
|
-
function Timestamped<T extends Constructor>(base: T) {
|
|
1536
|
-
return class extends base {
|
|
1537
|
-
createdAt = ''
|
|
1538
|
-
}
|
|
1539
|
-
}
|
|
1540
|
-
|
|
1541
|
-
@BidirektSchema()
|
|
1542
|
-
export class Pet {
|
|
1543
|
-
@BidirektField()
|
|
1544
|
-
name!: string
|
|
1545
|
-
}
|
|
1546
|
-
|
|
1547
|
-
@BidirektSchema()
|
|
1548
|
-
export class Dog extends Timestamped(Pet) {
|
|
1549
|
-
@BidirektField()
|
|
1550
|
-
breed!: string
|
|
1551
|
-
}
|
|
1552
|
-
```
|
|
1553
|
-
|
|
1554
|
-
```text
|
|
1555
|
-
pet.ts:19 cannot read the parent "Timestamped(Pet)" of class "Dog": declare its fields
|
|
1556
|
-
```
|
|
1557
|
-
|
|
1558
|
-
```ts
|
|
1559
|
-
// pet.ts
|
|
1560
|
-
/** @bidirekt schema */
|
|
1561
|
-
export interface Pet {
|
|
1562
|
-
/** @bidirekt field */
|
|
1563
|
-
name: string
|
|
1564
|
-
|
|
1565
|
-
/** @bidirekt field */
|
|
1566
|
-
secret: string
|
|
1567
|
-
}
|
|
1568
|
-
|
|
1569
|
-
/** @bidirekt schema */
|
|
1570
|
-
export interface PublicPet extends Omit<Pet, 'secret'> {
|
|
1571
|
-
/** @bidirekt field */
|
|
1572
|
-
breed: string
|
|
1573
|
-
}
|
|
1574
|
-
```
|
|
1575
|
-
|
|
1576
|
-
```text
|
|
1577
|
-
pet.ts:12 cannot read the parent "Omit<Pet, 'secret'>" of interface "PublicPet": declare its fields
|
|
1578
|
-
```
|
|
1579
|
-
|
|
1580
|
-
### Unions
|
|
1581
|
-
|
|
1582
|
-
A field typed by a union of schemas declares the key of each member in `union`, also when the union is the items of a list or the values of a map. A tag names each member by its name in the contract. `null` and `undefined` in the type still make the field, its items or its values `nullable` or `optional`:
|
|
1583
|
-
|
|
1584
|
-
```ts
|
|
1585
|
-
// order.ts
|
|
1586
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1587
|
-
|
|
1588
|
-
@BidirektSchema()
|
|
1589
|
-
export class CardPayment {
|
|
1590
|
-
@BidirektField()
|
|
1591
|
-
last4!: string
|
|
1592
|
-
}
|
|
1593
|
-
|
|
1594
|
-
@BidirektSchema()
|
|
1595
|
-
export class PixPayment {
|
|
1596
|
-
@BidirektField()
|
|
1597
|
-
key!: string
|
|
1598
|
-
}
|
|
1599
|
-
|
|
1600
|
-
@BidirektSchema()
|
|
1601
|
-
export class Order {
|
|
1602
|
-
@BidirektField({ union: { card: CardPayment, pix: PixPayment } })
|
|
1603
|
-
payment!: CardPayment | PixPayment
|
|
1604
|
-
|
|
1605
|
-
@BidirektField({ union: { card: CardPayment, pix: PixPayment } })
|
|
1606
|
-
refund?: CardPayment | PixPayment | null
|
|
1607
|
-
|
|
1608
|
-
@BidirektField({ union: { card: CardPayment, pix: PixPayment } })
|
|
1609
|
-
history!: Array<CardPayment | PixPayment>
|
|
1610
|
-
|
|
1611
|
-
@BidirektField({ union: { card: CardPayment, pix: PixPayment } })
|
|
1612
|
-
byMethod!: Record<string, CardPayment | PixPayment | null>
|
|
1613
|
-
}
|
|
1614
|
-
```
|
|
1615
|
-
|
|
1616
|
-
```ts
|
|
1617
|
-
// order.ts
|
|
1618
|
-
/** @bidirekt schema */
|
|
1619
|
-
export interface CardPayment {
|
|
1620
|
-
/** @bidirekt field */
|
|
1621
|
-
last4: string
|
|
1622
|
-
}
|
|
1623
|
-
|
|
1624
|
-
/** @bidirekt schema */
|
|
1625
|
-
export interface PixPayment {
|
|
1626
|
-
/** @bidirekt field */
|
|
1627
|
-
key: string
|
|
1628
|
-
}
|
|
1629
|
-
|
|
1630
|
-
/** @bidirekt schema */
|
|
1631
|
-
export interface Order {
|
|
1632
|
-
/** @bidirekt field union={card: CardPayment, pix: PixPayment} */
|
|
1633
|
-
payment: CardPayment | PixPayment
|
|
1634
|
-
|
|
1635
|
-
/** @bidirekt field union={card: CardPayment, pix: PixPayment} */
|
|
1636
|
-
refund?: CardPayment | PixPayment | null
|
|
1637
|
-
|
|
1638
|
-
/** @bidirekt field union={card:CardPayment,pix:PixPayment} */
|
|
1639
|
-
history: Array<CardPayment | PixPayment>
|
|
1640
|
-
|
|
1641
|
-
/** @bidirekt field union={card: CardPayment, pix: PixPayment} */
|
|
1642
|
-
byMethod: Record<string, CardPayment | PixPayment | null>
|
|
1643
|
-
}
|
|
1644
|
-
```
|
|
1645
|
-
|
|
1646
|
-
```yaml
|
|
1647
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
1648
|
-
schemas:
|
|
1649
|
-
CardPayment:
|
|
1650
|
-
type: object
|
|
1651
|
-
properties:
|
|
1652
|
-
last4:
|
|
1653
|
-
type: string
|
|
1654
|
-
Order:
|
|
1655
|
-
type: object
|
|
1656
|
-
properties:
|
|
1657
|
-
payment:
|
|
1658
|
-
type: union
|
|
1659
|
-
members:
|
|
1660
|
-
card:
|
|
1661
|
-
ref: CardPayment
|
|
1662
|
-
pix:
|
|
1663
|
-
ref: PixPayment
|
|
1664
|
-
refund:
|
|
1665
|
-
type: union
|
|
1666
|
-
members:
|
|
1667
|
-
card:
|
|
1668
|
-
ref: CardPayment
|
|
1669
|
-
pix:
|
|
1670
|
-
ref: PixPayment
|
|
1671
|
-
optional: true
|
|
1672
|
-
nullable: true
|
|
1673
|
-
history:
|
|
1674
|
-
type: array
|
|
1675
|
-
items:
|
|
1676
|
-
type: union
|
|
1677
|
-
members:
|
|
1678
|
-
card:
|
|
1679
|
-
ref: CardPayment
|
|
1680
|
-
pix:
|
|
1681
|
-
ref: PixPayment
|
|
1682
|
-
byMethod:
|
|
1683
|
-
type: map
|
|
1684
|
-
values:
|
|
1685
|
-
type: union
|
|
1686
|
-
members:
|
|
1687
|
-
card:
|
|
1688
|
-
ref: CardPayment
|
|
1689
|
-
pix:
|
|
1690
|
-
ref: PixPayment
|
|
1691
|
-
nullable: true
|
|
1692
|
-
PixPayment:
|
|
1693
|
-
type: object
|
|
1694
|
-
properties:
|
|
1695
|
-
key:
|
|
1696
|
-
type: string
|
|
1697
|
-
```
|
|
1698
|
-
|
|
1699
|
-
Spaces inside `union={...}` are optional. Without `union`, a union of schemas stops `generate`:
|
|
1700
|
-
|
|
1701
|
-
```ts
|
|
1702
|
-
// order.ts
|
|
1703
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1704
|
-
|
|
1705
|
-
@BidirektSchema()
|
|
1706
|
-
export class CardPayment {
|
|
1707
|
-
@BidirektField()
|
|
1708
|
-
last4!: string
|
|
1709
|
-
}
|
|
1710
|
-
|
|
1711
|
-
@BidirektSchema()
|
|
1712
|
-
export class PixPayment {
|
|
1713
|
-
@BidirektField()
|
|
1714
|
-
key!: string
|
|
1715
|
-
}
|
|
1716
|
-
|
|
1717
|
-
@BidirektSchema()
|
|
1718
|
-
export class Order {
|
|
1719
|
-
@BidirektField()
|
|
1720
|
-
payment!: CardPayment | PixPayment
|
|
1721
|
-
}
|
|
1722
|
-
```
|
|
1723
|
-
|
|
1724
|
-
```text
|
|
1725
|
-
order.ts:19 cannot translate union of classes "CardPayment | PixPayment": declare each member's key with @BidirektField({ union })
|
|
1726
|
-
```
|
|
1727
|
-
|
|
1728
|
-
```ts
|
|
1729
|
-
// order.ts
|
|
1730
|
-
/** @bidirekt schema */
|
|
1731
|
-
export interface CardPayment {
|
|
1732
|
-
/** @bidirekt field */
|
|
1733
|
-
last4: string
|
|
1734
|
-
}
|
|
1735
|
-
|
|
1736
|
-
/** @bidirekt schema */
|
|
1737
|
-
export interface PixPayment {
|
|
1738
|
-
/** @bidirekt field */
|
|
1739
|
-
key: string
|
|
1740
|
-
}
|
|
1741
|
-
|
|
1742
|
-
/** @bidirekt schema */
|
|
1743
|
-
export interface Order {
|
|
1744
|
-
/** @bidirekt field */
|
|
1745
|
-
payment: CardPayment | PixPayment
|
|
1746
|
-
}
|
|
1747
|
-
```
|
|
1748
|
-
|
|
1749
|
-
```text
|
|
1750
|
-
order.ts:17 cannot translate union of classes "CardPayment | PixPayment": declare each member's key with "@bidirekt field union={<key>: <schema>, ...}"
|
|
1751
|
-
```
|
|
1752
|
-
|
|
1753
|
-
The keys match the type: each schema of the union has one key, and each key names a schema of the union. A tag's `union=` is checked the same way:
|
|
1754
|
-
|
|
1755
|
-
```ts
|
|
1756
|
-
// order.ts
|
|
1757
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1758
|
-
|
|
1759
|
-
@BidirektSchema()
|
|
1760
|
-
export class CardPayment {
|
|
1761
|
-
@BidirektField()
|
|
1762
|
-
last4!: string
|
|
1763
|
-
}
|
|
1764
|
-
|
|
1765
|
-
@BidirektSchema()
|
|
1766
|
-
export class PixPayment {
|
|
1767
|
-
@BidirektField()
|
|
1768
|
-
key!: string
|
|
1769
|
-
}
|
|
1770
|
-
|
|
1771
|
-
@BidirektSchema()
|
|
1772
|
-
export class BoletoPayment {
|
|
1773
|
-
@BidirektField()
|
|
1774
|
-
code!: string
|
|
1775
|
-
}
|
|
1776
|
-
|
|
1777
|
-
@BidirektSchema()
|
|
1778
|
-
export class Order {
|
|
1779
|
-
@BidirektField({ union: { card: CardPayment, boleto: BoletoPayment } })
|
|
1780
|
-
payment!: CardPayment | PixPayment
|
|
1781
|
-
|
|
1782
|
-
@BidirektField({
|
|
1783
|
-
union: { card: CardPayment, debit: CardPayment, pix: PixPayment },
|
|
1784
|
-
})
|
|
1785
|
-
refund!: CardPayment | PixPayment
|
|
1786
|
-
}
|
|
1787
|
-
```
|
|
1788
|
-
|
|
1789
|
-
```text
|
|
1790
|
-
order.ts:25 the "union" of @BidirektField lists "BoletoPayment", which is not in "CardPayment | PixPayment": remove its key or add it to the type
|
|
1791
|
-
order.ts:30 the "union" of @BidirektField lists "CardPayment" more than once: keep one key for it
|
|
1792
|
-
```
|
|
1793
|
-
|
|
1794
|
-
A member of `union=` is a schema name, never a list or a map:
|
|
1795
|
-
|
|
1796
|
-
```ts
|
|
1797
|
-
// order.ts
|
|
1798
|
-
/** @bidirekt schema */
|
|
1799
|
-
export interface Card {
|
|
1800
|
-
/** @bidirekt field */
|
|
1801
|
-
last4: string
|
|
1802
|
-
}
|
|
1803
|
-
|
|
1804
|
-
/** @bidirekt schema */
|
|
1805
|
-
export interface Order {
|
|
1806
|
-
/** @bidirekt field union={card: Card[]} */
|
|
1807
|
-
payment: Card
|
|
1808
|
-
}
|
|
1809
|
-
```
|
|
1810
|
-
|
|
1811
|
-
```text
|
|
1812
|
-
order.ts:10 cannot read "union={card: Card[]}" in "@bidirekt field": write "union={<key>: <schema>, ...}"
|
|
1813
|
-
```
|
|
1814
|
-
|
|
1815
|
-
### Handlers
|
|
1816
|
-
|
|
1817
|
-
A handler declares one resource: its method, in uppercase (`GET`, `POST`, `PUT`, `PATCH`, `DELETE`), and its path as the contract writes it, with a `*` for each dynamic segment (`/pets/*` for `/pets/:id`). The request and each response status take a body:
|
|
1818
|
-
|
|
1819
|
-
| Body | Decorator | Tag | Contract |
|
|
1820
|
-
| ------------------------ | -------------------------------------- | ------------------------------------- | --------------- |
|
|
1821
|
-
| a schema | `Pet` | `Pet` | `Pet` |
|
|
1822
|
-
| a list | `[Pet]` | `Pet[]` | `PetList` |
|
|
1823
|
-
| a list with its own name | `{ list: Pet, name: 'PetCollection' }` | `Pet[] as PetCollection` | `PetCollection` |
|
|
1824
|
-
| no body | `null` | nothing after the status or `request` | `~` |
|
|
1825
|
-
|
|
1826
|
-
Without a request (no `request` option, no `@bidirekt request` tag), the resource has no `request` key. A tag names a schema by its name in the contract, which is not always its name in the code. A list is written once however many handlers use it.
|
|
1827
|
-
|
|
1828
|
-
In a tag, write a `*` before another segment as `*\/` (`/pets/*\/photos`): a bare `*/` would end the comment. `generate` reads `*\/` as `*/`:
|
|
1829
|
-
|
|
1830
|
-
```ts
|
|
1831
|
-
// photos.ts
|
|
1832
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
1833
|
-
|
|
1834
|
-
@BidirektSchema()
|
|
1835
|
-
export class Photo {
|
|
1836
|
-
@BidirektField()
|
|
1837
|
-
url!: string
|
|
1838
|
-
}
|
|
1839
|
-
|
|
1840
|
-
/**
|
|
1841
|
-
* @bidirekt provides GET /pets/*\/photos
|
|
1842
|
-
* @bidirekt response 200 Photo[]
|
|
1843
|
-
*/
|
|
1844
|
-
export function listPhotos() {}
|
|
1845
|
-
```
|
|
1846
|
-
|
|
1847
|
-
```yaml
|
|
1848
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
1849
|
-
provides:
|
|
1850
|
-
rest:
|
|
1851
|
-
/pets/*/photos:
|
|
1852
|
-
get:
|
|
1853
|
-
responses:
|
|
1854
|
-
200: PhotoList
|
|
1855
|
-
schemas:
|
|
1856
|
-
Photo:
|
|
1857
|
-
type: object
|
|
1858
|
-
properties:
|
|
1859
|
-
url:
|
|
1860
|
-
type: string
|
|
1861
|
-
PhotoList:
|
|
1862
|
-
type: array
|
|
1863
|
-
items:
|
|
1864
|
-
ref: Photo
|
|
1865
|
-
```
|
|
1866
|
-
|
|
1867
|
-
#### Providers
|
|
1868
|
-
|
|
1869
|
-
`@BidirektProvider` goes on a class method; `@bidirekt provides <METHOD> <path>` goes in the comment above a function, an exported arrow function, a method or a statement like `router.get(...)`:
|
|
1870
|
-
|
|
1871
|
-
```ts
|
|
1872
|
-
// pets.ts
|
|
1873
|
-
import {
|
|
1874
|
-
BidirektField,
|
|
1875
|
-
BidirektProvider,
|
|
1876
|
-
BidirektSchema,
|
|
1877
|
-
} from '@bidirekt/typescript-sdk'
|
|
1878
|
-
|
|
1879
|
-
@BidirektSchema()
|
|
1880
|
-
export class Pet {
|
|
1881
|
-
@BidirektField()
|
|
1882
|
-
name!: string
|
|
1883
|
-
}
|
|
1884
|
-
|
|
1885
|
-
@BidirektSchema()
|
|
1886
|
-
export class NewPet {
|
|
1887
|
-
@BidirektField()
|
|
1888
|
-
name!: string
|
|
1889
|
-
}
|
|
1890
|
-
|
|
1891
|
-
export class PetsController {
|
|
1892
|
-
@BidirektProvider({ method: 'GET', path: '/pets', responses: { 200: [Pet] } })
|
|
1893
|
-
list() {}
|
|
1894
|
-
|
|
1895
|
-
@BidirektProvider({
|
|
1896
|
-
method: 'GET',
|
|
1897
|
-
path: '/pets/featured',
|
|
1898
|
-
responses: { 200: { list: Pet, name: 'PetCollection' } },
|
|
1899
|
-
})
|
|
1900
|
-
featured() {}
|
|
1901
|
-
|
|
1902
|
-
@BidirektProvider({
|
|
1903
|
-
method: 'GET',
|
|
1904
|
-
path: '/pets/*',
|
|
1905
|
-
responses: { 200: Pet, 404: null },
|
|
1906
|
-
})
|
|
1907
|
-
find() {}
|
|
1908
|
-
|
|
1909
|
-
@BidirektProvider({
|
|
1910
|
-
method: 'POST',
|
|
1911
|
-
path: '/pets',
|
|
1912
|
-
request: NewPet,
|
|
1913
|
-
responses: { 201: Pet, 400: null },
|
|
1914
|
-
})
|
|
1915
|
-
create() {}
|
|
1916
|
-
|
|
1917
|
-
@BidirektProvider({
|
|
1918
|
-
method: 'PUT',
|
|
1919
|
-
path: '/pets/*',
|
|
1920
|
-
request: null,
|
|
1921
|
-
responses: { 204: null },
|
|
1922
|
-
})
|
|
1923
|
-
touch() {}
|
|
1924
|
-
|
|
1925
|
-
@BidirektProvider({
|
|
1926
|
-
method: 'DELETE',
|
|
1927
|
-
path: '/pets/*',
|
|
1928
|
-
responses: { 204: null },
|
|
1929
|
-
})
|
|
1930
|
-
remove() {}
|
|
1931
|
-
}
|
|
1932
|
-
```
|
|
1933
|
-
|
|
1934
|
-
```ts
|
|
1935
|
-
// pets.ts
|
|
1936
|
-
/** @bidirekt schema */
|
|
1937
|
-
export interface Pet {
|
|
1938
|
-
/** @bidirekt field */
|
|
1939
|
-
name: string
|
|
1940
|
-
}
|
|
1941
|
-
|
|
1942
|
-
/** @bidirekt schema */
|
|
1943
|
-
export interface NewPet {
|
|
1944
|
-
/** @bidirekt field */
|
|
1945
|
-
name: string
|
|
1946
|
-
}
|
|
1947
|
-
|
|
1948
|
-
/**
|
|
1949
|
-
* @bidirekt provides GET /pets
|
|
1950
|
-
* @bidirekt response 200 Pet[]
|
|
1951
|
-
*/
|
|
1952
|
-
export function listPets() {}
|
|
1953
|
-
|
|
1954
|
-
/**
|
|
1955
|
-
* @bidirekt provides GET /pets/featured
|
|
1956
|
-
* @bidirekt response 200 Pet[] as PetCollection
|
|
1957
|
-
*/
|
|
1958
|
-
export function featuredPets() {}
|
|
1959
|
-
|
|
1960
|
-
/**
|
|
1961
|
-
* @bidirekt provides GET /pets/*
|
|
1962
|
-
* @bidirekt response 200 Pet
|
|
1963
|
-
* @bidirekt response 404
|
|
1964
|
-
*/
|
|
1965
|
-
export const findPet = (id: string) => id
|
|
1966
|
-
|
|
1967
|
-
/**
|
|
1968
|
-
* @bidirekt provides POST /pets
|
|
1969
|
-
* @bidirekt request NewPet
|
|
1970
|
-
* @bidirekt response 201 Pet
|
|
1971
|
-
* @bidirekt response 400
|
|
1972
|
-
*/
|
|
1973
|
-
export function createPet(body: NewPet) {
|
|
1974
|
-
return body
|
|
1975
|
-
}
|
|
1976
|
-
|
|
1977
|
-
/**
|
|
1978
|
-
* @bidirekt provides PUT /pets/*
|
|
1979
|
-
* @bidirekt request
|
|
1980
|
-
* @bidirekt response 204
|
|
1981
|
-
*/
|
|
1982
|
-
export function touchPet() {}
|
|
1983
|
-
|
|
1984
|
-
/**
|
|
1985
|
-
* @bidirekt provides DELETE /pets/*
|
|
1986
|
-
* @bidirekt response 204
|
|
1987
|
-
*/
|
|
1988
|
-
export default function removePet() {}
|
|
1989
|
-
```
|
|
1990
|
-
|
|
1991
|
-
```yaml
|
|
1992
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
1993
|
-
provides:
|
|
1994
|
-
rest:
|
|
1995
|
-
/pets:
|
|
1996
|
-
get:
|
|
1997
|
-
responses:
|
|
1998
|
-
200: PetList
|
|
1999
|
-
post:
|
|
2000
|
-
request: NewPet
|
|
2001
|
-
responses:
|
|
2002
|
-
201: Pet
|
|
2003
|
-
400: ~
|
|
2004
|
-
/pets/*:
|
|
2005
|
-
delete:
|
|
2006
|
-
responses:
|
|
2007
|
-
204: ~
|
|
2008
|
-
get:
|
|
2009
|
-
responses:
|
|
2010
|
-
200: Pet
|
|
2011
|
-
404: ~
|
|
2012
|
-
put:
|
|
2013
|
-
request: ~
|
|
2014
|
-
responses:
|
|
2015
|
-
204: ~
|
|
2016
|
-
/pets/featured:
|
|
2017
|
-
get:
|
|
2018
|
-
responses:
|
|
2019
|
-
200: PetCollection
|
|
2020
|
-
schemas:
|
|
2021
|
-
NewPet:
|
|
2022
|
-
type: object
|
|
2023
|
-
properties:
|
|
2024
|
-
name:
|
|
2025
|
-
type: string
|
|
2026
|
-
Pet:
|
|
2027
|
-
type: object
|
|
2028
|
-
properties:
|
|
2029
|
-
name:
|
|
2030
|
-
type: string
|
|
2031
|
-
PetCollection:
|
|
2032
|
-
type: array
|
|
2033
|
-
items:
|
|
2034
|
-
ref: Pet
|
|
2035
|
-
PetList:
|
|
2036
|
-
type: array
|
|
2037
|
-
items:
|
|
2038
|
-
ref: Pet
|
|
2039
|
-
```
|
|
2040
|
-
|
|
2041
|
-
The same method and path provided by two handlers stops `generate`.
|
|
2042
|
-
|
|
2043
|
-
#### Consumers
|
|
2044
|
-
|
|
2045
|
-
`@BidirektConsumer` and `@bidirekt consumes <provider> <METHOD> <path>` also name the provider:
|
|
2046
|
-
|
|
2047
|
-
```ts
|
|
2048
|
-
// payments.ts
|
|
2049
|
-
import {
|
|
2050
|
-
BidirektConsumer,
|
|
2051
|
-
BidirektField,
|
|
2052
|
-
BidirektSchema,
|
|
2053
|
-
} from '@bidirekt/typescript-sdk'
|
|
2054
|
-
|
|
2055
|
-
@BidirektSchema()
|
|
2056
|
-
export class Charge {
|
|
2057
|
-
@BidirektField()
|
|
2058
|
-
id!: string
|
|
2059
|
-
}
|
|
2060
|
-
|
|
2061
|
-
@BidirektSchema()
|
|
2062
|
-
export class NewCharge {
|
|
2063
|
-
@BidirektField({ type: 'integer' })
|
|
2064
|
-
amount!: number
|
|
2065
|
-
}
|
|
2066
|
-
|
|
2067
|
-
export class PaymentsClient {
|
|
2068
|
-
@BidirektConsumer({
|
|
2069
|
-
provider: 'payments_api',
|
|
2070
|
-
method: 'POST',
|
|
2071
|
-
path: '/charges',
|
|
2072
|
-
request: NewCharge,
|
|
2073
|
-
responses: { 201: Charge, 402: null },
|
|
2074
|
-
})
|
|
2075
|
-
charge() {}
|
|
2076
|
-
|
|
2077
|
-
@BidirektConsumer({
|
|
2078
|
-
provider: 'payments_api',
|
|
2079
|
-
method: 'GET',
|
|
2080
|
-
path: '/charges/*',
|
|
2081
|
-
responses: { 200: Charge },
|
|
2082
|
-
})
|
|
2083
|
-
find() {}
|
|
2084
|
-
}
|
|
2085
|
-
```
|
|
2086
|
-
|
|
2087
|
-
```ts
|
|
2088
|
-
// payments.ts
|
|
2089
|
-
/** @bidirekt schema */
|
|
2090
|
-
export interface Charge {
|
|
2091
|
-
/** @bidirekt field */
|
|
2092
|
-
id: string
|
|
2093
|
-
}
|
|
2094
|
-
|
|
2095
|
-
/** @bidirekt schema */
|
|
2096
|
-
export interface NewCharge {
|
|
2097
|
-
/** @bidirekt field type=integer */
|
|
2098
|
-
amount: number
|
|
2099
|
-
}
|
|
2100
|
-
|
|
2101
|
-
/**
|
|
2102
|
-
* @bidirekt consumes payments_api POST /charges
|
|
2103
|
-
* @bidirekt request NewCharge
|
|
2104
|
-
* @bidirekt response 201 Charge
|
|
2105
|
-
* @bidirekt response 402
|
|
2106
|
-
*/
|
|
2107
|
-
export function createCharge(body: NewCharge) {
|
|
2108
|
-
return body
|
|
2109
|
-
}
|
|
2110
|
-
|
|
2111
|
-
/**
|
|
2112
|
-
* @bidirekt consumes payments_api GET /charges/*
|
|
2113
|
-
* @bidirekt response 200 Charge
|
|
2114
|
-
*/
|
|
2115
|
-
export function findCharge(id: string) {
|
|
2116
|
-
return id
|
|
2117
|
-
}
|
|
2118
|
-
```
|
|
2119
|
-
|
|
2120
|
-
```yaml
|
|
2121
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
2122
|
-
consumes:
|
|
2123
|
-
payments_api:
|
|
2124
|
-
rest:
|
|
2125
|
-
/charges:
|
|
2126
|
-
post:
|
|
2127
|
-
request: NewCharge
|
|
2128
|
-
responses:
|
|
2129
|
-
201: Charge
|
|
2130
|
-
402: ~
|
|
2131
|
-
/charges/*:
|
|
2132
|
-
get:
|
|
2133
|
-
responses:
|
|
2134
|
-
200: Charge
|
|
2135
|
-
schemas:
|
|
2136
|
-
Charge:
|
|
2137
|
-
type: object
|
|
2138
|
-
properties:
|
|
2139
|
-
id:
|
|
2140
|
-
type: string
|
|
2141
|
-
NewCharge:
|
|
2142
|
-
type: object
|
|
2143
|
-
properties:
|
|
2144
|
-
amount:
|
|
2145
|
-
type: integer
|
|
2146
|
-
```
|
|
2147
|
-
|
|
2148
|
-
The same resource consumed by several handlers, with decorators, tags or both, is written once with the statuses of all of them. They declare the same request, and the same schema for a status they share; otherwise `generate` stops, pointing at each handler:
|
|
2149
|
-
|
|
2150
|
-
```ts
|
|
2151
|
-
// checkout.ts
|
|
2152
|
-
import {
|
|
2153
|
-
BidirektConsumer,
|
|
2154
|
-
BidirektField,
|
|
2155
|
-
BidirektSchema,
|
|
2156
|
-
} from '@bidirekt/typescript-sdk'
|
|
2157
|
-
|
|
2158
|
-
@BidirektSchema()
|
|
2159
|
-
export class Charge {
|
|
2160
|
-
@BidirektField()
|
|
2161
|
-
id!: string
|
|
2162
|
-
}
|
|
2163
|
-
|
|
2164
|
-
export class CheckoutClient {
|
|
2165
|
-
@BidirektConsumer({
|
|
2166
|
-
provider: 'payments_api',
|
|
2167
|
-
method: 'POST',
|
|
2168
|
-
path: '/charges',
|
|
2169
|
-
request: null,
|
|
2170
|
-
responses: { 201: Charge, 402: null },
|
|
2171
|
-
})
|
|
2172
|
-
charge() {}
|
|
2173
|
-
}
|
|
2174
|
-
```
|
|
2175
|
-
|
|
2176
|
-
```ts
|
|
2177
|
-
// retry.ts
|
|
2178
|
-
/**
|
|
2179
|
-
* @bidirekt consumes payments_api POST /charges
|
|
2180
|
-
* @bidirekt request
|
|
2181
|
-
* @bidirekt response 201 Charge
|
|
2182
|
-
* @bidirekt response 409
|
|
2183
|
-
*/
|
|
2184
|
-
export function retryCharge() {}
|
|
2185
|
-
```
|
|
2186
|
-
|
|
2187
|
-
```yaml
|
|
2188
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
2189
|
-
consumes:
|
|
2190
|
-
payments_api:
|
|
2191
|
-
rest:
|
|
2192
|
-
/charges:
|
|
2193
|
-
post:
|
|
2194
|
-
request: ~
|
|
2195
|
-
responses:
|
|
2196
|
-
201: Charge
|
|
2197
|
-
402: ~
|
|
2198
|
-
409: ~
|
|
2199
|
-
schemas:
|
|
2200
|
-
Charge:
|
|
2201
|
-
type: object
|
|
2202
|
-
properties:
|
|
2203
|
-
id:
|
|
2204
|
-
type: string
|
|
2205
|
-
```
|
|
2206
|
-
|
|
2207
|
-
#### Mistakes `generate` stops at
|
|
2208
|
-
|
|
2209
|
-
A method in lowercase, a status with a leading zero, and a decorator and a tag on the same handler, schema or field:
|
|
2210
|
-
|
|
2211
|
-
```ts
|
|
2212
|
-
// pets.ts
|
|
2213
|
-
import { BidirektProvider } from '@bidirekt/typescript-sdk'
|
|
2214
|
-
|
|
2215
|
-
/**
|
|
2216
|
-
* @bidirekt provides get /pets
|
|
2217
|
-
* @bidirekt response 200
|
|
2218
|
-
*/
|
|
2219
|
-
export function listPets() {}
|
|
2220
|
-
|
|
2221
|
-
/**
|
|
2222
|
-
* @bidirekt provides POST /pets
|
|
2223
|
-
* @bidirekt response 0201
|
|
2224
|
-
*/
|
|
2225
|
-
export function createPet() {}
|
|
2226
|
-
|
|
2227
|
-
export class PetsController {
|
|
2228
|
-
/**
|
|
2229
|
-
* @bidirekt provides GET /pets/*
|
|
2230
|
-
* @bidirekt response 200
|
|
2231
|
-
*/
|
|
2232
|
-
@BidirektProvider({
|
|
2233
|
-
method: 'GET',
|
|
2234
|
-
path: '/pets/*',
|
|
2235
|
-
responses: { 200: null },
|
|
2236
|
-
})
|
|
2237
|
-
find() {}
|
|
2238
|
-
}
|
|
2239
|
-
```
|
|
2240
|
-
|
|
2241
|
-
```text
|
|
2242
|
-
pets.ts:5 method "get" must be uppercase: write "GET"
|
|
2243
|
-
pets.ts:12 response status "0201" must be written as 201
|
|
2244
|
-
pets.ts:18 @BidirektProvider and "@bidirekt provides GET /pets/*" are both on this handler: keep one
|
|
2245
|
-
pets.ts:21 @BidirektProvider and "@bidirekt provides GET /pets/*" are both on this handler: keep one
|
|
2246
|
-
```
|
|
2247
|
-
|
|
2248
|
-
A decorator status written as a string with a leading zero, like `'0201'`, gets the same error.
|
|
2249
|
-
|
|
2250
|
-
### JavaScript
|
|
2251
|
-
|
|
2252
|
-
In JavaScript, a schema is a class with tags. A field's type comes from `type=` or its JSDoc `@type`, never from its initial value; `@type` reads a `@typedef`, `import('./owner.js').Owner`, `?T` (`T | null`), `!T` (`T`) and `Object.<string, T>` (a map):
|
|
2253
|
-
|
|
2254
|
-
```js
|
|
2255
|
-
// owner.js
|
|
2256
|
-
/** @bidirekt schema */
|
|
2257
|
-
export class Owner {
|
|
2258
|
-
/** @bidirekt field
|
|
2259
|
-
* @type {string} */
|
|
2260
|
-
name
|
|
2261
|
-
}
|
|
2262
|
-
```
|
|
2263
|
-
|
|
2264
|
-
```js
|
|
2265
|
-
// pet.js
|
|
2266
|
-
/** @typedef {'small' | 'large'} Size */
|
|
2267
|
-
|
|
2268
|
-
/** @bidirekt schema */
|
|
2269
|
-
export class Pet {
|
|
2270
|
-
/** @bidirekt field
|
|
2271
|
-
* @type {Size} */
|
|
2272
|
-
size
|
|
2273
|
-
|
|
2274
|
-
/** @bidirekt field
|
|
2275
|
-
* @type {import('./owner.js').Owner} */
|
|
2276
|
-
owner
|
|
2277
|
-
|
|
2278
|
-
/** @bidirekt field
|
|
2279
|
-
* @type {?string} */
|
|
2280
|
-
nickname
|
|
2281
|
-
|
|
2282
|
-
/** @bidirekt field
|
|
2283
|
-
* @type {!string} */
|
|
2284
|
-
name
|
|
2285
|
-
|
|
2286
|
-
/** @bidirekt field
|
|
2287
|
-
* @type {Object.<string, number>} */
|
|
2288
|
-
scores
|
|
2289
|
-
}
|
|
2290
|
-
```
|
|
2291
|
-
|
|
2292
|
-
<!-- prettier-ignore -->
|
|
2293
|
-
```yaml
|
|
2294
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
2295
|
-
schemas:
|
|
2296
|
-
Owner:
|
|
2297
|
-
type: object
|
|
2298
|
-
properties:
|
|
2299
|
-
name:
|
|
2300
|
-
type: string
|
|
2301
|
-
Pet:
|
|
2302
|
-
type: object
|
|
2303
|
-
properties:
|
|
2304
|
-
size:
|
|
2305
|
-
type: enum
|
|
2306
|
-
members: ["small", "large"]
|
|
2307
|
-
owner:
|
|
2308
|
-
ref: Owner
|
|
2309
|
-
nickname:
|
|
2310
|
-
type: string
|
|
2311
|
-
nullable: true
|
|
2312
|
-
name:
|
|
2313
|
-
type: string
|
|
2314
|
-
scores:
|
|
2315
|
-
type: map
|
|
2316
|
-
values:
|
|
2317
|
-
type: number
|
|
2318
|
-
```
|
|
2319
|
-
|
|
2320
|
-
A `@typedef` is never a schema, even with a tag; `generate` warns, and a field typed by it stops it, as does `String`, `Number` or `Boolean` for its lowercase type:
|
|
2321
|
-
|
|
2322
|
-
```js
|
|
2323
|
-
// pet.js
|
|
2324
|
-
/**
|
|
2325
|
-
* @bidirekt schema
|
|
2326
|
-
* @typedef {object} Address
|
|
2327
|
-
* @property {string} street
|
|
2328
|
-
*/
|
|
2329
|
-
|
|
2330
|
-
/** @bidirekt schema */
|
|
2331
|
-
export class Pet {
|
|
2332
|
-
/** @bidirekt field
|
|
2333
|
-
* @type {Address} */
|
|
2334
|
-
address
|
|
2335
|
-
|
|
2336
|
-
/** @bidirekt field
|
|
2337
|
-
* @type {String} */
|
|
2338
|
-
name
|
|
2339
|
-
}
|
|
2340
|
-
```
|
|
2341
|
-
|
|
2342
|
-
```text
|
|
2343
|
-
pet.js:3 warning: "@bidirekt schema" is on a @typedef, so it is not read: declare the schema as a class with "@bidirekt schema"
|
|
2344
|
-
pet.js:12 cannot translate "Address" to a contract type: declare it with "@bidirekt field type=<type>" or a JSDoc @type
|
|
2345
|
-
pet.js:16 cannot translate "String" to a contract type: write "string" in lowercase
|
|
2346
|
-
```
|
|
2347
|
-
|
|
2348
|
-
`generate` reads the `.js` files of a project with a `jsconfig.json` or with no config at all; with a `tsconfig.json`, only when it sets `allowJs`.
|
|
2349
|
-
|
|
2350
|
-
A hand-written `.d.ts` beside a `.js` of the project is read too. There, `@bidirekt schema` and `@bidirekt field` on an interface or an object type are read, and every other tag and every decorator is not, like those TypeScript copies into the declarations it emits; a name the `.js` also exports is read from the `.js` only:
|
|
2351
|
-
|
|
2352
|
-
```ts
|
|
2353
|
-
// pets.d.ts
|
|
2354
|
-
/** @bidirekt schema */
|
|
2355
|
-
export interface Pet {
|
|
2356
|
-
/** @bidirekt field */
|
|
2357
|
-
name: string
|
|
2358
|
-
}
|
|
2359
|
-
|
|
2360
|
-
export declare function findPet(): Pet
|
|
2361
|
-
```
|
|
2362
|
-
|
|
2363
|
-
```js
|
|
2364
|
-
// pets.js
|
|
2365
|
-
/**
|
|
2366
|
-
* @bidirekt provides GET /pets/*
|
|
2367
|
-
* @bidirekt response 200 Pet
|
|
2368
|
-
*/
|
|
2369
|
-
export function findPet() {
|
|
2370
|
-
return { name: 'Rex' }
|
|
2371
|
-
}
|
|
2372
|
-
```
|
|
2373
|
-
|
|
2374
|
-
```yaml
|
|
2375
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
2376
|
-
provides:
|
|
2377
|
-
rest:
|
|
2378
|
-
/pets/*:
|
|
2379
|
-
get:
|
|
2380
|
-
responses:
|
|
2381
|
-
200: Pet
|
|
2382
|
-
schemas:
|
|
2383
|
-
Pet:
|
|
2384
|
-
type: object
|
|
2385
|
-
properties:
|
|
2386
|
-
name:
|
|
2387
|
-
type: string
|
|
2388
|
-
```
|
|
2389
|
-
|
|
2390
|
-
So a `Pet` the `.js` exports without tags is not a schema, whatever the `.d.ts` says:
|
|
2391
|
-
|
|
2392
|
-
```ts
|
|
2393
|
-
// pets.d.ts
|
|
2394
|
-
/** @bidirekt schema */
|
|
2395
|
-
export interface Pet {
|
|
2396
|
-
/** @bidirekt field */
|
|
2397
|
-
name: string
|
|
2398
|
-
}
|
|
2399
|
-
```
|
|
2400
|
-
|
|
2401
|
-
```js
|
|
2402
|
-
// pets.js
|
|
2403
|
-
export class Pet {
|
|
2404
|
-
name = ''
|
|
2405
|
-
}
|
|
2406
|
-
|
|
2407
|
-
/**
|
|
2408
|
-
* @bidirekt provides GET /pets/*
|
|
2409
|
-
* @bidirekt response 200 Pet
|
|
2410
|
-
*/
|
|
2411
|
-
export function findPet() {
|
|
2412
|
-
return new Pet()
|
|
2413
|
-
}
|
|
2414
|
-
```
|
|
2415
|
-
|
|
2416
|
-
```text
|
|
2417
|
-
pets.js:8 schema "Pet" is not declared: reference a schema by its name in the contract
|
|
2418
|
-
```
|
|
2419
|
-
|
|
2420
|
-
And a decorated class in a `.d.ts` is not read:
|
|
2421
|
-
|
|
2422
|
-
```ts
|
|
2423
|
-
// pet.d.ts
|
|
2424
|
-
import { BidirektField, BidirektSchema } from '@bidirekt/typescript-sdk'
|
|
2425
|
-
|
|
2426
|
-
@BidirektSchema()
|
|
2427
|
-
export declare class Pet {
|
|
2428
|
-
@BidirektField()
|
|
2429
|
-
name: string
|
|
2430
|
-
}
|
|
2431
|
-
```
|
|
2432
|
-
|
|
2433
|
-
```text
|
|
2434
|
-
no @bidirekt/typescript-sdk annotation found in ".": nothing to generate
|
|
2435
|
-
```
|
|
2436
|
-
|
|
2437
|
-
Nor is a tagged class there, so a field typed by it, or a class that extends it, stops `generate`:
|
|
2438
|
-
|
|
2439
|
-
```ts
|
|
2440
|
-
// owner.d.ts
|
|
2441
|
-
/** @bidirekt schema */
|
|
2442
|
-
export declare class Owner {
|
|
2443
|
-
/** @bidirekt field */
|
|
2444
|
-
name: string
|
|
2445
|
-
}
|
|
2446
|
-
```
|
|
2447
|
-
|
|
2448
|
-
```ts
|
|
2449
|
-
// pet.ts
|
|
2450
|
-
import type { Owner } from './owner.js'
|
|
2451
|
-
|
|
2452
|
-
/** @bidirekt schema */
|
|
2453
|
-
export interface Pet {
|
|
2454
|
-
/** @bidirekt field */
|
|
2455
|
-
owner: Owner
|
|
2456
|
-
}
|
|
2457
|
-
```
|
|
2458
|
-
|
|
2459
|
-
```text
|
|
2460
|
-
pet.ts:7 class "Owner" is declared in "owner.d.ts", where only an interface or object type is read: declare it as an interface there, or tag the class in its source
|
|
2461
|
-
```
|
|
531
|
+
## Reference
|
|
2462
532
|
|
|
2463
|
-
|
|
533
|
+
### Decorators
|
|
2464
534
|
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
}
|
|
2472
|
-
```
|
|
535
|
+
| Decorator | Goes on | Options |
|
|
536
|
+
| ---------------------------- | ---------- | -------------------------------------------------------------------------- |
|
|
537
|
+
| `@BidirektSchema(options?)` | a class | `name` |
|
|
538
|
+
| `@BidirektField(options?)` | a property | `type`, `name`, `optional`, `nullable`, `description`, `union`, see Fields |
|
|
539
|
+
| `@BidirektProvider(options)` | a method | `method`, `path`, `request`, `responses` |
|
|
540
|
+
| `@BidirektConsumer(options)` | a method | `provider`, `method`, `path`, `request`, `responses` |
|
|
2473
541
|
|
|
2474
|
-
|
|
2475
|
-
// dist/pet.d.ts
|
|
2476
|
-
/** @bidirekt schema */
|
|
2477
|
-
export interface Pet {
|
|
2478
|
-
/** @bidirekt field */
|
|
2479
|
-
name: string
|
|
2480
|
-
}
|
|
2481
|
-
```
|
|
542
|
+
`generate` recognizes a decorator by where it is imported from, `@bidirekt/typescript-sdk`, also through an alias, `import * as` or a re-export in your own code, never by its name. Options are written in place, as literals and classes, never through a variable. A decorator built at runtime (returned by a wrapper function, applied in a loop) is not read, and `generate` warns. A decorator on a `static` property is not read either, and `generate` warns.
|
|
2482
543
|
|
|
2483
|
-
|
|
2484
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
2485
|
-
schemas:
|
|
2486
|
-
Pet:
|
|
2487
|
-
type: object
|
|
2488
|
-
properties:
|
|
2489
|
-
name:
|
|
2490
|
-
type: string
|
|
2491
|
-
```
|
|
544
|
+
With `emitDecoratorMetadata` on (NestJS, TypeORM), TypeScript keeps the imports of decorated classes at runtime, so classes that reference each other can fail at startup with `Cannot access 'Pet' before initialization`, and each decorated field emits its type as metadata that other libraries act on (class-transformer's implicit conversion, for example, turns `'10'` into `10`). Annotate such classes with tags instead.
|
|
2492
545
|
|
|
2493
546
|
### Tags
|
|
2494
547
|
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
* Creates a pet, declared for @bidirekt/typescript-sdk and read by @bidirekt tooling.
|
|
2511
|
-
* It is listed by @bidirekt provides GET /pets.
|
|
2512
|
-
*
|
|
2513
|
-
* ```ts
|
|
2514
|
-
* // @bidirekt provides POST /fenced
|
|
2515
|
-
* ```
|
|
2516
|
-
*
|
|
2517
|
-
* @param body the new pet
|
|
2518
|
-
* @response 201 the created pet
|
|
2519
|
-
* @bidirekt provides POST /pets
|
|
2520
|
-
* @bidirekt response 201
|
|
2521
|
-
*
|
|
2522
|
-
* ```ts
|
|
2523
|
-
* createPet({ name: 'Rex' })
|
|
2524
|
-
* ```
|
|
2525
|
-
* @example
|
|
2526
|
-
* @bidirekt provides POST /example
|
|
2527
|
-
*/
|
|
2528
|
-
export function createPet(body: unknown) {
|
|
2529
|
-
return body
|
|
2530
|
-
}
|
|
2531
|
-
````
|
|
2532
|
-
|
|
2533
|
-
```text
|
|
2534
|
-
pets.ts:14 warning: "@bidirekt provides" is not at the start of a comment line, so it is not read: start the line with it
|
|
2535
|
-
pets.ts:29 warning: "@bidirekt provides" is inside an @example, so it is not read: move it above the @example
|
|
2536
|
-
```
|
|
2537
|
-
|
|
2538
|
-
```yaml
|
|
2539
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
2540
|
-
provides:
|
|
2541
|
-
rest:
|
|
2542
|
-
/pets:
|
|
2543
|
-
post:
|
|
2544
|
-
responses:
|
|
2545
|
-
201: ~
|
|
2546
|
-
```
|
|
2547
|
-
|
|
2548
|
-
Only a `description` value is quoted, and only a `union=` value is grouped in `{...}`; quotes or braces anywhere else stop `generate`:
|
|
2549
|
-
|
|
2550
|
-
```ts
|
|
2551
|
-
// pet.ts
|
|
2552
|
-
/** @bidirekt schema */
|
|
2553
|
-
export interface Pet {
|
|
2554
|
-
/** @bidirekt field description="The pet's name, as the owner says it" */
|
|
2555
|
-
name: string
|
|
2556
|
-
|
|
2557
|
-
/** @bidirekt field name="nick_name" */
|
|
2558
|
-
nickname: string
|
|
2559
|
-
}
|
|
2560
|
-
|
|
2561
|
-
/**
|
|
2562
|
-
* @bidirekt provides GET /owners/{ id }
|
|
2563
|
-
* @bidirekt response 200
|
|
2564
|
-
*/
|
|
2565
|
-
export function findOwner() {}
|
|
2566
|
-
```
|
|
2567
|
-
|
|
2568
|
-
```text
|
|
2569
|
-
pet.ts:7 cannot read "@bidirekt field name="nick_name"": remove the quotes, only description="<text>" takes them
|
|
2570
|
-
pet.ts:12 cannot read "@bidirekt provides GET /owners/{ id }": write "@bidirekt provides <METHOD> <path>"
|
|
2571
|
-
```
|
|
2572
|
-
|
|
2573
|
-
A misspelled tag name, like `@Bidirekt` or `@bidirektresponse`, is not read, and `generate` warns:
|
|
2574
|
-
|
|
2575
|
-
```ts
|
|
2576
|
-
// pets.ts
|
|
2577
|
-
/**
|
|
2578
|
-
* @bidirekt provides GET /pets
|
|
2579
|
-
* @bidirekt response 200
|
|
2580
|
-
* @Bidirekt response 404
|
|
2581
|
-
*/
|
|
2582
|
-
export function listPets() {}
|
|
2583
|
-
```
|
|
2584
|
-
|
|
2585
|
-
```text
|
|
2586
|
-
pets.ts:5 warning: "@Bidirekt" is not a @bidirekt tag: write "@bidirekt <keyword>"
|
|
2587
|
-
```
|
|
2588
|
-
|
|
2589
|
-
```yaml
|
|
2590
|
-
# Generated by @bidirekt/typescript-sdk. Do not edit.
|
|
2591
|
-
provides:
|
|
2592
|
-
rest:
|
|
2593
|
-
/pets:
|
|
2594
|
-
get:
|
|
2595
|
-
responses:
|
|
2596
|
-
200: ~
|
|
2597
|
-
```
|
|
2598
|
-
|
|
2599
|
-
Tags of a handler, a schema or a field written in two comments on the same element, or `@bidirekt schema` on two blocks of an interface, stop `generate`:
|
|
2600
|
-
|
|
2601
|
-
```ts
|
|
2602
|
-
// pets.ts
|
|
2603
|
-
/** @bidirekt provides GET /pets */
|
|
2604
|
-
/** @bidirekt provides GET /animals */
|
|
2605
|
-
export function listPets() {}
|
|
2606
|
-
|
|
2607
|
-
/** @bidirekt schema */
|
|
2608
|
-
export interface Pet {
|
|
2609
|
-
/** @bidirekt field name=pet_name */
|
|
2610
|
-
/** @bidirekt field */
|
|
2611
|
-
name: string
|
|
2612
|
-
}
|
|
548
|
+
| Tag | Goes above |
|
|
549
|
+
| ----------------------------------------------- | ------------------------------------------------------------------------------ |
|
|
550
|
+
| `@bidirekt schema [name=<Name>]` | a class, an `interface`, an object `type` or a string `enum` |
|
|
551
|
+
| `@bidirekt field [key=value ...]` | a property of a schema |
|
|
552
|
+
| `@bidirekt provides <METHOD> <path>` | a function, an exported arrow function, a method or a statement (`router.get`) |
|
|
553
|
+
| `@bidirekt consumes <provider> <METHOD> <path>` | the same |
|
|
554
|
+
| `@bidirekt request [body]` | follows `provides` or `consumes` |
|
|
555
|
+
| `@bidirekt response <status> [body]` | follows `provides` or `consumes`, once per status |
|
|
556
|
+
|
|
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`.
|
|
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
|
+
- 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
|
+
- In a path, write a `*` before another segment as `*\/` (`/pets/*\/photos`): a bare `*/` would end the comment. `generate` reads `*\/` as `*/`.
|
|
562
|
+
- `/* */` and `//` comments are not read, other tags in the same comment (`@param`) are left alone, and a `@bidirekt` later in a line, in a ` ``` ` fence or in an `@example` is not a tag. `generate` warns about a tag it leaves unread, a misspelled one (`@Bidirekt`) and a tag comment it can't attach to an element (between a decorator and its method, inside a chain of route calls), so none vanishes silently.
|
|
2613
563
|
|
|
2614
|
-
|
|
2615
|
-
export interface Pet {
|
|
2616
|
-
/** @bidirekt field */
|
|
2617
|
-
age: number
|
|
2618
|
-
}
|
|
2619
|
-
```
|
|
564
|
+
### Handlers
|
|
2620
565
|
|
|
2621
|
-
|
|
2622
|
-
pets.ts:3 a handler is declared more than once on this element: keep one "@bidirekt provides" or "@bidirekt consumes" comment
|
|
2623
|
-
pets.ts:9 "@bidirekt field" is declared more than once on property "name": keep one
|
|
2624
|
-
pets.ts:13 "@bidirekt schema" is declared more than once on interface "Pet": keep one
|
|
2625
|
-
```
|
|
566
|
+
A handler declares one resource: its method, in uppercase (`GET`, `POST`, `PUT`, `PATCH`, `DELETE`), and its path as the contract writes it, with a `*` for each dynamic segment (`/pets/*` for `/pets/:id`). The request and each response status take a body:
|
|
2626
567
|
|
|
2627
|
-
|
|
568
|
+
| Body | Decorator | Tag | Contract |
|
|
569
|
+
| ------------------------ | -------------------------------------- | ------------------------------------- | --------------- |
|
|
570
|
+
| a schema | `Pet` | `Pet` | `Pet` |
|
|
571
|
+
| a list | `[Pet]` | `Pet[]` | `PetList` |
|
|
572
|
+
| a list with its own name | `{ list: Pet, name: 'PetCollection' }` | `Pet[] as PetCollection` | `PetCollection` |
|
|
573
|
+
| no body | `null` | nothing after the status or `request` | `~` |
|
|
2628
574
|
|
|
2629
|
-
|
|
2630
|
-
// pets.ts
|
|
2631
|
-
function Get(_path: string) {
|
|
2632
|
-
return (_target: object, _key: string): void => {}
|
|
2633
|
-
}
|
|
575
|
+
Without a request (no `request` option, no `@bidirekt request` tag), the resource has no `request` key. A list is written once however many handlers use it. The same method and path provided by two handlers stops `generate`. The same resource consumed by several handlers, with decorators, tags or both, is written once with the statuses of all of them; they declare the same request, and the same schema for a status they share.
|
|
2634
576
|
|
|
2635
|
-
|
|
2636
|
-
route(_path: string): Router {
|
|
2637
|
-
return this
|
|
2638
|
-
}
|
|
577
|
+
### Fields
|
|
2639
578
|
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
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 |
|
|
2644
587
|
|
|
2645
|
-
|
|
2646
|
-
router
|
|
2647
|
-
.route('/pets/:id')
|
|
2648
|
-
/** @bidirekt provides GET /pets/* */
|
|
2649
|
-
.get(() => {})
|
|
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`.
|
|
2650
589
|
|
|
2651
|
-
|
|
2652
|
-
/**
|
|
2653
|
-
* @bidirekt provides GET /pets
|
|
2654
|
-
* @bidirekt response 200
|
|
2655
|
-
*/
|
|
2656
|
-
@Get('')
|
|
2657
|
-
findAll() {}
|
|
2658
|
-
|
|
2659
|
-
@Get(':id')
|
|
2660
|
-
/** @bidirekt provides GET /pets/* */
|
|
2661
|
-
findOne() {}
|
|
2662
|
-
}
|
|
2663
|
-
```
|
|
590
|
+
### Types
|
|
2664
591
|
|
|
2665
|
-
|
|
2666
|
-
pets.ts:19 warning: this comment is not above a statement or declaration, so its "@bidirekt" tags are not read: move it above the statement or declaration, before its decorators
|
|
2667
|
-
pets.ts:31 warning: this comment is not above a statement or declaration, so its "@bidirekt" tags are not read: move it above the statement or declaration, before its decorators
|
|
2668
|
-
```
|
|
592
|
+
A field's type comes from TypeScript, or from the JSDoc `@type` in JavaScript:
|
|
2669
593
|
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
594
|
+
| TypeScript | Contract |
|
|
595
|
+
| ------------------------------------------------------------------------------ | --------------------------------------------------------- |
|
|
596
|
+
| `string`, `boolean`, `number` | `string`, `boolean`, `number` |
|
|
597
|
+
| `T \| null` | `nullable: true` |
|
|
598
|
+
| `x?: T`, `T \| undefined` | `optional: true` |
|
|
599
|
+
| `'a' \| 'b'`, through aliases too, `Status \| 'unknown'`, `(typeof X)[number]` | `type: enum`, inline |
|
|
600
|
+
| a string `enum` | `ref` to an `enum` schema named after it, with its values |
|
|
601
|
+
| one member of a string `enum`, `PetStatus.Available` | `type: enum`, inline, with that member |
|
|
602
|
+
| a numeric `enum` | `integer` |
|
|
603
|
+
| `T[]`, `Array<T>`, `readonly T[]` | `type: array` |
|
|
604
|
+
| `Record<string, T>`, `{ [key: string]: T }` | `type: map` |
|
|
605
|
+
| `(T \| null)[]`, `Record<string, T \| null>` | `nullable: true` on the items or values |
|
|
606
|
+
| a schema: class, interface or object type | `ref` |
|
|
607
|
+
| a union of schemas | `type: union`, with the keys from `union` |
|
|
2679
608
|
|
|
2680
|
-
|
|
609
|
+
A type alias is followed to its type, a generic one too. A string enum or a literal union imported from a dependency translates the same way. A decorated class can reference a tagged interface, and the other way round.
|
|
2681
610
|
|
|
2682
|
-
|
|
611
|
+
A declared `type` is the whole type of the field, whatever TypeScript says: a primitive (`string`, `integer`, `float`, `number`, `boolean`), `T[]` for a list of `T`, or `map<T>` for a map of `T` (its keys are always strings), nested up to three levels in a decorator and to any depth in a tag (`integer[][]`, `map<float[]>`). `integer` and `float` come only from a declared type. `optional` and `nullable` still come from the TypeScript type when not declared.
|
|
2683
612
|
|
|
2684
|
-
|
|
2685
|
-
- `--out <file>` writes to another file.
|
|
2686
|
-
- `--check` writes nothing and exits with 1 when the file on disk differs from what `generate` would write.
|
|
613
|
+
### Schemas
|
|
2687
614
|
|
|
2688
|
-
|
|
615
|
+
- A schema is named after its class, interface or object type; `name` gives it another name in the contract. An anonymous class (`export default class`) needs a `name`.
|
|
616
|
+
- Every schema is written, even one no handler references. Two schemas with the same name in the contract stop `generate`; give one of them a `name`.
|
|
617
|
+
- A schema without a field stops `generate`, and so does `@bidirekt schema` above an object type that is only an index signature: that is a map, used as a field's type.
|
|
618
|
+
- A string enum used by a field is written as a schema named after it. `@bidirekt schema` above the enum gives it another name with `name=`, and writes it even when no field uses it, so tags can reference it (`@bidirekt response 200 PetStatus`). An enum without string members can't be a schema, and `generate` warns.
|
|
619
|
+
- A schema class takes the annotated fields of its parent classes, whether or not they are schemas; a schema interface takes those of the interfaces it extends, and of all its blocks. A parent `generate` can't read, like a mixin or a utility type (`Omit<Pet, 'secret'>`), stops it: declare the fields in the schema.
|
|
620
|
+
|
|
621
|
+
### Describe what the code does
|
|
622
|
+
|
|
623
|
+
The contract describes what the code does, not the type the code declares. The TypeScript type is where each field starts, and a declared `type`, `optional` or `nullable` wins over it. In the [Quickstart](#quickstart), `birthDate` is a `Date` in the code and a string in the body, so it declares `type: 'string'`. A `name: string | null` that the code always fills declares `nullable: false`; a field the code always sends, though typed `name?: string`, declares `optional: false`.
|
|
624
|
+
|
|
625
|
+
### Mistakes `generate` stops at
|
|
626
|
+
|
|
627
|
+
`generate` prints each at its `file:line` and writes nothing. The usual ones:
|
|
628
|
+
|
|
629
|
+
- `cannot translate "Date" to a contract type: declare it with @BidirektField({ type })`, and the same for `any`, `unknown`, `string | number` or the `string | undefined` of `Array<string | undefined>`; with a tag it says `declare it with "@bidirekt field type=<type>"`.
|
|
630
|
+
- `cannot translate numeric literal type "1 | 2": use string literals or declare it with @BidirektField({ type })`
|
|
631
|
+
- `cannot translate enum "Level" with string and numeric members: make all its members strings or all numbers`
|
|
632
|
+
- `cannot translate inline object type "{ street: string }": declare a class with @BidirektSchema and use it as the type`
|
|
633
|
+
- `cannot translate interface "Address": add "@bidirekt schema" above it`, and likewise for an object type or a class that is not a schema.
|
|
634
|
+
- `cannot translate generic type "Page<Pet>": declare a non-generic class with @BidirektSchema and use it as the type`
|
|
635
|
+
- `cannot translate "Record<number, string>": declare its key as string`
|
|
636
|
+
- `cannot read the type of "tag": write its type or declare it with @BidirektField({ type })`, for a property with only an initial value.
|
|
637
|
+
- `cannot translate union of classes "CardPayment | PixPayment": declare each member's key with @BidirektField({ union })`
|
|
638
|
+
- `cannot read the parent "Timestamped(Pet)" of class "Dog": declare its fields`
|
|
639
|
+
- `class "Owner" comes from "packages/dto/dist/index.d.ts", which has no decorators: point the package's "types" or "exports" at its source`
|
|
640
|
+
- `schema "Pet" has no field: annotate at least one field with @BidirektField`
|
|
641
|
+
- `schema "Pet" is not declared: reference a schema by its name in the contract`
|
|
642
|
+
- `schema name "Pet" is declared more than once: give each schema a distinct name`
|
|
643
|
+
- `method "get" must be uppercase: write "GET"`
|
|
644
|
+
- `response status "0201" must be written as 201`, also for a decorator status written as `'0201'`.
|
|
645
|
+
- `@BidirektProvider and "@bidirekt provides GET /pets/*" are both on this handler: keep one`; a decorator and a tag on the same schema or field stop it too.
|
|
2689
646
|
|
|
2690
647
|
## Docs
|
|
2691
648
|
|