66import { v4 as uuidv4 } from "uuid" ;
77import { Emitter } from ".." ;
88
9- import { CloudEventV1 , CloudEventV1Attributes , CloudEventV1OptionalAttributes } from "./interfaces" ;
9+ import { CloudEventV1 } from "./interfaces" ;
1010import { validateCloudEvent } from "./spec" ;
1111import { ValidationError , isBinary , asBase64 , isValidType } from "./validation" ;
1212
@@ -23,7 +23,7 @@ export const enum Version {
2323 * interoperability across services, platforms and systems.
2424 * @see https://github.com/cloudevents/spec/blob/v1.0/spec.md
2525 */
26- export class CloudEvent implements CloudEventV1 {
26+ export class CloudEvent < T = undefined > implements CloudEventV1 < T > {
2727 id : string ;
2828 type : string ;
2929 source : string ;
@@ -32,7 +32,7 @@ export class CloudEvent implements CloudEventV1 {
3232 dataschema ?: string ;
3333 subject ?: string ;
3434 time ?: string ;
35- #_data?: Record < string , unknown | string | number | boolean > | string | number | boolean | null | unknown ;
35+ #_data?: T ;
3636 data_base64 ?: string ;
3737
3838 // Extensions should not exist as it's own object, but instead
@@ -51,7 +51,7 @@ export class CloudEvent implements CloudEventV1 {
5151 * @param {object } event the event properties
5252 * @param {boolean? } strict whether to perform event validation when creating the object - default: true
5353 */
54- constructor ( event : CloudEventV1 | CloudEventV1Attributes , strict = true ) {
54+ constructor ( event : Partial < CloudEventV1 < T > > , strict = true ) {
5555 // copy the incoming event so that we can delete properties as we go
5656 // everything left after we have deleted know properties becomes an extension
5757 const properties = { ...event } ;
@@ -62,10 +62,10 @@ export class CloudEvent implements CloudEventV1 {
6262 this . time = properties . time || new Date ( ) . toISOString ( ) ;
6363 delete properties . time ;
6464
65- this . type = properties . type ;
65+ this . type = properties . type as string ;
6666 delete ( properties as any ) . type ;
6767
68- this . source = properties . source ;
68+ this . source = properties . source as string ;
6969 delete ( properties as any ) . source ;
7070
7171 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`);
126126 Object . freeze ( this ) ;
127127 }
128128
129- get data ( ) : unknown {
129+ get data ( ) : T | undefined {
130130 return this . #_data;
131131 }
132132
133- set data ( value : unknown ) {
133+ set data ( value : T | undefined ) {
134134 if ( isBinary ( value ) ) {
135- this . data_base64 = asBase64 ( value as Uint32Array ) ;
135+ this . data_base64 = asBase64 ( value ) ;
136136 }
137137 this . #_data = value ;
138138 }
@@ -184,16 +184,29 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`);
184184
185185 /**
186186 * 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
188204 * @param {boolean } strict whether or not to use strict validation when cloning (default: true)
189205 * @throws if the CloudEvent does not conform to the schema
190206 * @return {CloudEvent } returns a new CloudEvent
191207 */
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 ) ;
197210 }
198211
199212 /**
0 commit comments