Skip to content

Commit

Permalink
migrate to react-router
Browse files Browse the repository at this point in the history
  • Loading branch information
rphlmr committed Nov 23, 2024
1 parent 98af0e7 commit 9eb35f2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Context } from "hono";

import { createWorkersKVSessionStorage } from "@react-router/cloudflare";
import { createMiddleware } from "hono/factory";
import { cacheHeader } from "pretty-cache-header";
import {
CookieOptions,
SessionData,
createWorkersKVSessionStorage,
createCookieSessionStorage,
} from "@remix-run/cloudflare";
import { createMiddleware } from "hono/factory";
import { cacheHeader } from "pretty-cache-header";
} from "react-router";

import { session } from "./session.js";

Expand Down
6 changes: 3 additions & 3 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AppLoadContext, ServerBuild } from "@remix-run/server-runtime";
import type { Context } from "hono";
import type { AppLoadContext, ServerBuild } from "react-router";

import { createRequestHandler } from "@remix-run/server-runtime";
import { createMiddleware } from "hono/factory";
import { createRequestHandler } from "react-router";

export interface RemixMiddlewareOptions {
build: ServerBuild;
Expand All @@ -25,4 +25,4 @@ export function remix({
});
}

export { createRequestHandler } from "@remix-run/server-runtime";
export { createRequestHandler } from "react-router";
15 changes: 7 additions & 8 deletions src/i18next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import type { Context } from "hono";
import type { RemixI18NextOption } from "remix-i18next/server";

import { createMiddleware } from "hono/factory";
import { Namespace, TFunction } from "i18next";
import { FlatNamespace, TFunction } from "i18next";
import { RemixI18Next } from "remix-i18next/server";

const i18nSymbol = Symbol();
const LocaleSymbol = Symbol();
const TSymbol = Symbol();
const i18nSymbol = Symbol().toString();
const LocaleSymbol = Symbol().toString();
const TSymbol = Symbol().toString();

export function i18next(options: RemixI18NextOption | RemixI18Next) {
return createMiddleware(async (c, next) => {
Expand Down Expand Up @@ -46,10 +46,9 @@ i18next.getLocale = function getLocale(c: Context) {
return locale;
};

i18next.getFixedT = function getFixedT<Ns extends Namespace = "translation">(
c: Context,
{ namespace }: { namespace?: Ns } = {},
) {
i18next.getFixedT = function getFixedT<
Ns extends FlatNamespace = "translation",
>(c: Context, { namespace }: { namespace?: Ns } = {}) {
// If `namespace` is set, we return a new `t` function that is bound to the
// given namespace. Otherwise, we return the default `t` function.
if (namespace) {
Expand Down
10 changes: 3 additions & 7 deletions src/session.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import type {
Session,
SessionData,
SessionStorage,
} from "@remix-run/server-runtime";
import type { Context } from "hono";
import type { Session, SessionData, SessionStorage } from "react-router";

import { createMiddleware } from "hono/factory";

const sessionStorageSymbol = Symbol();
const sessionSymbol = Symbol();
const sessionStorageSymbol = Symbol().toString();
const sessionSymbol = Symbol().toString();

export function session<Data = SessionData, FlashData = Data>(options: {
autoCommit?: boolean;
Expand Down
12 changes: 7 additions & 5 deletions test/cloudflare.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {
createCookieSessionStorage,
createWorkersKVSessionStorage,
} from "@remix-run/cloudflare";
import { createWorkersKVSessionStorage } from "@react-router/cloudflare";
import { Context } from "hono";
import { createMiddleware } from "hono/factory";
import { createCookieSessionStorage } from "react-router";
import { describe, test, expect, vi, beforeEach, afterAll } from "vitest";

import {
Expand All @@ -13,9 +11,13 @@ import {
} from "../src/cloudflare";
import { session } from "../src/session";

vi.mock("@remix-run/cloudflare", () => {
vi.mock("@react-router/cloudflare", () => {
return {
createWorkersKVSessionStorage: vi.fn(),
};
});
vi.mock("react-router", () => {
return {
createCookieSessionStorage: vi.fn(),
};
});
Expand Down
9 changes: 2 additions & 7 deletions test/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ServerBuild } from "@remix-run/server-runtime";
import type { ServerBuild } from "react-router";

import { Hono } from "hono";
import { describe, test, expect, vi, beforeEach, afterAll } from "vitest";
Expand Down Expand Up @@ -29,11 +29,7 @@ const build = {
default: () => new Response("body"),
},
},
future: {
v3_fetcherPersist: true,
v3_relativeSplatPath: true,
v3_throwAbortReason: true,
},
future: {},
publicPath: "/",
routes: {
root: {
Expand All @@ -45,7 +41,6 @@ const build = {
},
},
isSpaMode: false,
mode: "production",
} satisfies ServerBuild;

describe(remix.name, () => {
Expand Down
14 changes: 7 additions & 7 deletions test/session.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createCookieSessionStorage } from "@remix-run/cloudflare";
import { Context } from "hono";
import { createCookieSessionStorage } from "react-router";
import { describe, test, expect, vi, beforeEach, afterAll } from "vitest";

import { getSession, getSessionStorage, session } from "../src/session";

vi.mock("@remix-run/node", () => {
vi.mock("@react-router/node", () => {
return {
createCookieSessionStorage: vi.fn(),
};
Expand Down Expand Up @@ -64,7 +64,7 @@ describe(session.name, () => {
expect(createSessionStorage).toHaveBeenCalledOnce();
expect(c.set).toHaveBeenNthCalledWith(
1,
expect.any(Symbol),
expect.any(String),
sessionStorage,
);
expect(next).toHaveBeenCalledOnce();
Expand All @@ -84,7 +84,7 @@ describe(session.name, () => {
expect(createSessionStorage).toHaveBeenCalledOnce();
expect(c.set).toHaveBeenNthCalledWith(
1,
expect.any(Symbol),
expect.any(String),
sessionStorage,
);
expect(spy.getSession).toHaveBeenCalledOnce();
Expand All @@ -93,7 +93,7 @@ describe(session.name, () => {

expect(c.set).toHaveBeenNthCalledWith(
2,
expect.any(Symbol),
expect.any(String),
sessionInContext,
);
expect(next).toHaveBeenCalledOnce();
Expand Down Expand Up @@ -121,7 +121,7 @@ describe(getSessionStorage.name, () => {
"A session middleware was not set.",
);

expect(c.get).toHaveBeenCalledWith(expect.any(Symbol));
expect(c.get).toHaveBeenCalledWith(expect.any(String));
});

test("returns session storage", async () => {
Expand All @@ -147,7 +147,7 @@ describe(getSession.name, () => {
"A session middleware was not set.",
);

expect(c.get).toHaveBeenCalledWith(expect.any(Symbol));
expect(c.get).toHaveBeenCalledWith(expect.any(String));
});

test("returns session", async () => {
Expand Down

0 comments on commit 9eb35f2

Please sign in to comment.