@akanjs/client 0.9.47 → 0.9.49

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/cjs/src/index.js CHANGED
@@ -22,3 +22,5 @@ __reExport(src_exports, require("./storage"), module.exports);
22
22
  __reExport(src_exports, require("./device"), module.exports);
23
23
  __reExport(src_exports, require("./createFont"), module.exports);
24
24
  __reExport(src_exports, require("./locale"), module.exports);
25
+ __reExport(src_exports, require("./useClient"), module.exports);
26
+ __reExport(src_exports, require("./makePageProto"), module.exports);
package/cjs/src/locale.js CHANGED
@@ -18,12 +18,8 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var locale_exports = {};
20
20
  __export(locale_exports, {
21
- dictionaryContext: () => dictionaryContext,
22
- useDictionary: () => useDictionary
21
+ dictionaryContext: () => dictionaryContext
23
22
  });
24
23
  module.exports = __toCommonJS(locale_exports);
25
24
  var import_react = require("react");
26
25
  const dictionaryContext = (0, import_react.createContext)({});
27
- const useDictionary = () => {
28
- return (0, import_react.useContext)(dictionaryContext);
29
- };
@@ -0,0 +1,156 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var makePageProto_exports = {};
19
+ __export(makePageProto_exports, {
20
+ makePageProto: () => makePageProto
21
+ });
22
+ module.exports = __toCommonJS(makePageProto_exports);
23
+ var import_base = require("@akanjs/base");
24
+ var import_client = require("@akanjs/client");
25
+ var import_common = require("@akanjs/common");
26
+ var import_dictionary2 = require("@akanjs/dictionary");
27
+ var import_signal = require("@akanjs/signal");
28
+ const clientTranslate = ({ lang, dictKey, param }) => {
29
+ const translator = import_base.baseClientEnv.side === "server" ? global.serverTranslator : import_dictionary2.serverTranslator;
30
+ return translator.getTransSync(lang, dictKey, param) ?? dictKey;
31
+ };
32
+ const getPageInfo = () => {
33
+ if (import_base.baseClientEnv.side !== "server") {
34
+ return {
35
+ locale: window.location.pathname.split("/")[1] ?? "en",
36
+ path: "/" + window.location.pathname.split("/").slice(2).join("/")
37
+ };
38
+ }
39
+ const locale = (0, import_client.getHeader)("x-locale") ?? "en";
40
+ const path = (0, import_client.getHeader)("x-path") ?? "/";
41
+ return { locale, path };
42
+ };
43
+ const msg = {
44
+ info: () => null,
45
+ success: () => null,
46
+ error: () => null,
47
+ warning: () => null,
48
+ loading: () => null
49
+ };
50
+ const makePageProto = (cnst) => {
51
+ class Revert extends Error {
52
+ constructor(key, data) {
53
+ super(key);
54
+ }
55
+ }
56
+ return {
57
+ Revert,
58
+ msg,
59
+ usePage: () => {
60
+ const { locale, path } = getPageInfo();
61
+ const lang = locale;
62
+ const l = (key, param) => clientTranslate({ lang, dictKey: key, param });
63
+ l.rich = (key, param) => /* @__PURE__ */ React.createElement(
64
+ "span",
65
+ {
66
+ dangerouslySetInnerHTML: {
67
+ __html: clientTranslate({
68
+ lang,
69
+ dictKey: key,
70
+ param: {
71
+ ...param,
72
+ // strong: (chunks: string) => `<b>${chunks}</b>`,
73
+ // "bg-primary": (chunks: string) => `<span className="bg-primary text-base-100">${chunks}</span>`,
74
+ // primary: (chunks: string) => `<span className="bg-base-100 text-primary">${chunks}</span>`,
75
+ br: `<br />`
76
+ }
77
+ })
78
+ }
79
+ }
80
+ );
81
+ l.field = (model, field) => {
82
+ const key = `${model}.${field}`;
83
+ return l(key);
84
+ };
85
+ l.desc = (model, field) => {
86
+ const key = `${model}.desc-${field}`;
87
+ return l(key);
88
+ };
89
+ l.enum = (model, field, value) => {
90
+ const key = `${model}.enum-${field}-${value}`;
91
+ return l(key);
92
+ };
93
+ l.enumdesc = (model, field, value) => {
94
+ const key = `${model}.enumdesc-${field}-${value}`;
95
+ return l(key);
96
+ };
97
+ l.api = (model, endpoint) => {
98
+ const key = `${model}.api-${endpoint}`;
99
+ return l(key);
100
+ };
101
+ l.apidesc = (model, endpoint) => {
102
+ const key = `${model}.apidesc-${endpoint}`;
103
+ return l(key);
104
+ };
105
+ l.arg = (model, endpoint, arg) => {
106
+ const key = `${model}.arg-${endpoint}-${arg}`;
107
+ return l(key);
108
+ };
109
+ l.argdesc = (model, endpoint, arg) => {
110
+ const key = `${model}.argdesc-${endpoint}-${arg}`;
111
+ return l(key);
112
+ };
113
+ l.qry = (model, queryKey) => {
114
+ const key = `${model}.qry-${queryKey}`;
115
+ return l(key);
116
+ };
117
+ l.qrydesc = (model, queryKey) => {
118
+ const key = `${model}.qrydesc-${queryKey}`;
119
+ return l(key);
120
+ };
121
+ l.qarg = (model, queryKey, arg) => {
122
+ const key = `${model}.qarg-${queryKey}-${arg}`;
123
+ return l(key);
124
+ };
125
+ l.qargdesc = (model, queryKey, arg) => {
126
+ const key = `${model}.qargdesc-${queryKey}-${arg}`;
127
+ return l(key);
128
+ };
129
+ l.trans = (translation) => {
130
+ return translation[lang] ?? "unknown translation";
131
+ };
132
+ return { path, l, lang };
133
+ },
134
+ fetch: import_base.baseClientEnv.side === "server" && import_base.baseClientEnv.operationMode === "local" ? global.builtFetch : global.fetch,
135
+ sig: {},
136
+ registerClient: async (lang) => {
137
+ while (!global.builtFetch) {
138
+ try {
139
+ const [dictionary, { fetch, signals }] = await Promise.all([
140
+ lang ? import_dictionary2.serverTranslator.init(lang) : import_dictionary2.serverTranslator.initAllLanguages(global.dictionary),
141
+ import_signal.signalInfo.registerClient(cnst)
142
+ ]);
143
+ global.builtFetch = fetch;
144
+ global.signals = signals;
145
+ global.dictionary = dictionary;
146
+ global.serverTranslator = import_dictionary2.serverTranslator;
147
+ return { dictionary, signals };
148
+ } catch (e) {
149
+ import_common.Logger.error(e instanceof Error ? e.message : String(e));
150
+ await (0, import_common.sleep)(3e3);
151
+ }
152
+ }
153
+ throw new Error("Unreachable");
154
+ }
155
+ };
156
+ };
package/cjs/src/router.js CHANGED
@@ -173,8 +173,7 @@ class Router {
173
173
  import_common.Logger.log(`redirect to:${pathname}`);
174
174
  (0, import_navigation.redirect)(pathname);
175
175
  } else {
176
- const { pathname } = getPathInfo(href, this.#lang, this.#prefix);
177
- this.#instance.replace(pathname);
176
+ this.#instance.replace(href);
178
177
  }
179
178
  return void 0;
180
179
  }
@@ -0,0 +1,29 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var useClient_exports = {};
19
+ __export(useClient_exports, {
20
+ Revert: () => Revert,
21
+ fetch: () => fetch,
22
+ msg: () => msg,
23
+ registerClient: () => registerClient,
24
+ sig: () => sig,
25
+ usePage: () => usePage
26
+ });
27
+ module.exports = __toCommonJS(useClient_exports);
28
+ var import_makePageProto = require("./makePageProto");
29
+ const { msg, Revert, usePage, fetch, sig, registerClient } = (0, import_makePageProto.makePageProto)({});
package/esm/src/index.js CHANGED
@@ -6,3 +6,5 @@ export * from "./storage";
6
6
  export * from "./device";
7
7
  export * from "./createFont";
8
8
  export * from "./locale";
9
+ export * from "./useClient";
10
+ export * from "./makePageProto";
package/esm/src/locale.js CHANGED
@@ -1,10 +1,6 @@
1
1
  "use client";
2
- import { createContext, useContext } from "react";
2
+ import { createContext } from "react";
3
3
  const dictionaryContext = createContext({});
4
- const useDictionary = () => {
5
- return useContext(dictionaryContext);
6
- };
7
4
  export {
8
- dictionaryContext,
9
- useDictionary
5
+ dictionaryContext
10
6
  };
@@ -0,0 +1,137 @@
1
+ import { baseClientEnv } from "@akanjs/base";
2
+ import { getHeader } from "@akanjs/client";
3
+ import { Logger, sleep } from "@akanjs/common";
4
+ import { serverTranslator } from "@akanjs/dictionary";
5
+ import { signalInfo } from "@akanjs/signal";
6
+ const clientTranslate = ({ lang, dictKey, param }) => {
7
+ const translator = baseClientEnv.side === "server" ? global.serverTranslator : serverTranslator;
8
+ return translator.getTransSync(lang, dictKey, param) ?? dictKey;
9
+ };
10
+ const getPageInfo = () => {
11
+ if (baseClientEnv.side !== "server") {
12
+ return {
13
+ locale: window.location.pathname.split("/")[1] ?? "en",
14
+ path: "/" + window.location.pathname.split("/").slice(2).join("/")
15
+ };
16
+ }
17
+ const locale = getHeader("x-locale") ?? "en";
18
+ const path = getHeader("x-path") ?? "/";
19
+ return { locale, path };
20
+ };
21
+ const msg = {
22
+ info: () => null,
23
+ success: () => null,
24
+ error: () => null,
25
+ warning: () => null,
26
+ loading: () => null
27
+ };
28
+ const makePageProto = (cnst) => {
29
+ class Revert extends Error {
30
+ constructor(key, data) {
31
+ super(key);
32
+ }
33
+ }
34
+ return {
35
+ Revert,
36
+ msg,
37
+ usePage: () => {
38
+ const { locale, path } = getPageInfo();
39
+ const lang = locale;
40
+ const l = (key, param) => clientTranslate({ lang, dictKey: key, param });
41
+ l.rich = (key, param) => /* @__PURE__ */ React.createElement(
42
+ "span",
43
+ {
44
+ dangerouslySetInnerHTML: {
45
+ __html: clientTranslate({
46
+ lang,
47
+ dictKey: key,
48
+ param: {
49
+ ...param,
50
+ // strong: (chunks: string) => `<b>${chunks}</b>`,
51
+ // "bg-primary": (chunks: string) => `<span className="bg-primary text-base-100">${chunks}</span>`,
52
+ // primary: (chunks: string) => `<span className="bg-base-100 text-primary">${chunks}</span>`,
53
+ br: `<br />`
54
+ }
55
+ })
56
+ }
57
+ }
58
+ );
59
+ l.field = (model, field) => {
60
+ const key = `${model}.${field}`;
61
+ return l(key);
62
+ };
63
+ l.desc = (model, field) => {
64
+ const key = `${model}.desc-${field}`;
65
+ return l(key);
66
+ };
67
+ l.enum = (model, field, value) => {
68
+ const key = `${model}.enum-${field}-${value}`;
69
+ return l(key);
70
+ };
71
+ l.enumdesc = (model, field, value) => {
72
+ const key = `${model}.enumdesc-${field}-${value}`;
73
+ return l(key);
74
+ };
75
+ l.api = (model, endpoint) => {
76
+ const key = `${model}.api-${endpoint}`;
77
+ return l(key);
78
+ };
79
+ l.apidesc = (model, endpoint) => {
80
+ const key = `${model}.apidesc-${endpoint}`;
81
+ return l(key);
82
+ };
83
+ l.arg = (model, endpoint, arg) => {
84
+ const key = `${model}.arg-${endpoint}-${arg}`;
85
+ return l(key);
86
+ };
87
+ l.argdesc = (model, endpoint, arg) => {
88
+ const key = `${model}.argdesc-${endpoint}-${arg}`;
89
+ return l(key);
90
+ };
91
+ l.qry = (model, queryKey) => {
92
+ const key = `${model}.qry-${queryKey}`;
93
+ return l(key);
94
+ };
95
+ l.qrydesc = (model, queryKey) => {
96
+ const key = `${model}.qrydesc-${queryKey}`;
97
+ return l(key);
98
+ };
99
+ l.qarg = (model, queryKey, arg) => {
100
+ const key = `${model}.qarg-${queryKey}-${arg}`;
101
+ return l(key);
102
+ };
103
+ l.qargdesc = (model, queryKey, arg) => {
104
+ const key = `${model}.qargdesc-${queryKey}-${arg}`;
105
+ return l(key);
106
+ };
107
+ l.trans = (translation) => {
108
+ return translation[lang] ?? "unknown translation";
109
+ };
110
+ return { path, l, lang };
111
+ },
112
+ fetch: baseClientEnv.side === "server" && baseClientEnv.operationMode === "local" ? global.builtFetch : global.fetch,
113
+ sig: {},
114
+ registerClient: async (lang) => {
115
+ while (!global.builtFetch) {
116
+ try {
117
+ const [dictionary, { fetch, signals }] = await Promise.all([
118
+ lang ? serverTranslator.init(lang) : serverTranslator.initAllLanguages(global.dictionary),
119
+ signalInfo.registerClient(cnst)
120
+ ]);
121
+ global.builtFetch = fetch;
122
+ global.signals = signals;
123
+ global.dictionary = dictionary;
124
+ global.serverTranslator = serverTranslator;
125
+ return { dictionary, signals };
126
+ } catch (e) {
127
+ Logger.error(e instanceof Error ? e.message : String(e));
128
+ await sleep(3e3);
129
+ }
130
+ }
131
+ throw new Error("Unreachable");
132
+ }
133
+ };
134
+ };
135
+ export {
136
+ makePageProto
137
+ };
package/esm/src/router.js CHANGED
@@ -150,8 +150,7 @@ class Router {
150
150
  Logger.log(`redirect to:${pathname}`);
151
151
  redirect(pathname);
152
152
  } else {
153
- const { pathname } = getPathInfo(href, this.#lang, this.#prefix);
154
- this.#instance.replace(pathname);
153
+ this.#instance.replace(href);
155
154
  }
156
155
  return void 0;
157
156
  }
@@ -0,0 +1,10 @@
1
+ import { makePageProto } from "./makePageProto";
2
+ const { msg, Revert, usePage, fetch, sig, registerClient } = makePageProto({});
3
+ export {
4
+ Revert,
5
+ fetch,
6
+ msg,
7
+ registerClient,
8
+ sig,
9
+ usePage
10
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/client",
3
- "version": "0.9.47",
3
+ "version": "0.9.49",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/src/index.d.ts CHANGED
@@ -6,3 +6,5 @@ export * from "./storage";
6
6
  export * from "./device";
7
7
  export * from "./createFont";
8
8
  export * from "./locale";
9
+ export * from "./useClient";
10
+ export * from "./makePageProto";
package/src/locale.d.ts CHANGED
@@ -3,8 +3,3 @@ export declare const dictionaryContext: import("react").Context<{
3
3
  [key: string]: string;
4
4
  };
5
5
  }>;
6
- export declare const useDictionary: () => {
7
- [key: string]: {
8
- [key: string]: string;
9
- };
10
- };
@@ -0,0 +1,63 @@
1
+ import { AllDictionary, type Translation, type TransMessage } from "@akanjs/dictionary";
2
+ import { type ReactNode } from "react";
3
+ export interface TransMessageOption {
4
+ key?: string;
5
+ duration?: number;
6
+ data?: {
7
+ [key: string]: any;
8
+ };
9
+ }
10
+ export declare const makePageProto: <Locale extends {
11
+ [key: string]: {
12
+ [key: string]: Translation;
13
+ };
14
+ }, Fetch, Sig>(cnst: any) => {
15
+ Revert: {
16
+ new (key: TransMessage<Locale>, data?: any): {
17
+ name: string;
18
+ message: string;
19
+ stack?: string;
20
+ };
21
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
22
+ prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
23
+ stackTraceLimit: number;
24
+ };
25
+ msg: {
26
+ info: (key: TransMessage<Locale>, option?: TransMessageOption) => void;
27
+ success: (key: TransMessage<Locale>, option?: TransMessageOption) => void;
28
+ error: (key: TransMessage<Locale>, option?: TransMessageOption) => void;
29
+ warning: (key: TransMessage<Locale>, option?: TransMessageOption) => void;
30
+ loading: (key: TransMessage<Locale>, option?: TransMessageOption) => void;
31
+ };
32
+ usePage: () => {
33
+ path: string;
34
+ l: {
35
+ (key: TransMessage<Locale>, param?: {
36
+ [key: string]: string | number;
37
+ }): string;
38
+ rich(key: TransMessage<Locale>, param?: {
39
+ [key: string]: string | number;
40
+ }): ReactNode;
41
+ field<ModelKey extends keyof Locale>(model: ModelKey, field: keyof Locale[ModelKey]): string;
42
+ desc<ModelKey extends keyof Locale>(model: ModelKey, field: keyof Locale[ModelKey]): string;
43
+ enum<ModelKey extends keyof Locale>(model: ModelKey, field: keyof Locale[ModelKey], value: string): string;
44
+ enumdesc<ModelKey extends keyof Locale>(model: ModelKey, field: keyof Locale[ModelKey], value: string): string;
45
+ api<ModelKey extends keyof Locale>(model: ModelKey, endpoint: keyof Locale[ModelKey]): string;
46
+ apidesc<ModelKey extends keyof Locale>(model: ModelKey, endpoint: keyof Locale[ModelKey]): string;
47
+ arg<ModelKey extends keyof Locale>(model: ModelKey, endpoint: keyof Locale[ModelKey], arg: string): string;
48
+ argdesc<ModelKey extends keyof Locale>(model: ModelKey, endpoint: keyof Locale[ModelKey], arg: string): string;
49
+ qry<ModelKey extends keyof Locale>(model: ModelKey, queryKey: keyof Locale[ModelKey]): string;
50
+ qrydesc<ModelKey extends keyof Locale>(model: ModelKey, queryKey: keyof Locale[ModelKey]): string;
51
+ qarg<ModelKey extends keyof Locale>(model: ModelKey, queryKey: keyof Locale[ModelKey], arg: string): string;
52
+ qargdesc<ModelKey extends keyof Locale>(model: ModelKey, queryKey: keyof Locale[ModelKey], arg: string): string;
53
+ trans<Returns extends ReactNode>(translation: Record<"en" | "ko" | (string & {}), Returns>): Returns extends string ? string : Returns;
54
+ };
55
+ lang: string;
56
+ };
57
+ fetch: Fetch;
58
+ sig: Sig;
59
+ registerClient: (lang?: string) => Promise<{
60
+ dictionary: AllDictionary;
61
+ signals: import("@akanjs/signal").SerializedSignal[];
62
+ }>;
63
+ };