Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ const mailerlite = new MailerLite({
});
```

### Edge & serverless runtimes

The SDK works in edge/serverless runtimes that don't ship Node's `http`
module — such as Vercel Edge Functions, Cloudflare Workers, Next.js edge
routes and Deno. Requests automatically fall back to the `fetch` adapter
in those environments, while Node and browsers keep using their existing
adapters unchanged.

- [Subscribers](src/modules/subscribers/README.md)
* [List all subscribers](src/modules/subscribers/README.md#list-all-subscribers)
* [Create/update subscriber](src/modules/subscribers/README.md#createupdate-subscriber)
Expand Down
30 changes: 30 additions & 0 deletions src/utils/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, it, expect, vi, beforeEach } from "vitest";

// `vi.mock` is hoisted to the top of the file, so the mock fn has to be
// created with `vi.hoisted` to be available inside the factory.
const axiosMock = vi.hoisted(() => vi.fn(() => Promise.resolve({ data: {} })));
vi.mock("axios", () => ({ default: axiosMock }));

import request from "./fetch";
import { Config } from "./types";

const config: Config = {
api_key: "test-key",
basePath: "https://connect.mailerlite.com",
};

describe("request adapter selection", () => {
beforeEach(() => {
axiosMock.mockClear();
});

it("asks axios to fall back through http -> xhr -> fetch", async () => {
await request("/api/subscribers", { method: "GET" }, config);

expect(axiosMock).toHaveBeenCalledTimes(1);
const passedConfig = axiosMock.mock.calls[0][0] as { adapter: unknown };
// Ordering matters: Node/browser keep their existing adapter, while
// edge/serverless runtimes fall back to `fetch`. See issue #31.
expect(passedConfig.adapter).toEqual(["http", "xhr", "fetch"]);
});
});

@Mantas97 Mantas97 Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding one more test along with the fix:

describe("request body handling", () => {
    beforeEach(() => {
        axiosMock.mockClear();
    });

    // Regression: `data: body && JSON.stringify(body)` yielded `null` on
    // bodyless calls. Node's http adapter ignores that, but workerd throws
    // "Request with a GET or HEAD method cannot have a body", breaking every
    // read call on Cloudflare Workers once the fetch adapter is in play.
    it("omits `data` entirely when there is no body", async () => {
        await request("/api/subscribers", { method: "GET" }, config);

        const passedConfig = axiosMock.mock.calls[0][0] as { data: unknown };
        expect(passedConfig.data).toBeUndefined();
        expect(passedConfig.data).not.toBeNull();
    });

    it("still serializes a body when one is given", async () => {
        await request(
            "/api/subscribers",
            { method: "POST", body: { email: "test@example.com" } },
            config
        );

        const passedConfig = axiosMock.mock.calls[0][0] as { data: unknown };
        expect(passedConfig.data).toBe('{"email":"test@example.com"}');
    });
})

7 changes: 6 additions & 1 deletion src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export default function request(endpoint: string = "", options: Options, config:
"Content-type": "application/json",
"accept-encoding": "null" // needed for axios
},
data: body && JSON.stringify(body)
data: body && JSON.stringify(body),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data: body && JSON.stringify(body),
// Must be `undefined`, not `null`, when there is no body: workerd
// (Cloudflare Workers) rejects a GET/HEAD Request that carries any
// body property, even a null one. Node's http adapter tolerates it.
data: body ? JSON.stringify(body) : undefined,

// Let axios pick the first adapter supported by the current runtime.
// Node keeps using `http` and browsers keep using `xhr` (no behaviour
// change), while edge/serverless runtimes (Vercel Edge, Cloudflare
// Workers, Deno, Next.js edge) that ship neither fall back to `fetch`.
adapter: ["http", "xhr", "fetch"]
})
}

Expand Down