@proton-app/preload 0.0.2 → 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/dist/index.d.ts CHANGED
@@ -1,22 +1,7 @@
1
- import type { IpcRenderer } from "electron";
2
- type IpcClient = Pick<IpcRenderer, "send" | "invoke">;
3
- export interface AppBridge {
4
- minimize: () => void;
5
- close: () => void;
6
- choosePath: () => Promise<string | null>;
7
- openLink: (url: string) => void;
8
- getValue: <T>(key: string) => T;
9
- setValue: <T>(key: string, value: T) => T;
10
- syncAuth: (accessToken: string) => void;
11
- }
12
- export declare const APP_CHANNELS: {
13
- readonly minimize: "app:minimize";
14
- readonly close: "app:close";
15
- readonly choosePath: "app:choose-path";
16
- readonly openLink: "app:open-link";
17
- readonly getValue: "app:get-value";
18
- readonly setValue: "app:set-value";
19
- readonly syncAuth: "app:sync-auth";
1
+ import { type IpcSchema } from "./ipc";
2
+ type SetupOptions<T extends Record<string, IpcSchema>> = {
3
+ ipc: T;
4
+ env: readonly string[];
20
5
  };
21
- export declare function createAppBridge(ipc: IpcClient): AppBridge;
6
+ export declare function setupPreload<T extends Record<string, IpcSchema>, const K extends readonly string[]>({ ipc, env }: SetupOptions<T>): void;
22
7
  export * from "./ipc";
package/dist/index.js CHANGED
@@ -14,40 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.APP_CHANNELS = void 0;
18
- exports.createAppBridge = createAppBridge;
19
- exports.APP_CHANNELS = {
20
- minimize: "app:minimize",
21
- close: "app:close",
22
- choosePath: "app:choose-path",
23
- openLink: "app:open-link",
24
- getValue: "app:get-value",
25
- setValue: "app:set-value",
26
- syncAuth: "app:sync-auth"
27
- };
28
- function createAppBridge(ipc) {
29
- return {
30
- minimize: () => {
31
- ipc.send(exports.APP_CHANNELS.minimize);
32
- },
33
- close: () => {
34
- ipc.send(exports.APP_CHANNELS.close);
35
- },
36
- choosePath: () => {
37
- return ipc.invoke(exports.APP_CHANNELS.choosePath);
38
- },
39
- openLink: (url) => {
40
- ipc.send(exports.APP_CHANNELS.openLink, url);
41
- },
42
- getValue: (key) => {
43
- return ipc.invoke(exports.APP_CHANNELS.getValue, key);
44
- },
45
- setValue: (key, value) => {
46
- return ipc.invoke(exports.APP_CHANNELS.setValue, key, value);
47
- },
48
- syncAuth: (accessToken) => {
49
- return ipc.send(exports.APP_CHANNELS.syncAuth, accessToken);
50
- }
51
- };
17
+ exports.setupPreload = setupPreload;
18
+ const electron_1 = require("electron");
19
+ const ipc_1 = require("./ipc");
20
+ function setupPreload({ ipc, env }) {
21
+ (0, ipc_1.setupIpcBridge)(ipc);
22
+ electron_1.contextBridge.exposeInMainWorld("env", Object.fromEntries(env.map(key => [key, process.env[key]])));
52
23
  }
53
24
  __exportStar(require("./ipc"), exports);
package/dist/ipc.d.ts CHANGED
@@ -1,4 +1,38 @@
1
+ import { type IpcRenderer } from "electron";
2
+ type IpcClient = Pick<IpcRenderer, "send" | "invoke">;
3
+ export type IpcSchema = Record<string, {
4
+ input: unknown;
5
+ result: unknown;
6
+ }>;
7
+ type KebabToCamel<S extends string> = S extends `${infer A}-${infer B}` ? `${A}${Capitalize<KebabToCamel<B>>}` : S;
8
+ type NamespaceApi<S extends IpcSchema> = {
9
+ [K in keyof S & string as KebabToCamel<K>]: (input: S[K]["input"]) => Promise<S[K]["result"]>;
10
+ };
11
+ export type IpcApi<S extends Record<string, IpcSchema>> = {
12
+ [N in keyof S]: NamespaceApi<S[N]>;
13
+ };
14
+ export interface AppBridge {
15
+ minimize: () => void;
16
+ close: () => void;
17
+ choosePath: () => Promise<string | null>;
18
+ openLink: (url: string) => void;
19
+ getValue: <T>(key: string) => T;
20
+ setValue: <T>(key: string, value: T) => T;
21
+ syncAuth: (accessToken: string) => void;
22
+ }
23
+ export declare const APP_CHANNELS: {
24
+ readonly minimize: "app:minimize";
25
+ readonly close: "app:close";
26
+ readonly choosePath: "app:choose-path";
27
+ readonly openLink: "app:open-link";
28
+ readonly getValue: "app:get-value";
29
+ readonly setValue: "app:set-value";
30
+ readonly syncAuth: "app:sync-auth";
31
+ };
32
+ export declare function createAppBridge(ipc: IpcClient): AppBridge;
33
+ export declare function setupIpcBridge<T extends Record<string, IpcSchema>>(schema: T): void;
1
34
  export declare function channel<Input, Result>(): {
2
35
  input: Input;
3
36
  result: Result;
4
37
  };
38
+ export {};
package/dist/ipc.js CHANGED
@@ -1,6 +1,61 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.APP_CHANNELS = void 0;
4
+ exports.createAppBridge = createAppBridge;
5
+ exports.setupIpcBridge = setupIpcBridge;
3
6
  exports.channel = channel;
7
+ const electron_1 = require("electron");
8
+ exports.APP_CHANNELS = {
9
+ minimize: "app:minimize",
10
+ close: "app:close",
11
+ choosePath: "app:choose-path",
12
+ openLink: "app:open-link",
13
+ getValue: "app:get-value",
14
+ setValue: "app:set-value",
15
+ syncAuth: "app:sync-auth"
16
+ };
17
+ function createAppBridge(ipc) {
18
+ return {
19
+ minimize: () => {
20
+ ipc.send(exports.APP_CHANNELS.minimize);
21
+ },
22
+ close: () => {
23
+ ipc.send(exports.APP_CHANNELS.close);
24
+ },
25
+ choosePath: () => {
26
+ return ipc.invoke(exports.APP_CHANNELS.choosePath);
27
+ },
28
+ openLink: (url) => {
29
+ ipc.send(exports.APP_CHANNELS.openLink, url);
30
+ },
31
+ getValue: (key) => {
32
+ return ipc.invoke(exports.APP_CHANNELS.getValue, key);
33
+ },
34
+ setValue: (key, value) => {
35
+ return ipc.invoke(exports.APP_CHANNELS.setValue, key, value);
36
+ },
37
+ syncAuth: (accessToken) => {
38
+ return ipc.send(exports.APP_CHANNELS.syncAuth, accessToken);
39
+ }
40
+ };
41
+ }
42
+ function setupIpcBridge(schema) {
43
+ electron_1.contextBridge.exposeInMainWorld("api", {
44
+ app: createAppBridge(electron_1.ipcRenderer)
45
+ });
46
+ const ipcApi = Object.fromEntries(Object.entries(schema).map(([namespace, channels]) => [
47
+ namespace,
48
+ Object.fromEntries(Object.keys(channels).map((channel) => [
49
+ kebabToCamel(channel),
50
+ (input) => electron_1.ipcRenderer.invoke(`${namespace}:${channel}`, input),
51
+ ])),
52
+ ]));
53
+ for (const [namespace, api] of Object.entries(ipcApi))
54
+ electron_1.contextBridge.exposeInMainWorld(namespace, api);
55
+ }
4
56
  function channel() {
5
57
  return null;
6
58
  }
59
+ function kebabToCamel(str) {
60
+ return str.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
61
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "@proton-app/preload",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "description": "",
6
6
  "keywords": [],
7
7
  "author": "",