Skip to content

Commit

Permalink
feat: fast fail when sending no events (#532)
Browse files Browse the repository at this point in the history
When the events payload is empty don't bother sending the request, fail
fast.
  • Loading branch information
JasonBerry authored May 22, 2024
1 parent fe023b5 commit de26c37
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/__tests__/_sendEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,14 @@ describe("sendEvents", () => {
}
);
});

it("should fail fast when sending no events via sendEvents", async () => {
const result = analyticsInstance.sendEvents([]);

expect(result).toBeInstanceOf(Promise);
expect(await result).toBe(false);
expect(fakeRequestFn).toHaveBeenCalledTimes(0);
});
});

it("applies default credentials when no custom ones are provided", () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/_sendEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export function makeSendEvents(requestFn: RequestFnType) {
return payload;
});

if (events.length === 0) {
return Promise.resolve(false);
}

return sendRequest(
requestFn,
this._ua,
Expand Down

0 comments on commit de26c37

Please sign in to comment.