@metamask-previews/subscription-controller 6.2.1-preview-b5147cfe0 → 6.2.1-preview-b156b30ed
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.
- package/CHANGELOG.md +6 -0
- package/dist/SubscriptionController.cjs +18 -26
- package/dist/SubscriptionController.cjs.map +1 -1
- package/dist/SubscriptionController.d.cts +5 -9
- package/dist/SubscriptionController.d.cts.map +1 -1
- package/dist/SubscriptionController.d.mts +5 -9
- package/dist/SubscriptionController.d.mts.map +1 -1
- package/dist/SubscriptionController.mjs +18 -26
- package/dist/SubscriptionController.mjs.map +1 -1
- package/dist/SubscriptionService-method-action-types.cjs +7 -0
- package/dist/SubscriptionService-method-action-types.cjs.map +1 -0
- package/dist/SubscriptionService-method-action-types.d.cts +151 -0
- package/dist/SubscriptionService-method-action-types.d.cts.map +1 -0
- package/dist/SubscriptionService-method-action-types.d.mts +151 -0
- package/dist/SubscriptionService-method-action-types.d.mts.map +1 -0
- package/dist/SubscriptionService-method-action-types.mjs +6 -0
- package/dist/SubscriptionService-method-action-types.mjs.map +1 -0
- package/dist/SubscriptionService-structs.cjs +135 -0
- package/dist/SubscriptionService-structs.cjs.map +1 -0
- package/dist/SubscriptionService-structs.d.cts +773 -0
- package/dist/SubscriptionService-structs.d.cts.map +1 -0
- package/dist/SubscriptionService-structs.d.mts +773 -0
- package/dist/SubscriptionService-structs.d.mts.map +1 -0
- package/dist/SubscriptionService-structs.mjs +132 -0
- package/dist/SubscriptionService-structs.mjs.map +1 -0
- package/dist/SubscriptionService.cjs +254 -87
- package/dist/SubscriptionService.cjs.map +1 -1
- package/dist/SubscriptionService.d.cts +84 -9
- package/dist/SubscriptionService.d.cts.map +1 -1
- package/dist/SubscriptionService.d.mts +84 -9
- package/dist/SubscriptionService.d.mts.map +1 -1
- package/dist/SubscriptionService.mjs +254 -87
- package/dist/SubscriptionService.mjs.map +1 -1
- package/dist/index.cjs +3 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SubscriptionService-structs.d.mts","sourceRoot":"","sources":["../src/SubscriptionService-structs.ts"],"names":[],"mappings":"AA8DA,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAkB7B,CAAC;AAEH,eAAO,MAAM,8BAA8B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAMzC,CAAC;AAEH,eAAO,MAAM,+BAA+B;;;;EAE1C,CAAC;AAEH,eAAO,MAAM,qCAAqC;;;;;;;;;;;;;;;;EAGhD,CAAC;AAEH,eAAO,MAAM,qCAAqC;;;;EAEhD,CAAC;AAEH,eAAO,MAAM,2BAA2B;;;;EAEtC,CAAC;AAEH,eAAO,MAAM,oCAAoC;;;;;;EAG/C,CAAC;AAsCH,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAGhC,CAAC;AASH,eAAO,MAAM,6BAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EASxC,CAAC;AAEH,eAAO,MAAM,kCAAkC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAE9C,CAAC"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { array, boolean, enums, nullable, number, optional, string, type, union } from "@metamask/superstruct";
|
|
2
|
+
import { StrictHexStruct, CaipAccountIdStruct } from "@metamask/utils";
|
|
3
|
+
import { CANCEL_TYPES, CRYPTO_PAYMENT_METHOD_ERRORS, PAYMENT_TYPES, PRODUCT_TYPES, RECURRING_INTERVALS, SUBSCRIPTION_STATUSES } from "./types.mjs";
|
|
4
|
+
const ProductTypeStruct = enums(Object.values(PRODUCT_TYPES));
|
|
5
|
+
const PaymentTypeStruct = enums(Object.values(PAYMENT_TYPES));
|
|
6
|
+
const RecurringIntervalStruct = enums(Object.values(RECURRING_INTERVALS));
|
|
7
|
+
const SubscriptionStatusStruct = enums(Object.values(SUBSCRIPTION_STATUSES));
|
|
8
|
+
const CancelTypeStruct = enums(Object.values(CANCEL_TYPES));
|
|
9
|
+
const CryptoPaymentMethodErrorStruct = enums(Object.values(CRYPTO_PAYMENT_METHOD_ERRORS));
|
|
10
|
+
const ProductStruct = type({
|
|
11
|
+
name: ProductTypeStruct,
|
|
12
|
+
currency: enums(['usd']),
|
|
13
|
+
unitAmount: number(),
|
|
14
|
+
unitDecimals: number(),
|
|
15
|
+
});
|
|
16
|
+
const SubscriptionCardPaymentMethodStruct = type({
|
|
17
|
+
type: enums([PAYMENT_TYPES.byCard]),
|
|
18
|
+
card: type({
|
|
19
|
+
brand: string(),
|
|
20
|
+
displayBrand: string(),
|
|
21
|
+
last4: string(),
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
const SubscriptionCryptoPaymentMethodStruct = type({
|
|
25
|
+
type: enums([PAYMENT_TYPES.byCrypto]),
|
|
26
|
+
crypto: type({
|
|
27
|
+
payerAddress: StrictHexStruct,
|
|
28
|
+
chainId: StrictHexStruct,
|
|
29
|
+
tokenSymbol: string(),
|
|
30
|
+
error: optional(CryptoPaymentMethodErrorStruct),
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
const SubscriptionPaymentMethodStruct = union([
|
|
34
|
+
SubscriptionCardPaymentMethodStruct,
|
|
35
|
+
SubscriptionCryptoPaymentMethodStruct,
|
|
36
|
+
]);
|
|
37
|
+
export const SubscriptionStruct = type({
|
|
38
|
+
id: string(),
|
|
39
|
+
products: array(ProductStruct),
|
|
40
|
+
currentPeriodStart: string(),
|
|
41
|
+
currentPeriodEnd: string(),
|
|
42
|
+
cancelAtPeriodEnd: optional(boolean()),
|
|
43
|
+
status: SubscriptionStatusStruct,
|
|
44
|
+
interval: RecurringIntervalStruct,
|
|
45
|
+
paymentMethod: SubscriptionPaymentMethodStruct,
|
|
46
|
+
trialPeriodDays: optional(number()),
|
|
47
|
+
trialStart: optional(string()),
|
|
48
|
+
trialEnd: optional(string()),
|
|
49
|
+
endDate: optional(string()),
|
|
50
|
+
canceledAt: optional(string()),
|
|
51
|
+
cancelType: CancelTypeStruct,
|
|
52
|
+
inactiveAt: optional(string()),
|
|
53
|
+
isEligibleForSupport: boolean(),
|
|
54
|
+
billingCycles: optional(number()),
|
|
55
|
+
});
|
|
56
|
+
export const GetSubscriptionsResponseStruct = type({
|
|
57
|
+
customerId: optional(string()),
|
|
58
|
+
subscriptions: array(SubscriptionStruct),
|
|
59
|
+
trialedProducts: array(ProductTypeStruct),
|
|
60
|
+
lastSubscription: optional(SubscriptionStruct),
|
|
61
|
+
rewardAccountId: optional(CaipAccountIdStruct),
|
|
62
|
+
});
|
|
63
|
+
export const StartSubscriptionResponseStruct = type({
|
|
64
|
+
checkoutSessionUrl: string(),
|
|
65
|
+
});
|
|
66
|
+
export const StartCryptoSubscriptionResponseStruct = type({
|
|
67
|
+
subscriptionId: string(),
|
|
68
|
+
status: SubscriptionStatusStruct,
|
|
69
|
+
});
|
|
70
|
+
export const UpdatePaymentMethodCardResponseStruct = type({
|
|
71
|
+
redirectUrl: string(),
|
|
72
|
+
});
|
|
73
|
+
export const BillingPortalResponseStruct = type({
|
|
74
|
+
url: string(),
|
|
75
|
+
});
|
|
76
|
+
export const SubscriptionApiGeneralResponseStruct = type({
|
|
77
|
+
success: boolean(),
|
|
78
|
+
message: optional(string()),
|
|
79
|
+
});
|
|
80
|
+
const ProductPriceStruct = type({
|
|
81
|
+
interval: RecurringIntervalStruct,
|
|
82
|
+
unitAmount: number(),
|
|
83
|
+
unitDecimals: number(),
|
|
84
|
+
currency: enums(['usd']),
|
|
85
|
+
trialPeriodDays: number(),
|
|
86
|
+
minBillingCycles: number(),
|
|
87
|
+
minBillingCyclesForBalance: number(),
|
|
88
|
+
});
|
|
89
|
+
const ProductPricingStruct = type({
|
|
90
|
+
name: ProductTypeStruct,
|
|
91
|
+
prices: array(ProductPriceStruct),
|
|
92
|
+
});
|
|
93
|
+
const TokenPaymentInfoStruct = type({
|
|
94
|
+
symbol: string(),
|
|
95
|
+
address: StrictHexStruct,
|
|
96
|
+
decimals: number(),
|
|
97
|
+
conversionRate: type({
|
|
98
|
+
usd: string(),
|
|
99
|
+
}),
|
|
100
|
+
});
|
|
101
|
+
const ChainPaymentInfoStruct = type({
|
|
102
|
+
chainId: StrictHexStruct,
|
|
103
|
+
paymentAddress: StrictHexStruct,
|
|
104
|
+
tokens: array(TokenPaymentInfoStruct),
|
|
105
|
+
isSponsorshipSupported: optional(boolean()),
|
|
106
|
+
});
|
|
107
|
+
const PricingPaymentMethodStruct = type({
|
|
108
|
+
type: PaymentTypeStruct,
|
|
109
|
+
chains: optional(array(ChainPaymentInfoStruct)),
|
|
110
|
+
});
|
|
111
|
+
export const PricingResponseStruct = type({
|
|
112
|
+
products: array(ProductPricingStruct),
|
|
113
|
+
paymentMethods: array(PricingPaymentMethodStruct),
|
|
114
|
+
});
|
|
115
|
+
const CohortStruct = type({
|
|
116
|
+
cohort: string(),
|
|
117
|
+
eligibilityRate: number(),
|
|
118
|
+
priority: number(),
|
|
119
|
+
eligible: boolean(),
|
|
120
|
+
});
|
|
121
|
+
export const SubscriptionEligibilityStruct = type({
|
|
122
|
+
product: ProductTypeStruct,
|
|
123
|
+
canSubscribe: optional(boolean()),
|
|
124
|
+
canViewEntryModal: optional(boolean()),
|
|
125
|
+
modalType: optional(enums(['A', 'B'])),
|
|
126
|
+
cohorts: optional(array(CohortStruct)),
|
|
127
|
+
assignedCohort: optional(nullable(string())),
|
|
128
|
+
assignedAt: optional(string()),
|
|
129
|
+
hasAssignedCohortExpired: optional(boolean()),
|
|
130
|
+
});
|
|
131
|
+
export const SubscriptionEligibilityArrayStruct = array(SubscriptionEligibilityStruct);
|
|
132
|
+
//# sourceMappingURL=SubscriptionService-structs.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SubscriptionService-structs.mjs","sourceRoot":"","sources":["../src/SubscriptionService-structs.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,OAAO,EACP,KAAK,EACL,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,MAAM,EACN,IAAI,EACJ,KAAK,EACN,8BAA8B;AAC/B,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,wBAAwB;AAEvE,OAAO,EACL,YAAY,EACZ,4BAA4B,EAC5B,aAAa,EACb,aAAa,EACb,mBAAmB,EACnB,qBAAqB,EACtB,oBAAmB;AAEpB,MAAM,iBAAiB,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;AAC9D,MAAM,iBAAiB,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM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H,MAAM,0BAA0B,GAAG,IAAI,CAAC;IACtC,IAAI,EAAE,iBAAiB;IACvB,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAC;CAChD,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,CAAC;IACxC,QAAQ,EAAE,KAAK,CAAC,oBAAoB,CAAC;IACrC,cAAc,EAAE,KAAK,CAAC,0BAA0B,CAAC;CAClD,CAAC,CAAC;AAEH,MAAM,YAAY,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,EAAE;IAChB,eAAe,EAAE,MAAM,EAAE;IACzB,QAAQ,EAAE,MAAM,EAAE;IAClB,QAAQ,EAAE,OAAO,EAAE;CACpB,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,6BAA6B,GAAG,IAAI,CAAC;IAChD,OAAO,EAAE,iBAAiB;IAC1B,YAAY,EAAE,QAAQ,CAAC,OAAO,EAAE,CAAC;IACjC,iBAAiB,EAAE,QAAQ,CAAC,OAAO,EAAE,CAAC;IACtC,SAAS,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IACtC,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACtC,cAAc,EAAE,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5C,UAAU,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;IAC9B,wBAAwB,EAAE,QAAQ,CAAC,OAAO,EAAE,CAAC;CAC9C,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,kCAAkC,GAAG,KAAK,CACrD,6BAA6B,CAC9B,CAAC","sourcesContent":["import {\n array,\n boolean,\n enums,\n nullable,\n number,\n optional,\n string,\n type,\n union,\n} from '@metamask/superstruct';\nimport { StrictHexStruct, CaipAccountIdStruct } from '@metamask/utils';\n\nimport {\n CANCEL_TYPES,\n CRYPTO_PAYMENT_METHOD_ERRORS,\n PAYMENT_TYPES,\n PRODUCT_TYPES,\n RECURRING_INTERVALS,\n SUBSCRIPTION_STATUSES,\n} from './types.js';\n\nconst ProductTypeStruct = enums(Object.values(PRODUCT_TYPES));\nconst PaymentTypeStruct = enums(Object.values(PAYMENT_TYPES));\nconst RecurringIntervalStruct = enums(Object.values(RECURRING_INTERVALS));\nconst SubscriptionStatusStruct = enums(Object.values(SUBSCRIPTION_STATUSES));\nconst CancelTypeStruct = enums(Object.values(CANCEL_TYPES));\nconst CryptoPaymentMethodErrorStruct = enums(\n Object.values(CRYPTO_PAYMENT_METHOD_ERRORS),\n);\n\nconst ProductStruct = type({\n name: ProductTypeStruct,\n currency: enums(['usd']),\n unitAmount: number(),\n unitDecimals: number(),\n});\n\nconst SubscriptionCardPaymentMethodStruct = type({\n type: enums([PAYMENT_TYPES.byCard]),\n card: type({\n brand: string(),\n displayBrand: string(),\n last4: string(),\n }),\n});\n\nconst SubscriptionCryptoPaymentMethodStruct = type({\n type: enums([PAYMENT_TYPES.byCrypto]),\n crypto: type({\n payerAddress: StrictHexStruct,\n chainId: StrictHexStruct,\n tokenSymbol: string(),\n error: optional(CryptoPaymentMethodErrorStruct),\n }),\n});\n\nconst SubscriptionPaymentMethodStruct = union([\n SubscriptionCardPaymentMethodStruct,\n SubscriptionCryptoPaymentMethodStruct,\n]);\n\nexport const SubscriptionStruct = type({\n id: string(),\n products: array(ProductStruct),\n currentPeriodStart: string(),\n currentPeriodEnd: string(),\n cancelAtPeriodEnd: optional(boolean()),\n status: SubscriptionStatusStruct,\n interval: RecurringIntervalStruct,\n paymentMethod: SubscriptionPaymentMethodStruct,\n trialPeriodDays: optional(number()),\n trialStart: optional(string()),\n trialEnd: optional(string()),\n endDate: optional(string()),\n canceledAt: optional(string()),\n cancelType: CancelTypeStruct,\n inactiveAt: optional(string()),\n isEligibleForSupport: boolean(),\n billingCycles: optional(number()),\n});\n\nexport const GetSubscriptionsResponseStruct = type({\n customerId: optional(string()),\n subscriptions: array(SubscriptionStruct),\n trialedProducts: array(ProductTypeStruct),\n lastSubscription: optional(SubscriptionStruct),\n rewardAccountId: optional(CaipAccountIdStruct),\n});\n\nexport const StartSubscriptionResponseStruct = type({\n checkoutSessionUrl: string(),\n});\n\nexport const StartCryptoSubscriptionResponseStruct = type({\n subscriptionId: string(),\n status: SubscriptionStatusStruct,\n});\n\nexport const UpdatePaymentMethodCardResponseStruct = type({\n redirectUrl: string(),\n});\n\nexport const BillingPortalResponseStruct = type({\n url: string(),\n});\n\nexport const SubscriptionApiGeneralResponseStruct = type({\n success: boolean(),\n message: optional(string()),\n});\n\nconst ProductPriceStruct = type({\n interval: RecurringIntervalStruct,\n unitAmount: number(),\n unitDecimals: number(),\n currency: enums(['usd']),\n trialPeriodDays: number(),\n minBillingCycles: number(),\n minBillingCyclesForBalance: number(),\n});\n\nconst ProductPricingStruct = type({\n name: ProductTypeStruct,\n prices: array(ProductPriceStruct),\n});\n\nconst TokenPaymentInfoStruct = type({\n symbol: string(),\n address: StrictHexStruct,\n decimals: number(),\n conversionRate: type({\n usd: string(),\n }),\n});\n\nconst ChainPaymentInfoStruct = type({\n chainId: StrictHexStruct,\n paymentAddress: StrictHexStruct,\n tokens: array(TokenPaymentInfoStruct),\n isSponsorshipSupported: optional(boolean()),\n});\n\nconst PricingPaymentMethodStruct = type({\n type: PaymentTypeStruct,\n chains: optional(array(ChainPaymentInfoStruct)),\n});\n\nexport const PricingResponseStruct = type({\n products: array(ProductPricingStruct),\n paymentMethods: array(PricingPaymentMethodStruct),\n});\n\nconst CohortStruct = type({\n cohort: string(),\n eligibilityRate: number(),\n priority: number(),\n eligible: boolean(),\n});\n\nexport const SubscriptionEligibilityStruct = type({\n product: ProductTypeStruct,\n canSubscribe: optional(boolean()),\n canViewEntryModal: optional(boolean()),\n modalType: optional(enums(['A', 'B'])),\n cohorts: optional(array(CohortStruct)),\n assignedCohort: optional(nullable(string())),\n assignedAt: optional(string()),\n hasAssignedCohortExpired: optional(boolean()),\n});\n\nexport const SubscriptionEligibilityArrayStruct = array(\n SubscriptionEligibilityStruct,\n);\n"]}
|
|
@@ -10,36 +10,103 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _SubscriptionService_instances, _SubscriptionService_env, _SubscriptionService_fetch, _SubscriptionService_captureException,
|
|
13
|
+
var _SubscriptionService_instances, _SubscriptionService_env, _SubscriptionService_fetch, _SubscriptionService_captureException, _SubscriptionService_fetchJson, _SubscriptionService_getAuthenticatedContext, _SubscriptionService_headersForToken, _SubscriptionService_getSubscriptionApiUrl;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.SubscriptionService = exports.SUBSCRIPTION_URL = void 0;
|
|
15
|
+
exports.SubscriptionService = exports.SUBSCRIPTION_URL = exports.serviceName = void 0;
|
|
16
|
+
const base_data_service_1 = require("@metamask/base-data-service");
|
|
17
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
18
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
19
|
+
const utils_1 = require("@metamask/utils");
|
|
16
20
|
const constants_js_1 = require("./constants.cjs");
|
|
17
21
|
const errors_js_1 = require("./errors.cjs");
|
|
18
22
|
const logger_js_1 = require("./logger.cjs");
|
|
23
|
+
const SubscriptionService_structs_js_1 = require("./SubscriptionService-structs.cjs");
|
|
24
|
+
exports.serviceName = 'SubscriptionService';
|
|
19
25
|
const SUBSCRIPTION_URL = (env, path) => `${(0, constants_js_1.getEnvUrls)(env).subscriptionApiUrl}/v1/${path}`;
|
|
20
26
|
exports.SUBSCRIPTION_URL = SUBSCRIPTION_URL;
|
|
27
|
+
const MESSENGER_EXPOSED_METHODS = [
|
|
28
|
+
'getSubscriptions',
|
|
29
|
+
'cancelSubscription',
|
|
30
|
+
'unCancelSubscription',
|
|
31
|
+
'startSubscriptionWithCard',
|
|
32
|
+
'startSubscriptionWithCrypto',
|
|
33
|
+
'updatePaymentMethodCard',
|
|
34
|
+
'updatePaymentMethodCrypto',
|
|
35
|
+
'getSubscriptionsEligibilities',
|
|
36
|
+
'submitUserEvent',
|
|
37
|
+
'assignUserToCohort',
|
|
38
|
+
'submitSponsorshipIntents',
|
|
39
|
+
'linkRewards',
|
|
40
|
+
'getPricing',
|
|
41
|
+
'getBillingPortalUrl',
|
|
42
|
+
];
|
|
21
43
|
const log = (0, logger_js_1.createModuleLogger)(logger_js_1.projectLogger, 'SubscriptionService');
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
function isRetryableError(error) {
|
|
45
|
+
if (error instanceof controller_utils_1.HttpError) {
|
|
46
|
+
if (error.httpStatus === 429) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
return error.httpStatus < 400 || error.httpStatus >= 500;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
const DEFAULT_POLICY_OPTIONS = {
|
|
54
|
+
retryFilterPolicy: (0, controller_utils_1.handleWhen)(isRetryableError),
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Data service for the Subscription API.
|
|
58
|
+
*
|
|
59
|
+
* All requests are authenticated via JWT Bearer tokens obtained from the
|
|
60
|
+
* `AuthenticationController:getBearerToken` messenger action.
|
|
61
|
+
*/
|
|
62
|
+
class SubscriptionService extends base_data_service_1.BaseDataService {
|
|
63
|
+
constructor({ messenger, fetchFunction, env = constants_js_1.Env.PRD, captureException, queryClientConfig = {}, policyOptions = {}, }) {
|
|
64
|
+
super({
|
|
65
|
+
name: exports.serviceName,
|
|
66
|
+
messenger,
|
|
67
|
+
queryClientConfig,
|
|
68
|
+
policyOptions: { ...DEFAULT_POLICY_OPTIONS, ...policyOptions },
|
|
69
|
+
});
|
|
24
70
|
_SubscriptionService_instances.add(this);
|
|
25
71
|
_SubscriptionService_env.set(this, void 0);
|
|
26
72
|
_SubscriptionService_fetch.set(this, void 0);
|
|
27
73
|
_SubscriptionService_captureException.set(this, void 0);
|
|
28
|
-
__classPrivateFieldSet(this, _SubscriptionService_env,
|
|
29
|
-
this
|
|
30
|
-
__classPrivateFieldSet(this,
|
|
31
|
-
|
|
74
|
+
__classPrivateFieldSet(this, _SubscriptionService_env, env, "f");
|
|
75
|
+
__classPrivateFieldSet(this, _SubscriptionService_fetch, fetchFunction, "f");
|
|
76
|
+
__classPrivateFieldSet(this, _SubscriptionService_captureException, captureException, "f");
|
|
77
|
+
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
32
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Fetches the user's subscriptions.
|
|
81
|
+
*
|
|
82
|
+
* @returns The subscriptions response.
|
|
83
|
+
*/
|
|
33
84
|
async getSubscriptions() {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
85
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
86
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
87
|
+
profileKey,
|
|
88
|
+
bearerToken,
|
|
89
|
+
methodName: 'getSubscriptions',
|
|
90
|
+
requestParams: null,
|
|
91
|
+
path: 'subscriptions',
|
|
37
92
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToGetSubscriptions,
|
|
38
93
|
});
|
|
94
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.GetSubscriptionsResponseStruct);
|
|
39
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Cancels a subscription.
|
|
98
|
+
*
|
|
99
|
+
* @param params - The cancel subscription request.
|
|
100
|
+
* @returns The updated subscription.
|
|
101
|
+
*/
|
|
40
102
|
async cancelSubscription(params) {
|
|
103
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
41
104
|
const path = `subscriptions/${params.subscriptionId}/cancel`;
|
|
42
|
-
|
|
105
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
106
|
+
profileKey,
|
|
107
|
+
bearerToken,
|
|
108
|
+
methodName: 'cancelSubscription',
|
|
109
|
+
requestParams: params,
|
|
43
110
|
path,
|
|
44
111
|
method: 'POST',
|
|
45
112
|
body: {
|
|
@@ -47,40 +114,87 @@ class SubscriptionService {
|
|
|
47
114
|
},
|
|
48
115
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToCancelSubscription,
|
|
49
116
|
});
|
|
117
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.SubscriptionStruct);
|
|
50
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Reverses a pending subscription cancellation.
|
|
121
|
+
*
|
|
122
|
+
* @param params - The uncancel subscription request.
|
|
123
|
+
* @param params.subscriptionId - The subscription ID to uncancel.
|
|
124
|
+
* @returns The updated subscription.
|
|
125
|
+
*/
|
|
51
126
|
async unCancelSubscription(params) {
|
|
127
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
52
128
|
const path = `subscriptions/${params.subscriptionId}/uncancel`;
|
|
53
|
-
|
|
129
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
130
|
+
profileKey,
|
|
131
|
+
bearerToken,
|
|
132
|
+
methodName: 'unCancelSubscription',
|
|
133
|
+
requestParams: params,
|
|
54
134
|
path,
|
|
55
135
|
method: 'POST',
|
|
56
136
|
body: {},
|
|
57
137
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToUncancelSubscription,
|
|
58
138
|
});
|
|
139
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.SubscriptionStruct);
|
|
59
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Starts a subscription with a card payment method.
|
|
143
|
+
*
|
|
144
|
+
* @param request - The start subscription request.
|
|
145
|
+
* @returns The checkout session response.
|
|
146
|
+
*/
|
|
60
147
|
async startSubscriptionWithCard(request) {
|
|
61
148
|
if (request.products.length === 0) {
|
|
62
149
|
throw new errors_js_1.SubscriptionServiceError(constants_js_1.SubscriptionControllerErrorMessage.SubscriptionProductsEmpty);
|
|
63
150
|
}
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
151
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
152
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
153
|
+
profileKey,
|
|
154
|
+
bearerToken,
|
|
155
|
+
methodName: 'startSubscriptionWithCard',
|
|
156
|
+
requestParams: request,
|
|
157
|
+
path: 'subscriptions/card',
|
|
67
158
|
method: 'POST',
|
|
68
159
|
body: request,
|
|
69
160
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToStartSubscriptionWithCard,
|
|
70
161
|
});
|
|
162
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.StartSubscriptionResponseStruct);
|
|
71
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Starts a subscription with a crypto payment method.
|
|
166
|
+
*
|
|
167
|
+
* @param request - The start crypto subscription request.
|
|
168
|
+
* @returns The created subscription response.
|
|
169
|
+
*/
|
|
72
170
|
async startSubscriptionWithCrypto(request) {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
171
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
172
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
173
|
+
profileKey,
|
|
174
|
+
bearerToken,
|
|
175
|
+
methodName: 'startSubscriptionWithCrypto',
|
|
176
|
+
requestParams: request,
|
|
177
|
+
path: 'subscriptions/crypto',
|
|
76
178
|
method: 'POST',
|
|
77
179
|
body: request,
|
|
78
180
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToStartSubscriptionWithCrypto,
|
|
79
181
|
});
|
|
182
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.StartCryptoSubscriptionResponseStruct);
|
|
80
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Updates a subscription's card payment method.
|
|
186
|
+
*
|
|
187
|
+
* @param request - The update payment method request.
|
|
188
|
+
* @returns The redirect URL response.
|
|
189
|
+
*/
|
|
81
190
|
async updatePaymentMethodCard(request) {
|
|
191
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
82
192
|
const path = `subscriptions/${request.subscriptionId}/payment-method/card`;
|
|
83
|
-
|
|
193
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
194
|
+
profileKey,
|
|
195
|
+
bearerToken,
|
|
196
|
+
methodName: 'updatePaymentMethodCard',
|
|
197
|
+
requestParams: request,
|
|
84
198
|
path,
|
|
85
199
|
method: 'PATCH',
|
|
86
200
|
body: {
|
|
@@ -89,10 +203,21 @@ class SubscriptionService {
|
|
|
89
203
|
},
|
|
90
204
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToUpdatePaymentMethodCard,
|
|
91
205
|
});
|
|
206
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.UpdatePaymentMethodCardResponseStruct);
|
|
92
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Updates a subscription's crypto payment method.
|
|
210
|
+
*
|
|
211
|
+
* @param request - The update payment method request.
|
|
212
|
+
*/
|
|
93
213
|
async updatePaymentMethodCrypto(request) {
|
|
214
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
94
215
|
const path = `subscriptions/${request.subscriptionId}/payment-method/crypto`;
|
|
95
|
-
await __classPrivateFieldGet(this, _SubscriptionService_instances, "m",
|
|
216
|
+
await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
217
|
+
profileKey,
|
|
218
|
+
bearerToken,
|
|
219
|
+
methodName: 'updatePaymentMethodCrypto',
|
|
220
|
+
requestParams: request,
|
|
96
221
|
path,
|
|
97
222
|
method: 'PATCH',
|
|
98
223
|
body: {
|
|
@@ -109,23 +234,28 @@ class SubscriptionService {
|
|
|
109
234
|
* @returns The eligibility for a shield subscription
|
|
110
235
|
*/
|
|
111
236
|
async getSubscriptionsEligibilities(request) {
|
|
112
|
-
const
|
|
113
|
-
let
|
|
237
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
238
|
+
let queryParams;
|
|
114
239
|
if (request?.balanceCategory !== undefined) {
|
|
115
|
-
|
|
240
|
+
queryParams = { balanceCategory: request.balanceCategory };
|
|
116
241
|
}
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
242
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
243
|
+
profileKey,
|
|
244
|
+
bearerToken,
|
|
245
|
+
methodName: 'getSubscriptionsEligibilities',
|
|
246
|
+
requestParams: request ?? null,
|
|
247
|
+
path: 'subscriptions/eligibility',
|
|
248
|
+
queryParams,
|
|
120
249
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToGetSubscriptionsEligibilities,
|
|
121
250
|
});
|
|
251
|
+
const results = (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.SubscriptionEligibilityArrayStruct);
|
|
122
252
|
return results.map((result) => ({
|
|
123
253
|
...result,
|
|
124
|
-
canSubscribe: result.canSubscribe
|
|
125
|
-
canViewEntryModal: result.canViewEntryModal
|
|
126
|
-
cohorts: result.cohorts
|
|
254
|
+
canSubscribe: result.canSubscribe ?? false,
|
|
255
|
+
canViewEntryModal: result.canViewEntryModal ?? false,
|
|
256
|
+
cohorts: result.cohorts ?? [],
|
|
127
257
|
assignedCohort: result.assignedCohort ?? null,
|
|
128
|
-
hasAssignedCohortExpired: result.hasAssignedCohortExpired
|
|
258
|
+
hasAssignedCohortExpired: result.hasAssignedCohortExpired ?? false,
|
|
129
259
|
}));
|
|
130
260
|
}
|
|
131
261
|
/**
|
|
@@ -135,9 +265,13 @@ class SubscriptionService {
|
|
|
135
265
|
* @example { event: SubscriptionUserEvent.ShieldEntryModalViewed, cohort: 'post_tx' }
|
|
136
266
|
*/
|
|
137
267
|
async submitUserEvent(request) {
|
|
138
|
-
const
|
|
139
|
-
await __classPrivateFieldGet(this, _SubscriptionService_instances, "m",
|
|
140
|
-
|
|
268
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
269
|
+
await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
270
|
+
profileKey,
|
|
271
|
+
bearerToken,
|
|
272
|
+
methodName: 'submitUserEvent',
|
|
273
|
+
requestParams: request,
|
|
274
|
+
path: 'user-events',
|
|
141
275
|
method: 'POST',
|
|
142
276
|
body: request,
|
|
143
277
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToSubmitUserEvent,
|
|
@@ -150,9 +284,13 @@ class SubscriptionService {
|
|
|
150
284
|
* @example { cohort: 'post_tx' }
|
|
151
285
|
*/
|
|
152
286
|
async assignUserToCohort(request) {
|
|
153
|
-
const
|
|
154
|
-
await __classPrivateFieldGet(this, _SubscriptionService_instances, "m",
|
|
155
|
-
|
|
287
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
288
|
+
await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
289
|
+
profileKey,
|
|
290
|
+
bearerToken,
|
|
291
|
+
methodName: 'assignUserToCohort',
|
|
292
|
+
requestParams: request,
|
|
293
|
+
path: 'cohorts/assign',
|
|
156
294
|
method: 'POST',
|
|
157
295
|
body: request,
|
|
158
296
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToAssignUserToCohort,
|
|
@@ -168,9 +306,13 @@ class SubscriptionService {
|
|
|
168
306
|
* @example { address: '0x1234567890123456789012345678901234567890', products: [ProductType.Shield] }
|
|
169
307
|
*/
|
|
170
308
|
async submitSponsorshipIntents(request) {
|
|
171
|
-
const
|
|
172
|
-
await __classPrivateFieldGet(this, _SubscriptionService_instances, "m",
|
|
173
|
-
|
|
309
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
310
|
+
await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
311
|
+
profileKey,
|
|
312
|
+
bearerToken,
|
|
313
|
+
methodName: 'submitSponsorshipIntents',
|
|
314
|
+
requestParams: request,
|
|
315
|
+
path: 'transaction-sponsorship/intents',
|
|
174
316
|
method: 'POST',
|
|
175
317
|
body: request,
|
|
176
318
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToSubmitSponsorshipIntents,
|
|
@@ -184,81 +326,105 @@ class SubscriptionService {
|
|
|
184
326
|
* @returns The response from the API.
|
|
185
327
|
*/
|
|
186
328
|
async linkRewards(request) {
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
329
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
330
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
331
|
+
profileKey,
|
|
332
|
+
bearerToken,
|
|
333
|
+
methodName: 'linkRewards',
|
|
334
|
+
requestParams: request,
|
|
335
|
+
path: 'rewards/link',
|
|
190
336
|
method: 'POST',
|
|
191
337
|
body: request,
|
|
192
338
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToLinkRewards,
|
|
193
339
|
});
|
|
340
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.SubscriptionApiGeneralResponseStruct);
|
|
194
341
|
}
|
|
342
|
+
/**
|
|
343
|
+
* Fetches subscription pricing information.
|
|
344
|
+
*
|
|
345
|
+
* @returns The pricing response.
|
|
346
|
+
*/
|
|
195
347
|
async getPricing() {
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
348
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
349
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
350
|
+
profileKey,
|
|
351
|
+
bearerToken,
|
|
352
|
+
methodName: 'getPricing',
|
|
353
|
+
requestParams: null,
|
|
354
|
+
path: 'pricing',
|
|
199
355
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToGetPricing,
|
|
200
356
|
});
|
|
357
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.PricingResponseStruct);
|
|
201
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Fetches the billing portal URL.
|
|
361
|
+
*
|
|
362
|
+
* @returns The billing portal response.
|
|
363
|
+
*/
|
|
202
364
|
async getBillingPortalUrl() {
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
365
|
+
const { profileKey, bearerToken } = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthenticatedContext).call(this);
|
|
366
|
+
const jsonResponse = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_fetchJson).call(this, {
|
|
367
|
+
profileKey,
|
|
368
|
+
bearerToken,
|
|
369
|
+
methodName: 'getBillingPortalUrl',
|
|
370
|
+
requestParams: null,
|
|
371
|
+
path: 'billing-portal',
|
|
206
372
|
errorMessage: constants_js_1.SubscriptionServiceErrorMessage.FailedToGetBillingPortalUrl,
|
|
207
373
|
});
|
|
374
|
+
return (0, superstruct_1.create)(jsonResponse, SubscriptionService_structs_js_1.BillingPortalResponseStruct);
|
|
208
375
|
}
|
|
209
376
|
}
|
|
210
377
|
exports.SubscriptionService = SubscriptionService;
|
|
211
|
-
_SubscriptionService_env = new WeakMap(), _SubscriptionService_fetch = new WeakMap(), _SubscriptionService_captureException = new WeakMap(), _SubscriptionService_instances = new WeakSet(),
|
|
212
|
-
/**
|
|
213
|
-
* Makes a request to the Subscription Service backend.
|
|
214
|
-
*
|
|
215
|
-
* @param params - The request object containing the path, method, body, query parameters, and error message.
|
|
216
|
-
* @param params.path - The path of the request.
|
|
217
|
-
* @param params.method - The method of the request.
|
|
218
|
-
* @param params.body - The body of the request.
|
|
219
|
-
* @param params.queryParams - The query parameters of the request.
|
|
220
|
-
* @param params.errorMessage - The error message to throw if the request fails.
|
|
221
|
-
* @returns The result of the request.
|
|
222
|
-
*/
|
|
223
|
-
async function _SubscriptionService_makeRequest({ path, method = 'GET', body, queryParams, errorMessage, }) {
|
|
378
|
+
_SubscriptionService_env = new WeakMap(), _SubscriptionService_fetch = new WeakMap(), _SubscriptionService_captureException = new WeakMap(), _SubscriptionService_instances = new WeakSet(), _SubscriptionService_fetchJson = async function _SubscriptionService_fetchJson({ profileKey, bearerToken, methodName, requestParams, path, method = 'GET', body, queryParams, errorMessage, }) {
|
|
224
379
|
const url = __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getSubscriptionApiUrl).call(this, path);
|
|
225
|
-
|
|
380
|
+
if (queryParams) {
|
|
381
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
382
|
+
url.searchParams.append(key, value);
|
|
383
|
+
});
|
|
384
|
+
}
|
|
226
385
|
try {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
386
|
+
return await this.fetchQuery({
|
|
387
|
+
queryKey: [
|
|
388
|
+
`${this.name}:${methodName}`,
|
|
389
|
+
profileKey,
|
|
390
|
+
requestParams,
|
|
391
|
+
],
|
|
392
|
+
staleTime: 0,
|
|
393
|
+
cacheTime: 0,
|
|
394
|
+
queryFn: async () => {
|
|
395
|
+
const response = await __classPrivateFieldGet(this, _SubscriptionService_fetch, "f").call(this, url.toString(), {
|
|
396
|
+
method,
|
|
397
|
+
headers: {
|
|
398
|
+
'Content-Type': 'application/json',
|
|
399
|
+
...__classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_headersForToken).call(this, bearerToken),
|
|
400
|
+
},
|
|
401
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
402
|
+
});
|
|
403
|
+
if (!response.ok) {
|
|
404
|
+
const apiError = await (0, errors_js_1.getSubscriptionErrorFromResponse)(response);
|
|
405
|
+
throw new controller_utils_1.HttpError(response.status, apiError.message);
|
|
406
|
+
}
|
|
407
|
+
return response.json();
|
|
237
408
|
},
|
|
238
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
239
409
|
});
|
|
240
|
-
if (!response.ok) {
|
|
241
|
-
const error = await (0, errors_js_1.getSubscriptionErrorFromResponse)(response);
|
|
242
|
-
throw error;
|
|
243
|
-
}
|
|
244
|
-
const data = await response.json();
|
|
245
|
-
return data;
|
|
246
410
|
}
|
|
247
411
|
catch (error) {
|
|
248
412
|
log(errorMessage, error);
|
|
249
413
|
const errorMessageWithUrl = `${errorMessage} (url: ${url.toString()})`;
|
|
250
414
|
const errorToCapture = error instanceof Error ? error : new Error(errorMessage);
|
|
251
415
|
__classPrivateFieldGet(this, _SubscriptionService_captureException, "f")?.call(this, (0, errors_js_1.createSentryError)(errorMessageWithUrl, errorToCapture));
|
|
416
|
+
if (error instanceof errors_js_1.SubscriptionServiceError) {
|
|
417
|
+
throw error;
|
|
418
|
+
}
|
|
252
419
|
throw new errors_js_1.SubscriptionServiceError(`Failed to make request. ${errorMessageWithUrl}`, {
|
|
253
420
|
cause: errorToCapture,
|
|
254
421
|
});
|
|
255
422
|
}
|
|
256
|
-
},
|
|
257
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
258
|
-
async function _SubscriptionService_getAuthorizationHeader() {
|
|
423
|
+
}, _SubscriptionService_getAuthenticatedContext = async function _SubscriptionService_getAuthenticatedContext() {
|
|
259
424
|
try {
|
|
260
|
-
const
|
|
261
|
-
|
|
425
|
+
const bearerToken = await this.messenger.call('AuthenticationController:getBearerToken');
|
|
426
|
+
const profileKey = (0, utils_1.bytesToHex)(await (0, utils_1.sha256)((0, utils_1.stringToBytes)(bearerToken)));
|
|
427
|
+
return { profileKey, bearerToken };
|
|
262
428
|
}
|
|
263
429
|
catch (error) {
|
|
264
430
|
const errorMessage = error instanceof Error
|
|
@@ -269,10 +435,11 @@ async function _SubscriptionService_getAuthorizationHeader() {
|
|
|
269
435
|
cause: error instanceof Error ? error : new Error(errorMessage),
|
|
270
436
|
});
|
|
271
437
|
}
|
|
438
|
+
}, _SubscriptionService_headersForToken = function _SubscriptionService_headersForToken(bearerToken) {
|
|
439
|
+
return { Authorization: `Bearer ${bearerToken}` };
|
|
272
440
|
}, _SubscriptionService_getSubscriptionApiUrl = function _SubscriptionService_getSubscriptionApiUrl(path) {
|
|
273
441
|
try {
|
|
274
|
-
|
|
275
|
-
return url;
|
|
442
|
+
return new URL((0, exports.SUBSCRIPTION_URL)(__classPrivateFieldGet(this, _SubscriptionService_env, "f"), path));
|
|
276
443
|
}
|
|
277
444
|
catch (error) {
|
|
278
445
|
const errorMessage = error instanceof Error
|