Skip to content

Commit ff52c42

Browse files
committed
Harden Capacitor local notification patching
- Update the patch script to preserve required notification validation - Handle empty arrays explicitly for cancel and remove flows - Switch type extraction to the newer Capacitor call API
1 parent 1351471 commit ff52c42

1 file changed

Lines changed: 62 additions & 8 deletions

File tree

scripts/patch-capacitor-local-notifications.ts

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,75 @@ const HANDLER_FILE = resolve(
1111
"node_modules/@capacitor/local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsHandler.swift",
1212
);
1313

14-
const REPLACEMENTS: ReadonlyArray<[RegExp, string]> = [
14+
const REPLACEMENTS: ReadonlyArray<[string, string]> = [
1515
[
16-
/guard let notifications = call\.getArray\("notifications", JSObject\.self\) else \{/g,
17-
'guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }) else {',
16+
[
17+
' guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }) else {\n',
18+
' call.reject("Must provide notifications array as notifications option")\n',
19+
" return\n",
20+
" }\n",
21+
].join(""),
22+
[
23+
' guard call.getValue("notifications") != nil else {\n',
24+
' call.reject("Must provide notifications array as notifications option")\n',
25+
" return\n",
26+
" }\n",
27+
' let notifications = call.getArray("notifications", []).compactMap({ $0 as? JSObject })\n',
28+
].join(""),
29+
],
30+
[
31+
[
32+
' guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }), notifications.count > 0 else {\n',
33+
' call.reject("Must supply notifications to cancel")\n',
34+
" return\n",
35+
" }\n",
36+
].join(""),
37+
[
38+
' guard call.getValue("notifications") != nil else {\n',
39+
' call.reject("Must supply notifications to cancel")\n',
40+
" return\n",
41+
" }\n",
42+
' let notifications = call.getArray("notifications", []).compactMap({ $0 as? JSObject })\n',
43+
" guard notifications.count > 0 else {\n",
44+
' call.reject("Must supply notifications to cancel")\n',
45+
" return\n",
46+
" }\n",
47+
].join(""),
1848
],
1949
[
20-
/guard let notifications = call\.getArray\("notifications", JSObject\.self\), notifications\.count > 0 else \{/g,
21-
'guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }), notifications.count > 0 else {',
50+
[
51+
' guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }), notifications.count > 0 else {\n',
52+
' call.reject("Must supply notifications to remove")\n',
53+
" return\n",
54+
" }\n",
55+
].join(""),
56+
[
57+
' guard call.getValue("notifications") != nil else {\n',
58+
' call.reject("Must supply notifications to remove")\n',
59+
" return\n",
60+
" }\n",
61+
' let notifications = call.getArray("notifications", []).compactMap({ $0 as? JSObject })\n',
62+
" guard notifications.count > 0 else {\n",
63+
' call.reject("Must supply notifications to remove")\n',
64+
" return\n",
65+
" }\n",
66+
].join(""),
2267
],
2368
[
24-
/guard let types = call\.getArray\("types", JSObject\.self\) else \{/g,
25-
'guard let types = call.getArray("types")?.compactMap({ $0 as? JSObject }) else {',
69+
[
70+
' guard let types = call.getArray("types")?.compactMap({ $0 as? JSObject }) else {\n',
71+
" return\n",
72+
" }\n",
73+
].join(""),
74+
[
75+
' guard call.getValue("types") != nil else {\n',
76+
" return\n",
77+
" }\n",
78+
' let types = call.getArray("types", []).compactMap({ $0 as? JSObject })\n',
79+
].join(""),
2680
],
2781
[
28-
/return bridge\?\.localURL\(fromWebURL: webURL\)/g,
82+
"return bridge?.localURL(fromWebURL: webURL)",
2983
[
3084
" switch webURL.scheme {",
3185
' case "res":',

0 commit comments

Comments
 (0)