node-turbo 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.c8rc.json +3 -1
- package/README.md +111 -18
- package/docs/API.md +250 -30
- package/lib/express/express-turbo-stream.js +5 -0
- package/lib/koa/koa-turbo-stream.js +3 -1
- package/lib/turbo-element.js +8 -2
- package/lib/turbo-frame.js +5 -0
- package/lib/turbo-readable.js +9 -2
- package/lib/turbo-stream-element.js +27 -21
- package/lib/turbo-stream.js +52 -96
- package/lib/ws/ws-turbo-stream.js +8 -0
- package/package.json +30 -15
- package/test/unit/core/turbo-element.test.js +29 -0
- package/test/unit/core/turbo-stream-element.test.js +7 -0
- package/test/unit/core/turbo-stream.test.js +31 -0
package/docs/API.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# node-turbo API documentation
|
|
2
2
|
|
|
3
|
-
Version 1.
|
|
3
|
+
Version 1.1.0
|
|
4
4
|
|
|
5
5
|
## Table of Contents
|
|
6
6
|
|
|
@@ -26,7 +26,24 @@ Version 1.0.0
|
|
|
26
26
|
- [turbostream.flush()](#turbostreamflush)
|
|
27
27
|
- [turbostream.custom(action, target, content)](#turbostreamcustomaction-target-content)
|
|
28
28
|
- [turbostream.customAll(action, targets, content)](#turbostreamcustomallaction-targets-content)
|
|
29
|
-
- [turbostream.createReadableStream(opts
|
|
29
|
+
- [turbostream.createReadableStream(opts, opts.continuous, streamOptions)](#turbostreamcreatereadablestreamopts-optscontinuous-streamoptions)
|
|
30
|
+
- [turbostream.append(targetOrAttributes, content)](#turbostreamappendtargetorattributes-content)
|
|
31
|
+
- [turbostream.appendAll(targetsOrAttributes, content)](#turbostreamappendalltargetsorattributes-content)
|
|
32
|
+
- [turbostream.prepend(targetOrAttributes, content)](#turbostreamprependtargetorattributes-content)
|
|
33
|
+
- [turbostream.prependAll(targetsOrAttributes, content)](#turbostreamprependalltargetsorattributes-content)
|
|
34
|
+
- [turbostream.replace(targetOrAttributes, content)](#turbostreamreplacetargetorattributes-content)
|
|
35
|
+
- [turbostream.replaceAll(targetsOrAttributes, content)](#turbostreamreplacealltargetsorattributes-content)
|
|
36
|
+
- [turbostream.update(targetOrAttributes, content)](#turbostreamupdatetargetorattributes-content)
|
|
37
|
+
- [turbostream.updateAll(targetsOrAttributes, content)](#turbostreamupdatealltargetsorattributes-content)
|
|
38
|
+
- [turbostream.remove(targetOrAttributes)](#turbostreamremovetargetorattributes)
|
|
39
|
+
- [turbostream.removeAll(targetsOrAttributes)](#turbostreamremovealltargetsorattributes)
|
|
40
|
+
- [turbostream.before(targetOrAttributes, content)](#turbostreambeforetargetorattributes-content)
|
|
41
|
+
- [turbostream.beforeAll(targetsOrAttributes, content)](#turbostreambeforealltargetsorattributes-content)
|
|
42
|
+
- [turbostream.after(targetOrAttributes, content)](#turbostreamaftertargetorattributes-content)
|
|
43
|
+
- [turbostream.afterAll(targetsOrAttributes, content)](#turbostreamafteralltargetsorattributes-content)
|
|
44
|
+
- [turbostream.morph(targetOrAttributes, content)](#turbostreammorphtargetorattributes-content)
|
|
45
|
+
- [turbostream.morphAll(targetsOrAttributes, content)](#turbostreammorphalltargetsorattributes-content)
|
|
46
|
+
- [turbostream.refresh(requestIdOrAttributes)](#turbostreamrefreshrequestidorattributes)
|
|
30
47
|
- [Class: TurboStreamElement](#class-turbostreamelement)
|
|
31
48
|
- [turbostreamelement.validate()](#turbostreamelementvalidate)
|
|
32
49
|
- [turbostreamelement.render()](#turbostreamelementrender)
|
|
@@ -61,7 +78,6 @@ Version 1.0.0
|
|
|
61
78
|
- [Class: KoaTurboStream](#class-koaturbostream)
|
|
62
79
|
- [new KoaTurboStream(koaCtx)](#new-koaturbostreamkoactx)
|
|
63
80
|
- [koaturbostream.koaCtx](#koaturbostreamkoactx)
|
|
64
|
-
- [koaturbostream.status](#koaturbostreamstatus)
|
|
65
81
|
- [turbochargeKoa(koaApp, opts, opts.autoRender)](#turbochargekoakoaapp-opts-optsautorender)
|
|
66
82
|
- [node-turbo/express](#node-turboexpress)
|
|
67
83
|
- [Class: ExpressTurboStream](#class-expressturbostream)
|
|
@@ -138,6 +154,8 @@ The HTML content.
|
|
|
138
154
|
Converts the attributes object to a string in the form
|
|
139
155
|
of HTML attributes ({ name: value } -> 'name="value"').
|
|
140
156
|
|
|
157
|
+
***
|
|
158
|
+
|
|
141
159
|
### Class: TurboStream
|
|
142
160
|
|
|
143
161
|
```javascript
|
|
@@ -145,7 +163,7 @@ import { TurboStream } from 'node-turbo';
|
|
|
145
163
|
```
|
|
146
164
|
|
|
147
165
|
***Extends:***
|
|
148
|
-
- events~EventEmitter
|
|
166
|
+
- node:events~EventEmitter
|
|
149
167
|
|
|
150
168
|
A Turbo Stream message.
|
|
151
169
|
|
|
@@ -161,7 +179,7 @@ See [https://turbo.hotwired.dev/handbook/streams#streaming-from-http-responses](
|
|
|
161
179
|
|
|
162
180
|
`static` {Array}
|
|
163
181
|
|
|
164
|
-
List of all supported actions:
|
|
182
|
+
List of all supported official actions: `append`, `prepend`, `replace`, `update`, `remove`, `before`, `after` and `refresh`.
|
|
165
183
|
|
|
166
184
|
See [https://turbo.hotwired.dev/handbook/streams#stream-messages-and-actions](https://turbo.hotwired.dev/handbook/streams#stream-messages-and-actions)
|
|
167
185
|
|
|
@@ -253,12 +271,184 @@ Adds a Turbo Stream Element with a custom action, targeting multiple DOM element
|
|
|
253
271
|
|
|
254
272
|
Returns: {TurboStream} The instance for chaining.
|
|
255
273
|
|
|
256
|
-
#### turbostream.createReadableStream(opts
|
|
274
|
+
#### turbostream.createReadableStream(opts, opts.continuous, streamOptions)
|
|
275
|
+
|
|
276
|
+
- `opts` {Object<String, String>} The options for stream creation.
|
|
277
|
+
- `opts.continuous` {Boolean} If true, a TurboReadable instance is returned.
|
|
278
|
+
If false, a readable stream created from the buffered items is returned.
|
|
279
|
+
- `streamOptions` {Object<String, String>} The options for the readable stream itself.
|
|
280
|
+
|
|
281
|
+
Creates a readable stream.
|
|
282
|
+
|
|
283
|
+
Returns: {stream.Readable | TurboReadable} Either a readable stream or a TurboReadable instance.
|
|
284
|
+
|
|
285
|
+
#### turbostream.append(targetOrAttributes, content)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
- `targetOrAttributes` {String | Object<String.String>} Either the target ID as string or all attributes as object.
|
|
289
|
+
|
|
290
|
+
- `content` {String} The HTML content of the element.
|
|
291
|
+
Adds a Turbo Stream element with the action `append` to the message.
|
|
292
|
+
|
|
293
|
+
Returns: {TurboStream} The instance for chaining.
|
|
294
|
+
|
|
295
|
+
#### turbostream.appendAll(targetsOrAttributes, content)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
- `targetsOrAttributes` {String | Object<String.String>} Either the query targeting multiple DOM elements as string or all attributes as object.
|
|
299
|
+
|
|
300
|
+
- `content` {String} The HTML content of the element.
|
|
301
|
+
Adds a Turbo Stream element with the action `append` to the message.
|
|
302
|
+
|
|
303
|
+
Returns: {TurboStream} The instance for chaining.
|
|
304
|
+
|
|
305
|
+
#### turbostream.prepend(targetOrAttributes, content)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
- `targetOrAttributes` {String | Object<String.String>} Either the target ID as string or all attributes as object.
|
|
309
|
+
|
|
310
|
+
- `content` {String} The HTML content of the element.
|
|
311
|
+
Adds a Turbo Stream element with the action `prepend` to the message.
|
|
312
|
+
|
|
313
|
+
Returns: {TurboStream} The instance for chaining.
|
|
314
|
+
|
|
315
|
+
#### turbostream.prependAll(targetsOrAttributes, content)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
- `targetsOrAttributes` {String | Object<String.String>} Either the query targeting multiple DOM elements as string or all attributes as object.
|
|
319
|
+
|
|
320
|
+
- `content` {String} The HTML content of the element.
|
|
321
|
+
Adds a Turbo Stream element with the action `prepend` to the message.
|
|
322
|
+
|
|
323
|
+
Returns: {TurboStream} The instance for chaining.
|
|
324
|
+
|
|
325
|
+
#### turbostream.replace(targetOrAttributes, content)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
- `targetOrAttributes` {String | Object<String.String>} Either the target ID as string or all attributes as object.
|
|
329
|
+
|
|
330
|
+
- `content` {String} The HTML content of the element.
|
|
331
|
+
Adds a Turbo Stream element with the action `replace` to the message.
|
|
332
|
+
|
|
333
|
+
Returns: {TurboStream} The instance for chaining.
|
|
334
|
+
|
|
335
|
+
#### turbostream.replaceAll(targetsOrAttributes, content)
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
- `targetsOrAttributes` {String | Object<String.String>} Either the query targeting multiple DOM elements as string or all attributes as object.
|
|
339
|
+
|
|
340
|
+
- `content` {String} The HTML content of the element.
|
|
341
|
+
Adds a Turbo Stream element with the action `replace` to the message.
|
|
342
|
+
|
|
343
|
+
Returns: {TurboStream} The instance for chaining.
|
|
344
|
+
|
|
345
|
+
#### turbostream.update(targetOrAttributes, content)
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
- `targetOrAttributes` {String | Object<String.String>} Either the target ID as string or all attributes as object.
|
|
349
|
+
|
|
350
|
+
- `content` {String} The HTML content of the element.
|
|
351
|
+
Adds a Turbo Stream element with the action `update` to the message.
|
|
352
|
+
|
|
353
|
+
Returns: {TurboStream} The instance for chaining.
|
|
354
|
+
|
|
355
|
+
#### turbostream.updateAll(targetsOrAttributes, content)
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
- `targetsOrAttributes` {String | Object<String.String>} Either the query targeting multiple DOM elements as string or all attributes as object.
|
|
359
|
+
|
|
360
|
+
- `content` {String} The HTML content of the element.
|
|
361
|
+
Adds a Turbo Stream element with the action `update` to the message.
|
|
362
|
+
|
|
363
|
+
Returns: {TurboStream} The instance for chaining.
|
|
364
|
+
|
|
365
|
+
#### turbostream.remove(targetOrAttributes)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
- `targetOrAttributes` {String | Object<String.String>} Either the target ID as string or all attributes as object.
|
|
369
|
+
|
|
370
|
+
Adds a Turbo Stream element with the action `remove` to the message.
|
|
371
|
+
|
|
372
|
+
Returns: {TurboStream} The instance for chaining.
|
|
373
|
+
|
|
374
|
+
#### turbostream.removeAll(targetsOrAttributes)
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
- `targetsOrAttributes` {String | Object<String.String>} Either the query targeting multiple DOM elements as string or all attributes as object.
|
|
378
|
+
|
|
379
|
+
Adds a Turbo Stream element with the action `remove` to the message.
|
|
380
|
+
|
|
381
|
+
Returns: {TurboStream} The instance for chaining.
|
|
382
|
+
|
|
383
|
+
#### turbostream.before(targetOrAttributes, content)
|
|
257
384
|
|
|
258
|
-
- `opts` {*}
|
|
259
|
-
- `streamOptions` {{}}
|
|
260
385
|
|
|
261
|
-
|
|
386
|
+
- `targetOrAttributes` {String | Object<String.String>} Either the target ID as string or all attributes as object.
|
|
387
|
+
|
|
388
|
+
- `content` {String} The HTML content of the element.
|
|
389
|
+
Adds a Turbo Stream element with the action `before` to the message.
|
|
390
|
+
|
|
391
|
+
Returns: {TurboStream} The instance for chaining.
|
|
392
|
+
|
|
393
|
+
#### turbostream.beforeAll(targetsOrAttributes, content)
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
- `targetsOrAttributes` {String | Object<String.String>} Either the query targeting multiple DOM elements as string or all attributes as object.
|
|
397
|
+
|
|
398
|
+
- `content` {String} The HTML content of the element.
|
|
399
|
+
Adds a Turbo Stream element with the action `before` to the message.
|
|
400
|
+
|
|
401
|
+
Returns: {TurboStream} The instance for chaining.
|
|
402
|
+
|
|
403
|
+
#### turbostream.after(targetOrAttributes, content)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
- `targetOrAttributes` {String | Object<String.String>} Either the target ID as string or all attributes as object.
|
|
407
|
+
|
|
408
|
+
- `content` {String} The HTML content of the element.
|
|
409
|
+
Adds a Turbo Stream element with the action `after` to the message.
|
|
410
|
+
|
|
411
|
+
Returns: {TurboStream} The instance for chaining.
|
|
412
|
+
|
|
413
|
+
#### turbostream.afterAll(targetsOrAttributes, content)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
- `targetsOrAttributes` {String | Object<String.String>} Either the query targeting multiple DOM elements as string or all attributes as object.
|
|
417
|
+
|
|
418
|
+
- `content` {String} The HTML content of the element.
|
|
419
|
+
Adds a Turbo Stream element with the action `after` to the message.
|
|
420
|
+
|
|
421
|
+
Returns: {TurboStream} The instance for chaining.
|
|
422
|
+
|
|
423
|
+
#### turbostream.morph(targetOrAttributes, content)
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
- `targetOrAttributes` {String | Object<String.String>} Either the target ID as string or all attributes as object.
|
|
427
|
+
|
|
428
|
+
- `content` {String} The HTML content of the element.
|
|
429
|
+
Adds a Turbo Stream element with the action `morph` to the message.
|
|
430
|
+
|
|
431
|
+
Returns: {TurboStream} The instance for chaining.
|
|
432
|
+
|
|
433
|
+
#### turbostream.morphAll(targetsOrAttributes, content)
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
- `targetsOrAttributes` {String | Object<String.String>} Either the query targeting multiple DOM elements as string or all attributes as object.
|
|
437
|
+
|
|
438
|
+
- `content` {String} The HTML content of the element.
|
|
439
|
+
Adds a Turbo Stream element with the action `morph` to the message.
|
|
440
|
+
|
|
441
|
+
Returns: {TurboStream} The instance for chaining.
|
|
442
|
+
|
|
443
|
+
#### turbostream.refresh(requestIdOrAttributes)
|
|
444
|
+
|
|
445
|
+
- `requestIdOrAttributes` {String | Object<String, String>} Either the request ID as string or all attributes as an object.
|
|
446
|
+
|
|
447
|
+
Adds a Turbo Stream Element with the action 'refresh' to the message.
|
|
448
|
+
|
|
449
|
+
Returns: {TurboStream} The instance for chaining.
|
|
450
|
+
|
|
451
|
+
***
|
|
262
452
|
|
|
263
453
|
### Class: TurboStreamElement
|
|
264
454
|
|
|
@@ -273,7 +463,7 @@ A Turbo Stream element. A Turbo Stream message consists of one or several Turbo
|
|
|
273
463
|
|
|
274
464
|
#### turbostreamelement.validate()
|
|
275
465
|
|
|
276
|
-
Validates the attributes. `attributes.target` (or `attributes.targets`)
|
|
466
|
+
Validates the attributes. `attributes.action` is mandatory. `attributes.target` (or `attributes.targets`) is mandatory for any action but 'refresh'. Gets called by the constructor.
|
|
277
467
|
|
|
278
468
|
Throws {AttributeMissingError} when mandatory attributes are missing.
|
|
279
469
|
Throws {AttributeMalformedError} when mandatory attributes are malformed.
|
|
@@ -281,7 +471,7 @@ Throws {AttributeInvalidError} when attributes are invalid.
|
|
|
281
471
|
|
|
282
472
|
#### turbostreamelement.render()
|
|
283
473
|
|
|
284
|
-
Renders this Turbo Stream element as HTML string. Omits `<template>[content]<template>` when the attribute `action` is 'remove'.
|
|
474
|
+
Renders this Turbo Stream element as HTML string. Omits `<template>[content]<template>` when the attribute `action` is 'remove' or 'refresh'.
|
|
285
475
|
|
|
286
476
|
Returns: {String} The rendered HTML fragment.
|
|
287
477
|
|
|
@@ -297,6 +487,8 @@ The HTML content.
|
|
|
297
487
|
Converts the attributes object to a string in the form
|
|
298
488
|
of HTML attributes ({ name: value } -> 'name="value"').
|
|
299
489
|
|
|
490
|
+
***
|
|
491
|
+
|
|
300
492
|
### Class: TurboElement
|
|
301
493
|
|
|
302
494
|
```javascript
|
|
@@ -338,6 +530,8 @@ Validation function to implement.
|
|
|
338
530
|
|
|
339
531
|
Render function to implement.
|
|
340
532
|
|
|
533
|
+
***
|
|
534
|
+
|
|
341
535
|
### Class: TurboReadable
|
|
342
536
|
|
|
343
537
|
```javascript
|
|
@@ -345,7 +539,7 @@ import { TurboReadable } from 'node-turbo';
|
|
|
345
539
|
```
|
|
346
540
|
|
|
347
541
|
***Extends:***
|
|
348
|
-
- stream~Readable
|
|
542
|
+
- node:stream~Readable
|
|
349
543
|
|
|
350
544
|
This class represents a readable stream which reads messages/elements from a Turbo Stream instance.
|
|
351
545
|
|
|
@@ -390,6 +584,8 @@ Gets called when the stream is being destroyed. The event listener for the event
|
|
|
390
584
|
|
|
391
585
|
Pushes `null` to the readable buffer to signal the end of the input.
|
|
392
586
|
|
|
587
|
+
***
|
|
588
|
+
|
|
393
589
|
#### isTurboStreamRequest(request)
|
|
394
590
|
|
|
395
591
|
```javascript
|
|
@@ -499,8 +695,9 @@ Sends the Turbo Stream element to the WebSocket.
|
|
|
499
695
|
#### Inherited from class [TurboStream](#class-turbostream):
|
|
500
696
|
|
|
501
697
|
> - `static` ACTIONS
|
|
502
|
-
List of all supported actions:
|
|
503
|
-
|
|
698
|
+
List of all supported official actions:
|
|
699
|
+
`append`, `prepend`, `replace`, `update`, `remove`, `before`, `after`
|
|
700
|
+
and `refresh`.
|
|
504
701
|
> - `static` MIME_TYPE
|
|
505
702
|
MIME type for Turbo Stream messages.
|
|
506
703
|
> - `get` length
|
|
@@ -515,14 +712,16 @@ Adds the element to the buffer, if config.buffer === true.
|
|
|
515
712
|
Fires the event 'element' with the added element.
|
|
516
713
|
> - clear()
|
|
517
714
|
Clears the buffer.
|
|
518
|
-
> - createReadableStream(opts
|
|
519
|
-
|
|
715
|
+
> - createReadableStream(opts, opts.continuous, streamOptions)
|
|
716
|
+
Creates a readable stream.
|
|
520
717
|
> - custom(action, target, content)
|
|
521
718
|
Adds a Turbo Stream Element with a custom action.
|
|
522
719
|
> - customAll(action, targets, content)
|
|
523
720
|
Adds a Turbo Stream Element with a custom action, targeting multiple DOM elements.
|
|
524
721
|
> - flush()
|
|
525
722
|
Renders this Turbo Stream message and clears the buffer.
|
|
723
|
+
> - refresh(requestIdOrAttributes)
|
|
724
|
+
Adds a Turbo Stream Element with the action 'refresh' to the message.
|
|
526
725
|
> - render()
|
|
527
726
|
Renders this Turbo Stream message if there are buffered elements.
|
|
528
727
|
> - renderElements()
|
|
@@ -530,6 +729,8 @@ If there are buffered elements, renders them and returns an array with the HTML
|
|
|
530
729
|
> - updateConfig(config)
|
|
531
730
|
Extends/Overwrites the configuration.
|
|
532
731
|
|
|
732
|
+
***
|
|
733
|
+
|
|
533
734
|
## node-turbo/koa
|
|
534
735
|
|
|
535
736
|
### Class: KoaTurboStream
|
|
@@ -555,15 +756,12 @@ Koa's context object.
|
|
|
555
756
|
|
|
556
757
|
See [https://koajs.com/#context](https://koajs.com/#context)
|
|
557
758
|
|
|
558
|
-
#### koaturbostream.status
|
|
559
|
-
|
|
560
|
-
{number}
|
|
561
|
-
|
|
562
759
|
#### Inherited from class [TurboStream](#class-turbostream):
|
|
563
760
|
|
|
564
761
|
> - `static` ACTIONS
|
|
565
|
-
List of all supported actions:
|
|
566
|
-
|
|
762
|
+
List of all supported official actions:
|
|
763
|
+
`append`, `prepend`, `replace`, `update`, `remove`, `before`, `after`
|
|
764
|
+
and `refresh`.
|
|
567
765
|
> - `static` MIME_TYPE
|
|
568
766
|
MIME type for Turbo Stream messages.
|
|
569
767
|
> - `get` length
|
|
@@ -578,14 +776,16 @@ Adds the element to the buffer, if config.buffer === true.
|
|
|
578
776
|
Fires the event 'element' with the added element.
|
|
579
777
|
> - clear()
|
|
580
778
|
Clears the buffer.
|
|
581
|
-
> - createReadableStream(opts
|
|
582
|
-
|
|
779
|
+
> - createReadableStream(opts, opts.continuous, streamOptions)
|
|
780
|
+
Creates a readable stream.
|
|
583
781
|
> - custom(action, target, content)
|
|
584
782
|
Adds a Turbo Stream Element with a custom action.
|
|
585
783
|
> - customAll(action, targets, content)
|
|
586
784
|
Adds a Turbo Stream Element with a custom action, targeting multiple DOM elements.
|
|
587
785
|
> - flush()
|
|
588
786
|
Renders this Turbo Stream message and clears the buffer.
|
|
787
|
+
> - refresh(requestIdOrAttributes)
|
|
788
|
+
Adds a Turbo Stream Element with the action 'refresh' to the message.
|
|
589
789
|
> - render()
|
|
590
790
|
Renders this Turbo Stream message if there are buffered elements.
|
|
591
791
|
> - renderElements()
|
|
@@ -593,6 +793,8 @@ If there are buffered elements, renders them and returns an array with the HTML
|
|
|
593
793
|
> - updateConfig(config)
|
|
594
794
|
Extends/Overwrites the configuration.
|
|
595
795
|
|
|
796
|
+
***
|
|
797
|
+
|
|
596
798
|
#### turbochargeKoa(koaApp, opts, opts.autoRender)
|
|
597
799
|
|
|
598
800
|
```javascript
|
|
@@ -656,8 +858,9 @@ Sends the rendered message as HTTP response with the correct MIME type.
|
|
|
656
858
|
#### Inherited from class [TurboStream](#class-turbostream):
|
|
657
859
|
|
|
658
860
|
> - `static` ACTIONS
|
|
659
|
-
List of all supported actions:
|
|
660
|
-
|
|
861
|
+
List of all supported official actions:
|
|
862
|
+
`append`, `prepend`, `replace`, `update`, `remove`, `before`, `after`
|
|
863
|
+
and `refresh`.
|
|
661
864
|
> - `static` MIME_TYPE
|
|
662
865
|
MIME type for Turbo Stream messages.
|
|
663
866
|
> - `get` length
|
|
@@ -672,14 +875,16 @@ Adds the element to the buffer, if config.buffer === true.
|
|
|
672
875
|
Fires the event 'element' with the added element.
|
|
673
876
|
> - clear()
|
|
674
877
|
Clears the buffer.
|
|
675
|
-
> - createReadableStream(opts
|
|
676
|
-
|
|
878
|
+
> - createReadableStream(opts, opts.continuous, streamOptions)
|
|
879
|
+
Creates a readable stream.
|
|
677
880
|
> - custom(action, target, content)
|
|
678
881
|
Adds a Turbo Stream Element with a custom action.
|
|
679
882
|
> - customAll(action, targets, content)
|
|
680
883
|
Adds a Turbo Stream Element with a custom action, targeting multiple DOM elements.
|
|
681
884
|
> - flush()
|
|
682
885
|
Renders this Turbo Stream message and clears the buffer.
|
|
886
|
+
> - refresh(requestIdOrAttributes)
|
|
887
|
+
Adds a Turbo Stream Element with the action 'refresh' to the message.
|
|
683
888
|
> - render()
|
|
684
889
|
Renders this Turbo Stream message if there are buffered elements.
|
|
685
890
|
> - renderElements()
|
|
@@ -687,6 +892,8 @@ If there are buffered elements, renders them and returns an array with the HTML
|
|
|
687
892
|
> - updateConfig(config)
|
|
688
893
|
Extends/Overwrites the configuration.
|
|
689
894
|
|
|
895
|
+
***
|
|
896
|
+
|
|
690
897
|
#### turbochargeExpress(expressApp, opts)
|
|
691
898
|
|
|
692
899
|
```javascript
|
|
@@ -780,8 +987,9 @@ Returns: {SseTurboStream} The instance for chaining.
|
|
|
780
987
|
#### Inherited from class [TurboStream](#class-turbostream):
|
|
781
988
|
|
|
782
989
|
> - `static` ACTIONS
|
|
783
|
-
List of all supported actions:
|
|
784
|
-
|
|
990
|
+
List of all supported official actions:
|
|
991
|
+
`append`, `prepend`, `replace`, `update`, `remove`, `before`, `after`
|
|
992
|
+
and `refresh`.
|
|
785
993
|
> - `get` length
|
|
786
994
|
The number of buffered Turbo Stream elements.
|
|
787
995
|
> - config
|
|
@@ -800,11 +1008,15 @@ Adds a Turbo Stream Element with a custom action.
|
|
|
800
1008
|
Adds a Turbo Stream Element with a custom action, targeting multiple DOM elements.
|
|
801
1009
|
> - flush()
|
|
802
1010
|
Renders this Turbo Stream message and clears the buffer.
|
|
1011
|
+
> - refresh(requestIdOrAttributes)
|
|
1012
|
+
Adds a Turbo Stream Element with the action 'refresh' to the message.
|
|
803
1013
|
> - renderElements()
|
|
804
1014
|
If there are buffered elements, renders them and returns an array with the HTML fragments.
|
|
805
1015
|
> - updateConfig(config)
|
|
806
1016
|
Extends/Overwrites the configuration.
|
|
807
1017
|
|
|
1018
|
+
***
|
|
1019
|
+
|
|
808
1020
|
## node-turbo/errors
|
|
809
1021
|
|
|
810
1022
|
### Class: ValidationError
|
|
@@ -818,6 +1030,8 @@ import { ValidationError } from 'node-turbo/errors';
|
|
|
818
1030
|
|
|
819
1031
|
Parent class for all validation errors.
|
|
820
1032
|
|
|
1033
|
+
***
|
|
1034
|
+
|
|
821
1035
|
### Class: AttributeMalformedError
|
|
822
1036
|
|
|
823
1037
|
```javascript
|
|
@@ -829,6 +1043,8 @@ import { AttributeMalformedError } from 'node-turbo/errors';
|
|
|
829
1043
|
|
|
830
1044
|
Gets thrown when mandatory attributes are malformed.
|
|
831
1045
|
|
|
1046
|
+
***
|
|
1047
|
+
|
|
832
1048
|
### Class: AttributeMissingError
|
|
833
1049
|
|
|
834
1050
|
```javascript
|
|
@@ -840,6 +1056,8 @@ import { AttributeMissingError } from 'node-turbo/errors';
|
|
|
840
1056
|
|
|
841
1057
|
Gets thrown when mandatory attributes are missing.
|
|
842
1058
|
|
|
1059
|
+
***
|
|
1060
|
+
|
|
843
1061
|
### Class: AttributeInvalidError
|
|
844
1062
|
|
|
845
1063
|
```javascript
|
|
@@ -851,6 +1069,8 @@ import { AttributeInvalidError } from 'node-turbo/errors';
|
|
|
851
1069
|
|
|
852
1070
|
Gets thrown when invalid attributes are discovered.
|
|
853
1071
|
|
|
1072
|
+
***
|
|
1073
|
+
|
|
854
1074
|
|
|
855
1075
|
***
|
|
856
1076
|
|
|
@@ -16,6 +16,7 @@ export class ExpressTurboStream extends TurboStream {
|
|
|
16
16
|
* Express' response object to send to.
|
|
17
17
|
*
|
|
18
18
|
* @type {Object}
|
|
19
|
+
* @since 1.0.0
|
|
19
20
|
*/
|
|
20
21
|
res;
|
|
21
22
|
|
|
@@ -25,6 +26,7 @@ export class ExpressTurboStream extends TurboStream {
|
|
|
25
26
|
* @param {Object} res - Express' response object to send to.
|
|
26
27
|
* @param {Object} [attributes] - Attributes of the added element.
|
|
27
28
|
* @param {String} [content] - The HTML content of the added element.
|
|
29
|
+
* @since 1.0.0
|
|
28
30
|
*/
|
|
29
31
|
constructor(res, ...args) {
|
|
30
32
|
super(...args);
|
|
@@ -33,9 +35,12 @@ export class ExpressTurboStream extends TurboStream {
|
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* Sends the rendered message as HTTP response with the correct MIME type.
|
|
38
|
+
*
|
|
39
|
+
* @since 1.0.0
|
|
36
40
|
*/
|
|
37
41
|
send() {
|
|
38
42
|
this.res.type(TurboStream.MIME_TYPE);
|
|
39
43
|
this.res.send(this.render());
|
|
40
44
|
}
|
|
45
|
+
|
|
41
46
|
}
|
|
@@ -16,13 +16,15 @@ export class KoaTurboStream extends TurboStream {
|
|
|
16
16
|
* Koa's context object.
|
|
17
17
|
*
|
|
18
18
|
* @type {Object}
|
|
19
|
-
* @see https://koajs.com/#context
|
|
19
|
+
* @see https://koajs.com/#context
|
|
20
|
+
* @since 1.0.0
|
|
20
21
|
*/
|
|
21
22
|
koaCtx;
|
|
22
23
|
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* @param {Object} koaCtx - Koa's context object.
|
|
27
|
+
* @since 1.0.0
|
|
26
28
|
*/
|
|
27
29
|
constructor(koaCtx) {
|
|
28
30
|
super();
|
package/lib/turbo-element.js
CHANGED
|
@@ -11,6 +11,7 @@ export class TurboElement {
|
|
|
11
11
|
/**
|
|
12
12
|
* The attribute object.
|
|
13
13
|
* @type {Object<String, String>}
|
|
14
|
+
* @since 1.0.0
|
|
14
15
|
*/
|
|
15
16
|
attributes = {};
|
|
16
17
|
|
|
@@ -18,6 +19,7 @@ export class TurboElement {
|
|
|
18
19
|
/**
|
|
19
20
|
* The HTML content.
|
|
20
21
|
* @type {String}
|
|
22
|
+
* @since 1.0.0
|
|
21
23
|
*/
|
|
22
24
|
content = '';
|
|
23
25
|
|
|
@@ -27,6 +29,7 @@ export class TurboElement {
|
|
|
27
29
|
*
|
|
28
30
|
* @param {Object} attributes - The attributes of this element.
|
|
29
31
|
* @param {String} content - The HTML content of this element.
|
|
32
|
+
* @since 1.0.0
|
|
30
33
|
*/
|
|
31
34
|
constructor(attributes, content) {
|
|
32
35
|
this.attributes = attributes;
|
|
@@ -41,19 +44,21 @@ export class TurboElement {
|
|
|
41
44
|
* of HTML attributes ({ name: value } -> 'name="value"').
|
|
42
45
|
*
|
|
43
46
|
* @returns {String} The HTML attribute string.
|
|
47
|
+
* @since 1.0.0
|
|
44
48
|
*/
|
|
45
49
|
renderAttributesAsHtml() {
|
|
46
50
|
return Object.entries(this.attributes)
|
|
47
|
-
.map(([name, value]) => `${name}="${value}"`)
|
|
51
|
+
.map(([name, value]) => (value !== null) ? `${name}="${value}"` : name)
|
|
48
52
|
.join(' ');
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
|
|
52
|
-
/* c8 ignore next
|
|
56
|
+
/* c8 ignore next 19 */
|
|
53
57
|
|
|
54
58
|
/**
|
|
55
59
|
* Validation function to implement.
|
|
56
60
|
* @abstract
|
|
61
|
+
* @since 1.0.0
|
|
57
62
|
*/
|
|
58
63
|
validate() {
|
|
59
64
|
throw new Error('validate() not implemented.');
|
|
@@ -63,6 +68,7 @@ export class TurboElement {
|
|
|
63
68
|
/**
|
|
64
69
|
* Render function to implement.
|
|
65
70
|
* @abstract
|
|
71
|
+
* @since 1.0.0
|
|
66
72
|
*/
|
|
67
73
|
render() {
|
|
68
74
|
throw new Error('render() not implemented.');
|
package/lib/turbo-frame.js
CHANGED
|
@@ -17,6 +17,7 @@ export class TurboFrame extends TurboElement {
|
|
|
17
17
|
*
|
|
18
18
|
* @type {String}
|
|
19
19
|
* @static
|
|
20
|
+
* @since 1.0.0
|
|
20
21
|
*/
|
|
21
22
|
static HEADER_KEY = 'turbo-frame';
|
|
22
23
|
|
|
@@ -26,6 +27,7 @@ export class TurboFrame extends TurboElement {
|
|
|
26
27
|
*
|
|
27
28
|
* @type {String}
|
|
28
29
|
* @static
|
|
30
|
+
* @since 1.0.0
|
|
29
31
|
*/
|
|
30
32
|
static MIME_TYPE = 'text/html';
|
|
31
33
|
|
|
@@ -34,6 +36,7 @@ export class TurboFrame extends TurboElement {
|
|
|
34
36
|
* @param {String|Object} idOrAttributes - Either the ID as string or an object
|
|
35
37
|
* containing all attributes (including `id`).
|
|
36
38
|
* @param {String} content - The HTML content of this Turbo Frame message.
|
|
39
|
+
* @since 1.0.0
|
|
37
40
|
*/
|
|
38
41
|
constructor(idOrAttributes, content) {
|
|
39
42
|
if (typeof idOrAttributes === 'string') {
|
|
@@ -51,6 +54,7 @@ export class TurboFrame extends TurboElement {
|
|
|
51
54
|
*
|
|
52
55
|
* @throws {AttributeMissingError} when mandatory attributes are missing.
|
|
53
56
|
* @throws {AttributeMalformedError} when mandatory attributes are malformed.
|
|
57
|
+
* @since 1.0.0
|
|
54
58
|
*/
|
|
55
59
|
validate() {
|
|
56
60
|
if (typeof this.attributes === 'undefined' || !isPlainObject(this.attributes) || !('id' in this.attributes)) {
|
|
@@ -71,6 +75,7 @@ export class TurboFrame extends TurboElement {
|
|
|
71
75
|
* Renders the Turbo Frame message as HTML string and returns it.
|
|
72
76
|
*
|
|
73
77
|
* @returns {String} The rendered HTML.
|
|
78
|
+
* @since 1.0.0
|
|
74
79
|
*/
|
|
75
80
|
render() {
|
|
76
81
|
return `<turbo-frame ${this.renderAttributesAsHtml()}>${this.content}</turbo-frame>`;
|