@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/README.md +1 -1
- package/dist/index.cjs +221 -81
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +315 -12
- package/dist/index.d.mts +315 -12
- package/dist/index.mjs +218 -82
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
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 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
|
+
accessToken: zod_mini.string(),
|
|
109
|
+
refreshToken: zod_mini.string(),
|
|
110
|
+
phone: zod_mini.string(),
|
|
111
|
+
pickupPoint: zod_mini.nullish(zod_mini.unknown()),
|
|
112
|
+
address: zod_mini.nullish(zod_mini.unknown()),
|
|
113
|
+
recipient: zod_mini.nullish(zod_mini.unknown()),
|
|
114
|
+
paymentId: zod_mini.nullish(zod_mini.number())
|
|
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"),
|
|
@@ -1069,7 +1228,7 @@ const LANGUAGES = {
|
|
|
1069
1228
|
/**
|
|
1070
1229
|
* Default application version code.
|
|
1071
1230
|
*/
|
|
1072
|
-
const DEFAULT_APP_VERSION = "
|
|
1231
|
+
const DEFAULT_APP_VERSION = "200";
|
|
1073
1232
|
|
|
1074
1233
|
//#endregion
|
|
1075
1234
|
//#region src/config.ts
|
|
@@ -1089,6 +1248,7 @@ const DEFAULT_CONFIG = {
|
|
|
1089
1248
|
function resolveConfig(config) {
|
|
1090
1249
|
return {
|
|
1091
1250
|
baseUrl: config?.baseUrl ?? DEFAULT_CONFIG.baseUrl,
|
|
1251
|
+
token: config?.token,
|
|
1092
1252
|
appVersion: config?.appVersion ?? DEFAULT_CONFIG.appVersion,
|
|
1093
1253
|
language: config?.language ?? DEFAULT_CONFIG.language,
|
|
1094
1254
|
timeout: config?.timeout ?? DEFAULT_CONFIG.timeout,
|
|
@@ -1108,23 +1268,16 @@ function buildUserAgent(appVersion) {
|
|
|
1108
1268
|
* Builds the headers object for API requests based on configuration.
|
|
1109
1269
|
*/
|
|
1110
1270
|
function buildHeaders(config) {
|
|
1111
|
-
|
|
1271
|
+
const headers = {
|
|
1112
1272
|
"accept-language": config.language,
|
|
1113
1273
|
"user-agent": buildUserAgent(config.appVersion),
|
|
1114
1274
|
"x-app-version": config.appVersion,
|
|
1115
1275
|
...config.headers
|
|
1116
1276
|
};
|
|
1277
|
+
if (config.token !== void 0 && config.token !== null) headers["authorization"] = `Bearer ${config.token}`;
|
|
1278
|
+
return headers;
|
|
1117
1279
|
}
|
|
1118
1280
|
|
|
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
1281
|
//#endregion
|
|
1129
1282
|
//#region src/errors/teez-api-error.ts
|
|
1130
1283
|
/**
|
|
@@ -1214,64 +1367,6 @@ var TeezTimeoutError = class extends TeezError {
|
|
|
1214
1367
|
}
|
|
1215
1368
|
};
|
|
1216
1369
|
|
|
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
1370
|
//#endregion
|
|
1276
1371
|
//#region src/http/client.ts
|
|
1277
1372
|
/**
|
|
@@ -1279,26 +1374,21 @@ function parseResponse(schema, data) {
|
|
|
1279
1374
|
*/
|
|
1280
1375
|
var HttpClient = class {
|
|
1281
1376
|
/**
|
|
1282
|
-
*
|
|
1377
|
+
* Client configuration.
|
|
1283
1378
|
*/
|
|
1284
|
-
|
|
1379
|
+
config;
|
|
1285
1380
|
/**
|
|
1286
1381
|
* Headers to include in all requests.
|
|
1287
1382
|
*/
|
|
1288
1383
|
headers;
|
|
1289
1384
|
/**
|
|
1290
|
-
* Request timeout in milliseconds.
|
|
1291
|
-
*/
|
|
1292
|
-
timeout;
|
|
1293
|
-
/**
|
|
1294
1385
|
* Initializes a new instance of the HttpClient.
|
|
1295
1386
|
*
|
|
1296
1387
|
* @param config Resolved client configuration.
|
|
1297
1388
|
*/
|
|
1298
1389
|
constructor(config) {
|
|
1299
|
-
this.
|
|
1390
|
+
this.config = config;
|
|
1300
1391
|
this.headers = buildHeaders(config);
|
|
1301
|
-
this.timeout = config.timeout;
|
|
1302
1392
|
}
|
|
1303
1393
|
/**
|
|
1304
1394
|
* Performs a low-level HTTP request.
|
|
@@ -1310,7 +1400,7 @@ var HttpClient = class {
|
|
|
1310
1400
|
const controller = new AbortController();
|
|
1311
1401
|
const timeoutId = setTimeout(() => {
|
|
1312
1402
|
controller.abort();
|
|
1313
|
-
}, this.timeout);
|
|
1403
|
+
}, this.config.timeout);
|
|
1314
1404
|
try {
|
|
1315
1405
|
const response = await fetch(url, {
|
|
1316
1406
|
...fetchOptions,
|
|
@@ -1334,12 +1424,13 @@ var HttpClient = class {
|
|
|
1334
1424
|
body
|
|
1335
1425
|
});
|
|
1336
1426
|
}
|
|
1427
|
+
if (response.status === 204) return;
|
|
1337
1428
|
return await response.json();
|
|
1338
1429
|
} catch (error) {
|
|
1339
1430
|
if (error instanceof TeezApiError) throw error;
|
|
1340
|
-
if (error instanceof DOMException && error.name === "AbortError") throw new TeezTimeoutError(`Request timed out after ${this.timeout}ms`, {
|
|
1431
|
+
if (error instanceof DOMException && error.name === "AbortError") throw new TeezTimeoutError(`Request timed out after ${this.config.timeout}ms`, {
|
|
1341
1432
|
url,
|
|
1342
|
-
timeout: this.timeout,
|
|
1433
|
+
timeout: this.config.timeout,
|
|
1343
1434
|
cause: error
|
|
1344
1435
|
});
|
|
1345
1436
|
throw new TeezNetworkError(`Network request failed`, {
|
|
@@ -1355,13 +1446,53 @@ var HttpClient = class {
|
|
|
1355
1446
|
*/
|
|
1356
1447
|
async get(options) {
|
|
1357
1448
|
const { path, params, schema, ...rest } = options;
|
|
1358
|
-
const url = buildUrl(path, this.baseUrl, params);
|
|
1449
|
+
const url = buildUrl(path, this.config.baseUrl, params);
|
|
1359
1450
|
return parseResponse(schema, await this.request({
|
|
1360
1451
|
url,
|
|
1361
1452
|
method: "GET",
|
|
1362
1453
|
...rest
|
|
1363
1454
|
}));
|
|
1364
1455
|
}
|
|
1456
|
+
/**
|
|
1457
|
+
* Performs a POST request.
|
|
1458
|
+
*/
|
|
1459
|
+
post(options) {
|
|
1460
|
+
const { path, params, body, ...rest } = options;
|
|
1461
|
+
const url = buildUrl(path, this.config.baseUrl, params);
|
|
1462
|
+
return this.request({
|
|
1463
|
+
url,
|
|
1464
|
+
method: "POST",
|
|
1465
|
+
headers: { "Content-Type": "application/json" },
|
|
1466
|
+
body: body != void 0 ? JSON.stringify(body) : void 0,
|
|
1467
|
+
...rest
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* Performs a PATCH request.
|
|
1472
|
+
*/
|
|
1473
|
+
patch(options) {
|
|
1474
|
+
const { path, params, body, ...rest } = options;
|
|
1475
|
+
const url = buildUrl(path, this.config.baseUrl, params);
|
|
1476
|
+
return this.request({
|
|
1477
|
+
url,
|
|
1478
|
+
method: "PATCH",
|
|
1479
|
+
headers: { "Content-Type": "application/json" },
|
|
1480
|
+
body: body != void 0 ? JSON.stringify(body) : void 0,
|
|
1481
|
+
...rest
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1484
|
+
/**
|
|
1485
|
+
* Performs a DELETE request.
|
|
1486
|
+
*/
|
|
1487
|
+
delete(options) {
|
|
1488
|
+
const { path, params, ...rest } = options;
|
|
1489
|
+
const url = buildUrl(path, this.config.baseUrl, params);
|
|
1490
|
+
return this.request({
|
|
1491
|
+
url,
|
|
1492
|
+
method: "DELETE",
|
|
1493
|
+
...rest
|
|
1494
|
+
});
|
|
1495
|
+
}
|
|
1365
1496
|
};
|
|
1366
1497
|
|
|
1367
1498
|
//#endregion
|
|
@@ -1385,6 +1516,10 @@ var TeezClient = class {
|
|
|
1385
1516
|
*/
|
|
1386
1517
|
http;
|
|
1387
1518
|
/**
|
|
1519
|
+
* API for authentication operations (login, verify OTP, check token).
|
|
1520
|
+
*/
|
|
1521
|
+
auth;
|
|
1522
|
+
/**
|
|
1388
1523
|
* API for retrieving banners.
|
|
1389
1524
|
*/
|
|
1390
1525
|
banners;
|
|
@@ -1424,6 +1559,7 @@ var TeezClient = class {
|
|
|
1424
1559
|
constructor(config) {
|
|
1425
1560
|
this.config = resolveConfig(config);
|
|
1426
1561
|
this.http = new HttpClient(this.config);
|
|
1562
|
+
this.auth = new AuthApi(this.http);
|
|
1427
1563
|
this.banners = new BannersApi(this.http);
|
|
1428
1564
|
this.categories = new CategoriesApi(this.http);
|
|
1429
1565
|
this.collections = new CollectionsApi(this.http);
|
|
@@ -1442,6 +1578,10 @@ var TeezClient = class {
|
|
|
1442
1578
|
};
|
|
1443
1579
|
|
|
1444
1580
|
//#endregion
|
|
1581
|
+
exports.AuthApi = AuthApi;
|
|
1582
|
+
exports.AuthApiCheckTokenResponseSchema = AuthApiCheckTokenResponseSchema;
|
|
1583
|
+
exports.AuthApiLoginResponseSchema = AuthApiLoginResponseSchema;
|
|
1584
|
+
exports.AuthApiVerifyResponseSchema = AuthApiVerifyResponseSchema;
|
|
1445
1585
|
exports.BASE_URL = BASE_URL;
|
|
1446
1586
|
exports.BannerActionTypesSchema = BannerActionTypesSchema;
|
|
1447
1587
|
exports.BannerImageTypeSchema = BannerImageTypeSchema;
|