react-native-nitro-cookies 0.0.1 → 1.0.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.
@@ -169,7 +169,101 @@ class NitroCookies : HybridNitroCookiesSpec() {
169
169
  return url
170
170
  }
171
171
 
172
- // MARK: - Main Cookie Operations
172
+ // MARK: - Synchronous Cookie Operations
173
+
174
+ /** Get cookies synchronously for a URL */
175
+ override fun getSync(url: String): Array<Cookie> {
176
+ val urlObj = validateURL(url)
177
+ val cookieManager = CookieManager.getInstance()
178
+ val cookieString = cookieManager.getCookie(url)
179
+
180
+ if (cookieString.isNullOrEmpty()) {
181
+ return emptyArray()
182
+ }
183
+
184
+ // Parse cookie string (format: "name1=value1; name2=value2")
185
+ val cookies = mutableListOf<Cookie>()
186
+ val cookiePairs = cookieString.split(";").map { it.trim() }
187
+
188
+ for (pair in cookiePairs) {
189
+ val nameValue = pair.split("=", limit = 2)
190
+ if (nameValue.size == 2) {
191
+ cookies.add(
192
+ Cookie(
193
+ name = nameValue[0].trim(),
194
+ value = nameValue[1].trim(),
195
+ path = "/",
196
+ domain = urlObj.host,
197
+ version = null,
198
+ expires = null,
199
+ secure = null,
200
+ httpOnly = null
201
+ )
202
+ )
203
+ }
204
+ }
205
+
206
+ return cookies.toTypedArray()
207
+ }
208
+
209
+ /** Set a cookie synchronously */
210
+ override fun setSync(url: String, cookie: Cookie): Boolean {
211
+ val urlObj = validateURL(url)
212
+ validateDomain(cookie, urlObj)
213
+
214
+ // Apply defaults
215
+ val cookieWithDefaults =
216
+ cookie.copy(path = cookie.path ?: "/", domain = cookie.domain ?: urlObj.host)
217
+
218
+ val cookieManager = CookieManager.getInstance()
219
+ cookieManager.setAcceptCookie(true)
220
+
221
+ val setCookieString = toRFC6265String(cookieWithDefaults)
222
+ cookieManager.setCookie(url, setCookieString)
223
+
224
+ return true
225
+ }
226
+
227
+ /** Parse and set cookies from Set-Cookie header synchronously */
228
+ override fun setFromResponseSync(url: String, value: String): Boolean {
229
+ validateURL(url) // Validate URL format
230
+ val cookieManager = CookieManager.getInstance()
231
+ cookieManager.setAcceptCookie(true)
232
+
233
+ // Set-Cookie header can contain multiple cookies
234
+ val setCookieHeaders = value.split("\n").map { it.trim() }
235
+ for (header in setCookieHeaders) {
236
+ if (header.isNotEmpty()) {
237
+ cookieManager.setCookie(url, header)
238
+ }
239
+ }
240
+
241
+ return true
242
+ }
243
+
244
+ /** Clear a specific cookie by name synchronously */
245
+ override fun clearByNameSync(url: String, name: String): Boolean {
246
+ val urlObj = validateURL(url)
247
+ val cookieManager = CookieManager.getInstance()
248
+
249
+ // Check if the cookie exists for the given URL
250
+ val cookieString = cookieManager.getCookie(url)
251
+ val cookies = cookieString?.split(";")?.map { it.trim() } ?: emptyList()
252
+ val found = cookies.any { it.startsWith("$name=") }
253
+
254
+ if (found) {
255
+ // Android CookieManager doesn't support removing specific cookies by name
256
+ // We can only expire them by setting a past expiration date
257
+ val expiredCookie =
258
+ "$name=; Path=/; Domain=${urlObj.host}; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
259
+ cookieManager.setCookie(url, expiredCookie)
260
+ return true
261
+ }
262
+
263
+ return false
264
+ }
265
+
266
+ // MARK: - Asynchronous Cookie Operations
173
267
 
174
268
  /** Set a single cookie */
175
269
  override fun set(url: String, cookie: Cookie, useWebKit: Boolean?): Promise<Boolean> {
@@ -289,19 +383,27 @@ class NitroCookies : HybridNitroCookiesSpec() {
289
383
  }
290
384
  }
291
385
 
292
- /** Clear specific cookie by name (iOS only - not fully supported on Android) */
386
+ /** Clear specific cookie by name */
293
387
  override fun clearByName(url: String, name: String, useWebKit: Boolean?): Promise<Boolean> {
294
388
  return Promise.async {
295
389
  val urlObj = validateURL(url)
296
390
  val cookieManager = CookieManager.getInstance()
297
391
 
298
- // Android CookieManager doesn't support removing specific cookies by name
299
- // We can only expire them by setting a past expiration date
300
- val expiredCookie =
301
- "$name=; Path=/; Domain=${urlObj.host}; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
302
- cookieManager.setCookie(url, expiredCookie)
303
-
304
- true
392
+ // Check if the cookie exists for the given URL
393
+ val cookieString = cookieManager.getCookie(url)
394
+ val cookies = cookieString?.split(";")?.map { it.trim() } ?: emptyList()
395
+ val found = cookies.any { it.startsWith("$name=") }
396
+
397
+ if (found) {
398
+ // Android CookieManager doesn't support removing specific cookies by name
399
+ // We can only expire them by setting a past expiration date
400
+ val expiredCookie =
401
+ "$name=; Path=/; Domain=${urlObj.host}; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
402
+ cookieManager.setCookie(url, expiredCookie)
403
+ true
404
+ } else {
405
+ false
406
+ }
305
407
  }
306
408
  }
307
409
 
@@ -134,7 +134,81 @@ public class HybridNitroCookies: HybridNitroCookiesSpec {
134
134
  return url
135
135
  }
136
136
 
137
- // MARK: - Main Cookie Operations
137
+ // MARK: - Synchronous Cookie Operations
138
+
139
+ /**
140
+ * Get cookies synchronously for a URL
141
+ * Uses NSHTTPCookieStorage only (WebKit not supported for sync operations)
142
+ */
143
+ public func getSync(url urlString: String) throws -> [Cookie] {
144
+ let url = try validateURL(urlString)
145
+ let allCookies = HTTPCookieStorage.shared.cookies ?? []
146
+ let filteredCookies = allCookies.filter { cookie in
147
+ self.isMatchingDomain(cookieDomain: cookie.domain,
148
+ urlHost: url.host ?? "")
149
+ }
150
+ return filteredCookies.map { self.createCookieData(from: $0) }
151
+ }
152
+
153
+ /**
154
+ * Set a cookie synchronously
155
+ * Uses NSHTTPCookieStorage only (WebKit not supported for sync operations)
156
+ */
157
+ public func setSync(url urlString: String, cookie: Cookie) throws -> Bool {
158
+ let url = try validateURL(urlString)
159
+ try validateDomain(cookie: cookie, url: url)
160
+
161
+ // Apply defaults
162
+ var cookieWithDefaults = cookie
163
+ if cookieWithDefaults.path == nil {
164
+ cookieWithDefaults.path = "/"
165
+ }
166
+ if cookieWithDefaults.domain == nil {
167
+ cookieWithDefaults.domain = url.host
168
+ }
169
+
170
+ let httpCookie = try makeHTTPCookie(from: cookieWithDefaults, url: url)
171
+ HTTPCookieStorage.shared.setCookie(httpCookie)
172
+ return true
173
+ }
174
+
175
+ /**
176
+ * Parse and set cookies from Set-Cookie header synchronously
177
+ */
178
+ public func setFromResponseSync(url urlString: String, value: String) throws -> Bool {
179
+ let url = try validateURL(urlString)
180
+ let headerFields = ["Set-Cookie": value]
181
+ let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url)
182
+
183
+ let storage = HTTPCookieStorage.shared
184
+ for cookie in cookies {
185
+ storage.setCookie(cookie)
186
+ }
187
+ return true
188
+ }
189
+
190
+ /**
191
+ * Clear a specific cookie by name synchronously
192
+ */
193
+ public func clearByNameSync(url urlString: String, name: String) throws -> Bool {
194
+ let url = try validateURL(urlString)
195
+ let storage = HTTPCookieStorage.shared
196
+ let cookies = storage.cookies ?? []
197
+ let matchingCookie = cookies.first { cookie in
198
+ cookie.name == name &&
199
+ self.isMatchingDomain(cookieDomain: cookie.domain,
200
+ urlHost: url.host ?? "")
201
+ }
202
+
203
+ if let cookie = matchingCookie {
204
+ storage.deleteCookie(cookie)
205
+ return true
206
+ } else {
207
+ return false
208
+ }
209
+ }
210
+
211
+ // MARK: - Asynchronous Cookie Operations
138
212
 
139
213
  /**
140
214
  * Set a single cookie
@@ -13,22 +13,112 @@ function cookiesToDictionary(cookies) {
13
13
  /**
14
14
  * Main NitroCookies export object with all cookie management methods.
15
15
  *
16
+ * Supports both synchronous and asynchronous APIs:
17
+ * - Synchronous methods (getSync, setSync, etc.): Direct return values, no Promise overhead
18
+ * - Asynchronous methods (get, set, etc.): Return Promises for WebKit and network operations
19
+ *
16
20
  * @example
17
21
  * ```typescript
18
22
  * import NitroCookies from 'react-native-nitro-cookies';
19
23
  *
20
- * // Set a cookie
21
- * await NitroCookies.set('https://example.com', {
22
- * name: 'session',
23
- * value: 'abc123',
24
- * secure: true,
25
- * });
24
+ * // Synchronous API (no await needed!)
25
+ * const cookies = NitroCookies.getSync('https://example.com');
26
+ * NitroCookies.setSync('https://example.com', { name: 'session', value: 'abc123' });
26
27
  *
27
- * // Get cookies
28
- * const cookies = await NitroCookies.get('https://example.com');
28
+ * // Asynchronous API (for WebKit/network operations)
29
+ * const cookies = await NitroCookies.get('https://example.com', true); // useWebKit
29
30
  * ```
30
31
  */
31
32
  export const NitroCookies = {
33
+ // ========================================
34
+ // SYNCHRONOUS METHODS
35
+ // ========================================
36
+
37
+ /**
38
+ * Get cookies synchronously for a URL.
39
+ *
40
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
41
+ * Does NOT support WebKit cookie store (use async `get` with `useWebKit: true`).
42
+ *
43
+ * @param url - The URL to match cookies against (must include protocol)
44
+ * @returns Dictionary of cookies keyed by name
45
+ * @throws {Error} INVALID_URL - URL is malformed or missing protocol
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * // No await needed!
50
+ * const cookies = NitroCookies.getSync('https://example.com');
51
+ * console.log(cookies); // { session: { name: 'session', value: 'abc123', ... } }
52
+ * ```
53
+ */
54
+ getSync(url) {
55
+ return cookiesToDictionary(NitroCookiesHybridObject.getSync(url));
56
+ },
57
+ /**
58
+ * Set a cookie synchronously.
59
+ *
60
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
61
+ * Does NOT support WebKit cookie store (use async `set` with `useWebKit: true`).
62
+ *
63
+ * @param url - The URL for which to set the cookie (must include protocol)
64
+ * @param cookie - The cookie object to store
65
+ * @returns true on success
66
+ * @throws {Error} INVALID_URL - URL is malformed or missing protocol
67
+ * @throws {Error} DOMAIN_MISMATCH - Cookie domain doesn't match URL host
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * // No await needed!
72
+ * NitroCookies.setSync('https://example.com', {
73
+ * name: 'session',
74
+ * value: 'abc123',
75
+ * path: '/',
76
+ * secure: true,
77
+ * });
78
+ * ```
79
+ */
80
+ setSync(url, cookie) {
81
+ return NitroCookiesHybridObject.setSync(url, cookie);
82
+ },
83
+ /**
84
+ * Parse and set cookies from Set-Cookie header synchronously.
85
+ *
86
+ * @param url - The URL associated with the Set-Cookie header
87
+ * @param value - The raw Set-Cookie header value
88
+ * @returns true on success
89
+ * @throws {Error} INVALID_URL - URL is malformed
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * NitroCookies.setFromResponseSync(
94
+ * 'https://example.com',
95
+ * 'session=abc123; path=/; secure; HttpOnly'
96
+ * );
97
+ * ```
98
+ */
99
+ setFromResponseSync(url, value) {
100
+ return NitroCookiesHybridObject.setFromResponseSync(url, value);
101
+ },
102
+ /**
103
+ * Clear a specific cookie by name synchronously.
104
+ *
105
+ * @param url - The URL to match the cookie domain
106
+ * @param name - The name of the cookie to remove
107
+ * @returns true if cookie was found and removed, false if not found
108
+ * @throws {Error} INVALID_URL - URL is malformed
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * const removed = NitroCookies.clearByNameSync('https://example.com', 'session');
113
+ * console.log(removed ? 'Cookie removed' : 'Cookie not found');
114
+ * ```
115
+ */
116
+ clearByNameSync(url, name) {
117
+ return NitroCookiesHybridObject.clearByNameSync(url, name);
118
+ },
119
+ // ========================================
120
+ // ASYNCHRONOUS METHODS
121
+ // ========================================
32
122
  /**
33
123
  * Set a single cookie for a specific URL.
34
124
  *
@@ -1 +1 @@
1
- {"version":3,"names":["NitroModules","NitroCookiesHybridObject","createHybridObject","cookiesToDictionary","cookies","result","cookie","name","NitroCookies","set","url","useWebKit","get","clearAll","setFromResponse","value","getFromResponse","getAll","clearByName","flush","removeSessionCookies"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,4BAA4B;AAIzD,MAAMC,wBAAwB,GAC5BD,YAAY,CAACE,kBAAkB,CAAmB,cAAc,CAAC;AAEnE,SAASC,mBAAmBA,CAACC,OAAiB,EAAW;EACvD,MAAMC,MAAe,GAAG,CAAC,CAAC;EAC1B,KAAK,MAAMC,MAAM,IAAIF,OAAO,EAAE;IAC5BC,MAAM,CAACC,MAAM,CAACC,IAAI,CAAC,GAAGD,MAAM;EAC9B;EACA,OAAOD,MAAM;AACf;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMG,YAAY,GAAG;EAC1B;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMC,GAAGA,CACPC,GAAW,EACXJ,MAAc,EACdK,SAAmB,EACD;IAClB,OAAOV,wBAAwB,CAACQ,GAAG,CAACC,GAAG,EAAEJ,MAAM,EAAEK,SAAS,IAAI,KAAK,CAAC;EACtE,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMC,GAAGA,CAACF,GAAW,EAAEC,SAAmB,EAAoB;IAC5D,MAAMP,OAAO,GAAG,MAAMH,wBAAwB,CAACW,GAAG,CAACF,GAAG,EAAEC,SAAS,IAAI,KAAK,CAAC;IAC3E,OAAOR,mBAAmB,CAACC,OAAO,CAAC;EACrC,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMS,QAAQA,CAACF,SAAmB,EAAoB;IACpD,OAAOV,wBAAwB,CAACY,QAAQ,CAACF,SAAS,IAAI,KAAK,CAAC;EAC9D,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMG,eAAeA,CAACJ,GAAW,EAAEK,KAAa,EAAoB;IAClE,OAAOd,wBAAwB,CAACa,eAAe,CAACJ,GAAG,EAAEK,KAAK,CAAC;EAC7D,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMC,eAAeA,CAACN,GAAW,EAAoB;IACnD,MAAMN,OAAO,GAAG,MAAMH,wBAAwB,CAACe,eAAe,CAACN,GAAG,CAAC;IACnE,OAAOP,mBAAmB,CAACC,OAAO,CAAC;EACrC,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMa,MAAMA,CAACN,SAAmB,EAAoB;IAClD,MAAMP,OAAO,GAAG,MAAMH,wBAAwB,CAACgB,MAAM,CAACN,SAAS,IAAI,KAAK,CAAC;IACzE,OAAOR,mBAAmB,CAACC,OAAO,CAAC;EACrC,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMc,WAAWA,CACfR,GAAW,EACXH,IAAY,EACZI,SAAmB,EACD;IAClB,OAAOV,wBAAwB,CAACiB,WAAW,CAACR,GAAG,EAAEH,IAAI,EAAEI,SAAS,IAAI,KAAK,CAAC;EAC5E,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMQ,KAAKA,CAAA,EAAkB;IAC3B,OAAOlB,wBAAwB,CAACkB,KAAK,CAAC,CAAC;EACzC,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMC,oBAAoBA,CAAA,EAAqB;IAC7C,OAAOnB,wBAAwB,CAACmB,oBAAoB,CAAC,CAAC;EACxD;AACF,CAAC;;AAED;;AAGA;AACA,eAAeZ,YAAY","ignoreList":[]}
1
+ {"version":3,"names":["NitroModules","NitroCookiesHybridObject","createHybridObject","cookiesToDictionary","cookies","result","cookie","name","NitroCookies","getSync","url","setSync","setFromResponseSync","value","clearByNameSync","set","useWebKit","get","clearAll","setFromResponse","getFromResponse","getAll","clearByName","flush","removeSessionCookies"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,4BAA4B;AAIzD,MAAMC,wBAAwB,GAC5BD,YAAY,CAACE,kBAAkB,CAAmB,cAAc,CAAC;AAEnE,SAASC,mBAAmBA,CAACC,OAAiB,EAAW;EACvD,MAAMC,MAAe,GAAG,CAAC,CAAC;EAC1B,KAAK,MAAMC,MAAM,IAAIF,OAAO,EAAE;IAC5BC,MAAM,CAACC,MAAM,CAACC,IAAI,CAAC,GAAGD,MAAM;EAC9B;EACA,OAAOD,MAAM;AACf;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMG,YAAY,GAAG;EAC1B;EACA;EACA;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEC,OAAOA,CAACC,GAAW,EAAW;IAC5B,OAAOP,mBAAmB,CAACF,wBAAwB,CAACQ,OAAO,CAACC,GAAG,CAAC,CAAC;EACnE,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEC,OAAOA,CAACD,GAAW,EAAEJ,MAAc,EAAW;IAC5C,OAAOL,wBAAwB,CAACU,OAAO,CAACD,GAAG,EAAEJ,MAAM,CAAC;EACtD,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEM,mBAAmBA,CAACF,GAAW,EAAEG,KAAa,EAAW;IACvD,OAAOZ,wBAAwB,CAACW,mBAAmB,CAACF,GAAG,EAAEG,KAAK,CAAC;EACjE,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEC,eAAeA,CAACJ,GAAW,EAAEH,IAAY,EAAW;IAClD,OAAON,wBAAwB,CAACa,eAAe,CAACJ,GAAG,EAAEH,IAAI,CAAC;EAC5D,CAAC;EAED;EACA;EACA;EACA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMQ,GAAGA,CACPL,GAAW,EACXJ,MAAc,EACdU,SAAmB,EACD;IAClB,OAAOf,wBAAwB,CAACc,GAAG,CAACL,GAAG,EAAEJ,MAAM,EAAEU,SAAS,IAAI,KAAK,CAAC;EACtE,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMC,GAAGA,CAACP,GAAW,EAAEM,SAAmB,EAAoB;IAC5D,MAAMZ,OAAO,GAAG,MAAMH,wBAAwB,CAACgB,GAAG,CAACP,GAAG,EAAEM,SAAS,IAAI,KAAK,CAAC;IAC3E,OAAOb,mBAAmB,CAACC,OAAO,CAAC;EACrC,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMc,QAAQA,CAACF,SAAmB,EAAoB;IACpD,OAAOf,wBAAwB,CAACiB,QAAQ,CAACF,SAAS,IAAI,KAAK,CAAC;EAC9D,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMG,eAAeA,CAACT,GAAW,EAAEG,KAAa,EAAoB;IAClE,OAAOZ,wBAAwB,CAACkB,eAAe,CAACT,GAAG,EAAEG,KAAK,CAAC;EAC7D,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMO,eAAeA,CAACV,GAAW,EAAoB;IACnD,MAAMN,OAAO,GAAG,MAAMH,wBAAwB,CAACmB,eAAe,CAACV,GAAG,CAAC;IACnE,OAAOP,mBAAmB,CAACC,OAAO,CAAC;EACrC,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMiB,MAAMA,CAACL,SAAmB,EAAoB;IAClD,MAAMZ,OAAO,GAAG,MAAMH,wBAAwB,CAACoB,MAAM,CAACL,SAAS,IAAI,KAAK,CAAC;IACzE,OAAOb,mBAAmB,CAACC,OAAO,CAAC;EACrC,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMkB,WAAWA,CACfZ,GAAW,EACXH,IAAY,EACZS,SAAmB,EACD;IAClB,OAAOf,wBAAwB,CAACqB,WAAW,CAACZ,GAAG,EAAEH,IAAI,EAAES,SAAS,IAAI,KAAK,CAAC;EAC5E,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMO,KAAKA,CAAA,EAAkB;IAC3B,OAAOtB,wBAAwB,CAACsB,KAAK,CAAC,CAAC;EACzC,CAAC;EAED;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMC,oBAAoBA,CAAA,EAAqB;IAC7C,OAAOvB,wBAAwB,CAACuB,oBAAoB,CAAC,CAAC;EACxD;AACF,CAAC;;AAED;;AAGA;AACA,eAAehB,YAAY","ignoreList":[]}
@@ -13,11 +13,55 @@ import type { Cookie } from './types';
13
13
  * Uses Nitro Modules JSI architecture to achieve 5x+ faster operations compared
14
14
  * to traditional React Native bridge-based implementations.
15
15
  *
16
+ * API Design:
17
+ * - Synchronous methods (xxxSync): Direct return values, no Promise wrapping
18
+ * - Asynchronous methods: Return Promise for operations requiring callbacks/network
16
19
  */
17
20
  export interface NitroCookies extends HybridObject<{
18
21
  ios: 'swift';
19
22
  android: 'kotlin';
20
23
  }> {
24
+ /**
25
+ * Get cookies synchronously for a URL
26
+ *
27
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
28
+ * Does NOT support WebKit cookie store (use async `get` with `useWebKit: true`).
29
+ *
30
+ * @param url - The URL to match cookies against (must include protocol)
31
+ * @returns Array of cookies matching the URL domain
32
+ * @throws Error if URL is invalid
33
+ */
34
+ getSync(url: string): Cookie[];
35
+ /**
36
+ * Set a cookie synchronously
37
+ *
38
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
39
+ * Does NOT support WebKit cookie store (use async `set` with `useWebKit: true`).
40
+ *
41
+ * @param url - The URL for which to set the cookie (must include protocol)
42
+ * @param cookie - The cookie object to store
43
+ * @returns true on success
44
+ * @throws Error if URL is invalid or domain mismatch
45
+ */
46
+ setSync(url: string, cookie: Cookie): boolean;
47
+ /**
48
+ * Parse and set cookies from Set-Cookie header synchronously
49
+ *
50
+ * @param url - The URL associated with the Set-Cookie header
51
+ * @param value - The raw Set-Cookie header value
52
+ * @returns true on success
53
+ * @throws Error if URL is invalid
54
+ */
55
+ setFromResponseSync(url: string, value: string): boolean;
56
+ /**
57
+ * Clear a specific cookie by name synchronously
58
+ *
59
+ * @param url - The URL to match the cookie domain
60
+ * @param name - The name of the cookie to remove
61
+ * @returns true if cookie was found and removed, false if not found
62
+ * @throws Error if URL is invalid
63
+ */
64
+ clearByNameSync(url: string, name: string): boolean;
21
65
  /**
22
66
  * Set a single cookie for a specific URL
23
67
  *
@@ -1 +1 @@
1
- {"version":3,"file":"NitroCookies.nitro.d.ts","sourceRoot":"","sources":["../../../src/NitroCookies.nitro.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAKtC;;;;;;;GAOG;AACH,MAAM,WAAW,YACf,SAAQ,YAAY,CAAC;IAAE,GAAG,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,QAAQ,CAAA;CAAE,CAAC;IACzD;;;;;;;OAOG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAExE;;;;;;OAMG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEzD;;;;;OAKG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEhD;;;;;;OAMG;IACH,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9D;;;;;OAKG;IACH,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEhD;;;;;OAKG;IACH,MAAM,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAE/C;;;;;;;OAOG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9E;;;;OAIG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB;;;;OAIG;IACH,oBAAoB,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1C"}
1
+ {"version":3,"file":"NitroCookies.nitro.d.ts","sourceRoot":"","sources":["../../../src/NitroCookies.nitro.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAKtC;;;;;;;;;;GAUG;AACH,MAAM,WAAW,YACf,SAAQ,YAAY,CAAC;IAAE,GAAG,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,QAAQ,CAAA;CAAE,CAAC;IAKzD;;;;;;;;;OASG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE/B;;;;;;;;;;OAUG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IAE9C;;;;;;;OAOG;IACH,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAEzD;;;;;;;OAOG;IACH,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAMpD;;;;;;;OAOG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAExE;;;;;;OAMG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEzD;;;;;OAKG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEhD;;;;;;OAMG;IACH,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9D;;;;;OAKG;IACH,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEhD;;;;;OAKG;IACH,MAAM,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAE/C;;;;;;;OAOG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9E;;;;OAIG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB;;;;OAIG;IACH,oBAAoB,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1C"}
@@ -2,22 +2,97 @@ import type { Cookie, Cookies, CookieErrorCode, CookieError } from './types';
2
2
  /**
3
3
  * Main NitroCookies export object with all cookie management methods.
4
4
  *
5
+ * Supports both synchronous and asynchronous APIs:
6
+ * - Synchronous methods (getSync, setSync, etc.): Direct return values, no Promise overhead
7
+ * - Asynchronous methods (get, set, etc.): Return Promises for WebKit and network operations
8
+ *
5
9
  * @example
6
10
  * ```typescript
7
11
  * import NitroCookies from 'react-native-nitro-cookies';
8
12
  *
9
- * // Set a cookie
10
- * await NitroCookies.set('https://example.com', {
11
- * name: 'session',
12
- * value: 'abc123',
13
- * secure: true,
14
- * });
13
+ * // Synchronous API (no await needed!)
14
+ * const cookies = NitroCookies.getSync('https://example.com');
15
+ * NitroCookies.setSync('https://example.com', { name: 'session', value: 'abc123' });
15
16
  *
16
- * // Get cookies
17
- * const cookies = await NitroCookies.get('https://example.com');
17
+ * // Asynchronous API (for WebKit/network operations)
18
+ * const cookies = await NitroCookies.get('https://example.com', true); // useWebKit
18
19
  * ```
19
20
  */
20
21
  export declare const NitroCookies: {
22
+ /**
23
+ * Get cookies synchronously for a URL.
24
+ *
25
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
26
+ * Does NOT support WebKit cookie store (use async `get` with `useWebKit: true`).
27
+ *
28
+ * @param url - The URL to match cookies against (must include protocol)
29
+ * @returns Dictionary of cookies keyed by name
30
+ * @throws {Error} INVALID_URL - URL is malformed or missing protocol
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * // No await needed!
35
+ * const cookies = NitroCookies.getSync('https://example.com');
36
+ * console.log(cookies); // { session: { name: 'session', value: 'abc123', ... } }
37
+ * ```
38
+ */
39
+ getSync(url: string): Cookies;
40
+ /**
41
+ * Set a cookie synchronously.
42
+ *
43
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
44
+ * Does NOT support WebKit cookie store (use async `set` with `useWebKit: true`).
45
+ *
46
+ * @param url - The URL for which to set the cookie (must include protocol)
47
+ * @param cookie - The cookie object to store
48
+ * @returns true on success
49
+ * @throws {Error} INVALID_URL - URL is malformed or missing protocol
50
+ * @throws {Error} DOMAIN_MISMATCH - Cookie domain doesn't match URL host
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * // No await needed!
55
+ * NitroCookies.setSync('https://example.com', {
56
+ * name: 'session',
57
+ * value: 'abc123',
58
+ * path: '/',
59
+ * secure: true,
60
+ * });
61
+ * ```
62
+ */
63
+ setSync(url: string, cookie: Cookie): boolean;
64
+ /**
65
+ * Parse and set cookies from Set-Cookie header synchronously.
66
+ *
67
+ * @param url - The URL associated with the Set-Cookie header
68
+ * @param value - The raw Set-Cookie header value
69
+ * @returns true on success
70
+ * @throws {Error} INVALID_URL - URL is malformed
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * NitroCookies.setFromResponseSync(
75
+ * 'https://example.com',
76
+ * 'session=abc123; path=/; secure; HttpOnly'
77
+ * );
78
+ * ```
79
+ */
80
+ setFromResponseSync(url: string, value: string): boolean;
81
+ /**
82
+ * Clear a specific cookie by name synchronously.
83
+ *
84
+ * @param url - The URL to match the cookie domain
85
+ * @param name - The name of the cookie to remove
86
+ * @returns true if cookie was found and removed, false if not found
87
+ * @throws {Error} INVALID_URL - URL is malformed
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * const removed = NitroCookies.clearByNameSync('https://example.com', 'session');
92
+ * console.log(removed ? 'Cookie removed' : 'Cookie not found');
93
+ * ```
94
+ */
95
+ clearByNameSync(url: string, name: string): boolean;
21
96
  /**
22
97
  * Set a single cookie for a specific URL.
23
98
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAa7E;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,YAAY;IACvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;aAEI,MAAM,UACH,MAAM,cACF,OAAO,GAClB,OAAO,CAAC,OAAO,CAAC;IAInB;;;;;;;;;;;;;;;;;;;OAmBG;aACY,MAAM,cAAc,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAK7D;;;;;;;;;;;;OAYG;yBACwB,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIrD;;;;;;;;;;;;;;;;;;;;;OAqBG;yBACwB,MAAM,SAAS,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAInE;;;;;;;;;;;;;;;;;;OAkBG;yBACwB,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAKpD;;;;;;;;;;;;;;;;;;;;;;OAsBG;uBACsB,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAKnD;;;;;;;;;;;;;;;;;;;;;OAqBG;qBAEI,MAAM,QACL,MAAM,cACA,OAAO,GAClB,OAAO,CAAC,OAAO,CAAC;IAInB;;;;;;;;;;;;;;;;;;;;OAoBG;aACY,OAAO,CAAC,IAAI,CAAC;IAI5B;;;;;;;;;;;;;;;;;;;;;OAqBG;4BAC2B,OAAO,CAAC,OAAO,CAAC;CAG/C,CAAC;AAGF,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,CAAC;AAG9D,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAa7E;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,YAAY;IAKvB;;;;;;;;;;;;;;;;OAgBG;iBACU,MAAM,GAAG,OAAO;IAI7B;;;;;;;;;;;;;;;;;;;;;;OAsBG;iBACU,MAAM,UAAU,MAAM,GAAG,OAAO;IAI7C;;;;;;;;;;;;;;;OAeG;6BACsB,MAAM,SAAS,MAAM,GAAG,OAAO;IAIxD;;;;;;;;;;;;;OAaG;yBACkB,MAAM,QAAQ,MAAM,GAAG,OAAO;IAOnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;aAEI,MAAM,UACH,MAAM,cACF,OAAO,GAClB,OAAO,CAAC,OAAO,CAAC;IAInB;;;;;;;;;;;;;;;;;;;OAmBG;aACY,MAAM,cAAc,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAK7D;;;;;;;;;;;;OAYG;yBACwB,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIrD;;;;;;;;;;;;;;;;;;;;;OAqBG;yBACwB,MAAM,SAAS,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAInE;;;;;;;;;;;;;;;;;;OAkBG;yBACwB,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAKpD;;;;;;;;;;;;;;;;;;;;;;OAsBG;uBACsB,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAKnD;;;;;;;;;;;;;;;;;;;;;OAqBG;qBAEI,MAAM,QACL,MAAM,cACA,OAAO,GAClB,OAAO,CAAC,OAAO,CAAC;IAInB;;;;;;;;;;;;;;;;;;;;OAoBG;aACY,OAAO,CAAC,IAAI,CAAC;IAI5B;;;;;;;;;;;;;;;;;;;;;OAqBG;4BAC2B,OAAO,CAAC,OAAO,CAAC;CAG/C,CAAC;AAGF,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,CAAC;AAG9D,eAAe,YAAY,CAAC"}
@@ -10,13 +10,13 @@
10
10
  // Forward declaration of `Cookie` to properly resolve imports.
11
11
  namespace margelo::nitro::nitrocookies { struct Cookie; }
12
12
 
13
- #include <NitroModules/Promise.hpp>
14
- #include <NitroModules/JPromise.hpp>
15
13
  #include "Cookie.hpp"
16
14
  #include <vector>
17
15
  #include "JCookie.hpp"
18
16
  #include <string>
19
17
  #include <optional>
18
+ #include <NitroModules/Promise.hpp>
19
+ #include <NitroModules/JPromise.hpp>
20
20
 
21
21
  namespace margelo::nitro::nitrocookies {
22
22
 
@@ -50,6 +50,35 @@ namespace margelo::nitro::nitrocookies {
50
50
 
51
51
 
52
52
  // Methods
53
+ std::vector<Cookie> JHybridNitroCookiesSpec::getSync(const std::string& url) {
54
+ static const auto method = javaClassStatic()->getMethod<jni::local_ref<jni::JArrayClass<JCookie>>(jni::alias_ref<jni::JString> /* url */)>("getSync");
55
+ auto __result = method(_javaPart, jni::make_jstring(url));
56
+ return [&]() {
57
+ size_t __size = __result->size();
58
+ std::vector<Cookie> __vector;
59
+ __vector.reserve(__size);
60
+ for (size_t __i = 0; __i < __size; __i++) {
61
+ auto __element = __result->getElement(__i);
62
+ __vector.push_back(__element->toCpp());
63
+ }
64
+ return __vector;
65
+ }();
66
+ }
67
+ bool JHybridNitroCookiesSpec::setSync(const std::string& url, const Cookie& cookie) {
68
+ static const auto method = javaClassStatic()->getMethod<jboolean(jni::alias_ref<jni::JString> /* url */, jni::alias_ref<JCookie> /* cookie */)>("setSync");
69
+ auto __result = method(_javaPart, jni::make_jstring(url), JCookie::fromCpp(cookie));
70
+ return static_cast<bool>(__result);
71
+ }
72
+ bool JHybridNitroCookiesSpec::setFromResponseSync(const std::string& url, const std::string& value) {
73
+ static const auto method = javaClassStatic()->getMethod<jboolean(jni::alias_ref<jni::JString> /* url */, jni::alias_ref<jni::JString> /* value */)>("setFromResponseSync");
74
+ auto __result = method(_javaPart, jni::make_jstring(url), jni::make_jstring(value));
75
+ return static_cast<bool>(__result);
76
+ }
77
+ bool JHybridNitroCookiesSpec::clearByNameSync(const std::string& url, const std::string& name) {
78
+ static const auto method = javaClassStatic()->getMethod<jboolean(jni::alias_ref<jni::JString> /* url */, jni::alias_ref<jni::JString> /* name */)>("clearByNameSync");
79
+ auto __result = method(_javaPart, jni::make_jstring(url), jni::make_jstring(name));
80
+ return static_cast<bool>(__result);
81
+ }
53
82
  std::shared_ptr<Promise<bool>> JHybridNitroCookiesSpec::set(const std::string& url, const Cookie& cookie, std::optional<bool> useWebKit) {
54
83
  static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* url */, jni::alias_ref<JCookie> /* cookie */, jni::alias_ref<jni::JBoolean> /* useWebKit */)>("set");
55
84
  auto __result = method(_javaPart, jni::make_jstring(url), JCookie::fromCpp(cookie), useWebKit.has_value() ? jni::JBoolean::valueOf(useWebKit.value()) : nullptr);
@@ -54,6 +54,10 @@ namespace margelo::nitro::nitrocookies {
54
54
 
55
55
  public:
56
56
  // Methods
57
+ std::vector<Cookie> getSync(const std::string& url) override;
58
+ bool setSync(const std::string& url, const Cookie& cookie) override;
59
+ bool setFromResponseSync(const std::string& url, const std::string& value) override;
60
+ bool clearByNameSync(const std::string& url, const std::string& name) override;
57
61
  std::shared_ptr<Promise<bool>> set(const std::string& url, const Cookie& cookie, std::optional<bool> useWebKit) override;
58
62
  std::shared_ptr<Promise<std::vector<Cookie>>> get(const std::string& url, std::optional<bool> useWebKit) override;
59
63
  std::shared_ptr<Promise<bool>> clearAll(std::optional<bool> useWebKit) override;
@@ -46,6 +46,22 @@ abstract class HybridNitroCookiesSpec: HybridObject() {
46
46
 
47
47
 
48
48
  // Methods
49
+ @DoNotStrip
50
+ @Keep
51
+ abstract fun getSync(url: String): Array<Cookie>
52
+
53
+ @DoNotStrip
54
+ @Keep
55
+ abstract fun setSync(url: String, cookie: Cookie): Boolean
56
+
57
+ @DoNotStrip
58
+ @Keep
59
+ abstract fun setFromResponseSync(url: String, value: String): Boolean
60
+
61
+ @DoNotStrip
62
+ @Keep
63
+ abstract fun clearByNameSync(url: String, name: String): Boolean
64
+
49
65
  @DoNotStrip
50
66
  @Keep
51
67
  abstract fun set(url: String, cookie: Cookie, useWebKit: Boolean?): Promise<Boolean>
@@ -36,6 +36,47 @@ namespace NitroCookies { class HybridNitroCookiesSpec_cxx; }
36
36
  */
37
37
  namespace margelo::nitro::nitrocookies::bridge::swift {
38
38
 
39
+ // pragma MARK: std::optional<std::string>
40
+ /**
41
+ * Specialized version of `std::optional<std::string>`.
42
+ */
43
+ using std__optional_std__string_ = std::optional<std::string>;
44
+ inline std::optional<std::string> create_std__optional_std__string_(const std::string& value) noexcept {
45
+ return std::optional<std::string>(value);
46
+ }
47
+ inline bool has_value_std__optional_std__string_(const std::optional<std::string>& optional) noexcept {
48
+ return optional.has_value();
49
+ }
50
+ inline std::string get_std__optional_std__string_(const std::optional<std::string>& optional) noexcept {
51
+ return *optional;
52
+ }
53
+
54
+ // pragma MARK: std::optional<bool>
55
+ /**
56
+ * Specialized version of `std::optional<bool>`.
57
+ */
58
+ using std__optional_bool_ = std::optional<bool>;
59
+ inline std::optional<bool> create_std__optional_bool_(const bool& value) noexcept {
60
+ return std::optional<bool>(value);
61
+ }
62
+ inline bool has_value_std__optional_bool_(const std::optional<bool>& optional) noexcept {
63
+ return optional.has_value();
64
+ }
65
+ inline bool get_std__optional_bool_(const std::optional<bool>& optional) noexcept {
66
+ return *optional;
67
+ }
68
+
69
+ // pragma MARK: std::vector<Cookie>
70
+ /**
71
+ * Specialized version of `std::vector<Cookie>`.
72
+ */
73
+ using std__vector_Cookie_ = std::vector<Cookie>;
74
+ inline std::vector<Cookie> create_std__vector_Cookie_(size_t size) noexcept {
75
+ std::vector<Cookie> vector;
76
+ vector.reserve(size);
77
+ return vector;
78
+ }
79
+
39
80
  // pragma MARK: std::shared_ptr<Promise<bool>>
40
81
  /**
41
82
  * Specialized version of `std::shared_ptr<Promise<bool>>`.
@@ -92,47 +133,6 @@ namespace margelo::nitro::nitrocookies::bridge::swift {
92
133
  return Func_void_std__exception_ptr_Wrapper(std::move(value));
93
134
  }
94
135
 
95
- // pragma MARK: std::optional<std::string>
96
- /**
97
- * Specialized version of `std::optional<std::string>`.
98
- */
99
- using std__optional_std__string_ = std::optional<std::string>;
100
- inline std::optional<std::string> create_std__optional_std__string_(const std::string& value) noexcept {
101
- return std::optional<std::string>(value);
102
- }
103
- inline bool has_value_std__optional_std__string_(const std::optional<std::string>& optional) noexcept {
104
- return optional.has_value();
105
- }
106
- inline std::string get_std__optional_std__string_(const std::optional<std::string>& optional) noexcept {
107
- return *optional;
108
- }
109
-
110
- // pragma MARK: std::optional<bool>
111
- /**
112
- * Specialized version of `std::optional<bool>`.
113
- */
114
- using std__optional_bool_ = std::optional<bool>;
115
- inline std::optional<bool> create_std__optional_bool_(const bool& value) noexcept {
116
- return std::optional<bool>(value);
117
- }
118
- inline bool has_value_std__optional_bool_(const std::optional<bool>& optional) noexcept {
119
- return optional.has_value();
120
- }
121
- inline bool get_std__optional_bool_(const std::optional<bool>& optional) noexcept {
122
- return *optional;
123
- }
124
-
125
- // pragma MARK: std::vector<Cookie>
126
- /**
127
- * Specialized version of `std::vector<Cookie>`.
128
- */
129
- using std__vector_Cookie_ = std::vector<Cookie>;
130
- inline std::vector<Cookie> create_std__vector_Cookie_(size_t size) noexcept {
131
- std::vector<Cookie> vector;
132
- vector.reserve(size);
133
- return vector;
134
- }
135
-
136
136
  // pragma MARK: std::shared_ptr<Promise<std::vector<Cookie>>>
137
137
  /**
138
138
  * Specialized version of `std::shared_ptr<Promise<std::vector<Cookie>>>`.
@@ -213,6 +213,24 @@ namespace margelo::nitro::nitrocookies::bridge::swift {
213
213
  using std__weak_ptr_HybridNitroCookiesSpec_ = std::weak_ptr<HybridNitroCookiesSpec>;
214
214
  inline std__weak_ptr_HybridNitroCookiesSpec_ weakify_std__shared_ptr_HybridNitroCookiesSpec_(const std::shared_ptr<HybridNitroCookiesSpec>& strong) noexcept { return strong; }
215
215
 
216
+ // pragma MARK: Result<std::vector<Cookie>>
217
+ using Result_std__vector_Cookie__ = Result<std::vector<Cookie>>;
218
+ inline Result_std__vector_Cookie__ create_Result_std__vector_Cookie__(const std::vector<Cookie>& value) noexcept {
219
+ return Result<std::vector<Cookie>>::withValue(value);
220
+ }
221
+ inline Result_std__vector_Cookie__ create_Result_std__vector_Cookie__(const std::exception_ptr& error) noexcept {
222
+ return Result<std::vector<Cookie>>::withError(error);
223
+ }
224
+
225
+ // pragma MARK: Result<bool>
226
+ using Result_bool_ = Result<bool>;
227
+ inline Result_bool_ create_Result_bool_(bool value) noexcept {
228
+ return Result<bool>::withValue(std::move(value));
229
+ }
230
+ inline Result_bool_ create_Result_bool_(const std::exception_ptr& error) noexcept {
231
+ return Result<bool>::withError(error);
232
+ }
233
+
216
234
  // pragma MARK: Result<std::shared_ptr<Promise<bool>>>
217
235
  using Result_std__shared_ptr_Promise_bool___ = Result<std::shared_ptr<Promise<bool>>>;
218
236
  inline Result_std__shared_ptr_Promise_bool___ create_Result_std__shared_ptr_Promise_bool___(const std::shared_ptr<Promise<bool>>& value) noexcept {
@@ -15,11 +15,11 @@ namespace NitroCookies { class HybridNitroCookiesSpec_cxx; }
15
15
  // Forward declaration of `Cookie` to properly resolve imports.
16
16
  namespace margelo::nitro::nitrocookies { struct Cookie; }
17
17
 
18
- #include <NitroModules/Promise.hpp>
19
- #include <string>
20
18
  #include "Cookie.hpp"
21
- #include <optional>
22
19
  #include <vector>
20
+ #include <string>
21
+ #include <optional>
22
+ #include <NitroModules/Promise.hpp>
23
23
 
24
24
  #include "NitroCookies-Swift-Cxx-Umbrella.hpp"
25
25
 
@@ -65,6 +65,38 @@ namespace margelo::nitro::nitrocookies {
65
65
 
66
66
  public:
67
67
  // Methods
68
+ inline std::vector<Cookie> getSync(const std::string& url) override {
69
+ auto __result = _swiftPart.getSync(url);
70
+ if (__result.hasError()) [[unlikely]] {
71
+ std::rethrow_exception(__result.error());
72
+ }
73
+ auto __value = std::move(__result.value());
74
+ return __value;
75
+ }
76
+ inline bool setSync(const std::string& url, const Cookie& cookie) override {
77
+ auto __result = _swiftPart.setSync(url, std::forward<decltype(cookie)>(cookie));
78
+ if (__result.hasError()) [[unlikely]] {
79
+ std::rethrow_exception(__result.error());
80
+ }
81
+ auto __value = std::move(__result.value());
82
+ return __value;
83
+ }
84
+ inline bool setFromResponseSync(const std::string& url, const std::string& value) override {
85
+ auto __result = _swiftPart.setFromResponseSync(url, value);
86
+ if (__result.hasError()) [[unlikely]] {
87
+ std::rethrow_exception(__result.error());
88
+ }
89
+ auto __value = std::move(__result.value());
90
+ return __value;
91
+ }
92
+ inline bool clearByNameSync(const std::string& url, const std::string& name) override {
93
+ auto __result = _swiftPart.clearByNameSync(url, name);
94
+ if (__result.hasError()) [[unlikely]] {
95
+ std::rethrow_exception(__result.error());
96
+ }
97
+ auto __value = std::move(__result.value());
98
+ return __value;
99
+ }
68
100
  inline std::shared_ptr<Promise<bool>> set(const std::string& url, const Cookie& cookie, std::optional<bool> useWebKit) override {
69
101
  auto __result = _swiftPart.set(url, std::forward<decltype(cookie)>(cookie), useWebKit);
70
102
  if (__result.hasError()) [[unlikely]] {
@@ -15,6 +15,10 @@ public protocol HybridNitroCookiesSpec_protocol: HybridObject {
15
15
 
16
16
 
17
17
  // Methods
18
+ func getSync(url: String) throws -> [Cookie]
19
+ func setSync(url: String, cookie: Cookie) throws -> Bool
20
+ func setFromResponseSync(url: String, value: String) throws -> Bool
21
+ func clearByNameSync(url: String, name: String) throws -> Bool
18
22
  func set(url: String, cookie: Cookie, useWebKit: Bool?) throws -> Promise<Bool>
19
23
  func get(url: String, useWebKit: Bool?) throws -> Promise<[Cookie]>
20
24
  func clearAll(useWebKit: Bool?) throws -> Promise<Bool>
@@ -118,6 +118,60 @@ open class HybridNitroCookiesSpec_cxx {
118
118
 
119
119
 
120
120
  // Methods
121
+ @inline(__always)
122
+ public final func getSync(url: std.string) -> bridge.Result_std__vector_Cookie__ {
123
+ do {
124
+ let __result = try self.__implementation.getSync(url: String(url))
125
+ let __resultCpp = { () -> bridge.std__vector_Cookie_ in
126
+ var __vector = bridge.create_std__vector_Cookie_(__result.count)
127
+ for __item in __result {
128
+ __vector.push_back(__item)
129
+ }
130
+ return __vector
131
+ }()
132
+ return bridge.create_Result_std__vector_Cookie__(__resultCpp)
133
+ } catch (let __error) {
134
+ let __exceptionPtr = __error.toCpp()
135
+ return bridge.create_Result_std__vector_Cookie__(__exceptionPtr)
136
+ }
137
+ }
138
+
139
+ @inline(__always)
140
+ public final func setSync(url: std.string, cookie: Cookie) -> bridge.Result_bool_ {
141
+ do {
142
+ let __result = try self.__implementation.setSync(url: String(url), cookie: cookie)
143
+ let __resultCpp = __result
144
+ return bridge.create_Result_bool_(__resultCpp)
145
+ } catch (let __error) {
146
+ let __exceptionPtr = __error.toCpp()
147
+ return bridge.create_Result_bool_(__exceptionPtr)
148
+ }
149
+ }
150
+
151
+ @inline(__always)
152
+ public final func setFromResponseSync(url: std.string, value: std.string) -> bridge.Result_bool_ {
153
+ do {
154
+ let __result = try self.__implementation.setFromResponseSync(url: String(url), value: String(value))
155
+ let __resultCpp = __result
156
+ return bridge.create_Result_bool_(__resultCpp)
157
+ } catch (let __error) {
158
+ let __exceptionPtr = __error.toCpp()
159
+ return bridge.create_Result_bool_(__exceptionPtr)
160
+ }
161
+ }
162
+
163
+ @inline(__always)
164
+ public final func clearByNameSync(url: std.string, name: std.string) -> bridge.Result_bool_ {
165
+ do {
166
+ let __result = try self.__implementation.clearByNameSync(url: String(url), name: String(name))
167
+ let __resultCpp = __result
168
+ return bridge.create_Result_bool_(__resultCpp)
169
+ } catch (let __error) {
170
+ let __exceptionPtr = __error.toCpp()
171
+ return bridge.create_Result_bool_(__exceptionPtr)
172
+ }
173
+ }
174
+
121
175
  @inline(__always)
122
176
  public final func set(url: std.string, cookie: Cookie, useWebKit: bridge.std__optional_bool_) -> bridge.Result_std__shared_ptr_Promise_bool___ {
123
177
  do {
@@ -14,6 +14,10 @@ namespace margelo::nitro::nitrocookies {
14
14
  HybridObject::loadHybridMethods();
15
15
  // load custom methods/properties
16
16
  registerHybrids(this, [](Prototype& prototype) {
17
+ prototype.registerHybridMethod("getSync", &HybridNitroCookiesSpec::getSync);
18
+ prototype.registerHybridMethod("setSync", &HybridNitroCookiesSpec::setSync);
19
+ prototype.registerHybridMethod("setFromResponseSync", &HybridNitroCookiesSpec::setFromResponseSync);
20
+ prototype.registerHybridMethod("clearByNameSync", &HybridNitroCookiesSpec::clearByNameSync);
17
21
  prototype.registerHybridMethod("set", &HybridNitroCookiesSpec::set);
18
22
  prototype.registerHybridMethod("get", &HybridNitroCookiesSpec::get);
19
23
  prototype.registerHybridMethod("clearAll", &HybridNitroCookiesSpec::clearAll);
@@ -16,11 +16,11 @@
16
16
  // Forward declaration of `Cookie` to properly resolve imports.
17
17
  namespace margelo::nitro::nitrocookies { struct Cookie; }
18
18
 
19
- #include <NitroModules/Promise.hpp>
20
- #include <string>
21
19
  #include "Cookie.hpp"
22
- #include <optional>
23
20
  #include <vector>
21
+ #include <string>
22
+ #include <NitroModules/Promise.hpp>
23
+ #include <optional>
24
24
 
25
25
  namespace margelo::nitro::nitrocookies {
26
26
 
@@ -53,6 +53,10 @@ namespace margelo::nitro::nitrocookies {
53
53
 
54
54
  public:
55
55
  // Methods
56
+ virtual std::vector<Cookie> getSync(const std::string& url) = 0;
57
+ virtual bool setSync(const std::string& url, const Cookie& cookie) = 0;
58
+ virtual bool setFromResponseSync(const std::string& url, const std::string& value) = 0;
59
+ virtual bool clearByNameSync(const std::string& url, const std::string& name) = 0;
56
60
  virtual std::shared_ptr<Promise<bool>> set(const std::string& url, const Cookie& cookie, std::optional<bool> useWebKit) = 0;
57
61
  virtual std::shared_ptr<Promise<std::vector<Cookie>>> get(const std::string& url, std::optional<bool> useWebKit) = 0;
58
62
  virtual std::shared_ptr<Promise<bool>> clearAll(std::optional<bool> useWebKit) = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-cookies",
3
- "version": "0.0.1",
3
+ "version": "1.0.0",
4
4
  "description": "Fetch, Get Cookies 🍪 at the Speed of Nitro",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -18,9 +18,65 @@ import type { Cookie } from './types';
18
18
  * Uses Nitro Modules JSI architecture to achieve 5x+ faster operations compared
19
19
  * to traditional React Native bridge-based implementations.
20
20
  *
21
+ * API Design:
22
+ * - Synchronous methods (xxxSync): Direct return values, no Promise wrapping
23
+ * - Asynchronous methods: Return Promise for operations requiring callbacks/network
21
24
  */
22
25
  export interface NitroCookies
23
26
  extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
27
+ // ========================================
28
+ // SYNCHRONOUS METHODS
29
+ // ========================================
30
+
31
+ /**
32
+ * Get cookies synchronously for a URL
33
+ *
34
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
35
+ * Does NOT support WebKit cookie store (use async `get` with `useWebKit: true`).
36
+ *
37
+ * @param url - The URL to match cookies against (must include protocol)
38
+ * @returns Array of cookies matching the URL domain
39
+ * @throws Error if URL is invalid
40
+ */
41
+ getSync(url: string): Cookie[];
42
+
43
+ /**
44
+ * Set a cookie synchronously
45
+ *
46
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
47
+ * Does NOT support WebKit cookie store (use async `set` with `useWebKit: true`).
48
+ *
49
+ * @param url - The URL for which to set the cookie (must include protocol)
50
+ * @param cookie - The cookie object to store
51
+ * @returns true on success
52
+ * @throws Error if URL is invalid or domain mismatch
53
+ */
54
+ setSync(url: string, cookie: Cookie): boolean;
55
+
56
+ /**
57
+ * Parse and set cookies from Set-Cookie header synchronously
58
+ *
59
+ * @param url - The URL associated with the Set-Cookie header
60
+ * @param value - The raw Set-Cookie header value
61
+ * @returns true on success
62
+ * @throws Error if URL is invalid
63
+ */
64
+ setFromResponseSync(url: string, value: string): boolean;
65
+
66
+ /**
67
+ * Clear a specific cookie by name synchronously
68
+ *
69
+ * @param url - The URL to match the cookie domain
70
+ * @param name - The name of the cookie to remove
71
+ * @returns true if cookie was found and removed, false if not found
72
+ * @throws Error if URL is invalid
73
+ */
74
+ clearByNameSync(url: string, name: string): boolean;
75
+
76
+ // ========================================
77
+ // ASYNCHRONOUS METHODS
78
+ // ========================================
79
+
24
80
  /**
25
81
  * Set a single cookie for a specific URL
26
82
  *
package/src/index.tsx CHANGED
@@ -16,22 +16,116 @@ function cookiesToDictionary(cookies: Cookie[]): Cookies {
16
16
  /**
17
17
  * Main NitroCookies export object with all cookie management methods.
18
18
  *
19
+ * Supports both synchronous and asynchronous APIs:
20
+ * - Synchronous methods (getSync, setSync, etc.): Direct return values, no Promise overhead
21
+ * - Asynchronous methods (get, set, etc.): Return Promises for WebKit and network operations
22
+ *
19
23
  * @example
20
24
  * ```typescript
21
25
  * import NitroCookies from 'react-native-nitro-cookies';
22
26
  *
23
- * // Set a cookie
24
- * await NitroCookies.set('https://example.com', {
25
- * name: 'session',
26
- * value: 'abc123',
27
- * secure: true,
28
- * });
27
+ * // Synchronous API (no await needed!)
28
+ * const cookies = NitroCookies.getSync('https://example.com');
29
+ * NitroCookies.setSync('https://example.com', { name: 'session', value: 'abc123' });
29
30
  *
30
- * // Get cookies
31
- * const cookies = await NitroCookies.get('https://example.com');
31
+ * // Asynchronous API (for WebKit/network operations)
32
+ * const cookies = await NitroCookies.get('https://example.com', true); // useWebKit
32
33
  * ```
33
34
  */
34
35
  export const NitroCookies = {
36
+ // ========================================
37
+ // SYNCHRONOUS METHODS
38
+ // ========================================
39
+
40
+ /**
41
+ * Get cookies synchronously for a URL.
42
+ *
43
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
44
+ * Does NOT support WebKit cookie store (use async `get` with `useWebKit: true`).
45
+ *
46
+ * @param url - The URL to match cookies against (must include protocol)
47
+ * @returns Dictionary of cookies keyed by name
48
+ * @throws {Error} INVALID_URL - URL is malformed or missing protocol
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * // No await needed!
53
+ * const cookies = NitroCookies.getSync('https://example.com');
54
+ * console.log(cookies); // { session: { name: 'session', value: 'abc123', ... } }
55
+ * ```
56
+ */
57
+ getSync(url: string): Cookies {
58
+ return cookiesToDictionary(NitroCookiesHybridObject.getSync(url));
59
+ },
60
+
61
+ /**
62
+ * Set a cookie synchronously.
63
+ *
64
+ * Uses NSHTTPCookieStorage (iOS) or CookieManager (Android).
65
+ * Does NOT support WebKit cookie store (use async `set` with `useWebKit: true`).
66
+ *
67
+ * @param url - The URL for which to set the cookie (must include protocol)
68
+ * @param cookie - The cookie object to store
69
+ * @returns true on success
70
+ * @throws {Error} INVALID_URL - URL is malformed or missing protocol
71
+ * @throws {Error} DOMAIN_MISMATCH - Cookie domain doesn't match URL host
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * // No await needed!
76
+ * NitroCookies.setSync('https://example.com', {
77
+ * name: 'session',
78
+ * value: 'abc123',
79
+ * path: '/',
80
+ * secure: true,
81
+ * });
82
+ * ```
83
+ */
84
+ setSync(url: string, cookie: Cookie): boolean {
85
+ return NitroCookiesHybridObject.setSync(url, cookie);
86
+ },
87
+
88
+ /**
89
+ * Parse and set cookies from Set-Cookie header synchronously.
90
+ *
91
+ * @param url - The URL associated with the Set-Cookie header
92
+ * @param value - The raw Set-Cookie header value
93
+ * @returns true on success
94
+ * @throws {Error} INVALID_URL - URL is malformed
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * NitroCookies.setFromResponseSync(
99
+ * 'https://example.com',
100
+ * 'session=abc123; path=/; secure; HttpOnly'
101
+ * );
102
+ * ```
103
+ */
104
+ setFromResponseSync(url: string, value: string): boolean {
105
+ return NitroCookiesHybridObject.setFromResponseSync(url, value);
106
+ },
107
+
108
+ /**
109
+ * Clear a specific cookie by name synchronously.
110
+ *
111
+ * @param url - The URL to match the cookie domain
112
+ * @param name - The name of the cookie to remove
113
+ * @returns true if cookie was found and removed, false if not found
114
+ * @throws {Error} INVALID_URL - URL is malformed
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * const removed = NitroCookies.clearByNameSync('https://example.com', 'session');
119
+ * console.log(removed ? 'Cookie removed' : 'Cookie not found');
120
+ * ```
121
+ */
122
+ clearByNameSync(url: string, name: string): boolean {
123
+ return NitroCookiesHybridObject.clearByNameSync(url, name);
124
+ },
125
+
126
+ // ========================================
127
+ // ASYNCHRONOUS METHODS
128
+ // ========================================
35
129
  /**
36
130
  * Set a single cookie for a specific URL.
37
131
  *