Skip to content

Commit b38a48f

Browse files
grantlance
andauthored
feat: add native logging with headers and body to CloudEvent (#437)
* feat: add native logging with headers and body to CloudEvent Signed-off-by: Grant Timmerman <timmerman+devrel@google.com> * ci: simplify Signed-off-by: Grant Timmerman <timmerman+devrel@google.com> * ci: simplify Signed-off-by: Grant Timmerman <timmerman+devrel@google.com> * ci: returns to return Signed-off-by: Grant Timmerman <timmerman+devrel@google.com> * ci: Update test/integration/cloud_event_test.ts Co-authored-by: Lance Ball <lball@redhat.com> * ci: revert symbol change Signed-off-by: Grant Timmerman <timmerman+devrel@google.com> * refactor: improve ts for toString Signed-off-by: Grant Timmerman <timmerman+devrel@google.com> Co-authored-by: Lance Ball <lball@redhat.com>
1 parent 2ff7852 commit b38a48f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/event/cloudevent.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,12 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`);
195195
): CloudEvent {
196196
return new CloudEvent(Object.assign({}, this.toJSON(), options) as CloudEvent, strict);
197197
}
198+
199+
/**
200+
* The native `console.log` value of the CloudEvent.
201+
* @return {string} The string representation of the CloudEvent.
202+
*/
203+
[Symbol.for("nodejs.util.inspect.custom")](): string {
204+
return this.toString();
205+
}
198206
}

test/integration/cloud_event_test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ describe("A CloudEvent", () => {
5555
expect(new CloudEvent(JSON.parse(JSON.stringify(ce)))).to.deep.equal(ce);
5656
});
5757

58+
it("serializes as JSON with raw log", () => {
59+
const ce = new CloudEvent({ ...fixture, data: { lunch: "tacos" } });
60+
const inspectSymbol = (Symbol.for("nodejs.util.inspect.custom") as unknown) as string;
61+
const ceToString = (ce[inspectSymbol] as CallableFunction).bind(ce);
62+
expect(ce.toString()).to.deep.equal(ceToString());
63+
});
64+
5865
it("Throw a validation error for invalid extension names", () => {
5966
expect(() => {
6067
new CloudEvent({ "ext-1": "extension1", ...fixture });

0 commit comments

Comments
 (0)