@teez-sdk/teez-b2c-api 1.0.1 → 2.1.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 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
+ accessToken: z.string(),
81
+ refreshToken: z.string(),
82
+ phone: z.string(),
83
+ pickupPoint: z.nullish(z.unknown()),
84
+ address: z.nullish(z.unknown()),
85
+ recipient: z.nullish(z.unknown()),
86
+ paymentId: z.nullish(z.number())
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"),
@@ -1039,20 +1198,9 @@ const LANGUAGES = {
1039
1198
  KZ: "kz"
1040
1199
  };
1041
1200
  /**
1042
- * Standard sort options for product and collection searches
1043
- */
1044
- const SORT_OPTIONS = {
1045
- BY_RELEVANCE: "byRelevance",
1046
- POPULARITY: "popularity",
1047
- HIGHEST_RATED: "highestRated",
1048
- NEW: "new",
1049
- PRICE: "price",
1050
- PRICE_DESC: "priceDesc"
1051
- };
1052
- /**
1053
1201
  * Default application version code.
1054
1202
  */
1055
- const DEFAULT_APP_VERSION = "193";
1203
+ const DEFAULT_APP_VERSION = "200";
1056
1204
 
1057
1205
  //#endregion
1058
1206
  //#region src/config.ts
@@ -1072,6 +1220,7 @@ const DEFAULT_CONFIG = {
1072
1220
  function resolveConfig(config) {
1073
1221
  return {
1074
1222
  baseUrl: config?.baseUrl ?? DEFAULT_CONFIG.baseUrl,
1223
+ token: config?.token,
1075
1224
  appVersion: config?.appVersion ?? DEFAULT_CONFIG.appVersion,
1076
1225
  language: config?.language ?? DEFAULT_CONFIG.language,
1077
1226
  timeout: config?.timeout ?? DEFAULT_CONFIG.timeout,
@@ -1091,23 +1240,16 @@ function buildUserAgent(appVersion) {
1091
1240
  * Builds the headers object for API requests based on configuration.
1092
1241
  */
1093
1242
  function buildHeaders(config) {
1094
- return {
1243
+ const headers = {
1095
1244
  "accept-language": config.language,
1096
1245
  "user-agent": buildUserAgent(config.appVersion),
1097
1246
  "x-app-version": config.appVersion,
1098
1247
  ...config.headers
1099
1248
  };
1249
+ if (config.token !== void 0 && config.token !== null) headers["authorization"] = `Bearer ${config.token}`;
1250
+ return headers;
1100
1251
  }
1101
1252
 
1102
- //#endregion
1103
- //#region src/errors/teez-error.ts
1104
- /**
1105
- * Base error class for all SDK-related errors.
1106
- */
1107
- var TeezError = class extends Error {
1108
- name = "TeezError";
1109
- };
1110
-
1111
1253
  //#endregion
1112
1254
  //#region src/errors/teez-api-error.ts
1113
1255
  /**
@@ -1197,64 +1339,6 @@ var TeezTimeoutError = class extends TeezError {
1197
1339
  }
1198
1340
  };
1199
1341
 
1200
- //#endregion
1201
- //#region src/errors/teez-validation-error.ts
1202
- /**
1203
- * Error thrown when validation fails.
1204
- */
1205
- var TeezValidationError = class extends TeezError {
1206
- name = "TeezValidationError";
1207
- /**
1208
- * List of standardized validation issues.
1209
- */
1210
- issues;
1211
- /**
1212
- * The raw data that failed validation.
1213
- */
1214
- data;
1215
- constructor(message, { issues, data, ...errorOptions }) {
1216
- super(message, errorOptions);
1217
- this.issues = issues;
1218
- this.data = data;
1219
- }
1220
- };
1221
-
1222
- //#endregion
1223
- //#region src/http/helpers.ts
1224
- /**
1225
- * Constructs a full URL with query parameters.
1226
- */
1227
- function buildUrl(path, baseUrl, queryParams) {
1228
- const url = new URL(path, baseUrl);
1229
- if (queryParams != void 0) for (const [key, value] of Object.entries(queryParams)) {
1230
- if (value == void 0) continue;
1231
- if (Array.isArray(value)) for (const item of value) url.searchParams.append(key, String(item));
1232
- else url.searchParams.set(key, String(value));
1233
- }
1234
- return String(url);
1235
- }
1236
- /**
1237
- * Converts Zod ZodError to abstract ValidationIssue[].
1238
- */
1239
- function toValidationIssues(error) {
1240
- return error.issues.map((issue) => ({
1241
- code: issue.code,
1242
- path: issue.path,
1243
- message: issue.message
1244
- }));
1245
- }
1246
- /**
1247
- * Validates and parses the API response data against a schema.
1248
- */
1249
- function parseResponse(schema, data) {
1250
- const result = z.safeParse(schema, data);
1251
- if (!result.success) throw new TeezValidationError("Response validation failed", {
1252
- issues: toValidationIssues(result.error),
1253
- data
1254
- });
1255
- return result.data;
1256
- }
1257
-
1258
1342
  //#endregion
1259
1343
  //#region src/http/client.ts
1260
1344
  /**
@@ -1262,26 +1346,21 @@ function parseResponse(schema, data) {
1262
1346
  */
1263
1347
  var HttpClient = class {
1264
1348
  /**
1265
- * Base URL for all requests.
1349
+ * Client configuration.
1266
1350
  */
1267
- baseUrl;
1351
+ config;
1268
1352
  /**
1269
1353
  * Headers to include in all requests.
1270
1354
  */
1271
1355
  headers;
1272
1356
  /**
1273
- * Request timeout in milliseconds.
1274
- */
1275
- timeout;
1276
- /**
1277
1357
  * Initializes a new instance of the HttpClient.
1278
1358
  *
1279
1359
  * @param config Resolved client configuration.
1280
1360
  */
1281
1361
  constructor(config) {
1282
- this.baseUrl = config.baseUrl;
1362
+ this.config = config;
1283
1363
  this.headers = buildHeaders(config);
1284
- this.timeout = config.timeout;
1285
1364
  }
1286
1365
  /**
1287
1366
  * Performs a low-level HTTP request.
@@ -1293,7 +1372,7 @@ var HttpClient = class {
1293
1372
  const controller = new AbortController();
1294
1373
  const timeoutId = setTimeout(() => {
1295
1374
  controller.abort();
1296
- }, this.timeout);
1375
+ }, this.config.timeout);
1297
1376
  try {
1298
1377
  const response = await fetch(url, {
1299
1378
  ...fetchOptions,
@@ -1317,12 +1396,13 @@ var HttpClient = class {
1317
1396
  body
1318
1397
  });
1319
1398
  }
1399
+ if (response.status === 204) return;
1320
1400
  return await response.json();
1321
1401
  } catch (error) {
1322
1402
  if (error instanceof TeezApiError) throw error;
1323
- if (error instanceof DOMException && error.name === "AbortError") throw new TeezTimeoutError(`Request timed out after ${this.timeout}ms`, {
1403
+ if (error instanceof DOMException && error.name === "AbortError") throw new TeezTimeoutError(`Request timed out after ${this.config.timeout}ms`, {
1324
1404
  url,
1325
- timeout: this.timeout,
1405
+ timeout: this.config.timeout,
1326
1406
  cause: error
1327
1407
  });
1328
1408
  throw new TeezNetworkError(`Network request failed`, {
@@ -1338,13 +1418,53 @@ var HttpClient = class {
1338
1418
  */
1339
1419
  async get(options) {
1340
1420
  const { path, params, schema, ...rest } = options;
1341
- const url = buildUrl(path, this.baseUrl, params);
1421
+ const url = buildUrl(path, this.config.baseUrl, params);
1342
1422
  return parseResponse(schema, await this.request({
1343
1423
  url,
1344
1424
  method: "GET",
1345
1425
  ...rest
1346
1426
  }));
1347
1427
  }
1428
+ /**
1429
+ * Performs a POST request.
1430
+ */
1431
+ post(options) {
1432
+ const { path, params, body, ...rest } = options;
1433
+ const url = buildUrl(path, this.config.baseUrl, params);
1434
+ return this.request({
1435
+ url,
1436
+ method: "POST",
1437
+ headers: { "Content-Type": "application/json" },
1438
+ body: body != void 0 ? JSON.stringify(body) : void 0,
1439
+ ...rest
1440
+ });
1441
+ }
1442
+ /**
1443
+ * Performs a PATCH request.
1444
+ */
1445
+ patch(options) {
1446
+ const { path, params, body, ...rest } = options;
1447
+ const url = buildUrl(path, this.config.baseUrl, params);
1448
+ return this.request({
1449
+ url,
1450
+ method: "PATCH",
1451
+ headers: { "Content-Type": "application/json" },
1452
+ body: body != void 0 ? JSON.stringify(body) : void 0,
1453
+ ...rest
1454
+ });
1455
+ }
1456
+ /**
1457
+ * Performs a DELETE request.
1458
+ */
1459
+ delete(options) {
1460
+ const { path, params, ...rest } = options;
1461
+ const url = buildUrl(path, this.config.baseUrl, params);
1462
+ return this.request({
1463
+ url,
1464
+ method: "DELETE",
1465
+ ...rest
1466
+ });
1467
+ }
1348
1468
  };
1349
1469
 
1350
1470
  //#endregion
@@ -1368,6 +1488,10 @@ var TeezClient = class {
1368
1488
  */
1369
1489
  http;
1370
1490
  /**
1491
+ * API for authentication operations (login, verify OTP, check token).
1492
+ */
1493
+ auth;
1494
+ /**
1371
1495
  * API for retrieving banners.
1372
1496
  */
1373
1497
  banners;
@@ -1407,6 +1531,7 @@ var TeezClient = class {
1407
1531
  constructor(config) {
1408
1532
  this.config = resolveConfig(config);
1409
1533
  this.http = new HttpClient(this.config);
1534
+ this.auth = new AuthApi(this.http);
1410
1535
  this.banners = new BannersApi(this.http);
1411
1536
  this.categories = new CategoriesApi(this.http);
1412
1537
  this.collections = new CollectionsApi(this.http);
@@ -1425,5 +1550,5 @@ var TeezClient = class {
1425
1550
  };
1426
1551
 
1427
1552
  //#endregion
1428
- 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, SORT_OPTIONS, 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 };
1553
+ 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, buildHeaders, buildUserAgent, resolveConfig };
1429
1554
  //# sourceMappingURL=index.mjs.map