@hulla/api-react-router 0.0.0-stage → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ import { FetchServerErrorInput } from "@hulla/api/fetch";
2
+ import { Awaitable, ServerAdapter, ServerContextInput, ServerExecutableFor } from "@hulla/api/server";
3
+ import { Contract } from "@hulla/api";
4
+ //#region src/server.d.ts
5
+ type ReactRouterRouteParams = Readonly<Record<string, string | undefined>>;
6
+ type ReactRouterHandlerInput<LoadContext = unknown, Params extends ReactRouterRouteParams = ReactRouterRouteParams> = {
7
+ readonly context: LoadContext;
8
+ readonly params: Params;
9
+ readonly request: Request;
10
+ };
11
+ type ReactRouterRouteHandler<LoadContext = unknown, Params extends ReactRouterRouteParams = ReactRouterRouteParams> = (input: ReactRouterHandlerInput<LoadContext, Params>) => Promise<Response>;
12
+ type ReactRouterAdapterContext<LoadContext, Params extends ReactRouterRouteParams> = {
13
+ readonly request: Request;
14
+ readonly reactRouterContext: LoadContext;
15
+ readonly reactRouterParams: Params;
16
+ };
17
+ type ReactRouterContextInput<ContractType extends Contract = Contract, LoadContext = unknown, Params extends ReactRouterRouteParams = ReactRouterRouteParams> = ServerContextInput<ContractType> & ReactRouterAdapterContext<LoadContext, Params>;
18
+ type ReactRouterServerErrorInput<LoadContext = unknown, Params extends ReactRouterRouteParams = ReactRouterRouteParams> = Omit<FetchServerErrorInput<ReactRouterHandlerInput<LoadContext, Params>>, 'handlerContext'> & {
19
+ readonly reactRouterContext: LoadContext;
20
+ readonly reactRouterParams: Params;
21
+ };
22
+ type ReactRouterServerOptions<LoadContext = unknown, Params extends ReactRouterRouteParams = ReactRouterRouteParams> = {
23
+ readonly onError?: ((input: ReactRouterServerErrorInput<LoadContext, Params>) => Awaitable<Response | undefined | void>) | undefined;
24
+ };
25
+ type ReactRouterRouteHandlers<LoadContext = unknown, Params extends ReactRouterRouteParams = ReactRouterRouteParams> = {
26
+ readonly action: ReactRouterRouteHandler<LoadContext, Params>;
27
+ readonly loader: ReactRouterRouteHandler<LoadContext, Params>;
28
+ };
29
+ type ReactRouterAdapter<LoadContext = unknown, Params extends ReactRouterRouteParams = ReactRouterRouteParams> = ServerAdapter<'react-router', ReactRouterAdapterContext<LoadContext, Params>> & {
30
+ readonly mount: <const ContractType extends Contract, const Context extends object>(implementation: ServerExecutableFor<ContractType, Context, 'react-router', ReactRouterAdapterContext<LoadContext, Params>>, options?: ReactRouterServerOptions<LoadContext, Params>) => ReactRouterRouteHandlers<LoadContext, Params>;
31
+ };
32
+ /** Creates a React Router v7 adapter with native route context and mounting operations. */
33
+ declare function reactRouterAdapter<LoadContext = unknown, Params extends ReactRouterRouteParams = ReactRouterRouteParams>(options?: ReactRouterServerOptions<LoadContext, Params>): ReactRouterAdapter<LoadContext, Params>;
34
+ //#endregion
35
+ export { type ReactRouterAdapter, type ReactRouterContextInput, type ReactRouterHandlerInput, type ReactRouterRouteHandler, type ReactRouterRouteHandlers, type ReactRouterRouteParams, type ReactRouterServerErrorInput, type ReactRouterServerOptions, reactRouterAdapter };
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ import { createAdapterRuntime } from "@hulla/api/adapters";
2
+ import { createFetchHandler } from "@hulla/api/fetch";
3
+ import { createServerAdapter } from "@hulla/api/server";
4
+
5
+ //#region src/server.ts
6
+ /** Creates the loader and action exported by a React Router v7 resource route. */
7
+ function createResourceRouteHandlers(implementation, options = {}) {
8
+ const unsupported = createAdapterRuntime(implementation).routes.filter((route) => route.method === "QUERY");
9
+ if (unsupported.length > 0) throw new TypeError(`React Router resource routes do not support QUERY routes: ${unsupported.map((route) => route.key.join(".")).join(", ")}`);
10
+ const handler = createFetchHandler(implementation, {
11
+ contextAdapter: "react-router",
12
+ contextInput: (_request, input) => ({
13
+ reactRouterContext: input.context,
14
+ reactRouterParams: input.params
15
+ }),
16
+ ...options.onError === void 0 ? {} : { onError: ({ handlerContext, ...input }) => options.onError?.({
17
+ ...input,
18
+ reactRouterContext: handlerContext.context,
19
+ reactRouterParams: handlerContext.params
20
+ }) }
21
+ });
22
+ const action = (input) => handler(input.request, input);
23
+ const loader = async (input) => {
24
+ if (input.request.method !== "HEAD") return handler(input.request, input);
25
+ const response = await handler(new Request(input.request, { method: "GET" }), input);
26
+ return new Response(null, response);
27
+ };
28
+ return Object.freeze({
29
+ action,
30
+ loader
31
+ });
32
+ }
33
+ let reactRouterAdapterValue;
34
+ function createReactRouterAdapter(defaults) {
35
+ return createServerAdapter("react-router", { mount: (implementation, options) => createResourceRouteHandlers(implementation, options === void 0 ? defaults : {
36
+ ...defaults,
37
+ ...options
38
+ }) });
39
+ }
40
+ /** Creates a React Router v7 adapter with native route context and mounting operations. */
41
+ function reactRouterAdapter(options) {
42
+ if (options !== void 0) return createReactRouterAdapter({ ...options });
43
+ reactRouterAdapterValue ??= createReactRouterAdapter({});
44
+ return reactRouterAdapterValue;
45
+ }
46
+
47
+ //#endregion
48
+ export { reactRouterAdapter };
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@hulla/api-react-router",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.0.1",
4
+ "description": "React Router v7 resource route integration for @hulla/api",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/hulladev/api.git",
9
+ "directory": "packages/react-router"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "registry": "https://registry.npmjs.org/"
25
+ },
26
+ "scripts": {
27
+ "build": "tsdown",
28
+ "clean": "rm -rf dist coverage",
29
+ "test": "bun run build && vitest run",
30
+ "test:coverage": "bun run build && vitest run --coverage",
31
+ "test:watch": "vitest",
32
+ "typecheck": "tsc --noEmit"
33
+ },
34
+ "devDependencies": {
35
+ "@hulla/api": "2.0.1",
36
+ "@react-router/dev": "7.15.1",
37
+ "@react-router/node": "7.15.1",
38
+ "@types/react": "^19.2.18",
39
+ "@types/react-dom": "^19.2.5",
40
+ "isbot": "^5.1.11",
41
+ "react": "^19.2.8",
42
+ "react-dom": "^19.2.8",
43
+ "react-router": "7.15.1",
44
+ "vite": "^8.2.2",
45
+ "zod": "^4.4.3"
46
+ },
47
+ "peerDependencies": {
48
+ "@hulla/api": "^2.0.1",
49
+ "react-router": "^7.0.0"
50
+ }
51
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.