@ailuracode/alpine-network 1.0.0 → 1.0.1

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/README.md CHANGED
@@ -30,7 +30,8 @@ Alpine.start();
30
30
  | | |
31
31
  |-|-|
32
32
  | **Magic** | `$network` |
33
- | **Properties** | `isOnline` (boolean) |
33
+ | **Getters** | `isOnline`, `isOffline` (boolean) |
34
+ | **Helpers** | `readNetworkState()`, `createNetworkState()` |
34
35
 
35
36
  ## License
36
37
 
package/dist/global.d.ts CHANGED
@@ -1,9 +1,14 @@
1
1
  /// <reference types="@types/alpinejs" />
2
2
 
3
3
  export interface NetworkMagic {
4
- isOnline: boolean;
4
+ readonly isOnline: boolean;
5
+ readonly isOffline: boolean;
5
6
  }
6
7
 
8
+ export function readNetworkState(): NetworkMagic;
9
+
10
+ export function createNetworkState(isOnline?: boolean): NetworkMagic;
11
+
7
12
  declare global {
8
13
  namespace Alpine {
9
14
  interface Magics<T> {
package/dist/index.d.ts CHANGED
@@ -1,8 +1,16 @@
1
1
  import AlpineType from 'alpinejs';
2
2
 
3
3
  interface NetworkMagic {
4
- isOnline: boolean;
4
+ readonly isOnline: boolean;
5
+ readonly isOffline: boolean;
5
6
  }
7
+ type NetworkStateRecord = NetworkMagic & {
8
+ _online: boolean;
9
+ };
10
+ /** Reads current connectivity from `navigator.onLine` (defaults to online when unavailable). */
11
+ declare function readNetworkState(): NetworkMagic;
12
+ /** Builds reactive network state with getter-based flags. */
13
+ declare function createNetworkState(isOnline?: boolean): NetworkStateRecord;
6
14
  /** Alpine.js network plugin. Registers reactive magic `$network`. */
7
15
  declare function networkPlugin(Alpine: AlpineType.Alpine): void;
8
16
  declare global {
@@ -13,4 +21,4 @@ declare global {
13
21
  }
14
22
  }
15
23
 
16
- export { type NetworkMagic, networkPlugin as default };
24
+ export { type NetworkMagic, createNetworkState, networkPlugin as default, readNetworkState };
package/dist/index.js CHANGED
@@ -1,14 +1,34 @@
1
1
  // src/index.ts
2
+ function readNetworkState() {
3
+ const isOnline = typeof navigator !== "undefined" ? navigator.onLine : true;
4
+ return {
5
+ isOnline,
6
+ isOffline: !isOnline
7
+ };
8
+ }
9
+ function createNetworkState(isOnline = readNetworkState().isOnline) {
10
+ return {
11
+ _online: isOnline,
12
+ get isOnline() {
13
+ return this._online;
14
+ },
15
+ get isOffline() {
16
+ return !this._online;
17
+ }
18
+ };
19
+ }
2
20
  function networkPlugin(Alpine) {
3
- const state = Alpine.reactive({ isOnline: navigator.onLine });
21
+ const state = Alpine.reactive(createNetworkState());
4
22
  Alpine.magic("network", () => state);
5
23
  const update = () => {
6
- state.isOnline = navigator.onLine;
24
+ state._online = navigator.onLine;
7
25
  };
8
26
  window.addEventListener("online", update);
9
27
  window.addEventListener("offline", update);
10
28
  }
11
29
  export {
12
- networkPlugin as default
30
+ createNetworkState,
31
+ networkPlugin as default,
32
+ readNetworkState
13
33
  };
14
34
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type AlpineType from \"alpinejs\";\n\nexport interface NetworkMagic {\n isOnline: boolean;\n}\n\n/** Alpine.js network plugin. Registers reactive magic `$network`. */\nexport default function networkPlugin(Alpine: AlpineType.Alpine): void {\n const state = Alpine.reactive<NetworkMagic>({ isOnline: navigator.onLine });\n\n Alpine.magic(\"network\", () => state);\n\n const update = () => {\n state.isOnline = navigator.onLine;\n };\n\n window.addEventListener(\"online\", update);\n window.addEventListener(\"offline\", update);\n}\n\ndeclare global {\n namespace Alpine {\n interface Magics<T> {\n $network: NetworkMagic;\n }\n }\n}\n"],"mappings":";AAOe,SAAR,cAA+B,QAAiC;AACrE,QAAM,QAAQ,OAAO,SAAuB,EAAE,UAAU,UAAU,OAAO,CAAC;AAE1E,SAAO,MAAM,WAAW,MAAM,KAAK;AAEnC,QAAM,SAAS,MAAM;AACnB,UAAM,WAAW,UAAU;AAAA,EAC7B;AAEA,SAAO,iBAAiB,UAAU,MAAM;AACxC,SAAO,iBAAiB,WAAW,MAAM;AAC3C;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type AlpineType from \"alpinejs\";\n\nexport interface NetworkMagic {\n readonly isOnline: boolean;\n readonly isOffline: boolean;\n}\n\ntype NetworkStateRecord = NetworkMagic & {\n _online: boolean;\n};\n\n/** Reads current connectivity from `navigator.onLine` (defaults to online when unavailable). */\nexport function readNetworkState(): NetworkMagic {\n const isOnline = typeof navigator !== \"undefined\" ? navigator.onLine : true;\n\n return {\n isOnline,\n isOffline: !isOnline,\n };\n}\n\n/** Builds reactive network state with getter-based flags. */\nexport function createNetworkState(isOnline = readNetworkState().isOnline): NetworkStateRecord {\n return {\n _online: isOnline,\n get isOnline() {\n return this._online;\n },\n get isOffline() {\n return !this._online;\n },\n };\n}\n\n/** Alpine.js network plugin. Registers reactive magic `$network`. */\nexport default function networkPlugin(Alpine: AlpineType.Alpine): void {\n const state = Alpine.reactive(createNetworkState());\n\n Alpine.magic(\"network\", () => state as NetworkMagic);\n\n const update = () => {\n state._online = navigator.onLine;\n };\n\n window.addEventListener(\"online\", update);\n window.addEventListener(\"offline\", update);\n}\n\ndeclare global {\n namespace Alpine {\n interface Magics<T> {\n $network: NetworkMagic;\n }\n }\n}\n"],"mappings":";AAYO,SAAS,mBAAiC;AAC/C,QAAM,WAAW,OAAO,cAAc,cAAc,UAAU,SAAS;AAEvE,SAAO;AAAA,IACL;AAAA,IACA,WAAW,CAAC;AAAA,EACd;AACF;AAGO,SAAS,mBAAmB,WAAW,iBAAiB,EAAE,UAA8B;AAC7F,SAAO;AAAA,IACL,SAAS;AAAA,IACT,IAAI,WAAW;AACb,aAAO,KAAK;AAAA,IACd;AAAA,IACA,IAAI,YAAY;AACd,aAAO,CAAC,KAAK;AAAA,IACf;AAAA,EACF;AACF;AAGe,SAAR,cAA+B,QAAiC;AACrE,QAAM,QAAQ,OAAO,SAAS,mBAAmB,CAAC;AAElD,SAAO,MAAM,WAAW,MAAM,KAAqB;AAEnD,QAAM,SAAS,MAAM;AACnB,UAAM,UAAU,UAAU;AAAA,EAC5B;AAEA,SAAO,iBAAiB,UAAU,MAAM;AACxC,SAAO,iBAAiB,WAAW,MAAM;AAC3C;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ailuracode/alpine-network",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Alpine.js network connectivity magic",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -51,6 +51,6 @@
51
51
  ],
52
52
  "scripts": {
53
53
  "build": "tsup src/index.ts --format esm --dts --clean --sourcemap --out-dir dist && cp src/global.d.ts dist/global.d.ts",
54
- "test": "vitest run --config ../../vitest.config.js test"
54
+ "test": "vitest run --config ../../vitest.config.ts test"
55
55
  }
56
56
  }