From d2a43ea0cb640cee14f951f0bc6ae94fd2225005 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Mon, 27 Sep 2021 18:23:16 -0700 Subject: [PATCH] feat: add native logging with headers and body to CloudEvent Signed-off-by: Grant Timmerman --- src/event/cloudevent.ts | 8 ++++++++ test/integration/cloud_event_test.ts | 5 +++++ 2 files changed, 13 insertions(+) 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 });