gomtm 0.0.1 → 0.0.3
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/dist/esm/clientlib.d.ts +6 -0
- package/dist/esm/clientlib.js +44 -0
- package/dist/esm/connectquery/call-unary-method.d.ts +8 -0
- package/dist/esm/connectquery/call-unary-method.js +47 -0
- package/dist/esm/connectquery/connect-query-key.d.ts +16 -0
- package/dist/esm/connectquery/connect-query-key.js +15 -0
- package/dist/esm/connectquery/create-use-infinite-query-options.d.ts +21 -0
- package/dist/esm/connectquery/create-use-infinite-query-options.js +92 -0
- package/dist/esm/connectquery/create-use-query-options.d.ts +19 -0
- package/dist/esm/connectquery/create-use-query-options.js +74 -0
- package/dist/esm/connectquery/default-options.d.ts +6 -0
- package/dist/esm/connectquery/default-options.js +9 -0
- package/dist/esm/connectquery/index.d.ts +16 -0
- package/dist/esm/connectquery/index.js +33 -0
- package/dist/esm/connectquery/method-unary-descriptor.d.ts +4 -0
- package/dist/esm/connectquery/method-unary-descriptor.js +0 -0
- package/dist/esm/connectquery/use-infinite-query.d.ts +12 -0
- package/dist/esm/connectquery/use-infinite-query.js +87 -0
- package/dist/esm/connectquery/use-mutation.d.ts +10 -0
- package/dist/esm/connectquery/use-mutation.js +79 -0
- package/dist/esm/connectquery/use-query.d.ts +12 -0
- package/dist/esm/connectquery/use-query.js +75 -0
- package/dist/esm/connectquery/use-transport.d.ts +7 -0
- package/dist/esm/connectquery/use-transport.js +23 -0
- package/dist/esm/connectquery/utils.d.ts +12 -0
- package/dist/esm/connectquery/utils.js +24 -0
- package/dist/esm/consts.d.ts +28 -0
- package/dist/esm/consts.js +58 -0
- package/dist/esm/http/cors.d.ts +15 -0
- package/dist/esm/http/cors.js +129 -0
- package/dist/esm/http/fetchMiddleWithCache.d.ts +2 -0
- package/dist/esm/http/fetchMiddleWithCache.js +87 -0
- package/dist/esm/http/fetchMiddleWithUrlProxy.d.ts +2 -0
- package/dist/esm/http/fetchMiddleWithUrlProxy.js +28 -0
- package/dist/esm/http/mthttp.d.ts +5 -0
- package/dist/esm/http/mthttp.js +61 -0
- package/dist/esm/http/response-cache.test.js +72 -0
- package/dist/esm/messageTypeRegistry.d.ts +4 -0
- package/dist/esm/messageTypeRegistry.js +36 -0
- package/dist/esm/mtmFetcher.d.ts +13 -0
- package/dist/esm/mtmFetcher.js +145 -0
- package/dist/esm/mtmcore.d.ts +5 -0
- package/dist/esm/mtmcore.js +19 -0
- package/dist/esm/providers/GomtmProvider.d.ts +68 -0
- package/dist/esm/providers/GomtmProvider.js +100 -0
- package/dist/esm/providers/MtConnectProvider.d.ts +2 -0
- package/dist/esm/providers/MtConnectProvider.js +18 -0
- package/dist/esm/providers/ReactQueryProvider.d.ts +4 -0
- package/dist/esm/providers/ReactQueryProvider.js +63 -0
- package/dist/esm/providers/userContext.d.ts +14 -0
- package/dist/esm/providers/userContext.js +99 -0
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.js +32 -0
- package/dist/tsconfig.type.tsbuildinfo +1 -1
- package/package.json +10 -3
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createRegistry } from "@bufbuild/protobuf";
|
|
2
|
+
import { BotService } from "gomtm/gomtmpb/mtm/sppb/bot_connect";
|
|
3
|
+
import { CmdStartNoVnc, CmdStartSimpleCrawler, ConsumMessageReq, CxPageReq, HelloBotMessage } from "gomtm/gomtmpb/mtm/sppb/bot_pb";
|
|
4
|
+
import { CxService } from "gomtm/gomtmpb/mtm/sppb/cx_connect";
|
|
5
|
+
import { JobService } from "gomtm/gomtmpb/mtm/sppb/job_connect";
|
|
6
|
+
import { MtmService } from "gomtm/gomtmpb/mtm/sppb/mtm_connect";
|
|
7
|
+
import { SpService } from "gomtm/gomtmpb/mtm/sppb/sp_connect";
|
|
8
|
+
const anypbTypeReg = createRegistry(
|
|
9
|
+
HelloBotMessage,
|
|
10
|
+
CxPageReq,
|
|
11
|
+
ConsumMessageReq,
|
|
12
|
+
CmdStartNoVnc,
|
|
13
|
+
CmdStartSimpleCrawler
|
|
14
|
+
);
|
|
15
|
+
const allServices = {};
|
|
16
|
+
const registerService = (...s) => {
|
|
17
|
+
s.map((item) => {
|
|
18
|
+
const servieTypeName = item.typeName;
|
|
19
|
+
allServices[servieTypeName] = item;
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const GetMtmServiceByTypeName = (serviceTypeName) => {
|
|
23
|
+
return allServices[serviceTypeName];
|
|
24
|
+
};
|
|
25
|
+
registerService(
|
|
26
|
+
MtmService,
|
|
27
|
+
SpService,
|
|
28
|
+
BotService,
|
|
29
|
+
JobService,
|
|
30
|
+
CxService
|
|
31
|
+
);
|
|
32
|
+
export {
|
|
33
|
+
GetMtmServiceByTypeName,
|
|
34
|
+
allServices,
|
|
35
|
+
anypbTypeReg
|
|
36
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Message, PartialMessage, ServiceType } from "@bufbuild/protobuf";
|
|
2
|
+
import { MtUnaryCallError } from "./connectquery/utils";
|
|
3
|
+
import { ExtInfo } from "./mtmcore";
|
|
4
|
+
type CustomRequestInit = RequestInit & {
|
|
5
|
+
enableCache?: boolean;
|
|
6
|
+
enableLog?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const newMtmFetch: (extInfo: ExtInfo, initGlobal?: CustomRequestInit) => (input: string | URL | globalThis.Request, init?: CustomRequestInit) => Promise<Response>;
|
|
9
|
+
export declare function getMtmApiUrl(backendUrl: string): string;
|
|
10
|
+
export declare function createMtConnectTransport(backendUrl: string, extInfo: ExtInfo): import("@connectrpc/connect").Transport;
|
|
11
|
+
export declare function createClient<T extends ServiceType>(extInfo: ExtInfo, svc: T, options: CustomRequestInit): import("@connectrpc/connect").PromiseClient<T>;
|
|
12
|
+
export declare function mtmCallUnaryMethod<I extends Message<I>, O extends Message<O>>(extInfo: ExtInfo, svc: string, method: string, input?: PartialMessage<I> | undefined): Promise<O | MtUnaryCallError>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
var fulfilled = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.next(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var rejected = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.throw(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
import { createPromiseClient } from "@connectrpc/connect";
|
|
38
|
+
import { createConnectTransport } from "@connectrpc/connect-web";
|
|
39
|
+
import { camelCase, merge } from "lodash";
|
|
40
|
+
import { fetchMiddleWithCache } from "./http/fetchMiddleWithCache";
|
|
41
|
+
import { GetMtmServiceByTypeName, anypbTypeReg } from "./messageTypeRegistry";
|
|
42
|
+
import { HeaderMtmHost } from "./consts";
|
|
43
|
+
import { extGetMemBackend, extGetSitehost, extGetToken } from "./mtmcore";
|
|
44
|
+
const newMtmFetch = (extInfo, initGlobal) => {
|
|
45
|
+
return (input, init) => __async(void 0, null, function* () {
|
|
46
|
+
if (init) {
|
|
47
|
+
delete init.mode;
|
|
48
|
+
delete init.credentials;
|
|
49
|
+
}
|
|
50
|
+
const req = merge(init, initGlobal, {
|
|
51
|
+
redirect: "follow"
|
|
52
|
+
}) || {};
|
|
53
|
+
const token = extGetToken(extInfo);
|
|
54
|
+
if (token) {
|
|
55
|
+
req.headers.set("Authorization", `Bearer ${token}`);
|
|
56
|
+
}
|
|
57
|
+
const siteHost = extGetSitehost(extInfo);
|
|
58
|
+
if (siteHost) {
|
|
59
|
+
req.headers.set(HeaderMtmHost, siteHost);
|
|
60
|
+
}
|
|
61
|
+
let fetcher = fetch;
|
|
62
|
+
if (req.enableCache) {
|
|
63
|
+
fetcher = fetchMiddleWithCache(fetch);
|
|
64
|
+
}
|
|
65
|
+
if (req.enableLog) {
|
|
66
|
+
fetcher = fetchWithLoger(fetcher);
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const responseData = yield fetcher(input, init);
|
|
70
|
+
return responseData;
|
|
71
|
+
} catch (e) {
|
|
72
|
+
console.error("mtmfetch error", e);
|
|
73
|
+
const errorRsp = {
|
|
74
|
+
error_code: "-1",
|
|
75
|
+
error_message: (e == null ? void 0 : e.toString()) || "unknow error"
|
|
76
|
+
};
|
|
77
|
+
const errorResponse = new Response(JSON.stringify(errorRsp), {
|
|
78
|
+
status: 200,
|
|
79
|
+
headers: {
|
|
80
|
+
"Content-Type": "application/json"
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return errorResponse;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
function getMtmApiUrl(backendUrl) {
|
|
88
|
+
return backendUrl + "/api";
|
|
89
|
+
}
|
|
90
|
+
function createMtConnectTransport(backendUrl, extInfo) {
|
|
91
|
+
return createConnectTransport({
|
|
92
|
+
baseUrl: getMtmApiUrl(backendUrl),
|
|
93
|
+
fetch: newMtmFetch(extInfo),
|
|
94
|
+
jsonOptions: {
|
|
95
|
+
typeRegistry: anypbTypeReg
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function createClient(extInfo, svc, options) {
|
|
100
|
+
const backendUrl = extGetMemBackend(extInfo);
|
|
101
|
+
return createPromiseClient(svc, createConnectTransport({
|
|
102
|
+
baseUrl: getMtmApiUrl(backendUrl || ""),
|
|
103
|
+
fetch: newMtmFetch(extInfo, __spreadValues({}, options))
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
function mtmCallUnaryMethod(extInfo, svc, method, input) {
|
|
107
|
+
return __async(this, null, function* () {
|
|
108
|
+
if (!method) {
|
|
109
|
+
throw new Error(`missing method: "${method}"`);
|
|
110
|
+
}
|
|
111
|
+
const service = GetMtmServiceByTypeName(svc);
|
|
112
|
+
if (!service) {
|
|
113
|
+
throw new Error(`not service by name ${svc}`);
|
|
114
|
+
}
|
|
115
|
+
const methodName = camelCase(method);
|
|
116
|
+
console.log(`[unary] ${svc}.${methodName}`, input);
|
|
117
|
+
const client = createClient(extInfo, service, {
|
|
118
|
+
enableCache: false
|
|
119
|
+
});
|
|
120
|
+
try {
|
|
121
|
+
const result = yield client[methodName](input);
|
|
122
|
+
return result;
|
|
123
|
+
} catch (e) {
|
|
124
|
+
console.error("[mtmCallUnaryMethod error]", e.stack);
|
|
125
|
+
const errorRsp = {
|
|
126
|
+
error_code: "-2",
|
|
127
|
+
error_message: (e == null ? void 0 : e.toString()) || "unknow error"
|
|
128
|
+
};
|
|
129
|
+
return errorRsp;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function fetchWithLoger(fetcher) {
|
|
134
|
+
return (input, init) => __async(this, null, function* () {
|
|
135
|
+
const response = yield fetcher(input, init);
|
|
136
|
+
return response;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
export {
|
|
140
|
+
createClient,
|
|
141
|
+
createMtConnectTransport,
|
|
142
|
+
getMtmApiUrl,
|
|
143
|
+
mtmCallUnaryMethod,
|
|
144
|
+
newMtmFetch
|
|
145
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type ExtInfo = (key: string) => string | null | undefined;
|
|
2
|
+
export declare const extGetToken: (extInfo: ExtInfo) => string | null | undefined;
|
|
3
|
+
export declare const extGetMemBackend: (extInfo: ExtInfo) => string | null | undefined;
|
|
4
|
+
export declare const extGetSitehost: (extInfo: ExtInfo) => string | null | undefined;
|
|
5
|
+
export declare const headersGet: (name: string, headers: Record<string, any>) => any;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ExtKey_Hostname, ExtKey_mtmaccessToken, ExtKey_mtmbackend } from "./consts";
|
|
2
|
+
const extGetToken = (extInfo) => {
|
|
3
|
+
return extInfo(ExtKey_mtmaccessToken);
|
|
4
|
+
};
|
|
5
|
+
const extGetMemBackend = (extInfo) => {
|
|
6
|
+
return extInfo(ExtKey_mtmbackend);
|
|
7
|
+
};
|
|
8
|
+
const extGetSitehost = (extInfo) => {
|
|
9
|
+
return extInfo(ExtKey_Hostname);
|
|
10
|
+
};
|
|
11
|
+
const headersGet = (name, headers) => {
|
|
12
|
+
return headers[name];
|
|
13
|
+
};
|
|
14
|
+
export {
|
|
15
|
+
extGetMemBackend,
|
|
16
|
+
extGetSitehost,
|
|
17
|
+
extGetToken,
|
|
18
|
+
headersGet
|
|
19
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Dispatch, PropsWithChildren } from 'react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export declare const mtmAppStore: {
|
|
4
|
+
get: <Value>(atom: import("jotai").Atom<Value>) => Value;
|
|
5
|
+
set: <Value_1, Args extends unknown[], Result>(atom: import("jotai").WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
|
|
6
|
+
sub: (atom: import("jotai").Atom<unknown>, listener: () => void) => () => void;
|
|
7
|
+
dev_subscribe_store?: ((l: (action: {
|
|
8
|
+
type: "write";
|
|
9
|
+
flushed: Set<import("jotai").Atom<unknown>>;
|
|
10
|
+
} | {
|
|
11
|
+
type: "async-write";
|
|
12
|
+
flushed: Set<import("jotai").Atom<unknown>>;
|
|
13
|
+
} | {
|
|
14
|
+
type: "sub";
|
|
15
|
+
flushed: Set<import("jotai").Atom<unknown>>;
|
|
16
|
+
} | {
|
|
17
|
+
type: "unsub";
|
|
18
|
+
} | {
|
|
19
|
+
type: "restore";
|
|
20
|
+
flushed: Set<import("jotai").Atom<unknown>>;
|
|
21
|
+
}) => void, rev: 2) => () => void) | undefined;
|
|
22
|
+
dev_get_mounted_atoms?: (() => IterableIterator<import("jotai").Atom<unknown>>) | undefined;
|
|
23
|
+
dev_get_atom_state?: ((a: import("jotai").Atom<unknown>) => ({
|
|
24
|
+
d: Map<import("jotai").Atom<unknown>, any & ({
|
|
25
|
+
e: unknown;
|
|
26
|
+
} | {
|
|
27
|
+
v: unknown;
|
|
28
|
+
})>;
|
|
29
|
+
} & ({
|
|
30
|
+
e: unknown;
|
|
31
|
+
} | {
|
|
32
|
+
v: unknown;
|
|
33
|
+
})) | undefined) | undefined;
|
|
34
|
+
dev_get_mounted?: ((a: import("jotai").Atom<unknown>) => {
|
|
35
|
+
l: Set<() => void>;
|
|
36
|
+
t: Set<import("jotai").Atom<unknown>>;
|
|
37
|
+
u?: (() => void) | undefined;
|
|
38
|
+
} | undefined) | undefined;
|
|
39
|
+
dev_restore_atoms?: ((values: Iterable<readonly [import("jotai").Atom<unknown>, unknown]>) => void) | undefined;
|
|
40
|
+
};
|
|
41
|
+
export type ActionHandler = (props: {
|
|
42
|
+
action: string;
|
|
43
|
+
values?: any;
|
|
44
|
+
}) => void;
|
|
45
|
+
interface IAppContext {
|
|
46
|
+
extKv: Record<string, any>;
|
|
47
|
+
token?: string | null;
|
|
48
|
+
hostname?: string | null;
|
|
49
|
+
backendUrl?: string | null;
|
|
50
|
+
trpcBackendUrl?: string | null;
|
|
51
|
+
isDebug?: boolean;
|
|
52
|
+
debugData?: any;
|
|
53
|
+
setDebugData: Dispatch<any>;
|
|
54
|
+
isOpenDebugView?: boolean;
|
|
55
|
+
setOpenDebugView: Dispatch<React.SetStateAction<boolean>>;
|
|
56
|
+
}
|
|
57
|
+
export declare function GomtmProvider(props: {
|
|
58
|
+
initExtKv: Record<string, any | undefined>;
|
|
59
|
+
} & PropsWithChildren): React.JSX.Element;
|
|
60
|
+
export declare function useMtmApp(): IAppContext;
|
|
61
|
+
export declare const useMtmBackendUrl: () => string | null | undefined;
|
|
62
|
+
export declare const useHostname: () => string;
|
|
63
|
+
export declare const useExtInfo: () => (key: string) => string | null | undefined;
|
|
64
|
+
export declare const useToken: () => {
|
|
65
|
+
token: string;
|
|
66
|
+
setToken: (newToken: string) => void;
|
|
67
|
+
};
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useCallback, useContext, useMemo, useState } from "react";
|
|
4
|
+
import { MtReactQueryProvider } from "gomtm/providers/ReactQueryProvider";
|
|
5
|
+
import { Provider } from "jotai";
|
|
6
|
+
import { ExtKey_Hostname, ExtKey_SelfBackend, ExtKey_mtmaccessToken, ExtKey_mtmbackend, MtM_TOKEN_NAME } from "../consts";
|
|
7
|
+
import { MtConnectProvider } from "./MtConnectProvider";
|
|
8
|
+
import { createStore } from "jotai";
|
|
9
|
+
import { getCookie } from "../clientlib";
|
|
10
|
+
const mtmAppStore = createStore();
|
|
11
|
+
const AppContext = createContext(void 0);
|
|
12
|
+
function GomtmProvider(props) {
|
|
13
|
+
const { children, initExtKv } = props;
|
|
14
|
+
const [isOpenDebugView, setOpenDebugView] = useState(false);
|
|
15
|
+
const [debugData, setDebugData] = useState(null);
|
|
16
|
+
const [isDebug, setIsDebug] = useState(false);
|
|
17
|
+
const trpcBackendUrl = useMemo(() => {
|
|
18
|
+
return initExtKv[ExtKey_SelfBackend];
|
|
19
|
+
}, [initExtKv]);
|
|
20
|
+
const backendUrl = useMemo(() => {
|
|
21
|
+
return initExtKv[ExtKey_mtmbackend];
|
|
22
|
+
}, [initExtKv]);
|
|
23
|
+
const hostname = useMemo(() => {
|
|
24
|
+
return initExtKv[ExtKey_Hostname];
|
|
25
|
+
}, [initExtKv]);
|
|
26
|
+
const token = useMemo(() => {
|
|
27
|
+
return initExtKv[ExtKey_mtmaccessToken];
|
|
28
|
+
}, [initExtKv]);
|
|
29
|
+
return /* @__PURE__ */ jsx(Provider, { store: mtmAppStore, children: /* @__PURE__ */ jsx(AppContext.Provider, { value: {
|
|
30
|
+
extKv: initExtKv,
|
|
31
|
+
token,
|
|
32
|
+
hostname,
|
|
33
|
+
backendUrl,
|
|
34
|
+
isDebug,
|
|
35
|
+
// setIsDebug: toggleDebug,
|
|
36
|
+
// meta, setMeta,
|
|
37
|
+
debugData,
|
|
38
|
+
setDebugData,
|
|
39
|
+
isOpenDebugView,
|
|
40
|
+
setOpenDebugView,
|
|
41
|
+
trpcBackendUrl
|
|
42
|
+
}, children: /* @__PURE__ */ jsx(MtReactQueryProvider, { children: /* @__PURE__ */ jsx(MtConnectProvider, { children }) }) }) });
|
|
43
|
+
}
|
|
44
|
+
function useMtmApp() {
|
|
45
|
+
const mtappContext = useContext(AppContext);
|
|
46
|
+
if (mtappContext === void 0) {
|
|
47
|
+
throw new Error("useMtmApp must be used within a MtAppProvider");
|
|
48
|
+
}
|
|
49
|
+
return mtappContext;
|
|
50
|
+
}
|
|
51
|
+
const useMtmBackendUrl = () => {
|
|
52
|
+
const { backendUrl } = useMtmApp();
|
|
53
|
+
return backendUrl;
|
|
54
|
+
};
|
|
55
|
+
const useHostname = () => {
|
|
56
|
+
const { hostname } = useMtmApp();
|
|
57
|
+
if (hostname)
|
|
58
|
+
return hostname;
|
|
59
|
+
return window.location.host;
|
|
60
|
+
};
|
|
61
|
+
const useExtInfo = () => {
|
|
62
|
+
const backendUrl = useMtmBackendUrl();
|
|
63
|
+
const siteHost = useHostname();
|
|
64
|
+
const { token } = useMtmApp();
|
|
65
|
+
const fn = useCallback((key) => {
|
|
66
|
+
switch (key) {
|
|
67
|
+
case ExtKey_mtmbackend:
|
|
68
|
+
return backendUrl;
|
|
69
|
+
case ExtKey_Hostname:
|
|
70
|
+
return siteHost;
|
|
71
|
+
case ExtKey_mtmaccessToken:
|
|
72
|
+
return token;
|
|
73
|
+
case ExtKey_SelfBackend:
|
|
74
|
+
return;
|
|
75
|
+
default:
|
|
76
|
+
throw new Error(`extinfo key error: ${key}`);
|
|
77
|
+
}
|
|
78
|
+
}, [backendUrl, siteHost, token]);
|
|
79
|
+
return fn;
|
|
80
|
+
};
|
|
81
|
+
const useToken = () => {
|
|
82
|
+
const [token] = useState(typeof window != "undefined" ? getCookie(MtM_TOKEN_NAME) : "");
|
|
83
|
+
return {
|
|
84
|
+
token: token || "",
|
|
85
|
+
setToken: (newToken) => {
|
|
86
|
+
if (typeof window != "undefined") {
|
|
87
|
+
document.cookie = `${MtM_TOKEN_NAME}=${newToken}`;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
export {
|
|
93
|
+
GomtmProvider,
|
|
94
|
+
mtmAppStore,
|
|
95
|
+
useExtInfo,
|
|
96
|
+
useHostname,
|
|
97
|
+
useMtmApp,
|
|
98
|
+
useMtmBackendUrl,
|
|
99
|
+
useToken
|
|
100
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createMtConnectTransport } from "gomtm/mtmFetcher";
|
|
4
|
+
import { useMemo } from "react";
|
|
5
|
+
import { TransportProvider } from "../connectquery";
|
|
6
|
+
import { useExtInfo, useMtmBackendUrl } from "./GomtmProvider";
|
|
7
|
+
const MtConnectProvider = (props) => {
|
|
8
|
+
const { children } = props;
|
|
9
|
+
const backendUrl = useMtmBackendUrl();
|
|
10
|
+
const extInfo = useExtInfo();
|
|
11
|
+
const transport = useMemo(() => {
|
|
12
|
+
return createMtConnectTransport(backendUrl || "", extInfo);
|
|
13
|
+
}, [backendUrl, extInfo]);
|
|
14
|
+
return /* @__PURE__ */ jsx(TransportProvider, { transport, children });
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
MtConnectProvider
|
|
18
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { PropsWithChildren } from 'react';
|
|
2
|
+
export declare const MtReactQueryProvider: (props: PropsWithChildren) => import("react").JSX.Element;
|
|
3
|
+
export declare function RqErrorBoundary(props: PropsWithChildren): import("react").JSX.Element;
|
|
4
|
+
export declare function RqErrorBoundaryUseHook(props: PropsWithChildren): import("react").JSX.Element;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import {
|
|
5
|
+
QueryClient,
|
|
6
|
+
QueryClientProvider,
|
|
7
|
+
QueryErrorResetBoundary,
|
|
8
|
+
useQueryErrorResetBoundary
|
|
9
|
+
} from "@tanstack/react-query";
|
|
10
|
+
import { ReactQueryStreamedHydration } from "@tanstack/react-query-next-experimental";
|
|
11
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
12
|
+
const MtReactQueryProvider = (props) => {
|
|
13
|
+
const { children } = props;
|
|
14
|
+
const [queryClient] = useState(() => new QueryClient({
|
|
15
|
+
// 这里的参数需要 正确设置,因为 设置不正确可能会导致 hydrate 莫名其妙的错误。
|
|
16
|
+
// 其中,默认参数,经测试 hydrate ssr 没问题
|
|
17
|
+
defaultOptions: {
|
|
18
|
+
queries: {
|
|
19
|
+
refetchOnWindowFocus: false,
|
|
20
|
+
// default: true
|
|
21
|
+
staleTime: Infinity,
|
|
22
|
+
refetchOnMount: false,
|
|
23
|
+
refetchOnReconnect: false,
|
|
24
|
+
retry: false
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}));
|
|
28
|
+
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(QueryClientProvider, { client: queryClient, children: /* @__PURE__ */ jsx(RqErrorBoundary, { children: /* @__PURE__ */ jsx(ReactQueryStreamedHydration, { children }) }) }) });
|
|
29
|
+
};
|
|
30
|
+
function RqErrorBoundary(props) {
|
|
31
|
+
const { children } = props;
|
|
32
|
+
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(QueryErrorResetBoundary, { children: ({ reset }) => /* @__PURE__ */ jsx(
|
|
33
|
+
ErrorBoundary,
|
|
34
|
+
{
|
|
35
|
+
onReset: reset,
|
|
36
|
+
fallbackRender: ({ resetErrorBoundary, error }) => /* @__PURE__ */ jsxs("div", { className: "green-red-100 prose dark:prose-invert container mx-auto p-8 text-lg", children: [
|
|
37
|
+
/* @__PURE__ */ jsx("div", { className: " pb-8 text-center", children: "some thing error" }),
|
|
38
|
+
/* @__PURE__ */ jsx("div", { children: error == null ? void 0 : error.toString() }),
|
|
39
|
+
/* @__PURE__ */ jsx("div", { className: "flex items-center justify-center p-8", children: /* @__PURE__ */ jsx("button", { onClick: () => resetErrorBoundary(), children: "Retry" }) })
|
|
40
|
+
] }),
|
|
41
|
+
children
|
|
42
|
+
}
|
|
43
|
+
) }) });
|
|
44
|
+
}
|
|
45
|
+
function RqErrorBoundaryUseHook(props) {
|
|
46
|
+
const { reset } = useQueryErrorResetBoundary();
|
|
47
|
+
return /* @__PURE__ */ jsx(
|
|
48
|
+
ErrorBoundary,
|
|
49
|
+
{
|
|
50
|
+
onReset: reset,
|
|
51
|
+
fallbackRender: ({ resetErrorBoundary }) => /* @__PURE__ */ jsxs("div", { children: [
|
|
52
|
+
/* @__PURE__ */ jsx("div", { children: "There was an error\uFF08RqErrorBoundaryUseHook\uFF09!" }),
|
|
53
|
+
/* @__PURE__ */ jsx("button", { onClick: () => resetErrorBoundary(), children: "Try again" })
|
|
54
|
+
] }),
|
|
55
|
+
children: props.children
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
MtReactQueryProvider,
|
|
61
|
+
RqErrorBoundary,
|
|
62
|
+
RqErrorBoundaryUseHook
|
|
63
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PlainMessage } from '@bufbuild/protobuf';
|
|
2
|
+
import React, { PropsWithChildren } from 'react';
|
|
3
|
+
import { UserinfoRes } from '../gomtmpb/mtm/sppb/mtm_pb';
|
|
4
|
+
export declare function UserProvider(props: {} & PropsWithChildren): React.JSX.Element;
|
|
5
|
+
export declare function useUser(): {
|
|
6
|
+
isLogined: boolean;
|
|
7
|
+
userInfo?: PlainMessage<UserinfoRes> | undefined;
|
|
8
|
+
};
|
|
9
|
+
export declare function useIsAdmin(): boolean | undefined;
|
|
10
|
+
export declare function useRoles(): string[];
|
|
11
|
+
export declare function isRoleMatch(currentRoles: string[], allowRoles?: string[]): boolean;
|
|
12
|
+
export declare const useAuth: () => {
|
|
13
|
+
login: import("@tanstack/react-query").UseMutationResult<import("../gomtmpb/mtm/sppb/mtm_pb").LoginReply, import("@connectrpc/connect").ConnectError, import("@bufbuild/protobuf").PartialMessage<import("../gomtmpb/mtm/sppb/mtm_pb").LoginReq>>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
|
+
import { jsx } from "react/jsx-runtime";
|
|
22
|
+
import { useSearchParams } from "next/navigation";
|
|
23
|
+
import React, { useMemo } from "react";
|
|
24
|
+
import { useMutation, useSuspenseQuery } from "../connectquery";
|
|
25
|
+
import { LOGIN_CALLBACK_URL_KEY, MtM_TOKEN_NAME } from "../consts";
|
|
26
|
+
import { login, userinfo } from "../gomtmpb/mtm/sppb/mtm-MtmService_connectquery";
|
|
27
|
+
import { useToken } from "./GomtmProvider";
|
|
28
|
+
const UserContext = React.createContext(void 0);
|
|
29
|
+
function UserProvider(props) {
|
|
30
|
+
const { children } = props;
|
|
31
|
+
const query = useSuspenseQuery(userinfo, {});
|
|
32
|
+
return /* @__PURE__ */ jsx(UserContext.Provider, { value: {
|
|
33
|
+
userInfo: query.data
|
|
34
|
+
}, children });
|
|
35
|
+
}
|
|
36
|
+
function useUser() {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
const context = React.useContext(UserContext);
|
|
39
|
+
if (context === void 0) {
|
|
40
|
+
throw new Error("useUserContext must be used within a UserProvider");
|
|
41
|
+
}
|
|
42
|
+
return __spreadProps(__spreadValues({}, context), {
|
|
43
|
+
isLogined: !!((_b = (_a = context.userInfo) == null ? void 0 : _a.userInfo) == null ? void 0 : _b.ID)
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function useIsAdmin() {
|
|
47
|
+
const user = useUser();
|
|
48
|
+
const isAdmin = useMemo(() => {
|
|
49
|
+
var _a, _b, _c;
|
|
50
|
+
return ((_a = user.userInfo) == null ? void 0 : _a.userInfo) && ((_c = (_b = user.userInfo) == null ? void 0 : _b.userInfo) == null ? void 0 : _c.roles.findIndex((x) => x == "admin")) >= 0;
|
|
51
|
+
}, [user.userInfo]);
|
|
52
|
+
return isAdmin;
|
|
53
|
+
}
|
|
54
|
+
function useRoles() {
|
|
55
|
+
const user = useUser();
|
|
56
|
+
const roles = useMemo(() => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
return ((_b = (_a = user.userInfo) == null ? void 0 : _a.userInfo) == null ? void 0 : _b.roles) || [];
|
|
59
|
+
}, [user.userInfo]);
|
|
60
|
+
return roles;
|
|
61
|
+
}
|
|
62
|
+
function isRoleMatch(currentRoles, allowRoles) {
|
|
63
|
+
if (!currentRoles) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
if (!(allowRoles == null ? void 0 : allowRoles.length)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
for (const roleName of allowRoles) {
|
|
70
|
+
const ok = currentRoles.find((x) => x == roleName);
|
|
71
|
+
if (ok) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const useAuth = () => {
|
|
78
|
+
const searchs = useSearchParams();
|
|
79
|
+
const { token, setToken } = useToken();
|
|
80
|
+
const loginMutation = useMutation(login, {
|
|
81
|
+
onSuccess(data, variables, context) {
|
|
82
|
+
console.log("set token", data.accessToken);
|
|
83
|
+
setToken(data.accessToken);
|
|
84
|
+
document.cookie = `${MtM_TOKEN_NAME}=${data.accessToken}`;
|
|
85
|
+
const backUrl = searchs.get(LOGIN_CALLBACK_URL_KEY) || "/";
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
login: loginMutation
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
export {
|
|
93
|
+
UserProvider,
|
|
94
|
+
isRoleMatch,
|
|
95
|
+
useAuth,
|
|
96
|
+
useIsAdmin,
|
|
97
|
+
useRoles,
|
|
98
|
+
useUser
|
|
99
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sha256WithAlphabets(message: any): Promise<string>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
function sha256WithAlphabets(message) {
|
|
22
|
+
return __async(this, null, function* () {
|
|
23
|
+
const buffer = new TextEncoder().encode(message);
|
|
24
|
+
const hashBuffer = yield crypto.subtle.digest("SHA-256", buffer);
|
|
25
|
+
const hashBase64 = btoa(String.fromCharCode(...new Uint8Array(hashBuffer)));
|
|
26
|
+
const alphabetsOnly = hashBase64.replace(/[^a-zA-Z]/g, "");
|
|
27
|
+
return alphabetsOnly;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
sha256WithAlphabets
|
|
32
|
+
};
|