@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/dist/index.mjs CHANGED
@@ -1,5 +1,163 @@
1
1
  import * as z from "zod/mini";
2
2
 
3
+ //#region src/errors/teez-error.ts
4
+ /**
5
+ * Base error class for all SDK-related errors.
6
+ */
7
+ var TeezError = class extends Error {
8
+ name = "TeezError";
9
+ };
10
+
11
+ //#endregion
12
+ //#region src/errors/teez-validation-error.ts
13
+ /**
14
+ * Error thrown when validation fails.
15
+ */
16
+ var TeezValidationError = class extends TeezError {
17
+ name = "TeezValidationError";
18
+ /**
19
+ * List of standardized validation issues.
20
+ */
21
+ issues;
22
+ /**
23
+ * The raw data that failed validation.
24
+ */
25
+ data;
26
+ constructor(message, { issues, data, ...errorOptions }) {
27
+ super(message, errorOptions);
28
+ this.issues = issues;
29
+ this.data = data;
30
+ }
31
+ };
32
+
33
+ //#endregion
34
+ //#region src/http/helpers.ts
35
+ /**
36
+ * Constructs a full URL with query parameters.
37
+ */
38
+ function buildUrl(path, baseUrl, queryParams) {
39
+ const url = new URL(path, baseUrl);
40
+ if (queryParams != void 0) for (const [key, value] of Object.entries(queryParams)) {
41
+ if (value == void 0) continue;
42
+ if (Array.isArray(value)) for (const item of value) url.searchParams.append(key, String(item));
43
+ else url.searchParams.set(key, String(value));
44
+ }
45
+ return String(url);
46
+ }
47
+ /**
48
+ * Converts Zod ZodError to abstract ValidationIssue[].
49
+ */
50
+ function toValidationIssues(error) {
51
+ return error.issues.map((issue) => ({
52
+ code: issue.code,
53
+ path: issue.path,
54
+ message: issue.message
55
+ }));
56
+ }
57
+ /**
58
+ * Validates and parses the API response data against a schema.
59
+ */
60
+ function parseResponse(schema, data) {
61
+ const result = z.safeParse(schema, data);
62
+ if (!result.success) throw new TeezValidationError("Response validation failed", {
63
+ issues: toValidationIssues(result.error),
64
+ data
65
+ });
66
+ return result.data;
67
+ }
68
+
69
+ //#endregion
70
+ //#region src/api/auth/schemas.ts
71
+ /**
72
+ * Response schema for initiating phone login.
73
+ */
74
+ const AuthApiLoginResponseSchema = z.void();
75
+ /**
76
+ * Response schema for OTP verification.
77
+ */
78
+ const AuthApiVerifyResponseSchema = z.object({
79
+ userId: z.string(),
80
+ phone: z.string(),
81
+ accessToken: z.string(),
82
+ refreshToken: z.string(),
83
+ paymentId: z.nullish(z.number()),
84
+ pickupPoint: z.nullish(z.unknown()),
85
+ address: z.nullish(z.unknown()),
86
+ recipient: z.nullish(z.unknown())
87
+ });
88
+ /**
89
+ * Response schema for token validation.
90
+ */
91
+ const AuthApiCheckTokenResponseSchema = z.object({
92
+ userId: z.string(),
93
+ phoneNumber: z.string(),
94
+ fullName: z.string(),
95
+ email: z.string(),
96
+ expiredTokenDate: z.string(),
97
+ language: z.enum(["ru", "kk"]),
98
+ hasOrders: z.boolean(),
99
+ hasAnyOrders: z.boolean()
100
+ });
101
+
102
+ //#endregion
103
+ //#region src/api/auth/api.ts
104
+ /**
105
+ * API for authentication operations.
106
+ */
107
+ var AuthApi = class {
108
+ /**
109
+ * Initializes a new instance of the AuthApi.
110
+ *
111
+ * @param http HTTP client instance.
112
+ */
113
+ constructor(http) {
114
+ this.http = http;
115
+ }
116
+ /**
117
+ * Initiates phone login by sending an OTP code to the specified phone number.
118
+ *
119
+ * @example
120
+ * await client.auth.login({
121
+ * phone: "+77071234567"
122
+ * });
123
+ */
124
+ async login(params) {
125
+ return parseResponse(AuthApiLoginResponseSchema, await this.http.post({
126
+ path: "/auth/login",
127
+ body: params
128
+ }));
129
+ }
130
+ /**
131
+ * Verifies OTP code and obtains JWT access and refresh tokens.
132
+ *
133
+ * @example
134
+ * const response = await client.auth.verify({
135
+ * phone: "+77071234567",
136
+ * otpCode: "2610"
137
+ * });
138
+ */
139
+ async verify(params) {
140
+ return parseResponse(AuthApiVerifyResponseSchema, await this.http.post({
141
+ path: "/auth/verify",
142
+ body: params
143
+ }));
144
+ }
145
+ /**
146
+ * Validates the current JWT token and retrieves user information.
147
+ *
148
+ * @example
149
+ * const response = await client.auth.checkToken();
150
+ */
151
+ async checkToken(params = {}) {
152
+ return await this.http.get({
153
+ path: "/auth/check-token",
154
+ params,
155
+ schema: AuthApiCheckTokenResponseSchema
156
+ });
157
+ }
158
+ };
159
+
160
+ //#endregion
3
161
  //#region src/api/banners/schemas.ts
4
162
  /**
5
163
  * Type literal for banner image resource type
@@ -414,6 +572,7 @@ var FeatureFlagsApi = class {
414
572
  * Type union for product sort keys
415
573
  */
416
574
  const ProductSortKeySchema = z.union([
575
+ z.literal("byRelevance"),
417
576
  z.literal("popularity"),
418
577
  z.literal("highestRated"),
419
578
  z.literal("new"),
@@ -1025,6 +1184,76 @@ var SkuApi = class {
1025
1184
  }
1026
1185
  };
1027
1186
 
1187
+ //#endregion
1188
+ //#region src/api/users/schemas.ts
1189
+ /**
1190
+ * Supported language enum for user preference
1191
+ */
1192
+ const UsersApiLanguageEnumSchema = z.enum(["ru", "kk"]);
1193
+ /**
1194
+ * Response schema for language update
1195
+ */
1196
+ const UsersApiUpdateLanguageResponseSchema = z.object({
1197
+ language: UsersApiLanguageEnumSchema,
1198
+ title: z.string(),
1199
+ message: z.string()
1200
+ });
1201
+ /**
1202
+ * Response schema for device registration
1203
+ */
1204
+ const UsersApiRegisterDeviceResponseSchema = z.nullish(z.null());
1205
+
1206
+ //#endregion
1207
+ //#region src/api/users/api.ts
1208
+ /**
1209
+ * API for user management operations.
1210
+ */
1211
+ var UsersApi = class {
1212
+ /**
1213
+ * Initializes a new instance of the UsersApi.
1214
+ *
1215
+ * @param http HTTP client instance.
1216
+ */
1217
+ constructor(http) {
1218
+ this.http = http;
1219
+ }
1220
+ /**
1221
+ * Updates the user's preferred language.
1222
+ *
1223
+ * @example
1224
+ * await client.users.updateLanguage({
1225
+ * language: "ru"
1226
+ * });
1227
+ */
1228
+ async updateLanguage(params) {
1229
+ return parseResponse(UsersApiUpdateLanguageResponseSchema, await this.http.patch({
1230
+ path: "/api/v1/users/me/language",
1231
+ body: params
1232
+ }));
1233
+ }
1234
+ /**
1235
+ * Registers device identity for analytics tracking.
1236
+ *
1237
+ * @example
1238
+ * await client.users.registerDevice({
1239
+ * deviceIdentity: {
1240
+ * sdkInformation: [
1241
+ * {
1242
+ * type: "Appsflyer",
1243
+ * deviceId: "1765694307025-6267413661002574019"
1244
+ * }
1245
+ * ]
1246
+ * }
1247
+ * });
1248
+ */
1249
+ async registerDevice(params) {
1250
+ return parseResponse(UsersApiRegisterDeviceResponseSchema, await this.http.post({
1251
+ path: "/api/v1/device-identities",
1252
+ body: params
1253
+ }));
1254
+ }
1255
+ };
1256
+
1028
1257
  //#endregion
1029
1258
  //#region src/common/constants.ts
1030
1259
  /**
@@ -1041,7 +1270,7 @@ const LANGUAGES = {
1041
1270
  /**
1042
1271
  * Default application version code.
1043
1272
  */
1044
- const DEFAULT_APP_VERSION = "193";
1273
+ const DEFAULT_APP_VERSION = "200";
1045
1274
 
1046
1275
  //#endregion
1047
1276
  //#region src/config.ts
@@ -1061,6 +1290,7 @@ const DEFAULT_CONFIG = {
1061
1290
  function resolveConfig(config) {
1062
1291
  return {
1063
1292
  baseUrl: config?.baseUrl ?? DEFAULT_CONFIG.baseUrl,
1293
+ token: config?.token,
1064
1294
  appVersion: config?.appVersion ?? DEFAULT_CONFIG.appVersion,
1065
1295
  language: config?.language ?? DEFAULT_CONFIG.language,
1066
1296
  timeout: config?.timeout ?? DEFAULT_CONFIG.timeout,
@@ -1080,23 +1310,16 @@ function buildUserAgent(appVersion) {
1080
1310
  * Builds the headers object for API requests based on configuration.
1081
1311
  */
1082
1312
  function buildHeaders(config) {
1083
- return {
1313
+ const headers = {
1084
1314
  "accept-language": config.language,
1085
1315
  "user-agent": buildUserAgent(config.appVersion),
1086
1316
  "x-app-version": config.appVersion,
1087
1317
  ...config.headers
1088
1318
  };
1319
+ if (config.token !== void 0 && config.token !== null) headers["authorization"] = `Bearer ${config.token}`;
1320
+ return headers;
1089
1321
  }
1090
1322
 
1091
- //#endregion
1092
- //#region src/errors/teez-error.ts
1093
- /**
1094
- * Base error class for all SDK-related errors.
1095
- */
1096
- var TeezError = class extends Error {
1097
- name = "TeezError";
1098
- };
1099
-
1100
1323
  //#endregion
1101
1324
  //#region src/errors/teez-api-error.ts
1102
1325
  /**
@@ -1186,64 +1409,6 @@ var TeezTimeoutError = class extends TeezError {
1186
1409
  }
1187
1410
  };
1188
1411
 
1189
- //#endregion
1190
- //#region src/errors/teez-validation-error.ts
1191
- /**
1192
- * Error thrown when validation fails.
1193
- */
1194
- var TeezValidationError = class extends TeezError {
1195
- name = "TeezValidationError";
1196
- /**
1197
- * List of standardized validation issues.
1198
- */
1199
- issues;
1200
- /**
1201
- * The raw data that failed validation.
1202
- */
1203
- data;
1204
- constructor(message, { issues, data, ...errorOptions }) {
1205
- super(message, errorOptions);
1206
- this.issues = issues;
1207
- this.data = data;
1208
- }
1209
- };
1210
-
1211
- //#endregion
1212
- //#region src/http/helpers.ts
1213
- /**
1214
- * Constructs a full URL with query parameters.
1215
- */
1216
- function buildUrl(path, baseUrl, queryParams) {
1217
- const url = new URL(path, baseUrl);
1218
- if (queryParams != void 0) for (const [key, value] of Object.entries(queryParams)) {
1219
- if (value == void 0) continue;
1220
- if (Array.isArray(value)) for (const item of value) url.searchParams.append(key, String(item));
1221
- else url.searchParams.set(key, String(value));
1222
- }
1223
- return String(url);
1224
- }
1225
- /**
1226
- * Converts Zod ZodError to abstract ValidationIssue[].
1227
- */
1228
- function toValidationIssues(error) {
1229
- return error.issues.map((issue) => ({
1230
- code: issue.code,
1231
- path: issue.path,
1232
- message: issue.message
1233
- }));
1234
- }
1235
- /**
1236
- * Validates and parses the API response data against a schema.
1237
- */
1238
- function parseResponse(schema, data) {
1239
- const result = z.safeParse(schema, data);
1240
- if (!result.success) throw new TeezValidationError("Response validation failed", {
1241
- issues: toValidationIssues(result.error),
1242
- data
1243
- });
1244
- return result.data;
1245
- }
1246
-
1247
1412
  //#endregion
1248
1413
  //#region src/http/client.ts
1249
1414
  /**
@@ -1251,26 +1416,21 @@ function parseResponse(schema, data) {
1251
1416
  */
1252
1417
  var HttpClient = class {
1253
1418
  /**
1254
- * Base URL for all requests.
1419
+ * Client configuration.
1255
1420
  */
1256
- baseUrl;
1421
+ config;
1257
1422
  /**
1258
1423
  * Headers to include in all requests.
1259
1424
  */
1260
1425
  headers;
1261
1426
  /**
1262
- * Request timeout in milliseconds.
1263
- */
1264
- timeout;
1265
- /**
1266
1427
  * Initializes a new instance of the HttpClient.
1267
1428
  *
1268
1429
  * @param config Resolved client configuration.
1269
1430
  */
1270
1431
  constructor(config) {
1271
- this.baseUrl = config.baseUrl;
1432
+ this.config = config;
1272
1433
  this.headers = buildHeaders(config);
1273
- this.timeout = config.timeout;
1274
1434
  }
1275
1435
  /**
1276
1436
  * Performs a low-level HTTP request.
@@ -1282,7 +1442,7 @@ var HttpClient = class {
1282
1442
  const controller = new AbortController();
1283
1443
  const timeoutId = setTimeout(() => {
1284
1444
  controller.abort();
1285
- }, this.timeout);
1445
+ }, this.config.timeout);
1286
1446
  try {
1287
1447
  const response = await fetch(url, {
1288
1448
  ...fetchOptions,
@@ -1306,12 +1466,13 @@ var HttpClient = class {
1306
1466
  body
1307
1467
  });
1308
1468
  }
1469
+ if (response.status === 204) return;
1309
1470
  return await response.json();
1310
1471
  } catch (error) {
1311
1472
  if (error instanceof TeezApiError) throw error;
1312
- if (error instanceof DOMException && error.name === "AbortError") throw new TeezTimeoutError(`Request timed out after ${this.timeout}ms`, {
1473
+ if (error instanceof DOMException && error.name === "AbortError") throw new TeezTimeoutError(`Request timed out after ${this.config.timeout}ms`, {
1313
1474
  url,
1314
- timeout: this.timeout,
1475
+ timeout: this.config.timeout,
1315
1476
  cause: error
1316
1477
  });
1317
1478
  throw new TeezNetworkError(`Network request failed`, {
@@ -1327,13 +1488,53 @@ var HttpClient = class {
1327
1488
  */
1328
1489
  async get(options) {
1329
1490
  const { path, params, schema, ...rest } = options;
1330
- const url = buildUrl(path, this.baseUrl, params);
1491
+ const url = buildUrl(path, this.config.baseUrl, params);
1331
1492
  return parseResponse(schema, await this.request({
1332
1493
  url,
1333
1494
  method: "GET",
1334
1495
  ...rest
1335
1496
  }));
1336
1497
  }
1498
+ /**
1499
+ * Performs a POST request.
1500
+ */
1501
+ post(options) {
1502
+ const { path, params, body, ...rest } = options;
1503
+ const url = buildUrl(path, this.config.baseUrl, params);
1504
+ return this.request({
1505
+ url,
1506
+ method: "POST",
1507
+ headers: { "Content-Type": "application/json" },
1508
+ body: body != void 0 ? JSON.stringify(body) : void 0,
1509
+ ...rest
1510
+ });
1511
+ }
1512
+ /**
1513
+ * Performs a PATCH request.
1514
+ */
1515
+ patch(options) {
1516
+ const { path, params, body, ...rest } = options;
1517
+ const url = buildUrl(path, this.config.baseUrl, params);
1518
+ return this.request({
1519
+ url,
1520
+ method: "PATCH",
1521
+ headers: { "Content-Type": "application/json" },
1522
+ body: body != void 0 ? JSON.stringify(body) : void 0,
1523
+ ...rest
1524
+ });
1525
+ }
1526
+ /**
1527
+ * Performs a DELETE request.
1528
+ */
1529
+ delete(options) {
1530
+ const { path, params, ...rest } = options;
1531
+ const url = buildUrl(path, this.config.baseUrl, params);
1532
+ return this.request({
1533
+ url,
1534
+ method: "DELETE",
1535
+ ...rest
1536
+ });
1537
+ }
1337
1538
  };
1338
1539
 
1339
1540
  //#endregion
@@ -1357,6 +1558,10 @@ var TeezClient = class {
1357
1558
  */
1358
1559
  http;
1359
1560
  /**
1561
+ * API for authentication operations.
1562
+ */
1563
+ auth;
1564
+ /**
1360
1565
  * API for retrieving banners.
1361
1566
  */
1362
1567
  banners;
@@ -1389,6 +1594,10 @@ var TeezClient = class {
1389
1594
  */
1390
1595
  sku;
1391
1596
  /**
1597
+ * API for user management operations.
1598
+ */
1599
+ users;
1600
+ /**
1392
1601
  * Initializes a new instance of the TeezClient.
1393
1602
  *
1394
1603
  * @param config Optional client configuration.
@@ -1396,6 +1605,7 @@ var TeezClient = class {
1396
1605
  constructor(config) {
1397
1606
  this.config = resolveConfig(config);
1398
1607
  this.http = new HttpClient(this.config);
1608
+ this.auth = new AuthApi(this.http);
1399
1609
  this.banners = new BannersApi(this.http);
1400
1610
  this.categories = new CategoriesApi(this.http);
1401
1611
  this.collections = new CollectionsApi(this.http);
@@ -1404,6 +1614,7 @@ var TeezClient = class {
1404
1614
  this.promo = new PromoApi(this.http);
1405
1615
  this.shops = new ShopsApi(this.http);
1406
1616
  this.sku = new SkuApi(this.http);
1617
+ this.users = new UsersApi(this.http);
1407
1618
  }
1408
1619
  /**
1409
1620
  * Returns the current client configuration.
@@ -1414,5 +1625,5 @@ var TeezClient = class {
1414
1625
  };
1415
1626
 
1416
1627
  //#endregion
1417
- export { BASE_URL, BannerActionTypesSchema, BannerImageTypeSchema, BannersApi, BannersApiActionSchema, BannersApiBannerItemSchema, BannersApiImageSchema, BannersApiListResponseSchema, CategoriesApi, CategoriesApiGetParentsResponseItemSchema, CategoriesApiGetParentsResponseSchema, CategoriesApiGetResponseSchema, CategoriesApiListResponseItemSchema, CategoriesApiListResponseSchema, CollectionTypeSchema, CollectionsApi, CollectionsApiGetResponseSchema, CollectionsApiGetSkusResponseSchema, CollectionsApiListItemSchema, CollectionsApiListResponseSchema, CollectionsApiSkuItemSchema, CollectionsApiStockAvailabilitySchema, CollectionsStockAvailabilityTypeSchema, DEFAULT_APP_VERSION, DEFAULT_CONFIG, FeatureFlagsApi, FeatureFlagsApiItemSchema, FeatureFlagsApiListResponseSchema, LANGUAGES, ProductSortKeySchema, ProductsApi, ProductsApiBadgeSchema, ProductsApiGetReviewsResponseSchema, ProductsApiGetSortOptionsResponseSchema, ProductsApiListResponseSchema, ProductsApiProductItemSchema, ProductsApiReviewItemSchema, ProductsApiSortOptionSchema, ProductsApiStockAvailabilitySchema, ProductsStockAvailabilityTypeSchema, PromoApi, PromoApiItemSchema, PromoApiListResponseSchema, ShopsApi, ShopsApiContactInfoSchema, ShopsApiGetMonobrandResponseSchema, ShopsApiGetProductsResponseSchema, ShopsApiGetResponseSchema, ShopsApiProductItemSchema, ShopsApiShopItemSchema, ShopsApiStockAvailabilitySchema, ShopsApiTagSchema, ShopsStockAvailabilityTypeSchema, SkuApi, SkuApiAttributePropertySchema, SkuApiAttributePropertyValueSchema, SkuApiAttributeSchema, SkuApiBrandSchema, SkuApiCategorySchema, SkuApiCollectionItemSchema, SkuApiGetCollectionsResponseSchema, SkuApiGetResponseSchema, SkuApiGetReviewAvailableResponseSchema, SkuApiGetSimilarResponseSchema, SkuApiInstallmentSchema, SkuApiShopSchema, SkuApiSimilarItemSchema, SkuApiStockAvailabilitySchema, SkuApiTagSchema, SkuStockAvailabilityTypeSchema, TeezApiError, TeezClient, TeezError, TeezNetworkError, TeezTimeoutError, TeezValidationError, buildHeaders, buildUserAgent, resolveConfig };
1628
+ export { AuthApi, AuthApiCheckTokenResponseSchema, AuthApiLoginResponseSchema, AuthApiVerifyResponseSchema, BASE_URL, BannerActionTypesSchema, BannerImageTypeSchema, BannersApi, BannersApiActionSchema, BannersApiBannerItemSchema, BannersApiImageSchema, BannersApiListResponseSchema, CategoriesApi, CategoriesApiGetParentsResponseItemSchema, CategoriesApiGetParentsResponseSchema, CategoriesApiGetResponseSchema, CategoriesApiListResponseItemSchema, CategoriesApiListResponseSchema, CollectionTypeSchema, CollectionsApi, CollectionsApiGetResponseSchema, CollectionsApiGetSkusResponseSchema, CollectionsApiListItemSchema, CollectionsApiListResponseSchema, CollectionsApiSkuItemSchema, CollectionsApiStockAvailabilitySchema, CollectionsStockAvailabilityTypeSchema, DEFAULT_APP_VERSION, DEFAULT_CONFIG, FeatureFlagsApi, FeatureFlagsApiItemSchema, FeatureFlagsApiListResponseSchema, LANGUAGES, ProductSortKeySchema, ProductsApi, ProductsApiBadgeSchema, ProductsApiGetReviewsResponseSchema, ProductsApiGetSortOptionsResponseSchema, ProductsApiListResponseSchema, ProductsApiProductItemSchema, ProductsApiReviewItemSchema, ProductsApiSortOptionSchema, ProductsApiStockAvailabilitySchema, ProductsStockAvailabilityTypeSchema, PromoApi, PromoApiItemSchema, PromoApiListResponseSchema, ShopsApi, ShopsApiContactInfoSchema, ShopsApiGetMonobrandResponseSchema, ShopsApiGetProductsResponseSchema, ShopsApiGetResponseSchema, ShopsApiProductItemSchema, ShopsApiShopItemSchema, ShopsApiStockAvailabilitySchema, ShopsApiTagSchema, ShopsStockAvailabilityTypeSchema, SkuApi, SkuApiAttributePropertySchema, SkuApiAttributePropertyValueSchema, SkuApiAttributeSchema, SkuApiBrandSchema, SkuApiCategorySchema, SkuApiCollectionItemSchema, SkuApiGetCollectionsResponseSchema, SkuApiGetResponseSchema, SkuApiGetReviewAvailableResponseSchema, SkuApiGetSimilarResponseSchema, SkuApiInstallmentSchema, SkuApiShopSchema, SkuApiSimilarItemSchema, SkuApiStockAvailabilitySchema, SkuApiTagSchema, SkuStockAvailabilityTypeSchema, TeezApiError, TeezClient, TeezError, TeezNetworkError, TeezTimeoutError, TeezValidationError, UsersApi, UsersApiLanguageEnumSchema, UsersApiRegisterDeviceResponseSchema, UsersApiUpdateLanguageResponseSchema, buildHeaders, buildUserAgent, resolveConfig };
1418
1629
  //# sourceMappingURL=index.mjs.map