@niyamulahsan/create-deskapp 1.2.4 → 1.2.6
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/package.json +16 -16
- package/template/.env.example +20 -13
- package/template/AGENTS.md +699 -0
- package/template/package.json +3 -0
- package/template/src/config/app.ts +26 -24
- package/template/src/config/winsetup.ts +93 -91
- package/template/src/core/api/binding-error.ts +57 -0
- package/template/src/core/api/bindings.generated.ts +2 -0
- package/template/src/core/api/generate.manifest.ts +84 -80
- package/template/src/core/api/registry.ts +58 -48
- package/template/src/core/config.ts +101 -99
- package/template/src/core/database/aggregate.ts +89 -85
- package/template/src/core/database/config.ts +50 -48
- package/template/src/core/database/connection.ts +116 -114
- package/template/src/core/database/migrate-hooks.ts +86 -84
- package/template/src/core/database/paginate.ts +226 -224
- package/template/src/core/database/reset.ts +21 -19
- package/template/src/core/database/seed.ts +169 -167
- package/template/src/core/database/status.ts +37 -35
- package/template/src/core/env.ts +108 -106
- package/template/src/core/facade.ts +63 -61
- package/template/src/core/maker/db/fresh.ts +10 -8
- package/template/src/core/maker/db/generate.ts +13 -11
- package/template/src/core/maker/db/helpers.ts +92 -96
- package/template/src/core/maker/db/migrate.ts +37 -35
- package/template/src/core/maker/db/mod.ts +93 -91
- package/template/src/core/maker/db/module-migrate.ts +56 -54
- package/template/src/core/maker/db/push.ts +23 -21
- package/template/src/core/maker/db/reset.ts +8 -6
- package/template/src/core/maker/db/seed.ts +9 -7
- package/template/src/core/maker/db/status.ts +8 -6
- package/template/src/core/maker/help.ts +59 -57
- package/template/src/core/maker/make/controller.ts +25 -22
- package/template/src/core/maker/make/helpers.ts +97 -91
- package/template/src/core/maker/make/mod.ts +69 -67
- package/template/src/core/maker/make/model.ts +16 -14
- package/template/src/core/maker/make/module.ts +53 -39
- package/template/src/core/maker/make/seeder.ts +23 -21
- package/template/src/core/maker/make/test.ts +33 -30
- package/template/src/core/maker/mod.ts +85 -83
- package/template/src/core/maker/run/mod.ts +134 -132
- package/template/src/core/maker/stub.ts +26 -24
- package/template/src/core/maker/stubs/controller/controller.controller.stub +57 -55
- package/template/src/core/maker/stubs/controller/controller.schema.stub +18 -13
- package/template/src/core/maker/stubs/facade/facade.stub +21 -0
- package/template/src/core/maker/stubs/model/model.stub +10 -8
- package/template/src/core/maker/stubs/seeder/seeder.stub +18 -16
- package/template/src/core/maker/stubs/test/test.stub +9 -7
- package/template/src/core/maker/sync-config.ts +93 -94
- package/template/src/core/utils/password.ts +29 -27
- package/template/src/core/utils/playwright.ts +123 -121
- package/template/src/core/utils/queue.ts +68 -66
- package/template/src/core/utils/scheduler.ts +62 -60
- package/template/src/core/utils/storage.ts +3 -1
- package/template/src/core/utils/validation.ts +37 -35
- package/template/src/database/schema.ts +2 -0
- package/template/src/main.ts +120 -118
- package/template/src/modules/app/__tests__/demo.controller.test.ts +75 -0
- package/template/src/modules/app/__tests__/net.controller.test.ts +65 -0
- package/template/src/modules/app/__tests__/playwright.controller.test.ts +50 -0
- package/template/src/modules/app/__tests__/storage.controller.test.ts +114 -0
- package/template/src/modules/app/__tests__/window.controller.test.ts +98 -0
- package/template/src/modules/app/controllers/demo.controller.ts +120 -118
- package/template/src/modules/app/controllers/net.controller.ts +50 -48
- package/template/src/modules/app/controllers/playwright.controller.ts +142 -140
- package/template/src/modules/app/controllers/storage.controller.ts +231 -229
- package/template/src/modules/app/controllers/window.controller.ts +2 -0
- package/template/src/modules/app/facade.ts +21 -0
- package/template/src/preload.ts +25 -2
- package/template/src/ui/src/App.vue +4 -2
- package/template/src/ui/src/app.ts +38 -23
- package/template/src/ui/src/bindings.d.ts +18 -16
- package/template/src/ui/src/components/Button.vue +34 -32
- package/template/src/ui/src/components/Checkbox.vue +26 -29
- package/template/src/ui/src/components/Input.vue +221 -227
- package/template/src/ui/src/components/InputGroup.vue +141 -0
- package/template/src/ui/src/components/InputPasswordToggle.vue +204 -0
- package/template/src/ui/src/components/Modal.vue +65 -55
- package/template/src/ui/src/components/Spinner.vue +44 -42
- package/template/src/ui/src/components/Switch.vue +62 -65
- package/template/src/ui/src/components/TextArea.vue +108 -121
- package/template/src/ui/src/components/Toast.vue +55 -56
- package/template/src/ui/src/components/datatable/DataTableSkeleton.vue +100 -98
- package/template/src/ui/src/components/datatable/Pagination.vue +153 -125
- package/template/src/ui/src/components/datatable/SelectOpption.vue +36 -35
- package/template/src/ui/src/components/datatable/index.vue +336 -258
- package/template/src/ui/src/env.d.ts +16 -13
- package/template/src/ui/src/global.d.ts +21 -2
- package/template/src/ui/src/helpers/nformatter.ts +12 -10
- package/template/src/ui/src/pages/dashboard/index.vue +690 -687
- package/template/src/ui/src/plugins/browserDetect.ts +57 -0
- package/template/src/ui/src/plugins/gum.ts +576 -549
- package/template/src/ui/src/plugins/pulse.ts +181 -147
- package/template/src/ui/src/plugins/routeProgress.ts +89 -0
- package/template/src/ui/src/router/index.ts +44 -25
- package/template/src/ui/src/stores/example.ts +2 -0
- package/template/src/ui/vite.config.ts +40 -38
- package/template/src/window/app-menu.ts +89 -87
- package/template/src/window/config.ts +14 -12
- package/template/src/window/context-menu.ts +23 -21
- package/template/src/window/dialogs.ts +92 -90
- package/template/src/window/manager.ts +2 -0
- package/template/src/window/native.ts +47 -45
- package/template/src/window/tray.ts +132 -130
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// ./src/modules/app/__tests__/storage.controller.test.ts
|
|
2
|
+
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
const mocks = vi.hoisted(() => {
|
|
6
|
+
const files = {
|
|
7
|
+
readBinaryFile: vi.fn(async () => Buffer.from("hello")),
|
|
8
|
+
fileMeta: vi.fn(async () => ({ size: 5, isFile: true, modifiedAt: new Date(0) })),
|
|
9
|
+
pathExists: vi.fn(async () => false),
|
|
10
|
+
writeTextFile: vi.fn(async () => undefined),
|
|
11
|
+
uploadFile: vi.fn(async () => "/uploads/f.txt"),
|
|
12
|
+
downloadFile: vi.fn(async (_s: string, d: string) => d),
|
|
13
|
+
removePath: vi.fn(async () => undefined),
|
|
14
|
+
listFiles: vi.fn(async () => ["a"]),
|
|
15
|
+
listDirectories: vi.fn(async () => ["d"]),
|
|
16
|
+
};
|
|
17
|
+
const pickers = {
|
|
18
|
+
open: vi.fn(async () => "/src.txt"),
|
|
19
|
+
save: vi.fn(async () => "/dst.txt"),
|
|
20
|
+
folder: vi.fn(async () => "/dir"),
|
|
21
|
+
};
|
|
22
|
+
const storage = {
|
|
23
|
+
init: vi.fn(async () => undefined),
|
|
24
|
+
put: vi.fn(async () => "k"),
|
|
25
|
+
get: vi.fn(async () => Buffer.from('{"a":1}')),
|
|
26
|
+
exists: vi.fn(async () => false),
|
|
27
|
+
files: vi.fn(async () => ["f"]),
|
|
28
|
+
directories: vi.fn(async () => ["d"]),
|
|
29
|
+
generateForDownload: vi.fn(async () => "generated/x"),
|
|
30
|
+
consumeGenerated: vi.fn(async () => Buffer.from("gen")),
|
|
31
|
+
delete: vi.fn(async () => undefined),
|
|
32
|
+
disk: vi.fn(() => ({ path: vi.fn(() => "/private") })),
|
|
33
|
+
};
|
|
34
|
+
return { files, pickers, storage };
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
vi.mock("@/core/facade", () => ({ files: mocks.files, pickers: mocks.pickers, storage: mocks.storage }));
|
|
38
|
+
vi.mock("node:fs", () => ({ default: { existsSync: () => true } }));
|
|
39
|
+
vi.mock("node:fs/promises", async (importOriginal) => {
|
|
40
|
+
const actual = await importOriginal<typeof import("node:fs/promises")>();
|
|
41
|
+
return { ...actual, default: actual, mkdtemp: vi.fn(async () => "/tmp/t") };
|
|
42
|
+
});
|
|
43
|
+
vi.mock("node:os", async (importOriginal) => {
|
|
44
|
+
const actual = await importOriginal<typeof import("node:os")>();
|
|
45
|
+
return { ...actual, default: { ...actual, tmpdir: () => "/tmp" }, tmpdir: () => "/tmp" };
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
import * as ctrl from "@/modules/app/controllers/storage.controller";
|
|
49
|
+
|
|
50
|
+
describe("app storage.controller", () => {
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
vi.clearAllMocks();
|
|
53
|
+
mocks.pickers.open.mockReset().mockResolvedValue("/src.txt");
|
|
54
|
+
mocks.pickers.save.mockReset().mockResolvedValue("/dst.txt");
|
|
55
|
+
mocks.pickers.folder.mockReset().mockResolvedValue("/dir");
|
|
56
|
+
mocks.files.downloadFile.mockReset().mockImplementation(async (_s: string, d: string) => d);
|
|
57
|
+
mocks.files.uploadFile.mockReset().mockResolvedValue("/uploads/f.txt");
|
|
58
|
+
mocks.files.removePath.mockReset().mockResolvedValue(undefined);
|
|
59
|
+
mocks.files.listFiles.mockReset().mockResolvedValue(["a"]);
|
|
60
|
+
mocks.files.listDirectories.mockReset().mockResolvedValue(["d"]);
|
|
61
|
+
mocks.files.readBinaryFile.mockReset().mockResolvedValue(Buffer.from("hello"));
|
|
62
|
+
mocks.files.fileMeta.mockReset().mockResolvedValue({ size: 5, isFile: true, modifiedAt: new Date(0) } as any);
|
|
63
|
+
mocks.files.pathExists.mockReset().mockResolvedValue(false);
|
|
64
|
+
mocks.files.writeTextFile.mockReset().mockResolvedValue(undefined);
|
|
65
|
+
mocks.storage.disk.mockReset().mockReturnValue({ path: vi.fn(() => "/private") } as any);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("exposes handlers", () => {
|
|
69
|
+
expect(ctrl.handlers.open).toBe(ctrl.open);
|
|
70
|
+
expect(ctrl.handlers.appSave).toBe(ctrl.appSave);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("open/save cancel and success paths", async () => {
|
|
74
|
+
mocks.pickers.open.mockResolvedValueOnce(null);
|
|
75
|
+
expect(await ctrl.open()).toMatchObject({ ok: false, error: "cancelled" });
|
|
76
|
+
mocks.pickers.open.mockResolvedValue("/src.txt");
|
|
77
|
+
expect(await ctrl.open()).toMatchObject({ ok: true, path: "/src.txt" });
|
|
78
|
+
|
|
79
|
+
mocks.pickers.save.mockResolvedValueOnce(null);
|
|
80
|
+
expect(await ctrl.save()).toMatchObject({ ok: false, error: "cancelled" });
|
|
81
|
+
mocks.pickers.save.mockResolvedValue("/dst.txt");
|
|
82
|
+
expect(await ctrl.save()).toMatchObject({ ok: true, path: "/dst.txt" });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("upload/download/remove/list", async () => {
|
|
86
|
+
mocks.pickers.open.mockResolvedValueOnce(null);
|
|
87
|
+
expect(await ctrl.upload()).toMatchObject({ ok: false, error: "cancelled" });
|
|
88
|
+
mocks.pickers.open.mockResolvedValueOnce("/src.txt");
|
|
89
|
+
expect(await ctrl.upload()).toMatchObject({ ok: true, dest: "/uploads/f.txt" });
|
|
90
|
+
|
|
91
|
+
mocks.pickers.save.mockResolvedValueOnce(null);
|
|
92
|
+
expect(await ctrl.download("https://example.com/file.txt")).toMatchObject({ ok: false, error: "cancelled" });
|
|
93
|
+
mocks.pickers.save.mockResolvedValueOnce("/dst.txt");
|
|
94
|
+
expect(await ctrl.download("https://example.com/file.txt")).toMatchObject({ ok: true });
|
|
95
|
+
|
|
96
|
+
mocks.pickers.open.mockResolvedValueOnce(null);
|
|
97
|
+
expect(await ctrl.remove()).toMatchObject({ ok: false, error: "cancelled" });
|
|
98
|
+
mocks.pickers.open.mockResolvedValue("/src.txt");
|
|
99
|
+
expect(await ctrl.remove()).toMatchObject({ ok: true });
|
|
100
|
+
|
|
101
|
+
mocks.pickers.folder.mockResolvedValueOnce(null);
|
|
102
|
+
expect(await ctrl.list()).toMatchObject({ ok: false, error: "cancelled" });
|
|
103
|
+
mocks.pickers.folder.mockResolvedValueOnce("/dir");
|
|
104
|
+
expect(await ctrl.list()).toMatchObject({ ok: true, files: ["a"] });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("app-managed helpers", async () => {
|
|
108
|
+
expect(await ctrl.appSave()).toMatchObject({ ok: true });
|
|
109
|
+
expect(await ctrl.appLoad()).toMatchObject({ ok: true });
|
|
110
|
+
expect(await ctrl.appList()).toMatchObject({ ok: true });
|
|
111
|
+
expect(await ctrl.appExport()).toMatchObject({ ok: true });
|
|
112
|
+
expect(await ctrl.appDelete()).toMatchObject({ ok: true });
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// ./src/modules/app/__tests__/window.controller.test.ts
|
|
2
|
+
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
const mocks = vi.hoisted(() => {
|
|
6
|
+
const setTitle = vi.fn();
|
|
7
|
+
const setSize = vi.fn();
|
|
8
|
+
const setAlwaysOnTop = vi.fn();
|
|
9
|
+
const isAlwaysOnTop = vi.fn(() => false);
|
|
10
|
+
const focus = vi.fn();
|
|
11
|
+
const loadURL = vi.fn();
|
|
12
|
+
const winObj = { setTitle, setSize, setAlwaysOnTop, isAlwaysOnTop, focus, loadURL };
|
|
13
|
+
const openWindow = vi.fn(async (_arg?: any) => winObj);
|
|
14
|
+
const installAppMenu = vi.fn();
|
|
15
|
+
const createAppTray = vi.fn();
|
|
16
|
+
const confirmDialog = vi.fn(async () => true);
|
|
17
|
+
const alertDialog = vi.fn(async () => undefined);
|
|
18
|
+
const quit = vi.fn();
|
|
19
|
+
return { winObj, openWindow, installAppMenu, createAppTray, confirmDialog, alertDialog, quit, setTitle, setSize, setAlwaysOnTop, isAlwaysOnTop, focus, loadURL };
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
vi.mock("electron", () => ({ app: { quit: mocks.quit } }));
|
|
23
|
+
vi.mock("@/core/config", () => ({ config: { appName: "Deskapp", appKebab: "deskapp" } }));
|
|
24
|
+
vi.mock("@/core/facade", () => ({
|
|
25
|
+
chrome: {
|
|
26
|
+
installAppMenu: mocks.installAppMenu,
|
|
27
|
+
createAppTray: mocks.createAppTray,
|
|
28
|
+
confirmDialog: mocks.confirmDialog,
|
|
29
|
+
alertDialog: mocks.alertDialog,
|
|
30
|
+
},
|
|
31
|
+
win: { getWindow: () => mocks.winObj, openWindow: mocks.openWindow },
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
import * as ctrl from "@/modules/app/controllers/window.controller";
|
|
35
|
+
|
|
36
|
+
describe("app window.controller", () => {
|
|
37
|
+
beforeEach(() => vi.clearAllMocks());
|
|
38
|
+
|
|
39
|
+
it("exposes handlers", () => {
|
|
40
|
+
expect(ctrl.handlers.setTitle).toBe(ctrl.setTitle);
|
|
41
|
+
expect(ctrl.handlers.quit).toBe(ctrl.quit);
|
|
42
|
+
expect(Object.keys(ctrl.handlers).sort()).toEqual([
|
|
43
|
+
"alertSomething",
|
|
44
|
+
"confirmSomething",
|
|
45
|
+
"createTray",
|
|
46
|
+
"focus",
|
|
47
|
+
"installMenu",
|
|
48
|
+
"open",
|
|
49
|
+
"openUrl",
|
|
50
|
+
"quit",
|
|
51
|
+
"setSize",
|
|
52
|
+
"setTitle",
|
|
53
|
+
"showContextMenu",
|
|
54
|
+
"toggleAlwaysOnTop",
|
|
55
|
+
]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("window controls act on the target window", () => {
|
|
59
|
+
ctrl.setTitle("hi");
|
|
60
|
+
expect(mocks.setTitle).toHaveBeenCalledWith("hi");
|
|
61
|
+
ctrl.setSize(100, 200);
|
|
62
|
+
expect(mocks.setSize).toHaveBeenCalledWith(100, 200);
|
|
63
|
+
mocks.isAlwaysOnTop.mockReturnValueOnce(false);
|
|
64
|
+
ctrl.toggleAlwaysOnTop();
|
|
65
|
+
expect(mocks.setAlwaysOnTop).toHaveBeenCalledWith(true);
|
|
66
|
+
ctrl.focus();
|
|
67
|
+
expect(mocks.focus).toHaveBeenCalled();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("open resolves presets and openUrl validates protocols", async () => {
|
|
71
|
+
expect(await ctrl.open("settings")).toBe(true);
|
|
72
|
+
expect(mocks.openWindow).toHaveBeenCalledWith("settings");
|
|
73
|
+
expect(await ctrl.open(123 as any)).toBe(false);
|
|
74
|
+
|
|
75
|
+
expect(await ctrl.openUrl("ftp://bad")).toBe(false);
|
|
76
|
+
expect(await ctrl.openUrl("not-a-url")).toBe(false);
|
|
77
|
+
expect(await ctrl.openUrl("https://example.com")).toBe(true);
|
|
78
|
+
expect(mocks.loadURL).toHaveBeenCalledWith("https://example.com/");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("menus/tray/dialog helpers delegate to chrome", async () => {
|
|
82
|
+
ctrl.installMenu();
|
|
83
|
+
expect(mocks.installAppMenu).toHaveBeenCalledWith(mocks.winObj);
|
|
84
|
+
ctrl.showContextMenu(); // no-op
|
|
85
|
+
expect(ctrl.createTray()).toBe(true);
|
|
86
|
+
expect(mocks.createAppTray).toHaveBeenCalled();
|
|
87
|
+
|
|
88
|
+
expect(await ctrl.confirmSomething("sure?")).toBe(true);
|
|
89
|
+
expect(mocks.confirmDialog).toHaveBeenCalledWith("sure?");
|
|
90
|
+
await ctrl.alertSomething("hi");
|
|
91
|
+
expect(mocks.alertDialog).toHaveBeenCalledWith("hi");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("quit quits the app", () => {
|
|
95
|
+
ctrl.quit();
|
|
96
|
+
expect(mocks.quit).toHaveBeenCalled();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -1,118 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
1
|
+
// ./src/modules/app/controllers/demo.controller.ts
|
|
2
|
+
|
|
3
|
+
import { cron, queue as queueApi } from "@/core/facade";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* demo.controller.ts (app module) - queue & cron sample handlers.
|
|
7
|
+
*
|
|
8
|
+
* Demonstrates the reusable facade primitives from inside real bindings:
|
|
9
|
+
* - `app.demo.queue` runs a bounded-concurrency in-process queue
|
|
10
|
+
* - `app.demo.schedule` ticks a cron every second for N seconds, then stops
|
|
11
|
+
*
|
|
12
|
+
* The pattern here is what an app module would use in production:
|
|
13
|
+
* import { queue, cron } from "@/core/facade.ts" in your own controller.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
|
|
17
|
+
|
|
18
|
+
export const queue = async (opts?: { items?: string[]; concurrency?: number; delayMs?: number }): Promise<Record<string, unknown>> => {
|
|
19
|
+
const items = opts?.items && opts.items.length > 0 ? opts.items : ["a", "b", "c"];
|
|
20
|
+
const concurrency = Math.min(Math.max(opts?.concurrency ?? 2, 1), 8);
|
|
21
|
+
const delayMs = Math.min(Math.max(opts?.delayMs ?? 50, 0), 1000);
|
|
22
|
+
const started = Date.now();
|
|
23
|
+
|
|
24
|
+
const q = queueApi.create<string>({ concurrency });
|
|
25
|
+
const results = await Promise.all(
|
|
26
|
+
items.map((name) =>
|
|
27
|
+
q.add(async () => {
|
|
28
|
+
await sleep(delayMs);
|
|
29
|
+
return `processed ${name}`;
|
|
30
|
+
})
|
|
31
|
+
)
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return { ok: true, results, concurrency, durationMs: Date.now() - started };
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const schedule = async (opts?: { seconds?: number }): Promise<Record<string, unknown>> => {
|
|
38
|
+
const seconds = Math.min(Math.max(opts?.seconds ?? 2, 1), 10);
|
|
39
|
+
let ticks = 0;
|
|
40
|
+
|
|
41
|
+
const job = cron.schedule("demo.tick", "* * * * * *", () => {
|
|
42
|
+
ticks += 1;
|
|
43
|
+
});
|
|
44
|
+
await sleep(seconds * 1000);
|
|
45
|
+
job.stop();
|
|
46
|
+
|
|
47
|
+
return { ok: true, name: "demo.tick", expression: "* * * * * *", ticked: ticks, seconds };
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* demo.controller.ts (app module) - gum data-table sample handlers.
|
|
52
|
+
*
|
|
53
|
+
* `app.demo.table` serves the same paginated envelope `paginate.*` produces
|
|
54
|
+
* (`data/current_page/last_page/per_page/total/from/to/path`), so the UI
|
|
55
|
+
* <DataTable> can bind to it with zero ceremony. Rows are generated in memory
|
|
56
|
+
* here so the starter needs no migration; a real module does exactly the same
|
|
57
|
+
* with a Drizzle model + paginate.model (see docs/guide/gum.md).
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
interface DemoRow {
|
|
61
|
+
id: number;
|
|
62
|
+
name: string;
|
|
63
|
+
email: string;
|
|
64
|
+
role: string;
|
|
65
|
+
created: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const demoSeed = Array.from({ length: 137 }, (_, i) => ({
|
|
69
|
+
id: i + 1,
|
|
70
|
+
name: `${["Ava", "Leo", "Mia", "Noah", "Zara", "Ivan", "Eli", "Ruth", "Omar", "Nia"][i % 10]} ${i + 1}`,
|
|
71
|
+
email: `user${i + 1}@example.com`,
|
|
72
|
+
role: ["admin", "editor", "viewer"][i % 3],
|
|
73
|
+
created: `2026-${String((i % 12) + 1).padStart(2, "0")}-${String((i % 28) + 1).padStart(2, "0")}`
|
|
74
|
+
}));
|
|
75
|
+
|
|
76
|
+
let demoRows: DemoRow[] = structuredClone(demoSeed);
|
|
77
|
+
|
|
78
|
+
function toPositiveInt(value: unknown, fallback: number): number {
|
|
79
|
+
const parsed = Number(value);
|
|
80
|
+
if (!Number.isFinite(parsed) || parsed < 1) return fallback;
|
|
81
|
+
return Math.floor(parsed);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const table = (opts?: { page?: number; size?: number; search?: string }): Record<string, unknown> => {
|
|
85
|
+
const perPage = Math.min(toPositiveInt(opts?.size, 15), 100);
|
|
86
|
+
const page = toPositiveInt(opts?.page, 1);
|
|
87
|
+
const search = String(opts?.search ?? "")
|
|
88
|
+
.trim()
|
|
89
|
+
.toLowerCase();
|
|
90
|
+
|
|
91
|
+
const all = search
|
|
92
|
+
? demoRows.filter((row) => [row.name, row.email, row.role].some((value) => value.toLowerCase().includes(search)))
|
|
93
|
+
: demoRows;
|
|
94
|
+
|
|
95
|
+
const total = all.length;
|
|
96
|
+
const lastPage = Math.max(1, Math.ceil(total / perPage));
|
|
97
|
+
const currentPage = Math.min(page, lastPage);
|
|
98
|
+
const offset = (currentPage - 1) * perPage;
|
|
99
|
+
const data = all.slice(offset, offset + perPage);
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
data,
|
|
103
|
+
current_page: currentPage,
|
|
104
|
+
last_page: lastPage,
|
|
105
|
+
per_page: perPage,
|
|
106
|
+
total,
|
|
107
|
+
from: total === 0 ? null : offset + 1,
|
|
108
|
+
to: total === 0 ? null : offset + data.length,
|
|
109
|
+
path: "app.demo.table"
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const destroy = (opts?: { ids?: Array<string | number> }): Record<string, unknown> => {
|
|
114
|
+
const ids = new Set((opts?.ids ?? []).map((id) => String(id)));
|
|
115
|
+
const before = demoRows.length;
|
|
116
|
+
demoRows = demoRows.filter((row) => !ids.has(String(row.id)));
|
|
117
|
+
return { ok: true, removed: before - demoRows.length };
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const handlers = { queue, schedule, table, destroy };
|
|
@@ -1,48 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
data = text;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return { ok:
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
// ./src/modules/app/controllers/net.controller.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* net.controller.ts (app module) - backend proxy for server API calls.
|
|
5
|
+
*
|
|
6
|
+
* The webview's own fetch() can be blocked by CORS on cross-origin APIs.
|
|
7
|
+
* Calling the API on the Deno backend avoids CORS entirely: the frontend asks
|
|
8
|
+
* `app.net.fetchJson(url)`, the backend fetches it, and the JSON-safe payload
|
|
9
|
+
* comes back through the bindings. The two demo handlers:
|
|
10
|
+
*
|
|
11
|
+
* fetchJson(url) -> app.net.fetchJson any http(s) URL (JSON or text)
|
|
12
|
+
* placeholder() -> app.net.placeholder jsonplaceholder.typicode.com
|
|
13
|
+
*
|
|
14
|
+
* See src/ui/src/App.vue for the matching "Server / API" demo section.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const MAX_BODY = 50_000;
|
|
18
|
+
|
|
19
|
+
/** Proxy one http(s) fetch from the backend; returns JSON (or text) safely. */
|
|
20
|
+
export const fetchJson = async (url: string | undefined): Promise<Record<string, unknown>> => {
|
|
21
|
+
const clean = (url ?? "").trim();
|
|
22
|
+
if (!/^https?:\/\//i.test(clean)) {
|
|
23
|
+
return { ok: false, action: "fetch", error: "enter a valid http(s) URL" };
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const res = await fetch(clean, { headers: { Accept: "application/json" } });
|
|
27
|
+
const text = await res.text();
|
|
28
|
+
let data: unknown;
|
|
29
|
+
if (text.length <= MAX_BODY) {
|
|
30
|
+
try {
|
|
31
|
+
data = JSON.parse(text);
|
|
32
|
+
} catch {
|
|
33
|
+
data = text;
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
data = `${text.slice(0, 2000)}... (truncated, ${text.length} bytes)`;
|
|
37
|
+
}
|
|
38
|
+
return { ok: res.ok, status: res.status, url: clean, bytes: text.length, data };
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return { ok: false, action: "fetch", url: clean, error: String(error) };
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** One-click demo against JSONPlaceholder (no input needed). */
|
|
45
|
+
export const placeholder = async (): Promise<Record<string, unknown>> => {
|
|
46
|
+
return await fetchJson("https://jsonplaceholder.typicode.com/todos/1");
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** Handlers exposed to the frontend via the bindings registry. */
|
|
50
|
+
export const handlers = { fetchJson, placeholder };
|