@spritz-finance/service-client 0.26.1 → 0.28.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.
@@ -21,7 +21,8 @@ const interceptor = function (tokenProvider, authenticate) {
21
21
  return tokenProvider({ getToken, headerFormatter });
22
22
  };
23
23
  const createGraphClient = (config) => {
24
- const serviceClient = axios_1.default.create(config);
24
+ // See DEFAULT_TIMEOUT_MS in serviceClient.ts — same rationale.
25
+ const serviceClient = axios_1.default.create({ timeout: 10000, ...config });
25
26
  serviceClient.interceptors.request.use(interceptor(axios_token_interceptor_1.default, credentials_1.getGraphCredentials));
26
27
  // Add trace header propagation interceptor
27
28
  serviceClient.interceptors.request.use((requestConfig) => {
@@ -1,7 +1,16 @@
1
1
  export * as GrowthClient from "./growthServiceClient";
2
2
  import { PayableAccountType } from "../lib/graphClient/generated";
3
- import { PointsRedemption, PointsRedemptionOption } from "./types";
3
+ import { PointsRedemption, PointsRedemptionOption, ReferrerAccount } from "./types";
4
+ /**
5
+ * @deprecated Returns the referrer's email address, which can belong to several
6
+ * accounts. Use `getUserReferrerAccount` and resolve the referrer by `userId`.
7
+ */
4
8
  export declare function getUserReferrer(userId: string): Promise<string | null>;
9
+ /**
10
+ * The account that referred `userId`, or `null` when the user has no referral
11
+ * row or was not referred.
12
+ */
13
+ export declare function getUserReferrerAccount(userId: string): Promise<import("../types").ApiFailure | import("../types").ApiSuccess<ReferrerAccount | null>>;
5
14
  export declare function initializePointsRedemption(input: {
6
15
  userId: string;
7
16
  paymentRequestId: string;
@@ -35,6 +35,7 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.GrowthClient = void 0;
37
37
  exports.getUserReferrer = getUserReferrer;
38
+ exports.getUserReferrerAccount = getUserReferrerAccount;
38
39
  exports.initializePointsRedemption = initializePointsRedemption;
39
40
  exports.getPointsRedemptionOption = getPointsRedemptionOption;
40
41
  exports.validatePointsRedemption = validatePointsRedemption;
@@ -42,11 +43,25 @@ exports.confirmPointsRedemption = confirmPointsRedemption;
42
43
  exports.GrowthClient = __importStar(require("./growthServiceClient"));
43
44
  const serviceClient_1 = require("../serviceClient");
44
45
  const GROWTH_ENDPOINT = "/growth";
46
+ const GROWTH_SERVICE_API_ENDPOINT = "/v2/growth/api";
47
+ /**
48
+ * @deprecated Returns the referrer's email address, which can belong to several
49
+ * accounts. Use `getUserReferrerAccount` and resolve the referrer by `userId`.
50
+ */
45
51
  async function getUserReferrer(userId) {
46
52
  return serviceClient_1.baseClient
47
53
  .get(`${GROWTH_ENDPOINT}/referrals/referrer/${userId}`)
48
54
  .then((res) => res.data);
49
55
  }
56
+ /**
57
+ * The account that referred `userId`, or `null` when the user has no referral
58
+ * row or was not referred.
59
+ */
60
+ async function getUserReferrerAccount(userId) {
61
+ return serviceClient_1.baseClient
62
+ .get(`${GROWTH_SERVICE_API_ENDPOINT}/referrals/${encodeURIComponent(userId)}/referrer`)
63
+ .then((res) => res.data);
64
+ }
50
65
  async function initializePointsRedemption(input) {
51
66
  return serviceClient_1.baseClient
52
67
  .post(`${GROWTH_ENDPOINT}/v2/points/redemption/initialize`, input)
@@ -34,3 +34,12 @@ export interface PointsRedemptionOption {
34
34
  accountTypes?: PayableAccountType[];
35
35
  redeemDescription?: string;
36
36
  }
37
+ /**
38
+ * The account that referred a user, as `GrowthClient.getUserReferrerAccount`
39
+ * returns it. Resolve the referrer by `userId`; `email` is the address growth
40
+ * keeps on the referral row and identifies no account on its own.
41
+ */
42
+ export type ReferrerAccount = {
43
+ userId: string;
44
+ email: string | null;
45
+ };
@@ -60,6 +60,8 @@ const credentials_1 = require("../credentials");
60
60
  const EXPIRATION_BUFFER_MS = 20 * 60 * 1000;
61
61
  const platformClient = axios_1.default.create({
62
62
  baseURL: config_1.config.platformEndpoint,
63
+ // See DEFAULT_TIMEOUT_MS in serviceClient.ts — same rationale.
64
+ timeout: 10000,
63
65
  });
64
66
  platformClient.interceptors.request.use((0, axios_token_interceptor_1.default)({
65
67
  getToken: axios_token_interceptor_1.default.tokenCache(credentials_1.getServiceCredentials, {
@@ -1,3 +1,4 @@
1
1
  import { AxiosRequestConfig } from "axios";
2
+ export declare const DEFAULT_TIMEOUT_MS = 10000;
2
3
  export declare const createServiceClient: (config: AxiosRequestConfig) => import("axios").AxiosInstance;
3
4
  export declare const baseClient: import("axios").AxiosInstance;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.baseClient = exports.createServiceClient = void 0;
6
+ exports.baseClient = exports.createServiceClient = exports.DEFAULT_TIMEOUT_MS = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
8
  // @ts-expect-error no type declarations for axios-token-interceptor
9
9
  const axios_token_interceptor_1 = __importDefault(require("axios-token-interceptor"));
@@ -13,6 +13,11 @@ const credentials_1 = require("./credentials");
13
13
  const EXPIRATION_BUFFER_MS = 20 * 60 * 1000; // 20 minutes buffer
14
14
  const RETRY_DELAY_MS = 300;
15
15
  const RETRY_STATUS_CODE = 502;
16
+ // Axios defaults to no timeout, so a hung downstream pins the calling Lambda
17
+ // for its full timeout (30s on the Graph lambda) while holding a concurrency
18
+ // slot. 10s + the single 502 retry still fits inside that budget. Callers
19
+ // with a legitimately slow endpoint override `timeout` in their own config.
20
+ exports.DEFAULT_TIMEOUT_MS = 10000;
16
21
  const getMaxAge = () => {
17
22
  return EXPIRATION_BUFFER_MS;
18
23
  };
@@ -25,7 +30,7 @@ const interceptor = function (tokenProvider, authenticate) {
25
30
  };
26
31
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
27
32
  const createServiceClient = (config) => {
28
- const serviceClient = axios_1.default.create(config);
33
+ const serviceClient = axios_1.default.create({ timeout: exports.DEFAULT_TIMEOUT_MS, ...config });
29
34
  serviceClient.interceptors.request.use(interceptor(axios_token_interceptor_1.default, credentials_1.getServiceCredentials));
30
35
  // Add trace header propagation interceptor
31
36
  serviceClient.interceptors.request.use((requestConfig) => {
@@ -13,12 +13,57 @@ export interface User {
13
13
  createdAt: string;
14
14
  deleted: boolean;
15
15
  integratorId?: string | null;
16
+ loginMode?: "cognito" | "headless";
17
+ previousEmails?: string[];
16
18
  allNotificationPreferences: {
17
19
  type: NotificationType;
18
20
  email: boolean;
19
21
  mobile: boolean;
20
22
  }[];
21
23
  }
24
+ /**
25
+ * Tenant key of an account that has no home integrator. An email address can
26
+ * be held by one account per tenant, so every email lookup names its tenant:
27
+ * the integrator id for partner accounts, this value for Spritz-native ones.
28
+ */
29
+ export declare const NATIVE_TENANT = "native";
30
+ /**
31
+ * The tenant an account's email lives in: its home `integratorId`, or
32
+ * `NATIVE_TENANT` when it has none. Pass the result as the `tenant` of
33
+ * `UsersClient.findByEmailInTenant`.
34
+ */
35
+ export declare function tenantKeyFor(account: Pick<User, "integratorId">): string;
36
+ /** Codes the users service puts in `error.code` of its failure envelope. */
37
+ export declare const UsersFailureCode: {
38
+ /**
39
+ * `UsersClient.findByEmail`: more than one live account holds the address,
40
+ * so no single account can be returned. Name the tenant with
41
+ * `findByEmailInTenant`, or list the holders with `listByEmail` and choose.
42
+ */
43
+ readonly AmbiguousEmail: "AMBIGUOUS_EMAIL";
44
+ };
45
+ /**
46
+ * One account that holds, or held, an email address: a row of
47
+ * `UsersClient.listByEmail`. Deliberately thin — enough to pick an account
48
+ * explicitly (id and tenant) and to tell live accounts from tombstones.
49
+ */
50
+ export type UserEmailAccount = {
51
+ id: string;
52
+ /** The account's current address. */
53
+ email: string | null;
54
+ /** Home tenant; `null` for Spritz-native accounts. */
55
+ integratorId: string | null;
56
+ deleted: boolean;
57
+ createdAt: string | null;
58
+ /**
59
+ * `current` when the queried address is the account's email, `previous` when
60
+ * the account changed away from it.
61
+ */
62
+ emailMatch: "current" | "previous";
63
+ };
64
+ export type UsersByEmailData = {
65
+ users: UserEmailAccount[];
66
+ };
22
67
  export type IntegratorWebhook = {
23
68
  id: string;
24
69
  integratorId: string;
@@ -1,9 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotificationType = void 0;
3
+ exports.UsersFailureCode = exports.NATIVE_TENANT = exports.NotificationType = void 0;
4
+ exports.tenantKeyFor = tenantKeyFor;
4
5
  var NotificationType;
5
6
  (function (NotificationType) {
6
7
  NotificationType["CardTransactionApproved"] = "cardTransaction.approved";
7
8
  NotificationType["CardTransactionDeclined"] = "cardTransaction.declined";
8
9
  NotificationType["CreditAdded"] = "credit.added";
9
10
  })(NotificationType || (exports.NotificationType = NotificationType = {}));
11
+ /**
12
+ * Tenant key of an account that has no home integrator. An email address can
13
+ * be held by one account per tenant, so every email lookup names its tenant:
14
+ * the integrator id for partner accounts, this value for Spritz-native ones.
15
+ */
16
+ exports.NATIVE_TENANT = "native";
17
+ /**
18
+ * The tenant an account's email lives in: its home `integratorId`, or
19
+ * `NATIVE_TENANT` when it has none. Pass the result as the `tenant` of
20
+ * `UsersClient.findByEmailInTenant`.
21
+ */
22
+ function tenantKeyFor(account) {
23
+ var _a, _b;
24
+ return (_b = (_a = account.integratorId) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : exports.NATIVE_TENANT;
25
+ }
26
+ /** Codes the users service puts in `error.code` of its failure envelope. */
27
+ exports.UsersFailureCode = {
28
+ /**
29
+ * `UsersClient.findByEmail`: more than one live account holds the address,
30
+ * so no single account can be returned. Name the tenant with
31
+ * `findByEmailInTenant`, or list the holders with `listByEmail` and choose.
32
+ */
33
+ AmbiguousEmail: "AMBIGUOUS_EMAIL",
34
+ };
@@ -1,10 +1,34 @@
1
1
  export * as UsersClient from "./usersServiceClient";
2
- import { Integrator, IntegratorReference, IntegratorWebhook, User } from "./types";
2
+ import { Integrator, IntegratorReference, IntegratorWebhook, User, UsersByEmailData } from "./types";
3
3
  export declare function getActiveUsers(): Promise<import("../types").ApiFailure | import("../types").ApiSuccess<string[]>>;
4
4
  export declare function getUserById(id: string): Promise<import("../types").ApiFailure | import("../types").ApiSuccess<User>>;
5
5
  export declare function setMembership(userId: string, membership: string): Promise<import("../types").ApiFailure | import("../types").ApiSuccess<User>>;
6
6
  export declare function findOrInitialize(email: string): Promise<import("../types").ApiFailure | import("../types").ApiSuccess<User>>;
7
+ /**
8
+ * @deprecated Single-valued lookup across every tenant. An address can be held
9
+ * by one account per home tenant, so there is not always one answer: when more
10
+ * than one live account holds the email the users service answers the failure
11
+ * envelope `{ status: "failure", error: { code: "AMBIGUOUS_EMAIL" } }`
12
+ * (`UsersFailureCode.AmbiguousEmail`) instead of picking one, so a caller fails
13
+ * closed rather than act on an arbitrary account. Use `findByEmailInTenant`
14
+ * when the tenant is known, or `listByEmail` and choose explicitly.
15
+ */
7
16
  export declare function findByEmail(email: string): Promise<import("../types").ApiFailure | import("../types").ApiSuccess<User | null>>;
17
+ /**
18
+ * The one account holding `email` in `tenant`: an integrator id for partner
19
+ * accounts, `NATIVE_TENANT` for Spritz-native ones (`tenantKeyFor` derives it
20
+ * from an account already in hand). One account can hold an address per
21
+ * tenant, so this is the single-valued email lookup that stays unambiguous.
22
+ * `null` when the tenant has no such account.
23
+ */
24
+ export declare function findByEmailInTenant(email: string, tenant: string): Promise<import("../types").ApiFailure | import("../types").ApiSuccess<User | null>>;
25
+ /**
26
+ * Every account that holds `email` now or held it before an email change,
27
+ * across all tenants and including deleted ones. For tooling and automation
28
+ * that must pick an account explicitly: when more than one live row matches,
29
+ * refuse and ask for an account id rather than take the first.
30
+ */
31
+ export declare function listByEmail(email: string): Promise<import("../types").ApiFailure | import("../types").ApiSuccess<UsersByEmailData>>;
8
32
  export declare function validateAdIdentifiers(adIdentifiers?: {
9
33
  gclid?: string;
10
34
  twclid?: string;
@@ -39,6 +39,8 @@ exports.getUserById = getUserById;
39
39
  exports.setMembership = setMembership;
40
40
  exports.findOrInitialize = findOrInitialize;
41
41
  exports.findByEmail = findByEmail;
42
+ exports.findByEmailInTenant = findByEmailInTenant;
43
+ exports.listByEmail = listByEmail;
42
44
  exports.validateAdIdentifiers = validateAdIdentifiers;
43
45
  exports.getIntegratorWebhooks = getIntegratorWebhooks;
44
46
  exports.getIntegrator = getIntegrator;
@@ -70,11 +72,44 @@ async function findOrInitialize(email) {
70
72
  })
71
73
  .then((res) => res.data);
72
74
  }
75
+ /**
76
+ * @deprecated Single-valued lookup across every tenant. An address can be held
77
+ * by one account per home tenant, so there is not always one answer: when more
78
+ * than one live account holds the email the users service answers the failure
79
+ * envelope `{ status: "failure", error: { code: "AMBIGUOUS_EMAIL" } }`
80
+ * (`UsersFailureCode.AmbiguousEmail`) instead of picking one, so a caller fails
81
+ * closed rather than act on an arbitrary account. Use `findByEmailInTenant`
82
+ * when the tenant is known, or `listByEmail` and choose explicitly.
83
+ */
73
84
  async function findByEmail(email) {
74
85
  return serviceClient_1.baseClient
75
86
  .get(`${NEW_USER_ENDPOINT}/users?email=${encodeURIComponent(email)}`)
76
87
  .then((res) => res.data);
77
88
  }
89
+ /**
90
+ * The one account holding `email` in `tenant`: an integrator id for partner
91
+ * accounts, `NATIVE_TENANT` for Spritz-native ones (`tenantKeyFor` derives it
92
+ * from an account already in hand). One account can hold an address per
93
+ * tenant, so this is the single-valued email lookup that stays unambiguous.
94
+ * `null` when the tenant has no such account.
95
+ */
96
+ async function findByEmailInTenant(email, tenant) {
97
+ const query = `email=${encodeURIComponent(email)}&tenant=${encodeURIComponent(tenant)}`;
98
+ return serviceClient_1.baseClient
99
+ .get(`${NEW_USER_ENDPOINT}/users/by-email?${query}`)
100
+ .then((res) => res.data);
101
+ }
102
+ /**
103
+ * Every account that holds `email` now or held it before an email change,
104
+ * across all tenants and including deleted ones. For tooling and automation
105
+ * that must pick an account explicitly: when more than one live row matches,
106
+ * refuse and ask for an account id rather than take the first.
107
+ */
108
+ async function listByEmail(email) {
109
+ return serviceClient_1.baseClient
110
+ .get(`${NEW_USER_ENDPOINT}/users/all-by-email?email=${encodeURIComponent(email)}`)
111
+ .then((res) => res.data);
112
+ }
78
113
  async function validateAdIdentifiers(adIdentifiers) {
79
114
  return serviceClient_1.baseClient
80
115
  .post(`${NEW_USER_ENDPOINT}/users/ad-identifiers/validations`, adIdentifiers)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spritz-finance/service-client",
3
- "version": "0.26.1",
3
+ "version": "0.28.0",
4
4
  "gitHead": "06f8e96b7b38c34df26a38da329331940675e4f8",
5
5
  "description": "Service client",
6
6
  "license": "ISC",