@teez-sdk/teez-b2c-api 2.0.0 → 2.2.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.
package/README.md CHANGED
@@ -62,7 +62,7 @@ interface TeezClientConfig {
62
62
  appVersion?: string;
63
63
 
64
64
  /** Language for API responses. Default: "ru" */
65
- language?: "ru" | "kk" | "uz";
65
+ language?: "ru" | "kk";
66
66
 
67
67
  /** Request timeout in milliseconds. Default: 30000 */
68
68
  timeout?: number;
package/dist/index.cjs CHANGED
@@ -28,6 +28,164 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  let zod_mini = require("zod/mini");
29
29
  zod_mini = __toESM(zod_mini);
30
30
 
31
+ //#region src/errors/teez-error.ts
32
+ /**
33
+ * Base error class for all SDK-related errors.
34
+ */
35
+ var TeezError = class extends Error {
36
+ name = "TeezError";
37
+ };
38
+
39
+ //#endregion
40
+ //#region src/errors/teez-validation-error.ts
41
+ /**
42
+ * Error thrown when validation fails.
43
+ */
44
+ var TeezValidationError = class extends TeezError {
45
+ name = "TeezValidationError";
46
+ /**
47
+ * List of standardized validation issues.
48
+ */
49
+ issues;
50
+ /**
51
+ * The raw data that failed validation.
52
+ */
53
+ data;
54
+ constructor(message, { issues, data, ...errorOptions }) {
55
+ super(message, errorOptions);
56
+ this.issues = issues;
57
+ this.data = data;
58
+ }
59
+ };
60
+
61
+ //#endregion
62
+ //#region src/http/helpers.ts
63
+ /**
64
+ * Constructs a full URL with query parameters.
65
+ */
66
+ function buildUrl(path, baseUrl, queryParams) {
67
+ const url = new URL(path, baseUrl);
68
+ if (queryParams != void 0) for (const [key, value] of Object.entries(queryParams)) {
69
+ if (value == void 0) continue;
70
+ if (Array.isArray(value)) for (const item of value) url.searchParams.append(key, String(item));
71
+ else url.searchParams.set(key, String(value));
72
+ }
73
+ return String(url);
74
+ }
75
+ /**
76
+ * Converts Zod ZodError to abstract ValidationIssue[].
77
+ */
78
+ function toValidationIssues(error) {
79
+ return error.issues.map((issue) => ({
80
+ code: issue.code,
81
+ path: issue.path,
82
+ message: issue.message
83
+ }));
84
+ }
85
+ /**
86
+ * Validates and parses the API response data against a schema.
87
+ */
88
+ function parseResponse(schema, data) {
89
+ const result = zod_mini.safeParse(schema, data);
90
+ if (!result.success) throw new TeezValidationError("Response validation failed", {
91
+ issues: toValidationIssues(result.error),
92
+ data
93
+ });
94
+ return result.data;
95
+ }
96
+
97
+ //#endregion
98
+ //#region src/api/auth/schemas.ts
99
+ /**
100
+ * Response schema for initiating phone login.
101
+ */
102
+ const AuthApiLoginResponseSchema = zod_mini.void();
103
+ /**
104
+ * Response schema for OTP verification.
105
+ */
106
+ const AuthApiVerifyResponseSchema = zod_mini.object({
107
+ userId: zod_mini.string(),
108
+ phone: zod_mini.string(),
109
+ accessToken: zod_mini.string(),
110
+ refreshToken: zod_mini.string(),
111
+ paymentId: zod_mini.nullish(zod_mini.number()),
112
+ pickupPoint: zod_mini.nullish(zod_mini.unknown()),
113
+ address: zod_mini.nullish(zod_mini.unknown()),
114
+ recipient: zod_mini.nullish(zod_mini.unknown())
115
+ });
116
+ /**
117
+ * Response schema for token validation.
118
+ */
119
+ const AuthApiCheckTokenResponseSchema = zod_mini.object({
120
+ userId: zod_mini.string(),
121
+ phoneNumber: zod_mini.string(),
122
+ fullName: zod_mini.string(),
123
+ email: zod_mini.string(),
124
+ expiredTokenDate: zod_mini.string(),
125
+ language: zod_mini.enum(["ru", "kk"]),
126
+ hasOrders: zod_mini.boolean(),
127
+ hasAnyOrders: zod_mini.boolean()
128
+ });
129
+
130
+ //#endregion
131
+ //#region src/api/auth/api.ts
132
+ /**
133
+ * API for authentication operations.
134
+ */
135
+ var AuthApi = class {
136
+ /**
137
+ * Initializes a new instance of the AuthApi.
138
+ *
139
+ * @param http HTTP client instance.
140
+ */
141
+ constructor(http) {
142
+ this.http = http;
143
+ }
144
+ /**
145
+ * Initiates phone login by sending an OTP code to the specified phone number.
146
+ *
147
+ * @example
148
+ * await client.auth.login({
149
+ * phone: "+77071234567"
150
+ * });
151
+ */
152
+ async login(params) {
153
+ return parseResponse(AuthApiLoginResponseSchema, await this.http.post({
154
+ path: "/auth/login",
155
+ body: params
156
+ }));
157
+ }
158
+ /**
159
+ * Verifies OTP code and obtains JWT access and refresh tokens.
160
+ *
161
+ * @example
162
+ * const response = await client.auth.verify({
163
+ * phone: "+77071234567",
164
+ * otpCode: "2610"
165
+ * });
166
+ */
167
+ async verify(params) {
168
+ return parseResponse(AuthApiVerifyResponseSchema, await this.http.post({
169
+ path: "/auth/verify",
170
+ body: params
171
+ }));
172
+ }
173
+ /**
174
+ * Validates the current JWT token and retrieves user information.
175
+ *
176
+ * @example
177
+ * const response = await client.auth.checkToken();
178
+ */
179
+ async checkToken(params = {}) {
180
+ return await this.http.get({
181
+ path: "/auth/check-token",
182
+ params,
183
+ schema: AuthApiCheckTokenResponseSchema
184
+ });
185
+ }
186
+ };
187
+
188
+ //#endregion
31
189
  //#region src/api/banners/schemas.ts
32
190
  /**
33
191
  * Type literal for banner image resource type
@@ -442,6 +600,7 @@ var FeatureFlagsApi = class {
442
600
  * Type union for product sort keys
443
601
  */
444
602
  const ProductSortKeySchema = zod_mini.union([
603
+ zod_mini.literal("byRelevance"),
445
604
  zod_mini.literal("popularity"),
446
605
  zod_mini.literal("highestRated"),
447
606
  zod_mini.literal("new"),
@@ -1053,6 +1212,76 @@ var SkuApi = class {
1053
1212
  }
1054
1213
  };
1055
1214
 
1215
+ //#endregion
1216
+ //#region src/api/users/schemas.ts
1217
+ /**
1218
+ * Supported language enum for user preference
1219
+ */
1220
+ const UsersApiLanguageEnumSchema = zod_mini.enum(["ru", "kk"]);
1221
+ /**
1222
+ * Response schema for language update
1223
+ */
1224
+ const UsersApiUpdateLanguageResponseSchema = zod_mini.object({
1225
+ language: UsersApiLanguageEnumSchema,
1226
+ title: zod_mini.string(),
1227
+ message: zod_mini.string()
1228
+ });
1229
+ /**
1230
+ * Response schema for device registration
1231
+ */
1232
+ const UsersApiRegisterDeviceResponseSchema = zod_mini.nullish(zod_mini.null());
1233
+
1234
+ //#endregion
1235
+ //#region src/api/users/api.ts
1236
+ /**
1237
+ * API for user management operations.
1238
+ */
1239
+ var UsersApi = class {
1240
+ /**
1241
+ * Initializes a new instance of the UsersApi.
1242
+ *
1243
+ * @param http HTTP client instance.
1244
+ */
1245
+ constructor(http) {
1246
+ this.http = http;
1247
+ }
1248
+ /**
1249
+ * Updates the user's preferred language.
1250
+ *
1251
+ * @example
1252
+ * await client.users.updateLanguage({
1253
+ * language: "ru"
1254
+ * });
1255
+ */
1256
+ async updateLanguage(params) {
1257
+ return parseResponse(UsersApiUpdateLanguageResponseSchema, await this.http.patch({
1258
+ path: "/api/v1/users/me/language",
1259
+ body: params
1260
+ }));
1261
+ }
1262
+ /**
1263
+ * Registers device identity for analytics tracking.
1264
+ *
1265
+ * @example
1266
+ * await client.users.registerDevice({
1267
+ * deviceIdentity: {
1268
+ * sdkInformation: [
1269
+ * {
1270
+ * type: "Appsflyer",
1271
+ * deviceId: "1765694307025-6267413661002574019"
1272
+ * }
1273
+ * ]
1274
+ * }
1275
+ * });
1276
+ */
1277
+ async registerDevice(params) {
1278
+ return parseResponse(UsersApiRegisterDeviceResponseSchema, await this.http.post({
1279
+ path: "/api/v1/device-identities",
1280
+ body: params
1281
+ }));
1282
+ }
1283
+ };
1284
+
1056
1285
  //#endregion
1057
1286
  //#region src/common/constants.ts
1058
1287
  /**
@@ -1069,7 +1298,7 @@ const LANGUAGES = {
1069
1298
  /**
1070
1299
  * Default application version code.
1071
1300
  */
1072
- const DEFAULT_APP_VERSION = "193";
1301
+ const DEFAULT_APP_VERSION = "200";
1073
1302
 
1074
1303
  //#endregion
1075
1304
  //#region src/config.ts
@@ -1089,6 +1318,7 @@ const DEFAULT_CONFIG = {
1089
1318
  function resolveConfig(config) {
1090
1319
  return {
1091
1320
  baseUrl: config?.baseUrl ?? DEFAULT_CONFIG.baseUrl,
1321
+ token: config?.token,
1092
1322
  appVersion: config?.appVersion ?? DEFAULT_CONFIG.appVersion,
1093
1323
  language: config?.language ?? DEFAULT_CONFIG.language,
1094
1324
  timeout: config?.timeout ?? DEFAULT_CONFIG.timeout,
@@ -1108,23 +1338,16 @@ function buildUserAgent(appVersion) {
1108
1338
  * Builds the headers object for API requests based on configuration.
1109
1339
  */
1110
1340
  function buildHeaders(config) {
1111
- return {
1341
+ const headers = {
1112
1342
  "accept-language": config.language,
1113
1343
  "user-agent": buildUserAgent(config.appVersion),
1114
1344
  "x-app-version": config.appVersion,
1115
1345
  ...config.headers
1116
1346
  };
1347
+ if (config.token !== void 0 && config.token !== null) headers["authorization"] = `Bearer ${config.token}`;
1348
+ return headers;
1117
1349
  }
1118
1350
 
1119
- //#endregion
1120
- //#region src/errors/teez-error.ts
1121
- /**
1122
- * Base error class for all SDK-related errors.
1123
- */
1124
- var TeezError = class extends Error {
1125
- name = "TeezError";
1126
- };
1127
-
1128
1351
  //#endregion
1129
1352
  //#region src/errors/teez-api-error.ts
1130
1353
  /**
@@ -1214,64 +1437,6 @@ var TeezTimeoutError = class extends TeezError {
1214
1437
  }
1215
1438
  };
1216
1439
 
1217
- //#endregion
1218
- //#region src/errors/teez-validation-error.ts
1219
- /**
1220
- * Error thrown when validation fails.
1221
- */
1222
- var TeezValidationError = class extends TeezError {
1223
- name = "TeezValidationError";
1224
- /**
1225
- * List of standardized validation issues.
1226
- */
1227
- issues;
1228
- /**
1229
- * The raw data that failed validation.
1230
- */
1231
- data;
1232
- constructor(message, { issues, data, ...errorOptions }) {
1233
- super(message, errorOptions);
1234
- this.issues = issues;
1235
- this.data = data;
1236
- }
1237
- };
1238
-
1239
- //#endregion
1240
- //#region src/http/helpers.ts
1241
- /**
1242
- * Constructs a full URL with query parameters.
1243
- */
1244
- function buildUrl(path, baseUrl, queryParams) {
1245
- const url = new URL(path, baseUrl);
1246
- if (queryParams != void 0) for (const [key, value] of Object.entries(queryParams)) {
1247
- if (value == void 0) continue;
1248
- if (Array.isArray(value)) for (const item of value) url.searchParams.append(key, String(item));
1249
- else url.searchParams.set(key, String(value));
1250
- }
1251
- return String(url);
1252
- }
1253
- /**
1254
- * Converts Zod ZodError to abstract ValidationIssue[].
1255
- */
1256
- function toValidationIssues(error) {
1257
- return error.issues.map((issue) => ({
1258
- code: issue.code,
1259
- path: issue.path,
1260
- message: issue.message
1261
- }));
1262
- }
1263
- /**
1264
- * Validates and parses the API response data against a schema.
1265
- */
1266
- function parseResponse(schema, data) {
1267
- const result = zod_mini.safeParse(schema, data);
1268
- if (!result.success) throw new TeezValidationError("Response validation failed", {
1269
- issues: toValidationIssues(result.error),
1270
- data
1271
- });
1272
- return result.data;
1273
- }
1274
-
1275
1440
  //#endregion
1276
1441
  //#region src/http/client.ts
1277
1442
  /**
@@ -1279,26 +1444,21 @@ function parseResponse(schema, data) {
1279
1444
  */
1280
1445
  var HttpClient = class {
1281
1446
  /**
1282
- * Base URL for all requests.
1447
+ * Client configuration.
1283
1448
  */
1284
- baseUrl;
1449
+ config;
1285
1450
  /**
1286
1451
  * Headers to include in all requests.
1287
1452
  */
1288
1453
  headers;
1289
1454
  /**
1290
- * Request timeout in milliseconds.
1291
- */
1292
- timeout;
1293
- /**
1294
1455
  * Initializes a new instance of the HttpClient.
1295
1456
  *
1296
1457
  * @param config Resolved client configuration.
1297
1458
  */
1298
1459
  constructor(config) {
1299
- this.baseUrl = config.baseUrl;
1460
+ this.config = config;
1300
1461
  this.headers = buildHeaders(config);
1301
- this.timeout = config.timeout;
1302
1462
  }
1303
1463
  /**
1304
1464
  * Performs a low-level HTTP request.
@@ -1310,7 +1470,7 @@ var HttpClient = class {
1310
1470
  const controller = new AbortController();
1311
1471
  const timeoutId = setTimeout(() => {
1312
1472
  controller.abort();
1313
- }, this.timeout);
1473
+ }, this.config.timeout);
1314
1474
  try {
1315
1475
  const response = await fetch(url, {
1316
1476
  ...fetchOptions,
@@ -1334,12 +1494,13 @@ var HttpClient = class {
1334
1494
  body
1335
1495
  });
1336
1496
  }
1497
+ if (response.status === 204) return;
1337
1498
  return await response.json();
1338
1499
  } catch (error) {
1339
1500
  if (error instanceof TeezApiError) throw error;
1340
- if (error instanceof DOMException && error.name === "AbortError") throw new TeezTimeoutError(`Request timed out after ${this.timeout}ms`, {
1501
+ if (error instanceof DOMException && error.name === "AbortError") throw new TeezTimeoutError(`Request timed out after ${this.config.timeout}ms`, {
1341
1502
  url,
1342
- timeout: this.timeout,
1503
+ timeout: this.config.timeout,
1343
1504
  cause: error
1344
1505
  });
1345
1506
  throw new TeezNetworkError(`Network request failed`, {
@@ -1355,13 +1516,53 @@ var HttpClient = class {
1355
1516
  */
1356
1517
  async get(options) {
1357
1518
  const { path, params, schema, ...rest } = options;
1358
- const url = buildUrl(path, this.baseUrl, params);
1519
+ const url = buildUrl(path, this.config.baseUrl, params);
1359
1520
  return parseResponse(schema, await this.request({
1360
1521
  url,
1361
1522
  method: "GET",
1362
1523
  ...rest
1363
1524
  }));
1364
1525
  }
1526
+ /**
1527
+ * Performs a POST request.
1528
+ */
1529
+ post(options) {
1530
+ const { path, params, body, ...rest } = options;
1531
+ const url = buildUrl(path, this.config.baseUrl, params);
1532
+ return this.request({
1533
+ url,
1534
+ method: "POST",
1535
+ headers: { "Content-Type": "application/json" },
1536
+ body: body != void 0 ? JSON.stringify(body) : void 0,
1537
+ ...rest
1538
+ });
1539
+ }
1540
+ /**
1541
+ * Performs a PATCH request.
1542
+ */
1543
+ patch(options) {
1544
+ const { path, params, body, ...rest } = options;
1545
+ const url = buildUrl(path, this.config.baseUrl, params);
1546
+ return this.request({
1547
+ url,
1548
+ method: "PATCH",
1549
+ headers: { "Content-Type": "application/json" },
1550
+ body: body != void 0 ? JSON.stringify(body) : void 0,
1551
+ ...rest
1552
+ });
1553
+ }
1554
+ /**
1555
+ * Performs a DELETE request.
1556
+ */
1557
+ delete(options) {
1558
+ const { path, params, ...rest } = options;
1559
+ const url = buildUrl(path, this.config.baseUrl, params);
1560
+ return this.request({
1561
+ url,
1562
+ method: "DELETE",
1563
+ ...rest
1564
+ });
1565
+ }
1365
1566
  };
1366
1567
 
1367
1568
  //#endregion
@@ -1385,6 +1586,10 @@ var TeezClient = class {
1385
1586
  */
1386
1587
  http;
1387
1588
  /**
1589
+ * API for authentication operations.
1590
+ */
1591
+ auth;
1592
+ /**
1388
1593
  * API for retrieving banners.
1389
1594
  */
1390
1595
  banners;
@@ -1417,6 +1622,10 @@ var TeezClient = class {
1417
1622
  */
1418
1623
  sku;
1419
1624
  /**
1625
+ * API for user management operations.
1626
+ */
1627
+ users;
1628
+ /**
1420
1629
  * Initializes a new instance of the TeezClient.
1421
1630
  *
1422
1631
  * @param config Optional client configuration.
@@ -1424,6 +1633,7 @@ var TeezClient = class {
1424
1633
  constructor(config) {
1425
1634
  this.config = resolveConfig(config);
1426
1635
  this.http = new HttpClient(this.config);
1636
+ this.auth = new AuthApi(this.http);
1427
1637
  this.banners = new BannersApi(this.http);
1428
1638
  this.categories = new CategoriesApi(this.http);
1429
1639
  this.collections = new CollectionsApi(this.http);
@@ -1432,6 +1642,7 @@ var TeezClient = class {
1432
1642
  this.promo = new PromoApi(this.http);
1433
1643
  this.shops = new ShopsApi(this.http);
1434
1644
  this.sku = new SkuApi(this.http);
1645
+ this.users = new UsersApi(this.http);
1435
1646
  }
1436
1647
  /**
1437
1648
  * Returns the current client configuration.
@@ -1442,6 +1653,10 @@ var TeezClient = class {
1442
1653
  };
1443
1654
 
1444
1655
  //#endregion
1656
+ exports.AuthApi = AuthApi;
1657
+ exports.AuthApiCheckTokenResponseSchema = AuthApiCheckTokenResponseSchema;
1658
+ exports.AuthApiLoginResponseSchema = AuthApiLoginResponseSchema;
1659
+ exports.AuthApiVerifyResponseSchema = AuthApiVerifyResponseSchema;
1445
1660
  exports.BASE_URL = BASE_URL;
1446
1661
  exports.BannerActionTypesSchema = BannerActionTypesSchema;
1447
1662
  exports.BannerImageTypeSchema = BannerImageTypeSchema;
@@ -1518,6 +1733,10 @@ exports.TeezError = TeezError;
1518
1733
  exports.TeezNetworkError = TeezNetworkError;
1519
1734
  exports.TeezTimeoutError = TeezTimeoutError;
1520
1735
  exports.TeezValidationError = TeezValidationError;
1736
+ exports.UsersApi = UsersApi;
1737
+ exports.UsersApiLanguageEnumSchema = UsersApiLanguageEnumSchema;
1738
+ exports.UsersApiRegisterDeviceResponseSchema = UsersApiRegisterDeviceResponseSchema;
1739
+ exports.UsersApiUpdateLanguageResponseSchema = UsersApiUpdateLanguageResponseSchema;
1521
1740
  exports.buildHeaders = buildHeaders;
1522
1741
  exports.buildUserAgent = buildUserAgent;
1523
1742
  exports.resolveConfig = resolveConfig;