@teez-sdk/teez-b2c-api 2.0.0 → 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"),
@@ -1041,7 +1200,7 @@ const LANGUAGES = {
1041
1200
  /**
1042
1201
  * Default application version code.
1043
1202
  */
1044
- const DEFAULT_APP_VERSION = "193";
1203
+ const DEFAULT_APP_VERSION = "200";
1045
1204
 
1046
1205
  //#endregion
1047
1206
  //#region src/config.ts
@@ -1061,6 +1220,7 @@ const DEFAULT_CONFIG = {
1061
1220
  function resolveConfig(config) {
1062
1221
  return {
1063
1222
  baseUrl: config?.baseUrl ?? DEFAULT_CONFIG.baseUrl,
1223
+ token: config?.token,
1064
1224
  appVersion: config?.appVersion ?? DEFAULT_CONFIG.appVersion,
1065
1225
  language: config?.language ?? DEFAULT_CONFIG.language,
1066
1226
  timeout: config?.timeout ?? DEFAULT_CONFIG.timeout,
@@ -1080,23 +1240,16 @@ function buildUserAgent(appVersion) {
1080
1240
  * Builds the headers object for API requests based on configuration.
1081
1241
  */
1082
1242
  function buildHeaders(config) {
1083
- return {
1243
+ const headers = {
1084
1244
  "accept-language": config.language,
1085
1245
  "user-agent": buildUserAgent(config.appVersion),
1086
1246
  "x-app-version": config.appVersion,
1087
1247
  ...config.headers
1088
1248
  };
1249
+ if (config.token !== void 0 && config.token !== null) headers["authorization"] = `Bearer ${config.token}`;
1250
+ return headers;
1089
1251
  }
1090
1252
 
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
1253
  //#endregion
1101
1254
  //#region src/errors/teez-api-error.ts
1102
1255
  /**
@@ -1186,64 +1339,6 @@ var TeezTimeoutError = class extends TeezError {
1186
1339
  }
1187
1340
  };
1188
1341
 
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
1342
  //#endregion
1248
1343
  //#region src/http/client.ts
1249
1344
  /**
@@ -1251,26 +1346,21 @@ function parseResponse(schema, data) {
1251
1346
  */
1252
1347
  var HttpClient = class {
1253
1348
  /**
1254
- * Base URL for all requests.
1349
+ * Client configuration.
1255
1350
  */
1256
- baseUrl;
1351
+ config;
1257
1352
  /**
1258
1353
  * Headers to include in all requests.
1259
1354
  */
1260
1355
  headers;
1261
1356
  /**
1262
- * Request timeout in milliseconds.
1263
- */
1264
- timeout;
1265
- /**
1266
1357
  * Initializes a new instance of the HttpClient.
1267
1358
  *
1268
1359
  * @param config Resolved client configuration.
1269
1360
  */
1270
1361
  constructor(config) {
1271
- this.baseUrl = config.baseUrl;
1362
+ this.config = config;
1272
1363
  this.headers = buildHeaders(config);
1273
- this.timeout = config.timeout;
1274
1364
  }
1275
1365
  /**
1276
1366
  * Performs a low-level HTTP request.
@@ -1282,7 +1372,7 @@ var HttpClient = class {
1282
1372
  const controller = new AbortController();
1283
1373
  const timeoutId = setTimeout(() => {
1284
1374
  controller.abort();
1285
- }, this.timeout);
1375
+ }, this.config.timeout);
1286
1376
  try {
1287
1377
  const response = await fetch(url, {
1288
1378
  ...fetchOptions,
@@ -1306,12 +1396,13 @@ var HttpClient = class {
1306
1396
  body
1307
1397
  });
1308
1398
  }
1399
+ if (response.status === 204) return;
1309
1400
  return await response.json();
1310
1401
  } catch (error) {
1311
1402
  if (error instanceof TeezApiError) throw error;
1312
- 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`, {
1313
1404
  url,
1314
- timeout: this.timeout,
1405
+ timeout: this.config.timeout,
1315
1406
  cause: error
1316
1407
  });
1317
1408
  throw new TeezNetworkError(`Network request failed`, {
@@ -1327,13 +1418,53 @@ var HttpClient = class {
1327
1418
  */
1328
1419
  async get(options) {
1329
1420
  const { path, params, schema, ...rest } = options;
1330
- const url = buildUrl(path, this.baseUrl, params);
1421
+ const url = buildUrl(path, this.config.baseUrl, params);
1331
1422
  return parseResponse(schema, await this.request({
1332
1423
  url,
1333
1424
  method: "GET",
1334
1425
  ...rest
1335
1426
  }));
1336
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
+ }
1337
1468
  };
1338
1469
 
1339
1470
  //#endregion
@@ -1357,6 +1488,10 @@ var TeezClient = class {
1357
1488
  */
1358
1489
  http;
1359
1490
  /**
1491
+ * API for authentication operations (login, verify OTP, check token).
1492
+ */
1493
+ auth;
1494
+ /**
1360
1495
  * API for retrieving banners.
1361
1496
  */
1362
1497
  banners;
@@ -1396,6 +1531,7 @@ var TeezClient = class {
1396
1531
  constructor(config) {
1397
1532
  this.config = resolveConfig(config);
1398
1533
  this.http = new HttpClient(this.config);
1534
+ this.auth = new AuthApi(this.http);
1399
1535
  this.banners = new BannersApi(this.http);
1400
1536
  this.categories = new CategoriesApi(this.http);
1401
1537
  this.collections = new CollectionsApi(this.http);
@@ -1414,5 +1550,5 @@ var TeezClient = class {
1414
1550
  };
1415
1551
 
1416
1552
  //#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 };
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 };
1418
1554
  //# sourceMappingURL=index.mjs.map