@vvedantb/native-ui 0.30.0 → 0.32.0

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.
@@ -152,6 +152,10 @@ export type {
152
152
  DropdownMenuRadioItemProps,
153
153
  } from "../DropdownMenu";
154
154
  export type { ConfirmDialogProps } from "../ConfirmDialog";
155
+ export { UpdateChecker, UpdatePrompt, V_STORE_UPDATES_URL } from "../UpdateChecker";
156
+ export type { UpdateCheckerProps, UpdateDismissStorage, UpdatePromptProps } from "../UpdateChecker";
157
+ export { fetchGitHubUpdate, isNewerVersion, newerGitHubRelease } from "../updateCheck";
158
+ export type { AvailableUpdate, GitHubRelease, GitHubReleaseSource } from "../updateCheck";
155
159
  export {
156
160
  Sheet,
157
161
  SheetTrigger,
package/src/index.ts CHANGED
@@ -189,6 +189,10 @@ export type {
189
189
  DropdownMenuRadioItemProps,
190
190
  } from "./DropdownMenu";
191
191
  export type { ConfirmDialogProps } from "./ConfirmDialog";
192
+ export { UpdateChecker, UpdatePrompt, V_STORE_UPDATES_URL } from "./UpdateChecker";
193
+ export type { UpdateCheckerProps, UpdateDismissStorage, UpdatePromptProps } from "./UpdateChecker";
194
+ export { fetchGitHubUpdate, isNewerVersion, newerGitHubRelease } from "./updateCheck";
195
+ export type { AvailableUpdate, GitHubRelease, GitHubReleaseSource } from "./updateCheck";
192
196
  export {
193
197
  Sheet,
194
198
  SheetTrigger,
@@ -0,0 +1,103 @@
1
+ // The update check behind `UpdateChecker`: for an app that ships its Android
2
+ // APK as GitHub releases tagged `<tagPrefix><version>`, with no store and no
3
+ // over-the-air updates. Plain functions, no React and no native module.
4
+
5
+ /** A newer version to offer. `url` is what Download opens: the APK itself, else its release page. */
6
+ export type AvailableUpdate = { version: string; url: string };
7
+
8
+ /** The fields of a GitHub release the check reads (`GET /repos/{owner}/{repo}/releases`). */
9
+ export type GitHubRelease = {
10
+ tag_name: string;
11
+ html_url: string;
12
+ draft: boolean;
13
+ prerelease: boolean;
14
+ assets: { name: string; browser_download_url: string }[];
15
+ };
16
+
17
+ export type GitHubReleaseSource = {
18
+ /** `"owner/name"` of a public repository. */
19
+ repo: string;
20
+ /** Tag prefix before the version, for example `"myapp-apk-"` for the tag `myapp-apk-1.2.3`. */
21
+ tagPrefix: string;
22
+ };
23
+
24
+ type VersionParts = [number, number, number];
25
+
26
+ /** `[major, minor, patch]` of "1.2.3", or null. */
27
+ function parseVersion(version: string): VersionParts | null {
28
+ const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version);
29
+ return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
30
+ }
31
+
32
+ /** Positive when `a` is newer than `b`. */
33
+ function compareParts(a: VersionParts, b: VersionParts): number {
34
+ return a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
35
+ }
36
+
37
+ /** True when `candidate` ("1.2.3") is newer than `current`. False when either is not `major.minor.patch`. */
38
+ export function isNewerVersion(candidate: string, current: string): boolean {
39
+ const a = parseVersion(candidate);
40
+ const b = parseVersion(current);
41
+ return a !== null && b !== null && compareParts(a, b) > 0;
42
+ }
43
+
44
+ function isRelease(value: unknown): value is GitHubRelease {
45
+ if (!value || typeof value !== "object") return false;
46
+ const release = value as Record<string, unknown>;
47
+ return (
48
+ typeof release.tag_name === "string" &&
49
+ typeof release.html_url === "string" &&
50
+ typeof release.draft === "boolean" &&
51
+ typeof release.prerelease === "boolean" &&
52
+ Array.isArray(release.assets) &&
53
+ release.assets.every((asset: unknown) => {
54
+ if (!asset || typeof asset !== "object") return false;
55
+ const item = asset as Record<string, unknown>;
56
+ return typeof item.name === "string" && typeof item.browser_download_url === "string";
57
+ })
58
+ );
59
+ }
60
+
61
+ /**
62
+ * The newest published release tagged `<tagPrefix><version>` that is newer
63
+ * than `currentVersion`, or null. Drafts, prereleases, other tags and versions
64
+ * that are not `major.minor.patch` are skipped. The download is the release's
65
+ * first `.apk` asset, else its release page.
66
+ */
67
+ export function newerGitHubRelease(
68
+ releases: readonly GitHubRelease[],
69
+ currentVersion: string,
70
+ tagPrefix: string,
71
+ ): AvailableUpdate | null {
72
+ const installed = parseVersion(currentVersion);
73
+ if (!installed) return null;
74
+ let best: { parts: VersionParts; update: AvailableUpdate } | null = null;
75
+ for (const release of releases) {
76
+ if (release.draft || release.prerelease || !release.tag_name.startsWith(tagPrefix)) continue;
77
+ const version = release.tag_name.slice(tagPrefix.length);
78
+ const parts = parseVersion(version);
79
+ if (!parts || compareParts(parts, installed) <= 0) continue;
80
+ if (best && compareParts(parts, best.parts) <= 0) continue;
81
+ const apk = release.assets.find((asset) => asset.name.endsWith(".apk"));
82
+ best = { parts, update: { version, url: apk?.browser_download_url ?? release.html_url } };
83
+ }
84
+ return best?.update ?? null;
85
+ }
86
+
87
+ /**
88
+ * Reads the repository's 30 latest releases from the public GitHub API and
89
+ * returns the newer one, or null. Null too when the request fails, is rate
90
+ * limited, or the answer has another shape. A network error rejects.
91
+ */
92
+ export async function fetchGitHubUpdate(
93
+ source: GitHubReleaseSource,
94
+ currentVersion: string,
95
+ ): Promise<AvailableUpdate | null> {
96
+ const response = await fetch(`https://api.github.com/repos/${source.repo}/releases?per_page=30`, {
97
+ headers: { Accept: "application/vnd.github+json" },
98
+ });
99
+ if (!response.ok) return null;
100
+ const body: unknown = await response.json();
101
+ if (!Array.isArray(body) || !body.every(isRelease)) return null;
102
+ return newerGitHubRelease(body, currentVersion, source.tagPrefix);
103
+ }