@akanjs/client 0.0.4

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/src/device.js ADDED
@@ -0,0 +1,131 @@
1
+ "use client";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var device_exports = {};
20
+ __export(device_exports, {
21
+ device: () => device
22
+ });
23
+ module.exports = __toCommonJS(device_exports);
24
+ var import_device = require("@capacitor/device");
25
+ var import_haptics = require("@capacitor/haptics");
26
+ var import_keyboard = require("@capacitor/keyboard");
27
+ var import_capacitor_plugin_safe_area = require("capacitor-plugin-safe-area");
28
+ var import_react_device_detect = require("react-device-detect");
29
+ class Device {
30
+ info;
31
+ lang;
32
+ topSafeArea;
33
+ bottomSafeArea;
34
+ isMobile = import_react_device_detect.isMobile;
35
+ #keyboard = import_keyboard.Keyboard;
36
+ #haptics = import_haptics.Haptics;
37
+ #pageContentRef = null;
38
+ async init({ lang, supportLanguages = [] } = {}) {
39
+ const [
40
+ info,
41
+ { value: languageCode },
42
+ {
43
+ insets: { top: topSafeArea, bottom: bottomSafeArea }
44
+ }
45
+ ] = await Promise.all([import_device.Device.getInfo(), import_device.Device.getLanguageCode(), import_capacitor_plugin_safe_area.SafeArea.getSafeAreaInsets()]);
46
+ const predefinedLangPath = window.location.pathname.split("/")[1]?.split("?")[0];
47
+ const predefinedLang = supportLanguages.find((language) => language === predefinedLangPath);
48
+ this.info = info;
49
+ this.lang = lang ?? predefinedLang ?? languageCode;
50
+ this.topSafeArea = topSafeArea;
51
+ this.bottomSafeArea = bottomSafeArea;
52
+ }
53
+ setPageContentRef(pageContentRef) {
54
+ this.#pageContentRef = pageContentRef;
55
+ }
56
+ async showKeyboard() {
57
+ if (this.info.platform === "web")
58
+ return;
59
+ await this.#keyboard.show();
60
+ }
61
+ async hideKeyboard() {
62
+ if (this.info.platform === "web")
63
+ return;
64
+ await this.#keyboard.hide();
65
+ }
66
+ listenKeyboardChanged(onKeyboardChanged) {
67
+ if (this.info.platform === "web")
68
+ return;
69
+ void this.#keyboard.addListener("keyboardWillShow", (keyboard) => {
70
+ onKeyboardChanged(keyboard.keyboardHeight);
71
+ });
72
+ void this.#keyboard.addListener("keyboardDidShow", (keyboard) => {
73
+ onKeyboardChanged(keyboard.keyboardHeight);
74
+ });
75
+ void this.#keyboard.addListener("keyboardWillHide", () => {
76
+ onKeyboardChanged(0);
77
+ });
78
+ void this.#keyboard.addListener("keyboardDidHide", () => {
79
+ onKeyboardChanged(0);
80
+ });
81
+ }
82
+ unlistenKeyboardChanged() {
83
+ if (this.info.platform === "web")
84
+ return;
85
+ void this.#keyboard.removeAllListeners();
86
+ }
87
+ async vibrate(type = "medium") {
88
+ if (typeof type === "number") {
89
+ await this.#haptics.vibrate({ duration: type });
90
+ return;
91
+ }
92
+ const handleImpact = {
93
+ light: async () => {
94
+ await this.#haptics.impact({ style: import_haptics.ImpactStyle.Light });
95
+ },
96
+ medium: async () => {
97
+ await this.#haptics.impact({ style: import_haptics.ImpactStyle.Medium });
98
+ },
99
+ heavy: async () => {
100
+ await this.#haptics.impact({ style: import_haptics.ImpactStyle.Heavy });
101
+ },
102
+ selectionStart: async () => {
103
+ await this.#haptics.selectionStart();
104
+ },
105
+ selectionChanged: async () => {
106
+ await this.#haptics.selectionChanged();
107
+ },
108
+ selectionEnd: async () => {
109
+ await this.#haptics.selectionEnd();
110
+ }
111
+ };
112
+ await handleImpact[type]();
113
+ }
114
+ getScrollTop() {
115
+ if (this.info.platform === "web")
116
+ return window.scrollY;
117
+ return this.#pageContentRef?.current?.scrollTop ?? 0;
118
+ }
119
+ setScrollTop(scrollTop) {
120
+ if (this.info.platform === "web") {
121
+ window.scrollTo({ top: scrollTop });
122
+ return;
123
+ }
124
+ return this.#pageContentRef?.current?.scrollTo({ top: scrollTop });
125
+ }
126
+ }
127
+ const device = new Device();
128
+ // Annotate the CommonJS export names for ESM import in node:
129
+ 0 && (module.exports = {
130
+ device
131
+ });
package/src/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./types";
2
+ export * from "./storeDecorators";
3
+ export * from "./csrTypes";
4
+ export * from "./router";
5
+ export * from "./cookie";
6
+ export * from "./storage";
7
+ export * from "./device";
8
+ export * from "./createFont";
package/src/index.js ADDED
@@ -0,0 +1,35 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var src_exports = {};
16
+ module.exports = __toCommonJS(src_exports);
17
+ __reExport(src_exports, require("./types"), module.exports);
18
+ __reExport(src_exports, require("./storeDecorators"), module.exports);
19
+ __reExport(src_exports, require("./csrTypes"), module.exports);
20
+ __reExport(src_exports, require("./router"), module.exports);
21
+ __reExport(src_exports, require("./cookie"), module.exports);
22
+ __reExport(src_exports, require("./storage"), module.exports);
23
+ __reExport(src_exports, require("./device"), module.exports);
24
+ __reExport(src_exports, require("./createFont"), module.exports);
25
+ // Annotate the CommonJS export names for ESM import in node:
26
+ 0 && (module.exports = {
27
+ ...require("./types"),
28
+ ...require("./storeDecorators"),
29
+ ...require("./csrTypes"),
30
+ ...require("./router"),
31
+ ...require("./cookie"),
32
+ ...require("./storage"),
33
+ ...require("./device"),
34
+ ...require("./createFont")
35
+ });
@@ -0,0 +1,44 @@
1
+ export interface RouterInstance {
2
+ push: (href: string) => void;
3
+ replace: (href: string) => void;
4
+ back: () => void;
5
+ refresh: () => void;
6
+ }
7
+ interface RouterOptions {
8
+ prefix?: string;
9
+ lang?: string;
10
+ }
11
+ interface NextServerRouterOption extends RouterOptions {
12
+ type: "next";
13
+ side: "server";
14
+ }
15
+ interface NextClientRouterOption extends RouterOptions {
16
+ type: "next";
17
+ side: "client";
18
+ router: RouterInstance;
19
+ }
20
+ interface CSRClientRouterOption extends RouterOptions {
21
+ type: "csr";
22
+ router: RouterInstance;
23
+ }
24
+ export declare const getPathInfo: (href: string, lang: string, prefix: string) => {
25
+ path: string;
26
+ pathname: string;
27
+ };
28
+ declare class Router {
29
+ #private;
30
+ isInitialized: boolean;
31
+ init(options: NextClientRouterOption | NextServerRouterOption | CSRClientRouterOption): void;
32
+ push(href: string): never;
33
+ replace(href: string): never;
34
+ back(): never;
35
+ refresh(): never;
36
+ redirect(href: string): Promise<never>;
37
+ notFound(): never;
38
+ setLang(lang: string): never;
39
+ getPath(pathname?: string): string;
40
+ getPrefix(): string;
41
+ getPrefixedPath(path: string): string;
42
+ }
43
+ export declare const router: Router;
44
+ export {};
package/src/router.js ADDED
@@ -0,0 +1,214 @@
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 router_exports = {};
19
+ __export(router_exports, {
20
+ getPathInfo: () => getPathInfo,
21
+ router: () => router
22
+ });
23
+ module.exports = __toCommonJS(router_exports);
24
+ var import_base = require("@akanjs/base");
25
+ var import_common = require("@akanjs/common");
26
+ var import_navigation = require("next/navigation");
27
+ const getPathInfo = (href, lang, prefix) => {
28
+ const langLength = lang.length + 1;
29
+ const pathWithSubRoute = href === `/${lang}` ? "/" : href.startsWith(`/${lang}/`) ? href.slice(langLength) : href;
30
+ const prefixLength = prefix ? prefix.length + 1 : 0;
31
+ const path = !prefixLength ? pathWithSubRoute : pathWithSubRoute === `/${prefix}` ? "/" : pathWithSubRoute.startsWith(`/${prefix}`) ? pathWithSubRoute.slice(prefixLength) : pathWithSubRoute;
32
+ const subRoute = prefix ? `/${prefix}` : "";
33
+ const pathname = path.startsWith("http") ? path : path === "/" ? `/${lang}${subRoute}` : `/${lang}${subRoute}${path}`;
34
+ return { path, pathname };
35
+ };
36
+ class Router {
37
+ isInitialized = false;
38
+ #prefix = "";
39
+ #lang = "en";
40
+ #instance = {
41
+ push: (href) => {
42
+ const { pathname } = this.#getPathInfo(href);
43
+ import_common.Logger.log(`push to:${pathname}`);
44
+ if (import_base.baseClientEnv.side === "server")
45
+ void (0, import_navigation.redirect)(pathname);
46
+ },
47
+ replace: (href) => {
48
+ const { pathname } = this.#getPathInfo(href);
49
+ import_common.Logger.log(`replace to:${pathname}`);
50
+ if (import_base.baseClientEnv.side === "server")
51
+ void (0, import_navigation.redirect)(pathname);
52
+ },
53
+ back: () => {
54
+ throw new Error("back is only available in client");
55
+ },
56
+ refresh: () => {
57
+ throw new Error("refresh is only available in client");
58
+ }
59
+ };
60
+ init(options) {
61
+ this.#prefix = options.prefix ?? "";
62
+ this.#lang = options.lang ?? "en";
63
+ if (options.type === "csr")
64
+ this.#initCSRClientRouter(options);
65
+ else if (options.side === "server")
66
+ this.#initNextServerRouter(options);
67
+ else
68
+ this.#initNextClientRouter(options);
69
+ this.isInitialized = true;
70
+ import_common.Logger.verbose("Router initialized");
71
+ }
72
+ #initNextServerRouter(options) {
73
+ }
74
+ #initNextClientRouter(options) {
75
+ this.#instance = {
76
+ push: (href) => {
77
+ const { path, pathname } = this.#getPathInfo(href);
78
+ this.#postPathChange({ path, pathname });
79
+ options.router.push(pathname);
80
+ },
81
+ replace: (href) => {
82
+ const { path, pathname } = this.#getPathInfo(href);
83
+ this.#postPathChange({ path, pathname });
84
+ options.router.replace(pathname);
85
+ },
86
+ back: () => {
87
+ const { path, pathname } = this.#getPathInfo(document.referrer);
88
+ this.#postPathChange({ path, pathname });
89
+ options.router.back();
90
+ },
91
+ refresh: () => {
92
+ const { path, pathname } = this.#getPathInfo(location.pathname);
93
+ this.#postPathChange({ path, pathname });
94
+ options.router.refresh();
95
+ }
96
+ };
97
+ }
98
+ #initCSRClientRouter(options) {
99
+ this.#instance = {
100
+ push: (href) => {
101
+ const { path, pathname } = this.#getPathInfo(href);
102
+ if (location.pathname === pathname)
103
+ return;
104
+ this.#postPathChange({ path, pathname });
105
+ options.router.push(pathname);
106
+ },
107
+ replace: (href) => {
108
+ const { path, pathname } = this.#getPathInfo(href);
109
+ if (location.pathname === pathname)
110
+ return;
111
+ this.#postPathChange({ path, pathname });
112
+ options.router.replace(pathname);
113
+ },
114
+ back: () => {
115
+ const { path, pathname } = this.#getPathInfo(document.referrer);
116
+ if (location.pathname === pathname)
117
+ return;
118
+ this.#postPathChange({ path, pathname });
119
+ options.router.back();
120
+ },
121
+ refresh: () => {
122
+ const { path, pathname } = this.#getPathInfo(location.pathname);
123
+ this.#postPathChange({ path, pathname });
124
+ options.router.refresh();
125
+ }
126
+ };
127
+ }
128
+ #checkInitialized() {
129
+ if (!this.isInitialized)
130
+ throw new Error("Router is not initialized");
131
+ }
132
+ #getPathInfo(href, prefix = this.#prefix) {
133
+ return getPathInfo(href, this.#lang, prefix);
134
+ }
135
+ #postPathChange({ path, pathname }) {
136
+ import_common.Logger.log(`pathChange-start:${path}`);
137
+ window.parent.postMessage({ type: "pathChange", path, pathname }, "*");
138
+ }
139
+ push(href) {
140
+ this.#checkInitialized();
141
+ this.#instance.push(href);
142
+ return void 0;
143
+ }
144
+ replace(href) {
145
+ this.#checkInitialized();
146
+ this.#instance.replace(href);
147
+ return void 0;
148
+ }
149
+ back() {
150
+ if (import_base.baseClientEnv.side === "server")
151
+ throw new Error("back is only available in client side");
152
+ this.#checkInitialized();
153
+ this.#instance.back();
154
+ return void 0;
155
+ }
156
+ refresh() {
157
+ if (import_base.baseClientEnv.side === "server")
158
+ throw new Error("refresh is only available in client side");
159
+ this.#checkInitialized();
160
+ this.#instance.refresh();
161
+ return void 0;
162
+ }
163
+ async redirect(href) {
164
+ if (import_base.baseClientEnv.side === "server") {
165
+ const nextHeaders = require("next/headers");
166
+ const headers = await nextHeaders.headers?.() ?? /* @__PURE__ */ new Map();
167
+ const lang = headers.get("x-locale") ?? this.#lang;
168
+ const basePath = headers.get("x-base-path");
169
+ const { pathname } = getPathInfo(href, lang, basePath ?? "");
170
+ import_common.Logger.log(`redirect to:${pathname}`);
171
+ (0, import_navigation.redirect)(pathname);
172
+ } else {
173
+ const { pathname } = getPathInfo(href, this.#lang, this.#prefix);
174
+ this.#instance.replace(pathname);
175
+ }
176
+ return void 0;
177
+ }
178
+ notFound() {
179
+ this.#checkInitialized();
180
+ if (import_base.baseClientEnv.side === "server") {
181
+ import_common.Logger.log(`redirect to:/404`);
182
+ (0, import_navigation.notFound)();
183
+ } else
184
+ this.#instance.replace("/404");
185
+ return void 0;
186
+ }
187
+ setLang(lang) {
188
+ if (import_base.baseClientEnv.side === "server")
189
+ throw new Error("setLang is only available in client side");
190
+ this.#checkInitialized();
191
+ const { path } = getPathInfo(window.location.pathname, this.#lang, this.#prefix);
192
+ this.#lang = lang;
193
+ this.#instance.replace(`/${lang}${path}`);
194
+ return void 0;
195
+ }
196
+ getPath(pathname = window.location.pathname) {
197
+ if (import_base.baseClientEnv.side === "server")
198
+ throw new Error("getPath is only available in client side");
199
+ const { path } = getPathInfo(pathname, this.#lang, this.#prefix);
200
+ return path;
201
+ }
202
+ getPrefix() {
203
+ return this.#prefix;
204
+ }
205
+ getPrefixedPath(path) {
206
+ return this.#prefix ? `${this.#lang ? `/${this.#lang}` : ""}/${this.#prefix}${path}` : path;
207
+ }
208
+ }
209
+ const router = new Router();
210
+ // Annotate the CommonJS export names for ESM import in node:
211
+ 0 && (module.exports = {
212
+ getPathInfo,
213
+ router
214
+ });
@@ -0,0 +1,5 @@
1
+ export declare const storage: {
2
+ getItem: (key: string) => Promise<string | null | undefined>;
3
+ setItem: (key: string, value: string) => Promise<void>;
4
+ removeItem: (key: string) => Promise<void> | undefined;
5
+ };
package/src/storage.js ADDED
@@ -0,0 +1,58 @@
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 storage_exports = {};
19
+ __export(storage_exports, {
20
+ storage: () => storage
21
+ });
22
+ module.exports = __toCommonJS(storage_exports);
23
+ var import_base = require("@akanjs/base");
24
+ var import_preferences = require("@capacitor/preferences");
25
+ const storage = {
26
+ getItem: async (key) => {
27
+ if (import_base.baseClientEnv.side === "server")
28
+ return;
29
+ if (import_base.baseClientEnv.renderMode === "ssr")
30
+ return localStorage.getItem(key);
31
+ else
32
+ return (await import_preferences.Preferences.get({ key })).value;
33
+ },
34
+ setItem: async (key, value) => {
35
+ if (import_base.baseClientEnv.side === "server")
36
+ return;
37
+ if (import_base.baseClientEnv.renderMode === "ssr") {
38
+ localStorage.setItem(key, value);
39
+ return;
40
+ } else {
41
+ await import_preferences.Preferences.set({ key, value });
42
+ return;
43
+ }
44
+ },
45
+ removeItem: (key) => {
46
+ if (import_base.baseClientEnv.side === "server")
47
+ return;
48
+ if (import_base.baseClientEnv.renderMode === "ssr") {
49
+ localStorage.removeItem(key);
50
+ return;
51
+ } else
52
+ return import_preferences.Preferences.remove({ key });
53
+ }
54
+ };
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ storage
58
+ });