Skip to content
Merged
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
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"astro": "npm:astro@^7.0.0",
"byte-encodings": "npm:byte-encodings@^1.0.11",
"chalk": "npm:chalk@^5.6.2",
"elysia": "npm:elysia@^1.3.8",
"es-toolkit": "npm:es-toolkit@^1.46.1",
"h3": "npm:h3@^1.15.0",
"hono": "jsr:@hono/hono@^4.8.3",
Expand Down
111 changes: 61 additions & 50 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/elysia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"build:self": "tsdown",
"build": "pnpm --filter @fedify/elysia... run build:self",
"prepack": "pnpm build",
"prepublish": "pnpm build"
"prepublish": "pnpm build",
"test": "node --experimental-transform-types --test",
"test:bun": "bun test src/"
},
"peerDependencies": {
"elysia": "^1.3.6",
Expand Down
49 changes: 49 additions & 0 deletions packages/elysia/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Elysia } from "elysia";
import { strict as assert } from "node:assert";
import { describe, test } from "node:test";
import { fedify } from "./index.ts";

interface MockFederation {
fetch(request: Request, options: unknown): Promise<Response>;
}

describe("[elysia] fedify() plugin", () => {
test("fedify() calls the context data factory when handling a request", async () => {
const elysia = new Elysia();
const mockFederation: MockFederation = {
fetch: () => Promise.resolve(new Response("OK")),
};
let count = 0;
const mockContextDataFactory = () => {
// increases count if this method called
count++;
};

elysia.use(fedify(mockFederation as never, mockContextDataFactory));
await elysia.handle(new Request("http://localhost/"));

assert.strictEqual(count, 1, "the context data factory must be called");
});

test("fedify() calls federation.fetch() with the context data", async () => {
const elysia = new Elysia();
const mockFederation: MockFederation = {
fetch: (_req, opts) => {
// return context data
const contextData = (opts as { contextData: string }).contextData;
return Promise.resolve(new Response(contextData));
},
};
const mockContextDataFactory = () => "Hello World";

elysia.use(fedify(mockFederation as never, mockContextDataFactory));
const actual = await elysia.handle(new Request("http://localhost/"))
.then((res) => res.text());

assert.strictEqual(
actual,
"Hello World",
"federation.fetch() must receive the context data returned by the factory",
);
});
});
Loading
Loading