diff --git a/src/event/cloudevent.ts b/src/event/cloudevent.ts index 870d866..999ff98 100644 --- a/src/event/cloudevent.ts +++ b/src/event/cloudevent.ts @@ -9,6 +9,7 @@ import { Emitter } from ".."; import { CloudEventV1, CloudEventV1Attributes, CloudEventV1OptionalAttributes } from "./interfaces"; import { validateCloudEvent } from "./spec"; import { ValidationError, isBinary, asBase64, isValidType } from "./validation"; +import * as util from 'util'; /** * An enum representing the CloudEvent specification version @@ -195,4 +196,11 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`); ): CloudEvent { return new CloudEvent(Object.assign({}, this.toJSON(), options) as CloudEvent, strict); } + + /** + * The native `console.log` value of the CloudEvent. + */ + [util.inspect.custom](depth: any, opts: any) { + return this.toString(); + } } diff --git a/test/integration/cloud_event_test.ts b/test/integration/cloud_event_test.ts index 1c7ee42..e3f8759 100644 --- a/test/integration/cloud_event_test.ts +++ b/test/integration/cloud_event_test.ts @@ -55,6 +55,11 @@ describe("A CloudEvent", () => { expect(new CloudEvent(JSON.parse(JSON.stringify(ce)))).to.deep.equal(ce); }); + it("serializes as JSON with raw log", () => { + const ce = new CloudEvent({ ...fixture, data: { lunch: "tacos" } }); + expect(ce.toString()).to.deep.equal(JSON.stringify(ce)); + }); + it("Throw a validation error for invalid extension names", () => { expect(() => { new CloudEvent({ "ext-1": "extension1", ...fixture });