@webority-technologies/mobile-core 0.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.
Files changed (102) hide show
  1. package/README.md +15 -0
  2. package/lib/commonjs/api/publicClient.js +80 -0
  3. package/lib/commonjs/api/userClient.js +368 -0
  4. package/lib/commonjs/auth/authStore.js +155 -0
  5. package/lib/commonjs/auth/index.js +44 -0
  6. package/lib/commonjs/auth/jwt.js +88 -0
  7. package/lib/commonjs/config/index.js +65 -0
  8. package/lib/commonjs/formatters/currency.js +69 -0
  9. package/lib/commonjs/formatters/date.js +168 -0
  10. package/lib/commonjs/formatters/index.js +77 -0
  11. package/lib/commonjs/formatters/initials.js +32 -0
  12. package/lib/commonjs/formatters/number.js +88 -0
  13. package/lib/commonjs/formatters/phone.js +67 -0
  14. package/lib/commonjs/index.js +383 -0
  15. package/lib/commonjs/initSDK.js +45 -0
  16. package/lib/commonjs/initUserAuth.js +38 -0
  17. package/lib/commonjs/logger/index.js +137 -0
  18. package/lib/commonjs/network/index.js +25 -0
  19. package/lib/commonjs/network/networkStatus.js +98 -0
  20. package/lib/commonjs/package.json +1 -0
  21. package/lib/commonjs/permissions/index.js +200 -0
  22. package/lib/commonjs/storage/index.js +193 -0
  23. package/lib/commonjs/utils/imageCompression.js +75 -0
  24. package/lib/commonjs/utils/index.js +13 -0
  25. package/lib/commonjs/validators/index.js +193 -0
  26. package/lib/commonjs/versionCheck/index.js +395 -0
  27. package/lib/module/api/publicClient.js +76 -0
  28. package/lib/module/api/userClient.js +360 -0
  29. package/lib/module/auth/authStore.js +148 -0
  30. package/lib/module/auth/index.js +5 -0
  31. package/lib/module/auth/jwt.js +82 -0
  32. package/lib/module/config/index.js +57 -0
  33. package/lib/module/formatters/currency.js +64 -0
  34. package/lib/module/formatters/date.js +160 -0
  35. package/lib/module/formatters/index.js +8 -0
  36. package/lib/module/formatters/initials.js +27 -0
  37. package/lib/module/formatters/number.js +81 -0
  38. package/lib/module/formatters/phone.js +61 -0
  39. package/lib/module/index.js +30 -0
  40. package/lib/module/initSDK.js +40 -0
  41. package/lib/module/initUserAuth.js +34 -0
  42. package/lib/module/logger/index.js +133 -0
  43. package/lib/module/network/index.js +4 -0
  44. package/lib/module/network/networkStatus.js +91 -0
  45. package/lib/module/package.json +1 -0
  46. package/lib/module/permissions/index.js +197 -0
  47. package/lib/module/storage/index.js +189 -0
  48. package/lib/module/utils/imageCompression.js +69 -0
  49. package/lib/module/utils/index.js +4 -0
  50. package/lib/module/validators/index.js +171 -0
  51. package/lib/module/versionCheck/index.js +390 -0
  52. package/lib/typescript/commonjs/api/publicClient.d.ts +11 -0
  53. package/lib/typescript/commonjs/api/userClient.d.ts +49 -0
  54. package/lib/typescript/commonjs/auth/authStore.d.ts +43 -0
  55. package/lib/typescript/commonjs/auth/index.d.ts +5 -0
  56. package/lib/typescript/commonjs/auth/jwt.d.ts +31 -0
  57. package/lib/typescript/commonjs/config/index.d.ts +49 -0
  58. package/lib/typescript/commonjs/formatters/currency.d.ts +24 -0
  59. package/lib/typescript/commonjs/formatters/date.d.ts +22 -0
  60. package/lib/typescript/commonjs/formatters/index.d.ts +8 -0
  61. package/lib/typescript/commonjs/formatters/initials.d.ts +11 -0
  62. package/lib/typescript/commonjs/formatters/number.d.ts +15 -0
  63. package/lib/typescript/commonjs/formatters/phone.d.ts +13 -0
  64. package/lib/typescript/commonjs/index.d.ts +30 -0
  65. package/lib/typescript/commonjs/initSDK.d.ts +40 -0
  66. package/lib/typescript/commonjs/initUserAuth.d.ts +22 -0
  67. package/lib/typescript/commonjs/logger/index.d.ts +42 -0
  68. package/lib/typescript/commonjs/network/index.d.ts +3 -0
  69. package/lib/typescript/commonjs/network/networkStatus.d.ts +27 -0
  70. package/lib/typescript/commonjs/package.json +1 -0
  71. package/lib/typescript/commonjs/permissions/index.d.ts +20 -0
  72. package/lib/typescript/commonjs/storage/index.d.ts +42 -0
  73. package/lib/typescript/commonjs/utils/imageCompression.d.ts +31 -0
  74. package/lib/typescript/commonjs/utils/index.d.ts +3 -0
  75. package/lib/typescript/commonjs/validators/index.d.ts +46 -0
  76. package/lib/typescript/commonjs/versionCheck/index.d.ts +80 -0
  77. package/lib/typescript/module/api/publicClient.d.ts +11 -0
  78. package/lib/typescript/module/api/userClient.d.ts +49 -0
  79. package/lib/typescript/module/auth/authStore.d.ts +43 -0
  80. package/lib/typescript/module/auth/index.d.ts +5 -0
  81. package/lib/typescript/module/auth/jwt.d.ts +31 -0
  82. package/lib/typescript/module/config/index.d.ts +49 -0
  83. package/lib/typescript/module/formatters/currency.d.ts +24 -0
  84. package/lib/typescript/module/formatters/date.d.ts +22 -0
  85. package/lib/typescript/module/formatters/index.d.ts +8 -0
  86. package/lib/typescript/module/formatters/initials.d.ts +11 -0
  87. package/lib/typescript/module/formatters/number.d.ts +15 -0
  88. package/lib/typescript/module/formatters/phone.d.ts +13 -0
  89. package/lib/typescript/module/index.d.ts +30 -0
  90. package/lib/typescript/module/initSDK.d.ts +40 -0
  91. package/lib/typescript/module/initUserAuth.d.ts +22 -0
  92. package/lib/typescript/module/logger/index.d.ts +42 -0
  93. package/lib/typescript/module/network/index.d.ts +3 -0
  94. package/lib/typescript/module/network/networkStatus.d.ts +27 -0
  95. package/lib/typescript/module/package.json +1 -0
  96. package/lib/typescript/module/permissions/index.d.ts +20 -0
  97. package/lib/typescript/module/storage/index.d.ts +42 -0
  98. package/lib/typescript/module/utils/imageCompression.d.ts +31 -0
  99. package/lib/typescript/module/utils/index.d.ts +3 -0
  100. package/lib/typescript/module/validators/index.d.ts +46 -0
  101. package/lib/typescript/module/versionCheck/index.d.ts +80 -0
  102. package/package.json +112 -0
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useNetworkStatus = exports.getNetworkStatus = exports.addNetworkStatusListener = void 0;
7
+ var _react = require("react");
8
+ var _index = require("../logger/index.js");
9
+ const DEFAULT_STATUS = {
10
+ isConnected: true,
11
+ isInternetReachable: null,
12
+ type: 'unknown'
13
+ };
14
+ let netInfo = null;
15
+ let resolved = false;
16
+ const resolveNetInfo = () => {
17
+ if (resolved) {
18
+ return netInfo;
19
+ }
20
+ resolved = true;
21
+ try {
22
+ const mod = require('@react-native-community/netinfo');
23
+ netInfo = mod.default ?? mod;
24
+ } catch {
25
+ _index.Logger.warn('[network] @react-native-community/netinfo is not installed; ' + 'useNetworkStatus() will report a default "online" status.');
26
+ netInfo = null;
27
+ }
28
+ return netInfo;
29
+ };
30
+ const normalize = state => {
31
+ if (!state) {
32
+ return DEFAULT_STATUS;
33
+ }
34
+ return {
35
+ isConnected: state.isConnected !== false,
36
+ isInternetReachable: state.isInternetReachable,
37
+ type: state.type ?? 'unknown'
38
+ };
39
+ };
40
+
41
+ /**
42
+ * Imperative one-shot fetch of current network status. Resolves to the default
43
+ * online status when NetInfo is unavailable so callers never crash.
44
+ */
45
+ const getNetworkStatus = async () => {
46
+ const ni = resolveNetInfo();
47
+ if (!ni) {
48
+ return DEFAULT_STATUS;
49
+ }
50
+ try {
51
+ const state = await ni.fetch();
52
+ return normalize(state);
53
+ } catch (error) {
54
+ _index.Logger.error('[network] getNetworkStatus failed', error);
55
+ return DEFAULT_STATUS;
56
+ }
57
+ };
58
+
59
+ /**
60
+ * Subscribe to network status changes. Returns an unsubscribe function.
61
+ */
62
+ exports.getNetworkStatus = getNetworkStatus;
63
+ const addNetworkStatusListener = listener => {
64
+ const ni = resolveNetInfo();
65
+ if (!ni) {
66
+ return () => undefined;
67
+ }
68
+ return ni.addEventListener(state => listener(normalize(state)));
69
+ };
70
+
71
+ /**
72
+ * React hook that returns the current network status and re-renders on change.
73
+ * Reports an optimistic online default when NetInfo is not installed.
74
+ */
75
+ exports.addNetworkStatusListener = addNetworkStatusListener;
76
+ const useNetworkStatus = () => {
77
+ const [status, setStatus] = (0, _react.useState)(DEFAULT_STATUS);
78
+ (0, _react.useEffect)(() => {
79
+ let mounted = true;
80
+ void getNetworkStatus().then(s => {
81
+ if (mounted) {
82
+ setStatus(s);
83
+ }
84
+ });
85
+ const unsubscribe = addNetworkStatusListener(s => {
86
+ if (mounted) {
87
+ setStatus(s);
88
+ }
89
+ });
90
+ return () => {
91
+ mounted = false;
92
+ unsubscribe();
93
+ };
94
+ }, []);
95
+ return status;
96
+ };
97
+ exports.useNetworkStatus = useNetworkStatus;
98
+ //# sourceMappingURL=networkStatus.js.map
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Permissions = void 0;
7
+ var _reactNative = require("react-native");
8
+ var _index = require("../logger/index.js");
9
+ // Android only — legacy WRITE_EXTERNAL_STORAGE
10
+
11
+ let permsModule = null;
12
+ let resolved = false;
13
+ const resolveModule = () => {
14
+ if (resolved) {
15
+ return permsModule;
16
+ }
17
+ resolved = true;
18
+ try {
19
+ const mod = require('react-native-permissions');
20
+ permsModule = mod.default ?? mod;
21
+ } catch {
22
+ _index.Logger.warn('[permissions] react-native-permissions is not installed; ' + 'permission checks will report "unavailable".');
23
+ permsModule = null;
24
+ }
25
+ return permsModule;
26
+ };
27
+ const mapToNative = kind => {
28
+ const m = resolveModule();
29
+ if (!m) {
30
+ return null;
31
+ }
32
+ const {
33
+ IOS,
34
+ ANDROID
35
+ } = m.PERMISSIONS;
36
+ if (_reactNative.Platform.OS === 'ios') {
37
+ switch (kind) {
38
+ case 'camera':
39
+ return IOS.CAMERA ?? null;
40
+ case 'microphone':
41
+ return IOS.MICROPHONE ?? null;
42
+ case 'photos':
43
+ return IOS.PHOTO_LIBRARY ?? null;
44
+ case 'photosAdd':
45
+ return IOS.PHOTO_LIBRARY_ADD_ONLY ?? null;
46
+ case 'mediaLibrary':
47
+ return IOS.MEDIA_LIBRARY ?? null;
48
+ case 'location':
49
+ return IOS.LOCATION_WHEN_IN_USE ?? null;
50
+ case 'locationAlways':
51
+ return IOS.LOCATION_ALWAYS ?? null;
52
+ case 'contacts':
53
+ return IOS.CONTACTS ?? null;
54
+ case 'calendar':
55
+ return IOS.CALENDARS ?? null;
56
+ case 'reminders':
57
+ return IOS.REMINDERS ?? null;
58
+ case 'notifications':
59
+ return null;
60
+ // handled separately on iOS
61
+ case 'bluetooth':
62
+ return IOS.BLUETOOTH ?? null;
63
+ case 'storage':
64
+ return null;
65
+ default:
66
+ return null;
67
+ }
68
+ }
69
+ switch (kind) {
70
+ case 'camera':
71
+ return ANDROID.CAMERA ?? null;
72
+ case 'microphone':
73
+ return ANDROID.RECORD_AUDIO ?? null;
74
+ case 'photos':
75
+ case 'mediaLibrary':
76
+ return ANDROID.READ_MEDIA_IMAGES ?? ANDROID.READ_EXTERNAL_STORAGE ?? null;
77
+ case 'photosAdd':
78
+ return ANDROID.READ_MEDIA_IMAGES ?? null;
79
+ case 'location':
80
+ case 'locationAlways':
81
+ return ANDROID.ACCESS_FINE_LOCATION ?? null;
82
+ case 'contacts':
83
+ return ANDROID.READ_CONTACTS ?? null;
84
+ case 'calendar':
85
+ return ANDROID.READ_CALENDAR ?? null;
86
+ case 'reminders':
87
+ return null;
88
+ case 'notifications':
89
+ return ANDROID.POST_NOTIFICATIONS ?? null;
90
+ case 'bluetooth':
91
+ return ANDROID.BLUETOOTH_CONNECT ?? null;
92
+ case 'storage':
93
+ return ANDROID.WRITE_EXTERNAL_STORAGE ?? null;
94
+ default:
95
+ return null;
96
+ }
97
+ };
98
+ const mapResult = raw => {
99
+ const m = resolveModule();
100
+ if (!m || !raw) {
101
+ return 'unavailable';
102
+ }
103
+ const {
104
+ RESULTS
105
+ } = m;
106
+ if (raw === RESULTS.GRANTED) {
107
+ return 'granted';
108
+ }
109
+ if (raw === RESULTS.DENIED) {
110
+ return 'denied';
111
+ }
112
+ if (raw === RESULTS.LIMITED) {
113
+ return 'limited';
114
+ }
115
+ if (raw === RESULTS.BLOCKED) {
116
+ return 'blocked';
117
+ }
118
+ return 'unavailable';
119
+ };
120
+ const checkPermission = async kind => {
121
+ const m = resolveModule();
122
+ if (!m) {
123
+ return 'unavailable';
124
+ }
125
+ const native = mapToNative(kind);
126
+ if (!native) {
127
+ return 'unavailable';
128
+ }
129
+ try {
130
+ const raw = await m.check(native);
131
+ return mapResult(raw);
132
+ } catch (error) {
133
+ _index.Logger.error(`[permissions] check(${kind}) failed`, error);
134
+ return 'unavailable';
135
+ }
136
+ };
137
+ const requestPermission = async kind => {
138
+ const m = resolveModule();
139
+ if (!m) {
140
+ return 'unavailable';
141
+ }
142
+ const native = mapToNative(kind);
143
+ if (!native) {
144
+ return 'unavailable';
145
+ }
146
+ try {
147
+ const raw = await m.request(native);
148
+ return mapResult(raw);
149
+ } catch (error) {
150
+ _index.Logger.error(`[permissions] request(${kind}) failed`, error);
151
+ return 'unavailable';
152
+ }
153
+ };
154
+ const openAppSettings = async () => {
155
+ const m = resolveModule();
156
+ if (!m) {
157
+ try {
158
+ await _reactNative.Linking.openSettings();
159
+ return true;
160
+ } catch (error) {
161
+ _index.Logger.error('[permissions] openAppSettings fallback failed', error);
162
+ return false;
163
+ }
164
+ }
165
+ try {
166
+ await m.openSettings();
167
+ return true;
168
+ } catch (error) {
169
+ _index.Logger.error('[permissions] openSettings failed', error);
170
+ return false;
171
+ }
172
+ };
173
+
174
+ /**
175
+ * Cross-platform permissions wrapper. Maps an abstract `PermissionKind`
176
+ * (camera, photos, location, notifications, …) to the right
177
+ * iOS / Android permission string under the hood.
178
+ *
179
+ * Requires `react-native-permissions` as an optional peer dependency;
180
+ * when missing, every check/request resolves to `'unavailable'`.
181
+ */
182
+ const Permissions = exports.Permissions = {
183
+ check: checkPermission,
184
+ request: requestPermission,
185
+ /** Convenience: returns true when the permission is `granted` or `limited`. */
186
+ isAuthorized: async kind => {
187
+ const s = await checkPermission(kind);
188
+ return s === 'granted' || s === 'limited';
189
+ },
190
+ /** Convenience: check, and request only if not yet granted. */
191
+ ensure: async kind => {
192
+ const s = await checkPermission(kind);
193
+ if (s === 'granted' || s === 'limited' || s === 'blocked' || s === 'unavailable') {
194
+ return s;
195
+ }
196
+ return requestPermission(kind);
197
+ },
198
+ openSettings: openAppSettings
199
+ };
200
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.setStorageImplementation = exports.Storage = void 0;
7
+ var _index = require("../logger/index.js");
8
+ /**
9
+ * Minimal AsyncStorage shape we depend on.
10
+ * Lets us avoid a hard import on `@react-native-async-storage/async-storage`.
11
+ */
12
+
13
+ let backend = null;
14
+ let resolved = false;
15
+ const resolveBackend = () => {
16
+ if (resolved) {
17
+ return backend;
18
+ }
19
+ resolved = true;
20
+ try {
21
+ const mod = require('@react-native-async-storage/async-storage');
22
+ backend = mod.default ?? mod;
23
+ } catch {
24
+ backend = null;
25
+ }
26
+ return backend;
27
+ };
28
+ const requireBackend = op => {
29
+ const b = resolveBackend();
30
+ if (!b) {
31
+ const err = new Error(`[Storage] @react-native-async-storage/async-storage is not installed; cannot ${op}. ` + 'Install the peer dep or call setStorageImplementation() with a custom backend.');
32
+ _index.Logger.error(err.message);
33
+ throw err;
34
+ }
35
+ return b;
36
+ };
37
+
38
+ /**
39
+ * Inject a custom storage backend (e.g. MMKV adapter) at runtime.
40
+ * Pass `null` to reset to the default AsyncStorage backend.
41
+ */
42
+ const setStorageImplementation = impl => {
43
+ backend = impl;
44
+ resolved = true;
45
+ };
46
+ exports.setStorageImplementation = setStorageImplementation;
47
+ const safeJsonParse = raw => {
48
+ if (raw == null) {
49
+ return null;
50
+ }
51
+ try {
52
+ return JSON.parse(raw);
53
+ } catch (error) {
54
+ _index.Logger.warn('[Storage] Failed to parse JSON value, returning null', error);
55
+ return null;
56
+ }
57
+ };
58
+ const getItem = async key => {
59
+ try {
60
+ return await requireBackend('getItem').getItem(key);
61
+ } catch (error) {
62
+ _index.Logger.error(`[Storage] getItem failed for key "${key}"`, error);
63
+ return null;
64
+ }
65
+ };
66
+ const setItem = async (key, value) => {
67
+ try {
68
+ await requireBackend('setItem').setItem(key, value);
69
+ return true;
70
+ } catch (error) {
71
+ _index.Logger.error(`[Storage] setItem failed for key "${key}"`, error);
72
+ return false;
73
+ }
74
+ };
75
+ const removeItem = async key => {
76
+ try {
77
+ await requireBackend('removeItem').removeItem(key);
78
+ return true;
79
+ } catch (error) {
80
+ _index.Logger.error(`[Storage] removeItem failed for key "${key}"`, error);
81
+ return false;
82
+ }
83
+ };
84
+ const getJson = async key => {
85
+ const raw = await getItem(key);
86
+ return safeJsonParse(raw);
87
+ };
88
+ const setJson = async (key, value) => {
89
+ try {
90
+ return await setItem(key, JSON.stringify(value));
91
+ } catch (error) {
92
+ _index.Logger.error(`[Storage] setJson failed for key "${key}"`, error);
93
+ return false;
94
+ }
95
+ };
96
+ const multiGet = async keys => {
97
+ try {
98
+ const b = requireBackend('multiGet');
99
+ if (!b.multiGet) {
100
+ const out = {};
101
+ await Promise.all(keys.map(async k => {
102
+ out[k] = await b.getItem(k);
103
+ }));
104
+ return out;
105
+ }
106
+ const pairs = await b.multiGet(keys);
107
+ const out = {};
108
+ for (const [k, v] of pairs) {
109
+ out[k] = v;
110
+ }
111
+ return out;
112
+ } catch (error) {
113
+ _index.Logger.error('[Storage] multiGet failed', error);
114
+ return {};
115
+ }
116
+ };
117
+ const multiSet = async entries => {
118
+ try {
119
+ const b = requireBackend('multiSet');
120
+ const pairs = Object.entries(entries);
121
+ if (!b.multiSet) {
122
+ await Promise.all(pairs.map(([k, v]) => b.setItem(k, v)));
123
+ return true;
124
+ }
125
+ await b.multiSet(pairs);
126
+ return true;
127
+ } catch (error) {
128
+ _index.Logger.error('[Storage] multiSet failed', error);
129
+ return false;
130
+ }
131
+ };
132
+ const multiRemove = async keys => {
133
+ try {
134
+ const b = requireBackend('multiRemove');
135
+ if (!b.multiRemove) {
136
+ await Promise.all(keys.map(k => b.removeItem(k)));
137
+ return true;
138
+ }
139
+ await b.multiRemove(keys);
140
+ return true;
141
+ } catch (error) {
142
+ _index.Logger.error('[Storage] multiRemove failed', error);
143
+ return false;
144
+ }
145
+ };
146
+ const clear = async () => {
147
+ try {
148
+ const b = requireBackend('clear');
149
+ if (!b.clear) {
150
+ _index.Logger.warn('[Storage] backend does not support clear()');
151
+ return false;
152
+ }
153
+ await b.clear();
154
+ return true;
155
+ } catch (error) {
156
+ _index.Logger.error('[Storage] clear failed', error);
157
+ return false;
158
+ }
159
+ };
160
+ const getAllKeys = async () => {
161
+ try {
162
+ const b = requireBackend('getAllKeys');
163
+ if (!b.getAllKeys) {
164
+ return [];
165
+ }
166
+ const keys = await b.getAllKeys();
167
+ return [...keys];
168
+ } catch (error) {
169
+ _index.Logger.error('[Storage] getAllKeys failed', error);
170
+ return [];
171
+ }
172
+ };
173
+ /**
174
+ * Typed AsyncStorage wrapper with safe JSON helpers.
175
+ * All operations swallow errors (logging them) and return a falsy result
176
+ * so that storage problems never crash the app.
177
+ *
178
+ * Backed by `@react-native-async-storage/async-storage` by default;
179
+ * override with `setStorageImplementation()` for MMKV or custom adapters.
180
+ */
181
+ const Storage = exports.Storage = {
182
+ getItem,
183
+ setItem,
184
+ removeItem,
185
+ getJson,
186
+ setJson,
187
+ multiGet,
188
+ multiSet,
189
+ multiRemove,
190
+ clear,
191
+ getAllKeys
192
+ };
193
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.qualityForFileSize = exports.compressImage = void 0;
7
+ var _index = require("../logger/index.js");
8
+ let compressor = null;
9
+ let resolved = false;
10
+ const resolveCompressor = () => {
11
+ if (resolved) {
12
+ return compressor;
13
+ }
14
+ resolved = true;
15
+ try {
16
+ compressor = require('react-native-compressor');
17
+ } catch {
18
+ _index.Logger.warn('[imageCompression] react-native-compressor is not installed; ' + 'compressImage() will return the original URI unchanged.');
19
+ compressor = null;
20
+ }
21
+ return compressor;
22
+ };
23
+ const EIGHT_MB = 8 * 1024 * 1024;
24
+ const THREE_MB = 3 * 1024 * 1024;
25
+
26
+ /**
27
+ * Bigger source files get compressed harder — a fixed quality either over-compresses
28
+ * small photos or leaves large ones too heavy to upload quickly. Unknown size falls
29
+ * back to the middle rung rather than assuming either extreme.
30
+ */
31
+ const qualityForFileSize = fileSize => {
32
+ if (fileSize == null) {
33
+ return 0.7;
34
+ }
35
+ if (fileSize > EIGHT_MB) {
36
+ return 0.6;
37
+ }
38
+ if (fileSize > THREE_MB) {
39
+ return 0.7;
40
+ }
41
+ return 0.8;
42
+ };
43
+
44
+ /**
45
+ * Compress an image referenced by a URI. Returns a new URI on success, or the
46
+ * original URI on failure / when the optional `react-native-compressor`
47
+ * dependency is missing — so callers can rely on always getting a usable URI.
48
+ */
49
+ exports.qualityForFileSize = qualityForFileSize;
50
+ const compressImage = async (uri, options = {}) => {
51
+ const lib = resolveCompressor();
52
+ if (!lib || !uri) {
53
+ return uri;
54
+ }
55
+ const {
56
+ maxSize = 1500,
57
+ output = 'jpg',
58
+ fileSize
59
+ } = options;
60
+ const quality = options.quality ?? qualityForFileSize(fileSize);
61
+ try {
62
+ return await lib.Image.compress(uri, {
63
+ maxWidth: maxSize,
64
+ maxHeight: maxSize,
65
+ quality,
66
+ compressionMethod: 'manual',
67
+ output
68
+ });
69
+ } catch (error) {
70
+ _index.Logger.error('[imageCompression] compressImage failed; returning original URI', error);
71
+ return uri;
72
+ }
73
+ };
74
+ exports.compressImage = compressImage;
75
+ //# sourceMappingURL=imageCompression.js.map
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "compressImage", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _imageCompression.compressImage;
10
+ }
11
+ });
12
+ var _imageCompression = require("./imageCompression.js");
13
+ //# sourceMappingURL=index.js.map