@revenuecat/purchases-typescript-internal 19.2.0 → 19.3.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.
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Fills in the fields {@link PurchasesError} declares but the platform bridges
3
+ * leave nested.
4
+ *
5
+ * Hybrid SDKs should call this on every error rejected by the native module,
6
+ * then rethrow the returned value.
7
+ *
8
+ * The error is mutated in place and returned. Copying it into a new object
9
+ * would discard its prototype and stack, which would break `instanceof Error`
10
+ * for consumers and `instanceof CapacitorException` on Capacitor.
11
+ *
12
+ * Values the bridge did not send fall back to the same defaults the native
13
+ * layer already applies, so the result always satisfies {@link PurchasesError}.
14
+ *
15
+ * Anything that is not an object, or that carries no error code, is returned
16
+ * untouched.
17
+ *
18
+ * @public
19
+ */
20
+ export declare function normalizePurchasesError(error: unknown): unknown;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.normalizePurchasesError = normalizePurchasesError;
15
+ var error_codes_1 = require("./generated/error-codes");
16
+ /**
17
+ * React Native nests `ErrorContainer.info` under `userInfo`, Capacitor under `data`,
18
+ * and `purchases-js-hybrid-mappings` under `info`.
19
+ */
20
+ var PAYLOAD_KEYS = ["userInfo", "data", "info"];
21
+ function isRecord(value) {
22
+ return typeof value === "object" && value !== null;
23
+ }
24
+ function readPayload(error) {
25
+ for (var _i = 0, PAYLOAD_KEYS_1 = PAYLOAD_KEYS; _i < PAYLOAD_KEYS_1.length; _i++) {
26
+ var key = PAYLOAD_KEYS_1[_i];
27
+ var candidate = error[key];
28
+ if (isRecord(candidate)) {
29
+ return candidate;
30
+ }
31
+ }
32
+ return {};
33
+ }
34
+ function firstString() {
35
+ var values = [];
36
+ for (var _i = 0; _i < arguments.length; _i++) {
37
+ values[_i] = arguments[_i];
38
+ }
39
+ for (var _a = 0, values_1 = values; _a < values_1.length; _a++) {
40
+ var value = values_1[_a];
41
+ if (typeof value === "string") {
42
+ return value;
43
+ }
44
+ }
45
+ return "";
46
+ }
47
+ /**
48
+ * Every code the SDK emits is a PURCHASES_ERROR_CODE value, which is always numeric.
49
+ * Plugin level rejections use names such as "UNIMPLEMENTED" or "PAYWALL_ERROR" and are
50
+ * not ours to touch.
51
+ *
52
+ * Deliberately looser than PURCHASES_ERROR_CODE membership: that enum omits codes the
53
+ * native SDKs emit (36 to 41), and Android and iOS disagree on 28 and 36, so matching
54
+ * against it would reject genuine errors.
55
+ */
56
+ function readCode(error, payload) {
57
+ var _a;
58
+ var candidate = (_a = error.code) !== null && _a !== void 0 ? _a : payload.code;
59
+ var code = String(candidate);
60
+ return /^\d+$/.test(code) ? code : undefined;
61
+ }
62
+ /**
63
+ * Fills in the fields {@link PurchasesError} declares but the platform bridges
64
+ * leave nested.
65
+ *
66
+ * Hybrid SDKs should call this on every error rejected by the native module,
67
+ * then rethrow the returned value.
68
+ *
69
+ * The error is mutated in place and returned. Copying it into a new object
70
+ * would discard its prototype and stack, which would break `instanceof Error`
71
+ * for consumers and `instanceof CapacitorException` on Capacitor.
72
+ *
73
+ * Values the bridge did not send fall back to the same defaults the native
74
+ * layer already applies, so the result always satisfies {@link PurchasesError}.
75
+ *
76
+ * Anything that is not an object, or that carries no error code, is returned
77
+ * untouched.
78
+ *
79
+ * @public
80
+ */
81
+ function normalizePurchasesError(error) {
82
+ if (!isRecord(error)) {
83
+ return error;
84
+ }
85
+ var payload = readPayload(error);
86
+ var code = readCode(error, payload);
87
+ if (code === undefined) {
88
+ return error;
89
+ }
90
+ error.code = code;
91
+ var userInfo = __assign({}, payload);
92
+ if (typeof userInfo.readableErrorCode !== "string") {
93
+ userInfo.readableErrorCode = firstString(payload.readableErrorCode, error.readableErrorCode);
94
+ }
95
+ error.userInfo = userInfo;
96
+ if (typeof error.message !== "string" || error.message === "") {
97
+ error.message = firstString(payload.message);
98
+ }
99
+ if (typeof error.readableErrorCode !== "string") {
100
+ error.readableErrorCode = userInfo.readableErrorCode;
101
+ }
102
+ if (typeof error.underlyingErrorMessage !== "string") {
103
+ error.underlyingErrorMessage = firstString(payload.underlyingErrorMessage);
104
+ }
105
+ // The bridges only send a userCancelled flag on purchase flows, derived from this code.
106
+ error.userCancelled = code === error_codes_1.PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR;
107
+ return error;
108
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './errors';
2
+ export * from './errorNormalizer';
2
3
  export * from './customerInfo';
3
4
  export * from './offerings';
4
5
  export * from './enums';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./errors"), exports);
18
+ __exportStar(require("./errorNormalizer"), exports);
18
19
  __exportStar(require("./customerInfo"), exports);
19
20
  __exportStar(require("./offerings"), exports);
20
21
  __exportStar(require("./enums"), exports);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@revenuecat/purchases-typescript-internal",
3
3
  "title": "Purchases typescript internal shared code",
4
- "version": "19.2.0",
4
+ "version": "19.3.0",
5
5
  "description": "Typescript code to be used by RevenueCat's hybrid SDKs. Not meant for external usage.",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -11,7 +11,8 @@
11
11
  ],
12
12
  "scripts": {
13
13
  "apitests": "tsc --p apitesters/",
14
- "lint": "eslint",
14
+ "lint": "eslint src",
15
+ "test": "jest",
15
16
  "build": "tsc",
16
17
  "build-watch": "tsc --watch",
17
18
  "extract-api": "api-extractor run --local --verbose"
@@ -32,9 +33,12 @@
32
33
  "packageManager": "yarn@1.22.22",
33
34
  "devDependencies": {
34
35
  "@microsoft/api-extractor": "^7.43.1",
36
+ "@types/jest": "^29.5.14",
35
37
  "@typescript-eslint/eslint-plugin": "^5.61.0",
36
38
  "@typescript-eslint/parser": "^5.61.0",
37
39
  "eslint": "^8.44.0",
40
+ "jest": "^29.7.0",
41
+ "ts-jest": "^29.1.2",
38
42
  "typescript": "^5.1.6"
39
43
  }
40
44
  }