6
6
import { v4 as uuidv4 } from "uuid" ;
7
7
import { Emitter } from ".." ;
8
8
9
- import { CloudEventV1 , CloudEventV1Attributes , CloudEventV1OptionalAttributes } from "./interfaces" ;
9
+ import { CloudEventV1 } from "./interfaces" ;
10
10
import { validateCloudEvent } from "./spec" ;
11
11
import { ValidationError , isBinary , asBase64 , isValidType } from "./validation" ;
12
12
@@ -23,7 +23,7 @@ export const enum Version {
23
23
* interoperability across services, platforms and systems.
24
24
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md
25
25
*/
26
- export class CloudEvent implements CloudEventV1 {
26
+ export class CloudEvent < T = undefined > implements CloudEventV1 < T > {
27
27
id : string ;
28
28
type : string ;
29
29
source : string ;
@@ -32,7 +32,7 @@ export class CloudEvent implements CloudEventV1 {
32
32
dataschema ?: string ;
33
33
subject ?: string ;
34
34
time ?: string ;
35
- #_data?: Record < string , unknown | string | number | boolean > | string | number | boolean | null | unknown ;
35
+ #_data?: T ;
36
36
data_base64 ?: string ;
37
37
38
38
// Extensions should not exist as it's own object, but instead
@@ -51,7 +51,7 @@ export class CloudEvent implements CloudEventV1 {
51
51
* @param {object } event the event properties
52
52
* @param {boolean? } strict whether to perform event validation when creating the object - default: true
53
53
*/
54
- constructor ( event : CloudEventV1 | CloudEventV1Attributes , strict = true ) {
54
+ constructor ( event : Partial < CloudEventV1 < T > > , strict = true ) {
55
55
// copy the incoming event so that we can delete properties as we go
56
56
// everything left after we have deleted know properties becomes an extension
57
57
const properties = { ...event } ;
@@ -62,10 +62,10 @@ export class CloudEvent implements CloudEventV1 {
62
62
this . time = properties . time || new Date ( ) . toISOString ( ) ;
63
63
delete properties . time ;
64
64
65
- this . type = properties . type ;
65
+ this . type = properties . type as string ;
66
66
delete ( properties as any ) . type ;
67
67
68
- this . source = properties . source ;
68
+ this . source = properties . source as string ;
69
69
delete ( properties as any ) . source ;
70
70
71
71
this . specversion = ( properties . specversion as Version ) || Version . V1 ;
@@ -126,13 +126,13 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`);
126
126
Object . freeze ( this ) ;
127
127
}
128
128
129
- get data ( ) : unknown {
129
+ get data ( ) : T | undefined {
130
130
return this . #_data;
131
131
}
132
132
133
- set data ( value : unknown ) {
133
+ set data ( value : T | undefined ) {
134
134
if ( isBinary ( value ) ) {
135
- this . data_base64 = asBase64 ( value as Uint32Array ) ;
135
+ this . data_base64 = asBase64 ( value ) ;
136
136
}
137
137
this . #_data = value ;
138
138
}
@@ -184,16 +184,29 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`);
184
184
185
185
/**
186
186
* Clone a CloudEvent with new/update attributes
187
- * @param {object } options attributes to augment the CloudEvent with
187
+ * @param {object } options attributes to augment the CloudEvent with an `data` property
188
+ * @param {boolean } strict whether or not to use strict validation when cloning (default: true)
189
+ * @throws if the CloudEvent does not conform to the schema
190
+ * @return {CloudEvent } returns a new CloudEvent<T>
191
+ */
192
+ public cloneWith ( options : Partial < Exclude < CloudEventV1 < never > , "data" > > , strict ?: boolean ) : CloudEvent < T > ;
193
+ /**
194
+ * Clone a CloudEvent with new/update attributes
195
+ * @param {object } options attributes to augment the CloudEvent with a `data` property
196
+ * @param {boolean } strict whether or not to use strict validation when cloning (default: true)
197
+ * @throws if the CloudEvent does not conform to the schema
198
+ * @return {CloudEvent } returns a new CloudEvent<D>
199
+ */
200
+ public cloneWith < D > ( options : Partial < CloudEvent < D > > , strict ?: boolean ) : CloudEvent < D > ;
201
+ /**
202
+ * Clone a CloudEvent with new/update attributes
203
+ * @param {object } options attributes to augment the CloudEvent
188
204
* @param {boolean } strict whether or not to use strict validation when cloning (default: true)
189
205
* @throws if the CloudEvent does not conform to the schema
190
206
* @return {CloudEvent } returns a new CloudEvent
191
207
*/
192
- public cloneWith (
193
- options : CloudEventV1 | CloudEventV1Attributes | CloudEventV1OptionalAttributes ,
194
- strict = true ,
195
- ) : CloudEvent {
196
- return new CloudEvent ( Object . assign ( { } , this . toJSON ( ) , options ) as CloudEvent , strict ) ;
208
+ public cloneWith < D > ( options : Partial < CloudEventV1 < D > > , strict = true ) : CloudEvent < D | T > {
209
+ return new CloudEvent ( Object . assign ( { } , this . toJSON ( ) , options ) , strict ) ;
197
210
}
198
211
199
212
/**
0 commit comments