@cybermp/rpc-router-react 0.1.0-rc.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.
- package/LICENSE +21 -0
- package/dist/index.cjs +43 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.mts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.mjs +39 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CyberMP-RPC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const react = require('react');
|
|
4
|
+
const jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
|
|
6
|
+
const routerContext = react.createContext(
|
|
7
|
+
{}
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
const useRouter = () => {
|
|
11
|
+
const router = react.useContext(routerContext);
|
|
12
|
+
if (!router) {
|
|
13
|
+
throw new Error("You did not provided router");
|
|
14
|
+
}
|
|
15
|
+
return router;
|
|
16
|
+
};
|
|
17
|
+
function useImplement(contract, handler, deps) {
|
|
18
|
+
const router = useRouter();
|
|
19
|
+
react.useEffect(() => {
|
|
20
|
+
if (Array.isArray(handler) || typeof handler === "function") {
|
|
21
|
+
router.implement(
|
|
22
|
+
contract,
|
|
23
|
+
...Array.isArray(handler) ? handler : [handler]
|
|
24
|
+
);
|
|
25
|
+
} else {
|
|
26
|
+
router.implement(contract, handler);
|
|
27
|
+
}
|
|
28
|
+
return () => {
|
|
29
|
+
router.unimplement(contract);
|
|
30
|
+
};
|
|
31
|
+
}, deps ?? []);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const RpcRouterProvider = ({
|
|
35
|
+
router,
|
|
36
|
+
children
|
|
37
|
+
}) => {
|
|
38
|
+
return /* @__PURE__ */ jsxRuntime.jsx(routerContext.Provider, { value: router, children });
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
exports.RpcRouterProvider = RpcRouterProvider;
|
|
42
|
+
exports.useImplement = useImplement;
|
|
43
|
+
exports.useRouter = useRouter;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as _cybermp_rpc_router_server from '@cybermp/rpc-router/server';
|
|
2
|
+
import { AnyContract, InferImplementHandler, ContractRouter, RpcRouter } from '@cybermp/rpc-router/server';
|
|
3
|
+
import { RpcBrowserContext } from '@cybermp/rpc-browser';
|
|
4
|
+
import { DependencyList, PropsWithChildren } from 'react';
|
|
5
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
declare const useRouter: () => _cybermp_rpc_router_server.RpcRouter<RpcBrowserContext<any, Record<string, any>>>;
|
|
8
|
+
type ImplementContractOptions<TContract extends AnyContract, TContext extends RpcBrowserContext = RpcBrowserContext> = {
|
|
9
|
+
contract: TContract;
|
|
10
|
+
handler: InferImplementHandler<TContext, TContract> | InferImplementHandler<TContext, TContract>[];
|
|
11
|
+
deps?: DependencyList;
|
|
12
|
+
};
|
|
13
|
+
type ImplementContractTreeOptions<TContract extends ContractRouter, TContext extends RpcBrowserContext = RpcBrowserContext> = {
|
|
14
|
+
contract: TContract;
|
|
15
|
+
handler: InferImplementHandler<TContext, TContract>;
|
|
16
|
+
deps?: DependencyList;
|
|
17
|
+
};
|
|
18
|
+
declare function useImplement<TContract extends AnyContract, TContext extends RpcBrowserContext = RpcBrowserContext>(contract: TContract, handler: InferImplementHandler<TContext, TContract> | InferImplementHandler<TContext, TContract>[], deps?: DependencyList): void;
|
|
19
|
+
declare function useImplement<TContract extends ContractRouter, TContext extends RpcBrowserContext = RpcBrowserContext>(contract: TContract, handler: InferImplementHandler<TContext, TContract>, deps?: DependencyList): void;
|
|
20
|
+
|
|
21
|
+
type RpcRouterProviderProps = PropsWithChildren<{
|
|
22
|
+
router: RpcRouter<RpcBrowserContext>;
|
|
23
|
+
}>;
|
|
24
|
+
declare const RpcRouterProvider: ({ router, children, }: RpcRouterProviderProps) => react_jsx_runtime.JSX.Element;
|
|
25
|
+
|
|
26
|
+
export { RpcRouterProvider, useImplement, useRouter };
|
|
27
|
+
export type { ImplementContractOptions, ImplementContractTreeOptions };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as _cybermp_rpc_router_server from '@cybermp/rpc-router/server';
|
|
2
|
+
import { AnyContract, InferImplementHandler, ContractRouter, RpcRouter } from '@cybermp/rpc-router/server';
|
|
3
|
+
import { RpcBrowserContext } from '@cybermp/rpc-browser';
|
|
4
|
+
import { DependencyList, PropsWithChildren } from 'react';
|
|
5
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
declare const useRouter: () => _cybermp_rpc_router_server.RpcRouter<RpcBrowserContext<any, Record<string, any>>>;
|
|
8
|
+
type ImplementContractOptions<TContract extends AnyContract, TContext extends RpcBrowserContext = RpcBrowserContext> = {
|
|
9
|
+
contract: TContract;
|
|
10
|
+
handler: InferImplementHandler<TContext, TContract> | InferImplementHandler<TContext, TContract>[];
|
|
11
|
+
deps?: DependencyList;
|
|
12
|
+
};
|
|
13
|
+
type ImplementContractTreeOptions<TContract extends ContractRouter, TContext extends RpcBrowserContext = RpcBrowserContext> = {
|
|
14
|
+
contract: TContract;
|
|
15
|
+
handler: InferImplementHandler<TContext, TContract>;
|
|
16
|
+
deps?: DependencyList;
|
|
17
|
+
};
|
|
18
|
+
declare function useImplement<TContract extends AnyContract, TContext extends RpcBrowserContext = RpcBrowserContext>(contract: TContract, handler: InferImplementHandler<TContext, TContract> | InferImplementHandler<TContext, TContract>[], deps?: DependencyList): void;
|
|
19
|
+
declare function useImplement<TContract extends ContractRouter, TContext extends RpcBrowserContext = RpcBrowserContext>(contract: TContract, handler: InferImplementHandler<TContext, TContract>, deps?: DependencyList): void;
|
|
20
|
+
|
|
21
|
+
type RpcRouterProviderProps = PropsWithChildren<{
|
|
22
|
+
router: RpcRouter<RpcBrowserContext>;
|
|
23
|
+
}>;
|
|
24
|
+
declare const RpcRouterProvider: ({ router, children, }: RpcRouterProviderProps) => react_jsx_runtime.JSX.Element;
|
|
25
|
+
|
|
26
|
+
export { RpcRouterProvider, useImplement, useRouter };
|
|
27
|
+
export type { ImplementContractOptions, ImplementContractTreeOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as _cybermp_rpc_router_server from '@cybermp/rpc-router/server';
|
|
2
|
+
import { AnyContract, InferImplementHandler, ContractRouter, RpcRouter } from '@cybermp/rpc-router/server';
|
|
3
|
+
import { RpcBrowserContext } from '@cybermp/rpc-browser';
|
|
4
|
+
import { DependencyList, PropsWithChildren } from 'react';
|
|
5
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
declare const useRouter: () => _cybermp_rpc_router_server.RpcRouter<RpcBrowserContext<any, Record<string, any>>>;
|
|
8
|
+
type ImplementContractOptions<TContract extends AnyContract, TContext extends RpcBrowserContext = RpcBrowserContext> = {
|
|
9
|
+
contract: TContract;
|
|
10
|
+
handler: InferImplementHandler<TContext, TContract> | InferImplementHandler<TContext, TContract>[];
|
|
11
|
+
deps?: DependencyList;
|
|
12
|
+
};
|
|
13
|
+
type ImplementContractTreeOptions<TContract extends ContractRouter, TContext extends RpcBrowserContext = RpcBrowserContext> = {
|
|
14
|
+
contract: TContract;
|
|
15
|
+
handler: InferImplementHandler<TContext, TContract>;
|
|
16
|
+
deps?: DependencyList;
|
|
17
|
+
};
|
|
18
|
+
declare function useImplement<TContract extends AnyContract, TContext extends RpcBrowserContext = RpcBrowserContext>(contract: TContract, handler: InferImplementHandler<TContext, TContract> | InferImplementHandler<TContext, TContract>[], deps?: DependencyList): void;
|
|
19
|
+
declare function useImplement<TContract extends ContractRouter, TContext extends RpcBrowserContext = RpcBrowserContext>(contract: TContract, handler: InferImplementHandler<TContext, TContract>, deps?: DependencyList): void;
|
|
20
|
+
|
|
21
|
+
type RpcRouterProviderProps = PropsWithChildren<{
|
|
22
|
+
router: RpcRouter<RpcBrowserContext>;
|
|
23
|
+
}>;
|
|
24
|
+
declare const RpcRouterProvider: ({ router, children, }: RpcRouterProviderProps) => react_jsx_runtime.JSX.Element;
|
|
25
|
+
|
|
26
|
+
export { RpcRouterProvider, useImplement, useRouter };
|
|
27
|
+
export type { ImplementContractOptions, ImplementContractTreeOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect } from 'react';
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
|
|
4
|
+
const routerContext = createContext(
|
|
5
|
+
{}
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
const useRouter = () => {
|
|
9
|
+
const router = useContext(routerContext);
|
|
10
|
+
if (!router) {
|
|
11
|
+
throw new Error("You did not provided router");
|
|
12
|
+
}
|
|
13
|
+
return router;
|
|
14
|
+
};
|
|
15
|
+
function useImplement(contract, handler, deps) {
|
|
16
|
+
const router = useRouter();
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (Array.isArray(handler) || typeof handler === "function") {
|
|
19
|
+
router.implement(
|
|
20
|
+
contract,
|
|
21
|
+
...Array.isArray(handler) ? handler : [handler]
|
|
22
|
+
);
|
|
23
|
+
} else {
|
|
24
|
+
router.implement(contract, handler);
|
|
25
|
+
}
|
|
26
|
+
return () => {
|
|
27
|
+
router.unimplement(contract);
|
|
28
|
+
};
|
|
29
|
+
}, deps ?? []);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const RpcRouterProvider = ({
|
|
33
|
+
router,
|
|
34
|
+
children
|
|
35
|
+
}) => {
|
|
36
|
+
return /* @__PURE__ */ jsx(routerContext.Provider, { value: router, children });
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { RpcRouterProvider, useImplement, useRouter };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cybermp/rpc-router-react",
|
|
3
|
+
"version": "0.1.0-rc.1",
|
|
4
|
+
"keywords": [],
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"require": "./dist/index.js",
|
|
10
|
+
"import": "./dist/index.mjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"unbuild": {
|
|
17
|
+
"failOnWarn": false,
|
|
18
|
+
"declaration": true,
|
|
19
|
+
"rollup": {
|
|
20
|
+
"esbuild": {
|
|
21
|
+
"jsx": "automatic"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/react": "^19.2.7"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"react": ">=18.0.0",
|
|
30
|
+
"@cybermp/browser-types": "^1.0.0",
|
|
31
|
+
"@cybermp/rpc-router": "0.1.0-rc.1",
|
|
32
|
+
"@cybermp/rpc-browser": "0.1.0-rc.1"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"dev": "pnpm run build --watch",
|
|
36
|
+
"build": "unbuild",
|
|
37
|
+
"type:check": "tsc -b"
|
|
38
|
+
}
|
|
39
|
+
}
|