From 5006162bf754d9973e518d55083b0426f14ab0ff Mon Sep 17 00:00:00 2001 From: Ahsan Ali Date: Thu, 12 Nov 2026 23:51:01 +0000 Subject: [PATCH 1/2] Parity with native SDK 0.2.0 Pin LinkTrailSDK ~> 0.2.0 and io.linktrail:sdk:0.2.0, add the invalidEvent error code with its reason on both bridges, report the wrapper in the iOS SDK header, and remove the deprecated legacyCode. Version 0.2.0. --- CHANGELOG.md | 21 ++++ LinktrailReactNative.podspec | 2 +- README.md | 12 +- __tests__/index.test.ts | 2 - __tests__/native-bridges.test.ts | 106 ++++++++++++++++++ __tests__/parity.test.ts | 105 ++++++++++++----- android/build.gradle | 2 +- .../linktrail/reactnative/LinkTrailModule.kt | 34 +++--- ios/LinkTrailModuleImpl.swift | 29 +++-- package-lock.json | 4 +- package.json | 2 +- src/NativeLinkTrail.ts | 2 +- src/errors.ts | 76 +++++-------- src/index.ts | 7 +- src/types.ts | 4 +- src/version.ts | 2 +- 16 files changed, 295 insertions(+), 115 deletions(-) create mode 100644 __tests__/native-bridges.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 923af9c..9a1ecc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,26 @@ # Changelog +## 0.2.0 + +Adopts the native **0.2.0** SDKs. All four LinkTrail SDKs (iOS, Android, React Native, Flutter) +now share one version. + +### Changed +- Native pins: iOS `LinkTrailSDK ~> 0.2.0` (from `~> 0.1.0`) and Android `io.linktrail:sdk:0.2.0` + (from `0.1.0`). The example app builds against the same pins. +- iOS now reports the wrapper in the `X-LinkTrail-SDK` header (`react-native/0.2.0`), as Android + already did. + +### Added +- Error code `invalidEvent`, with the rejected `reason` (string) on the error and on `onError` + events. `trackEvent` reports it when the name is blank, the `value` is NaN, infinite or + negative, or the `currency` isn't 3 letters, and sends nothing. The message is the native one + (`Invalid event: `). + +### Removed +- **Breaking:** `error.legacyCode` and the `LinkTrailLegacyErrorCode` type (deprecated in 0.1.1). + Use `error.code`. Snake_case codes from native are no longer translated; they map to `unknown`. + ## 0.1.1 Implements [wrapper parity 0.1.1](https://github.com/linktrail-io/workspace/blob/main/contracts/wrapper-parity-0.1.1.md). diff --git a/LinktrailReactNative.podspec b/LinktrailReactNative.podspec index 431c30f..bf4a3ab 100644 --- a/LinktrailReactNative.podspec +++ b/LinktrailReactNative.podspec @@ -18,7 +18,7 @@ Pod::Spec.new do |s| # The binary LinkTrail iOS SDK, published to the CocoaPods trunk — CocoaPods # resolves it automatically, so the consuming app needs no extra Podfile line. - s.dependency "LinkTrailSDK", "~> 0.1.0" + s.dependency "LinkTrailSDK", "~> 0.2.0" s.pod_target_xcconfig = { "DEFINES_MODULE" => "YES", diff --git a/README.md b/README.md index 1dda21b..1093945 100644 --- a/README.md +++ b/README.md @@ -79,10 +79,14 @@ Every promise rejects with a `LinkTrailError`, and `onError` gets the same field | Field | | | --- | --- | -| `code` | `missingApiKey`, `invalidApiKey`, `notConfigured`, `invalidUrl`, `notALinkTrailUrl`, `transport`, `server`, `decoding`, `emptyResponse`, `invalidArgument`, `unknown` | +| `code` | `missingApiKey`, `invalidApiKey`, `notConfigured`, `invalidUrl`, `notALinkTrailUrl`, `transport`, `server`, `decoding`, `emptyResponse`, `invalidArgument`, `invalidEvent`, `unknown` | | `message` | The native SDK's message, unchanged | | `statusCode`, `body` | `server` errors only (`body` when the server sent one) | -| `legacyCode` | The pre-0.1.1 snake_case code. Deprecated; removed in the next minor release | +| `reason` | `invalidEvent` errors only: why the event was rejected, e.g. `name is blank` | + +`trackEvent` rejects with `invalidEvent` (and reports it to `onError`) when the name is blank, the +`value` is NaN, infinite or negative, or the `currency` isn't 3 letters. Nothing is sent. The rules +come from the native SDK; this package adds none of its own. ```ts try { @@ -103,8 +107,8 @@ const attribution = await LinkTrail.getLastAttribution(); const lastLink = await LinkTrail.getLastDeepLink(); // Versions: this package, and the embedded native SDK. -LinkTrail.version; // "0.1.1" -LinkTrail.nativeSdkVersion; // e.g. "0.1.0" +LinkTrail.version; // "0.2.0" +LinkTrail.nativeSdkVersion; // e.g. "0.2.0" // iOS ATT / SKAdNetwork. On Android there is no ATT: requestTrackingAuthorization() // is not applicable and resolves `true` (the OS does not restrict tracking); the diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index ccfaa84..ec069c6 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -111,7 +111,6 @@ describe('configure', () => { mockNative.configure.mockRejectedValue(err); await expect(api.LinkTrail.configure('')).rejects.toMatchObject({ code: 'missingApiKey', - legacyCode: 'missing_api_key', message: 'blank', }); expect(Linking.addEventListener).not.toHaveBeenCalled(); @@ -220,7 +219,6 @@ describe('event emitters', () => { emit({ code: 'transport', message: 'offline' }); expect(listener).toHaveBeenCalledWith({ code: 'transport', - legacyCode: 'transport', message: 'offline', }); s.remove(); diff --git a/__tests__/native-bridges.test.ts b/__tests__/native-bridges.test.ts new file mode 100644 index 0000000..186f693 --- /dev/null +++ b/__tests__/native-bridges.test.ts @@ -0,0 +1,106 @@ +/* eslint-env jest */ +// Source checks for the native bridges (Kotlin / Swift), which Jest can't run: error-code +// mapping, error details, the iOS SDK header call, and the native pins. +import { readFileSync } from 'fs'; +import { join } from 'path'; + +const read = (path: string) => + readFileSync(join(__dirname, '..', path), 'utf8'); +const pkg = require('../package.json') as { version: string }; + +const kotlin = read( + 'android/src/main/java/io/linktrail/reactnative/LinkTrailModule.kt', +); +const swift = read('ios/LinkTrailModuleImpl.swift'); + +/** The body of a Kotlin `fun name(...)` or Swift `func name(...)`, up to the next declaration. */ +function body(source: string, name: string): string { + const start = source.search(new RegExp(`\\bfunc? ${name}\\(`)); + expect(start).toBeGreaterThanOrEqual(0); + const rest = source.slice(start + 1); + const next = rest.search( + /\n {2}(?:@objc |private |public |override |static )*(?:func?|val|var) /, + ); + return next < 0 ? rest : rest.slice(0, next); +} + +describe('Android bridge', () => { + it('maps LinkTrailError.InvalidEvent to invalidEvent, before the unknown fallback', () => { + const mapping = body(kotlin, 'errorCode'); + expect(mapping).toMatch( + /is LinkTrailError\.InvalidEvent -> "invalidEvent"/, + ); + expect(mapping.indexOf('InvalidEvent')).toBeLessThan( + mapping.indexOf('else -> "unknown"'), + ); + }); + + it('passes reason for InvalidEvent in the error details (reject + onError)', () => { + const details = body(kotlin, 'errorDetails'); + expect(details).toMatch( + /is LinkTrailError\.InvalidEvent -> [\s\S]*putString\("reason", error\.reason\)/, + ); + expect(body(kotlin, 'reject')).toContain('errorDetails(error)'); + expect(body(kotlin, 'configure')).toContain('errorDetails(error)'); + }); + + it('sets the wrapper platform before configure', () => { + const configure = body(kotlin, 'configure'); + const header = configure.indexOf( + 'LinkTrailWrapper.setPlatform("react-native", BuildConfig.LINKTRAIL_RN_VERSION)', + ); + expect(header).toBeGreaterThanOrEqual(0); + expect(header).toBeLessThan(configure.indexOf('LinkTrail.configure(')); + }); + + it('pins io.linktrail:sdk:0.2.0 exactly', () => { + expect(read('android/build.gradle')).toMatch( + /implementation "io\.linktrail:sdk:0\.2\.0"/, + ); + }); +}); + +describe('iOS bridge', () => { + it('maps .invalidEvent to invalidEvent', () => { + expect(body(swift, 'errorCode')).toMatch( + /case \.invalidEvent: return "invalidEvent"/, + ); + }); + + it('passes reason for .invalidEvent in the error user info (reject + onError)', () => { + expect(body(swift, 'errorUserInfo')).toMatch( + /case let \.invalidEvent\(reason\)\?:\s*return \["reason": reason\]/, + ); + expect(body(swift, 'errorPayload')).toContain('errorUserInfo(error)'); + }); + + it('calls setSDKIdentifier(platform: "react-native") before configure', () => { + const configure = body(swift, 'configure'); + const header = configure.indexOf( + 'LinkTrailSDK.LinkTrail.setSDKIdentifier(platform: "react-native", version: Self.wrapperVersion)', + ); + expect(header).toBeGreaterThanOrEqual(0); + expect(header).toBeLessThan( + configure.indexOf('LinkTrailSDK.LinkTrail.configure('), + ); + }); + + it('sends the package.json version in the header', () => { + expect(swift).toContain(`static let wrapperVersion = "${pkg.version}"`); + }); + + it('pins LinkTrailSDK ~> 0.2.0', () => { + expect(read('LinktrailReactNative.podspec')).toMatch( + /s\.dependency "LinkTrailSDK", "~> 0\.2\.0"/, + ); + }); +}); + +describe('legacyCode removal', () => { + it.each(['src/errors.ts', 'src/index.ts', 'src/types.ts', 'README.md'])( + '%s no longer mentions legacyCode', + (path) => { + expect(read(path)).not.toMatch(/legacyCode|LinkTrailLegacyErrorCode/); + }, + ); +}); diff --git a/__tests__/parity.test.ts b/__tests__/parity.test.ts index b5b443d..cf861e0 100644 --- a/__tests__/parity.test.ts +++ b/__tests__/parity.test.ts @@ -1,5 +1,5 @@ /* eslint-env jest */ -// wrapper-parity 0.1.1: link/attribution buffering, error codes, version. +// Wrapper parity: link/attribution buffering, error codes (incl. invalidEvent), version. import { Linking } from 'react-native'; type Listener = (payload: unknown) => void; @@ -193,7 +193,7 @@ describe('onAttribution replay', () => { }); describe('error codes', () => { - it('rejects with a LinkTrailError: camelCase code, legacyCode, native message', async () => { + it('rejects with a LinkTrailError: camelCase code, native message', async () => { mockNative.trackEvent.mockRejectedValue( Object.assign(new Error('Server returned status 500: boom'), { code: 'server', @@ -204,7 +204,6 @@ describe('error codes', () => { expect(err).toBeInstanceOf(api.LinkTrailError); expect(err).toMatchObject({ code: 'server', - legacyCode: 'server', message: 'Server returned status 500: boom', statusCode: 500, body: 'boom', @@ -212,31 +211,48 @@ describe('error codes', () => { }); it.each([ - ['missingApiKey', 'missing_api_key'], - ['invalidApiKey', 'invalid_api_key'], - ['notConfigured', 'not_configured'], - ['invalidUrl', 'invalid_url'], - ['notALinkTrailUrl', 'not_a_linktrail_url'], - ['transport', 'transport'], - ['decoding', 'decoding'], - ['emptyResponse', 'empty_response'], - ['invalidArgument', 'invalid_argument'], - ])( - 'maps %s <-> legacy %s (either form from native)', - async (code, legacy) => { - for (const nativeCode of [code, legacy]) { - mockNative.trackInstall.mockRejectedValueOnce( - Object.assign(new Error('m'), { code: nativeCode }), - ); - await expect(api.LinkTrail.trackInstall()).rejects.toMatchObject({ - code, - legacyCode: legacy, - }); - } + 'missingApiKey', + 'invalidApiKey', + 'notConfigured', + 'invalidUrl', + 'notALinkTrailUrl', + 'transport', + 'decoding', + 'emptyResponse', + 'invalidArgument', + 'invalidEvent', + ])('passes the native code %s through', async (code) => { + mockNative.trackInstall.mockRejectedValueOnce( + Object.assign(new Error('m'), { code }), + ); + await expect(api.LinkTrail.trackInstall()).rejects.toMatchObject({ code }); + }); + + it('has no legacyCode (removed in 0.2.0)', async () => { + mockNative.trackInstall.mockRejectedValueOnce( + Object.assign(new Error('m'), { code: 'transport' }), + ); + const err = await api.LinkTrail.trackInstall().catch((e) => e); + expect(err).not.toHaveProperty('legacyCode'); + const listener = jest.fn(); + api.LinkTrail.onError(listener); + nativeEmit('onError', { code: 'transport', message: 'offline' }); + expect(listener.mock.calls[0][0]).not.toHaveProperty('legacyCode'); + }); + + it.each(['missing_api_key', 'invalid_event', 'E_WHAT'])( + 'maps the unrecognised code %s to unknown', + async (nativeCode) => { + mockNative.trackInstall.mockRejectedValueOnce( + Object.assign(new Error('m'), { code: nativeCode }), + ); + await expect(api.LinkTrail.trackInstall()).rejects.toMatchObject({ + code: 'unknown', + }); }, ); - it('maps unrecognised codes to unknown and omits statusCode/body', async () => { + it('maps unrecognised codes to unknown and omits statusCode/body/reason', async () => { mockNative.handleDeepLink.mockRejectedValue( Object.assign(new Error('weird'), { code: 'E_WHAT' }), ); @@ -244,6 +260,7 @@ describe('error codes', () => { expect(err.code).toBe('unknown'); expect(err).not.toHaveProperty('statusCode'); expect(err).not.toHaveProperty('body'); + expect(err).not.toHaveProperty('reason'); }); it('onError carries statusCode and body for server errors', () => { @@ -256,20 +273,52 @@ describe('error codes', () => { }); expect(listener).toHaveBeenCalledWith({ code: 'server', - legacyCode: 'server', message: 'Server returned status 503', statusCode: 503, }); }); }); +describe('invalidEvent (wrapper parity 0.2.0)', () => { + const message = 'Invalid event: value must not be negative (got -1.0)'; + const reason = 'value must not be negative (got -1.0)'; + + it('rejects trackEvent with code invalidEvent, the native message and reason', async () => { + mockNative.trackEvent.mockRejectedValue( + Object.assign(new Error(message), { + code: 'invalidEvent', + userInfo: { reason }, + }), + ); + const err = await api.LinkTrail.trackEvent('purchase', { value: -1 }).catch( + (e) => e, + ); + expect(err).toBeInstanceOf(api.LinkTrailError); + expect(err).toMatchObject({ code: 'invalidEvent', message, reason }); + expect(err).not.toHaveProperty('statusCode'); + }); + + it('passes the arguments to native unchanged (no wrapper-side validation)', async () => { + mockNative.trackEvent.mockResolvedValue({ attributed: false }); + await api.LinkTrail.trackEvent(' ', { value: NaN, currency: 'usdollar' }); + expect(mockNative.trackEvent).toHaveBeenCalledWith(' ', NaN, 'usdollar'); + }); + + it('onError carries reason for invalidEvent', () => { + const listener = jest.fn(); + api.LinkTrail.onError(listener); + nativeEmit('onError', { code: 'invalidEvent', message, reason }); + expect(listener).toHaveBeenCalledWith({ code: 'invalidEvent', message, reason }); + }); +}); + describe('version', () => { it('LinkTrail.version equals package.json', () => { expect(api.LinkTrail.version).toBe(require('../package.json').version); }); it('nativeSdkVersion comes from native', () => { - mockNative.nativeSdkVersion.mockReturnValue('0.1.0'); - expect(api.LinkTrail.nativeSdkVersion).toBe('0.1.0'); + mockNative.nativeSdkVersion.mockReturnValue('0.2.0'); + expect(api.LinkTrail.nativeSdkVersion).toBe('0.2.0'); }); }); diff --git a/android/build.gradle b/android/build.gradle index 272d9dc..42f0d7d 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -79,6 +79,6 @@ repositories { dependencies { implementation "com.facebook.react:react-android" - implementation "io.linktrail:sdk:0.1.0" + implementation "io.linktrail:sdk:0.2.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0" } diff --git a/android/src/main/java/io/linktrail/reactnative/LinkTrailModule.kt b/android/src/main/java/io/linktrail/reactnative/LinkTrailModule.kt index b174345..7ff0397 100644 --- a/android/src/main/java/io/linktrail/reactnative/LinkTrailModule.kt +++ b/android/src/main/java/io/linktrail/reactnative/LinkTrailModule.kt @@ -49,10 +49,7 @@ class LinkTrailModule(reactContext: ReactApplicationContext) : Arguments.createMap().apply { putString("code", errorCode(error)) putString("message", error.message ?: "Unknown error") - (error as? LinkTrailError.Server)?.let { - putInt("statusCode", it.statusCode) - it.body?.let { body -> putString("body", body) } - } + errorDetails(error)?.let { merge(it) } } ) } @@ -74,7 +71,7 @@ class LinkTrailModule(reactContext: ReactApplicationContext) : LinkTrail.shared?.let(::registerOnLink) } - /** The embedded native LinkTrail Android SDK version, e.g. `"0.1.0"`. */ + /** The embedded native LinkTrail Android SDK version, e.g. `"0.2.0"`. */ override fun nativeSdkVersion(): String = LinkTrail.VERSION private fun registerOnLink(sdk: LinkTrail) { @@ -276,21 +273,30 @@ class LinkTrailModule(reactContext: ReactApplicationContext) : is LinkTrailError.EmptyResponse -> "emptyResponse" is LinkTrailError.InvalidUrl -> "invalidUrl" is LinkTrailError.NotALinkTrailUrl -> "notALinkTrailUrl" + is LinkTrailError.InvalidEvent -> "invalidEvent" else -> "unknown" } - /** Rejects with the camelCase code, the native message unchanged, and statusCode/body for server errors. */ + /** statusCode/body for `server` errors, reason for `invalidEvent` errors; null otherwise. */ + private fun errorDetails(error: Throwable): WritableMap? = when (error) { + is LinkTrailError.Server -> Arguments.createMap().apply { + putInt("statusCode", error.statusCode) + error.body?.let { putString("body", it) } + } + is LinkTrailError.InvalidEvent -> Arguments.createMap().apply { + putString("reason", error.reason) + } + else -> null + } + + /** Rejects with the camelCase code, the native message unchanged, and the error's details. */ private fun reject(promise: Promise, error: Throwable) { - val server = error as? LinkTrailError.Server - if (server == null) { + val details = errorDetails(error) + if (details == null) { promise.reject(errorCode(error), error.message, error) - return - } - val userInfo = Arguments.createMap().apply { - putInt("statusCode", server.statusCode) - server.body?.let { putString("body", it) } + } else { + promise.reject(errorCode(error), error.message, error, details) } - promise.reject(errorCode(error), error.message, error, userInfo) } private fun ReadableMap.booleanOrDefault(key: String, fallback: Boolean): Boolean = diff --git a/ios/LinkTrailModuleImpl.swift b/ios/LinkTrailModuleImpl.swift index 0073e38..d111abc 100644 --- a/ios/LinkTrailModuleImpl.swift +++ b/ios/LinkTrailModuleImpl.swift @@ -7,8 +7,12 @@ import LinkTrailSDK @objc(LinkTrailModuleImpl) public final class LinkTrailModuleImpl: NSObject { + /// This package's version, sent in the `X-LinkTrail-SDK` header. Kept equal to package.json by a test. + static let wrapperVersion = "0.2.0" + public typealias Resolve = (Any?) -> Void - /// `(code, message, userInfo)`; `userInfo` carries `statusCode`/`body` for `server` errors. + /// `(code, message, userInfo)`; `userInfo` carries `statusCode`/`body` for `server` errors and + /// `reason` for `invalidEvent` errors. public typealias Reject = (String, String, NSDictionary?) -> Void /// Event sinks wired up by the ObjC++ module to the codegen emitters. @@ -26,6 +30,9 @@ public final class LinkTrailModuleImpl: NSObject { resolve: @escaping Resolve, reject: @escaping Reject ) { + // Before configure, so the very first request already reports the wrapper: + // `X-LinkTrail-SDK: react-native/`. + LinkTrailSDK.LinkTrail.setSDKIdentifier(platform: "react-native", version: Self.wrapperVersion) do { let sdk = try LinkTrailSDK.LinkTrail.configure(apiKey: apiKey, options: Self.parseOptions(options)) sdk.onAttribution { [weak self] attribution in @@ -51,7 +58,7 @@ public final class LinkTrailModuleImpl: NSObject { if let sdk { registerOnLink(sdk) } } - /// The embedded native LinkTrail iOS SDK version, e.g. `"0.1.0"`. + /// The embedded native LinkTrail iOS SDK version, e.g. `"0.2.0"`. @objc public func nativeSdkVersion() -> String { LinkTrailSDK.LinkTrail.version } @@ -285,11 +292,13 @@ public final class LinkTrailModuleImpl: NSObject { case .notALinkTrailURL: return "notALinkTrailUrl" case .missingAPIKey: return "missingApiKey" case .invalidAPIKey: return "invalidApiKey" + case .invalidEvent: return "invalidEvent" @unknown default: return "unknown" } } - /// `code`, the native message unchanged, and `statusCode`/`body` for `server` errors. + /// `code`, the native message unchanged, `statusCode`/`body` for `server` errors and `reason` + /// for `invalidEvent` errors. static func errorPayload(_ error: Error) -> [String: Any] { var payload: [String: Any] = [ "code": errorCode(error), @@ -300,10 +309,16 @@ public final class LinkTrailModuleImpl: NSObject { } static func errorUserInfo(_ error: Error) -> [String: Any] { - guard case let LinkTrailError.server(statusCode, body)? = error as? LinkTrailError else { return [:] } - var info: [String: Any] = ["statusCode": statusCode] - if let body { info["body"] = body } - return info + switch error as? LinkTrailError { + case let .server(statusCode, body)?: + var info: [String: Any] = ["statusCode": statusCode] + if let body { info["body"] = body } + return info + case let .invalidEvent(reason)?: + return ["reason": reason] + default: + return [:] + } } private static func reject(_ reject: Reject, _ error: Error) { diff --git a/package-lock.json b/package-lock.json index ae12c7a..4affce4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "linktrail-react-native", - "version": "0.1.1", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "linktrail-react-native", - "version": "0.1.1", + "version": "0.2.0", "license": "MIT", "devDependencies": { "@react-native/babel-preset": "^0.80.3", diff --git a/package.json b/package.json index 9211d4a..875dc88 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "linktrail-react-native", - "version": "0.1.1", + "version": "0.2.0", "description": "React Native SDK for LinkTrail — mobile attribution and deferred deep linking. A New Architecture TurboModule wrapping the native LinkTrail iOS and Android SDKs.", "main": "./lib/module/index.js", "types": "./lib/typescript/src/index.d.ts", diff --git a/src/NativeLinkTrail.ts b/src/NativeLinkTrail.ts index 9536d05..6d6775a 100644 --- a/src/NativeLinkTrail.ts +++ b/src/NativeLinkTrail.ts @@ -63,7 +63,7 @@ export interface Spec extends TurboModule { */ startLinkDelivery(): void; - /** The embedded native LinkTrail SDK version, e.g. `"0.1.0"`. */ + /** The embedded native LinkTrail SDK version, e.g. `"0.2.0"`. */ nativeSdkVersion(): string; /** Clears the install flag, cached attribution, queued events, device id. */ diff --git a/src/errors.ts b/src/errors.ts index 9831236..1bd8c8b 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -13,77 +13,58 @@ export type LinkTrailErrorCode = | 'decoding' | 'emptyResponse' | 'invalidArgument' + | 'invalidEvent' | 'unknown'; -/** The snake_case codes used up to 0.1.0. Exposed as `legacyCode` for one release. */ -export type LinkTrailLegacyErrorCode = - | 'missing_api_key' - | 'invalid_api_key' - | 'not_configured' - | 'invalid_url' - | 'not_a_linktrail_url' - | 'transport' - | 'server' - | 'decoding' - | 'empty_response' - | 'invalid_argument' - | 'unknown'; - -const LEGACY: Record = { - missingApiKey: 'missing_api_key', - invalidApiKey: 'invalid_api_key', - notConfigured: 'not_configured', - invalidUrl: 'invalid_url', - notALinkTrailUrl: 'not_a_linktrail_url', - transport: 'transport', - server: 'server', - decoding: 'decoding', - emptyResponse: 'empty_response', - invalidArgument: 'invalid_argument', - unknown: 'unknown', -}; - -const FROM_LEGACY = Object.fromEntries( - Object.entries(LEGACY).map(([code, legacy]) => [legacy, code]), -) as Record; +const CODES: ReadonlySet = new Set([ + 'missingApiKey', + 'invalidApiKey', + 'notConfigured', + 'invalidUrl', + 'notALinkTrailUrl', + 'transport', + 'server', + 'decoding', + 'emptyResponse', + 'invalidArgument', + 'invalidEvent', + 'unknown', +]); -/** Maps a native code (camelCase, or snake_case from an older native build) to camelCase. */ +/** Maps a native code to a `LinkTrailErrorCode`; anything unrecognised is `unknown`. */ export function normalizeErrorCode(raw: unknown): LinkTrailErrorCode { - if (typeof raw !== 'string') return 'unknown'; - if (raw in LEGACY) return raw as LinkTrailErrorCode; - return FROM_LEGACY[raw] ?? 'unknown'; + return typeof raw === 'string' && CODES.has(raw) + ? (raw as LinkTrailErrorCode) + : 'unknown'; } /** The fields shared by rejected promises and `onError` events. */ export interface LinkTrailErrorInfo { code: LinkTrailErrorCode; - /** - * The pre-0.1.1 snake_case code (`missing_api_key`, …). - * @deprecated Use `code`. Removed in the next minor release. - */ - legacyCode: LinkTrailLegacyErrorCode; /** The native error's message, unchanged. */ message: string; /** HTTP status, for `server` errors. */ statusCode?: number; /** Response body, for `server` errors when the server sent one. */ body?: string; + /** Why the event was rejected, for `invalidEvent` errors (e.g. `name is blank`). */ + reason?: string; } /** The error every LinkTrail promise rejects with. */ export class LinkTrailError extends Error implements LinkTrailErrorInfo { readonly code: LinkTrailErrorCode; - readonly legacyCode: LinkTrailLegacyErrorCode; readonly statusCode?: number; readonly body?: string; + readonly reason?: string; - constructor(info: Omit) { + constructor(info: LinkTrailErrorInfo) { super(info.message); this.name = 'LinkTrailError'; this.code = info.code; - this.legacyCode = LEGACY[info.code]; if (info.statusCode !== undefined) this.statusCode = info.statusCode; if (info.body !== undefined) this.body = info.body; + if (info.reason !== undefined) this.reason = info.reason; } } @@ -92,22 +73,23 @@ type RawError = { message?: unknown; statusCode?: unknown; body?: unknown; - userInfo?: { statusCode?: unknown; body?: unknown } | null; + reason?: unknown; + userInfo?: { statusCode?: unknown; body?: unknown; reason?: unknown } | null; }; /** Builds the error fields from a native rejection or `onError` payload. */ export function toErrorInfo(raw: unknown): LinkTrailErrorInfo { const e = (raw ?? {}) as RawError; - const code = normalizeErrorCode(e.code); const statusCode = e.statusCode ?? e.userInfo?.statusCode; const body = e.body ?? e.userInfo?.body; + const reason = e.reason ?? e.userInfo?.reason; const info: LinkTrailErrorInfo = { - code, - legacyCode: LEGACY[code], + code: normalizeErrorCode(e.code), message: typeof e.message === 'string' ? e.message : String(raw), }; if (typeof statusCode === 'number') info.statusCode = statusCode; if (typeof body === 'string') info.body = body; + if (typeof reason === 'string') info.reason = reason; return info; } diff --git a/src/index.ts b/src/index.ts index 54c9e9e..ea5a94d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,14 +19,13 @@ export { LinkTrailError, type LinkTrailErrorCode, type LinkTrailErrorInfo, - type LinkTrailLegacyErrorCode, } from './errors'; export { LinkTrailPasteButton, type LinkTrailPasteButtonProps, } from './LinkTrailPasteButton'; -/** Rejects with a `LinkTrailError` (camelCase `code`, `legacyCode`, native message). */ +/** Rejects with a `LinkTrailError` (camelCase `code`, native message). */ function call(promise: Promise): Promise { return promise.then( (value) => value as T, @@ -114,12 +113,12 @@ function stopLinkForwarding(): void { } export const LinkTrail = { - /** This package's version (`linktrail-react-native`), e.g. `"0.1.1"`. */ + /** This package's version (`linktrail-react-native`), e.g. `"0.2.0"`. */ version: LINKTRAIL_RN_VERSION, /** * The embedded native LinkTrail SDK version (iOS `LinkTrailSDK` or Android - * `io.linktrail:sdk`), e.g. `"0.1.0"`. + * `io.linktrail:sdk`), e.g. `"0.2.0"`. */ get nativeSdkVersion(): string { return NativeLinkTrail.nativeSdkVersion(); diff --git a/src/types.ts b/src/types.ts index c779a17..1700c69 100644 --- a/src/types.ts +++ b/src/types.ts @@ -52,8 +52,8 @@ export interface LinkTrailEventResult { /** * An error reported by the native SDK via the `onError` callback: `code` - * (camelCase), `legacyCode` (old snake_case, deprecated), the native `message`, - * and `statusCode`/`body` for `server` errors. + * (camelCase), the native `message`, `statusCode`/`body` for `server` errors + * and `reason` for `invalidEvent` errors. */ export type LinkTrailErrorEvent = LinkTrailErrorInfo; diff --git a/src/version.ts b/src/version.ts index b524ce7..35d9cf3 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,2 +1,2 @@ /** The linktrail-react-native package version. Kept equal to package.json by a test. */ -export const LINKTRAIL_RN_VERSION = '0.1.1'; +export const LINKTRAIL_RN_VERSION = '0.2.0'; From 416518e279d9f1e4952d574255ccd85abbc8ed1b Mon Sep 17 00:00:00 2001 From: Ahsan Ali Date: Fri, 13 Nov 2026 16:07:33 +0000 Subject: [PATCH 2/2] Example: bump LinkTrailSDK pod lock to 0.2.0 --- example/ios/Podfile.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 5bed9df..878e54d 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -37,14 +37,14 @@ PODS: - hermes-engine (0.80.3): - hermes-engine/Pre-built (= 0.80.3) - hermes-engine/Pre-built (0.80.3) - - LinktrailReactNative (0.1.1): + - LinktrailReactNative (0.2.0): - boost - DoubleConversion - fast_float - fmt - glog - hermes-engine - - LinkTrailSDK (~> 0.1.0) + - LinkTrailSDK (~> 0.2.0) - RCT-Folly - RCT-Folly/Fabric - RCTRequired @@ -67,7 +67,7 @@ PODS: - ReactCommon/turbomodule/core - SocketRocket - Yoga - - LinkTrailSDK (0.1.0) + - LinkTrailSDK (0.2.0) - RCT-Folly (2024.11.18.00): - boost - DoubleConversion @@ -2521,8 +2521,8 @@ SPEC CHECKSUMS: fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd glog: 5683914934d5b6e4240e497e0f4a3b42d1854183 hermes-engine: fff23161928673afcd95f63146e651726f68b384 - LinktrailReactNative: b3151edec47a2dfa9c70e816f43d85307d779461 - LinkTrailSDK: 622127713ad7b158aae26e2c1a8ad143d74342b2 + LinktrailReactNative: cdeefd890225a95e74d18e5fcbd544b73c48d009 + LinkTrailSDK: bc06d8d56613fd926642b10c10f33e4822d72a99 RCT-Folly: 846fda9475e61ec7bcbf8a3fe81edfcaeb090669 RCTDeprecation: 2ebc9cc0329ede460c5d52956ce5e1ab715c2c2c RCTRequired: b83daef658afafdc68d85b97ed082f4497a8ffa4