3a-ecommerce-utils 1.0.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.
Files changed (45) hide show
  1. package/README.md +163 -0
  2. package/dist/chunk-PEAZVBSD.mjs +597 -0
  3. package/dist/client-DYGi_pyp.d.mts +87 -0
  4. package/dist/client-DYGi_pyp.d.ts +87 -0
  5. package/dist/index.d.mts +496 -0
  6. package/dist/index.d.ts +496 -0
  7. package/dist/index.js +17707 -0
  8. package/dist/index.mjs +17043 -0
  9. package/dist/validation/server.d.mts +50 -0
  10. package/dist/validation/server.d.ts +50 -0
  11. package/dist/validation/server.js +518 -0
  12. package/dist/validation/server.mjs +168 -0
  13. package/package.json +69 -0
  14. package/src/api/address.queries.ts +96 -0
  15. package/src/api/category.queries.ts +85 -0
  16. package/src/api/coupon.queries.ts +120 -0
  17. package/src/api/dashboard.queries.ts +35 -0
  18. package/src/api/errorHandler.ts +164 -0
  19. package/src/api/graphqlClient.ts +113 -0
  20. package/src/api/index.ts +10 -0
  21. package/src/api/logger.client.ts +89 -0
  22. package/src/api/logger.ts +135 -0
  23. package/src/api/order.queries.ts +211 -0
  24. package/src/api/product.queries.ts +144 -0
  25. package/src/api/review.queries.ts +56 -0
  26. package/src/api/user.queries.ts +232 -0
  27. package/src/assets/3A.png +0 -0
  28. package/src/assets/index.ts +1 -0
  29. package/src/assets.d.ts +29 -0
  30. package/src/auth.ts +176 -0
  31. package/src/config/jest.backend.config.js +42 -0
  32. package/src/config/jest.frontend.config.js +50 -0
  33. package/src/config/postcss.config.js +6 -0
  34. package/src/config/tailwind.config.ts +70 -0
  35. package/src/config/tsconfig.base.json +36 -0
  36. package/src/config/vite.config.ts +86 -0
  37. package/src/config/vitest.base.config.ts +74 -0
  38. package/src/config/webpack.base.config.ts +126 -0
  39. package/src/constants/index.ts +312 -0
  40. package/src/cookies.ts +104 -0
  41. package/src/helpers.ts +400 -0
  42. package/src/index.ts +32 -0
  43. package/src/validation/client.ts +287 -0
  44. package/src/validation/index.ts +3 -0
  45. package/src/validation/server.ts +32 -0
@@ -0,0 +1,168 @@
1
+ import {
2
+ LogLevel,
3
+ batchValidate,
4
+ isValidCouponCode,
5
+ isValidEmail,
6
+ isValidObjectId,
7
+ isValidPassword,
8
+ isValidPhone,
9
+ isValidPostalCode,
10
+ isValidSku,
11
+ isValidUrl,
12
+ validateCouponCode,
13
+ validateDate,
14
+ validateDateRange,
15
+ validateDiscountPercentage,
16
+ validateEmail,
17
+ validateName,
18
+ validateObjectId,
19
+ validatePagination,
20
+ validatePassword,
21
+ validatePhone,
22
+ validatePostalCode,
23
+ validatePrice,
24
+ validateQuantity,
25
+ validateRating,
26
+ validateSku,
27
+ validateUrl
28
+ } from "../chunk-PEAZVBSD.mjs";
29
+
30
+ // src/validation/server.ts
31
+ import { validationResult } from "express-validator";
32
+
33
+ // src/api/logger.ts
34
+ import fs from "fs";
35
+ import path from "path";
36
+ var LOG_LEVEL_PRIORITY = {
37
+ [LogLevel.DEBUG]: 0,
38
+ [LogLevel.INFO]: 1,
39
+ [LogLevel.WARN]: 2,
40
+ [LogLevel.ERROR]: 3
41
+ };
42
+ var Logger = class {
43
+ static configure(options) {
44
+ if (options.maxLogs !== void 0) this.maxLogs = options.maxLogs;
45
+ if (options.enableConsole !== void 0) this.enableConsole = options.enableConsole;
46
+ if (options.enableFile !== void 0) this.enableFile = options.enableFile;
47
+ if (options.logFilePath) this.logFilePath = options.logFilePath;
48
+ if (options.logLevel) {
49
+ const level = typeof options.logLevel === "string" ? options.logLevel.toUpperCase() : options.logLevel;
50
+ if (Object.values(LogLevel).includes(level)) {
51
+ this.logLevel = level;
52
+ }
53
+ }
54
+ }
55
+ static shouldLog(level) {
56
+ return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[this.logLevel];
57
+ }
58
+ static addLog(entry) {
59
+ this.logs.push(entry);
60
+ if (this.logs.length > this.maxLogs) this.logs.shift();
61
+ if (this.enableFile) {
62
+ fs.mkdirSync(path.dirname(this.logFilePath), { recursive: true });
63
+ fs.appendFileSync(this.logFilePath, JSON.stringify(entry) + "\n");
64
+ }
65
+ }
66
+ static format(entry) {
67
+ return `[${entry.timestamp}] [${entry.level}]${entry.context ? ` [${entry.context}]` : ""}: ${entry.message}`;
68
+ }
69
+ static debug(message, data, context) {
70
+ this.log(LogLevel.DEBUG, message, data, context);
71
+ }
72
+ static info(message, data, context) {
73
+ this.log(LogLevel.INFO, message, data, context);
74
+ }
75
+ static warn(message, data, context) {
76
+ this.log(LogLevel.WARN, message, data, context);
77
+ }
78
+ static error(message, error, context) {
79
+ this.log(LogLevel.ERROR, message, error, context);
80
+ }
81
+ static log(level, message, data, context) {
82
+ if (!this.shouldLog(level)) return;
83
+ const entry = {
84
+ level,
85
+ message,
86
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
87
+ data,
88
+ context
89
+ };
90
+ this.addLog(entry);
91
+ if (!this.enableConsole) return;
92
+ const formatted = this.format(entry);
93
+ switch (level) {
94
+ case LogLevel.ERROR:
95
+ console.error(formatted, data ?? "");
96
+ break;
97
+ case LogLevel.WARN:
98
+ console.warn(formatted, data ?? "");
99
+ break;
100
+ case LogLevel.INFO:
101
+ console.info(formatted, data ?? "");
102
+ break;
103
+ default:
104
+ console.debug(formatted, data ?? "");
105
+ }
106
+ }
107
+ static getLogs(level) {
108
+ return level ? this.logs.filter((l) => l.level === level) : [...this.logs];
109
+ }
110
+ static clearLogs() {
111
+ this.logs = [];
112
+ if (this.enableFile && fs.existsSync(this.logFilePath)) {
113
+ fs.unlinkSync(this.logFilePath);
114
+ }
115
+ }
116
+ };
117
+ Logger.logs = [];
118
+ Logger.maxLogs = 1e3;
119
+ Logger.enableConsole = true;
120
+ Logger.enableFile = false;
121
+ Logger.logFilePath = path.join(process.cwd(), "logs/app.log");
122
+ Logger.logLevel = LogLevel.DEBUG;
123
+
124
+ // src/validation/server.ts
125
+ var validate = (req, res, next) => {
126
+ const errors = validationResult(req);
127
+ if (!errors.isEmpty()) {
128
+ res.status(400).json({
129
+ success: false,
130
+ message: "Validation failed",
131
+ errors: errors.array().map((err) => ({
132
+ field: err.type === "field" ? err.path : void 0,
133
+ message: err.msg
134
+ }))
135
+ });
136
+ return;
137
+ }
138
+ next();
139
+ };
140
+ export {
141
+ Logger,
142
+ batchValidate,
143
+ isValidCouponCode,
144
+ isValidEmail,
145
+ isValidObjectId,
146
+ isValidPassword,
147
+ isValidPhone,
148
+ isValidPostalCode,
149
+ isValidSku,
150
+ isValidUrl,
151
+ validate,
152
+ validateCouponCode,
153
+ validateDate,
154
+ validateDateRange,
155
+ validateDiscountPercentage,
156
+ validateEmail,
157
+ validateName,
158
+ validateObjectId,
159
+ validatePagination,
160
+ validatePassword,
161
+ validatePhone,
162
+ validatePostalCode,
163
+ validatePrice,
164
+ validateQuantity,
165
+ validateRating,
166
+ validateSku,
167
+ validateUrl
168
+ };
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "3a-ecommerce-utils",
3
+ "version": "1.0.0",
4
+ "description": "Utility functions for 3A Softwares E-Commerce",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ },
14
+ "./server": {
15
+ "types": "./dist/validation/server.d.ts",
16
+ "import": "./dist/validation/server.mjs",
17
+ "require": "./dist/validation/server.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "src"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsup src/index.ts src/validation/server.ts --format cjs,esm --dts --clean --external 3a-ecommerce-types",
26
+ "prepublishOnly": "npm run build",
27
+ "test": "vitest",
28
+ "test:coverage": "vitest run --coverage"
29
+ },
30
+ "keywords": [
31
+ "ecommerce",
32
+ "utils",
33
+ "utilities",
34
+ "typescript",
35
+ "microservices"
36
+ ],
37
+ "author": "3aSoftwares",
38
+ "license": "MIT",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "https://github.com/3asoftwares/E-Commerce-Microservices-Platform.git",
42
+ "directory": "packages/utils"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "dependencies": {
48
+ "3a-ecommerce-types": "^1.0.0"
49
+ },
50
+ "peerDependencies": {
51
+ "express": "^4.18.2",
52
+ "express-validator": "^7.0.1"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "express": {
56
+ "optional": true
57
+ },
58
+ "express-validator": {
59
+ "optional": true
60
+ }
61
+ },
62
+ "devDependencies": {
63
+ "@types/node": "^25.0.3",
64
+ "@vitest/coverage-v8": "^4.0.16",
65
+ "tsup": "^8.5.1",
66
+ "typescript": "^5.9.3",
67
+ "vitest": "^4.0.16"
68
+ }
69
+ }
@@ -0,0 +1,96 @@
1
+ // Address Queries
2
+ export const GET_MY_ADDRESSES_QUERY = `
3
+ query GetMyAddresses {
4
+ myAddresses {
5
+ addresses {
6
+ id
7
+ userId
8
+ street
9
+ city
10
+ state
11
+ zip
12
+ country
13
+ isDefault
14
+ label
15
+ createdAt
16
+ updatedAt
17
+ }
18
+ }
19
+ }
20
+ `;
21
+
22
+ // Address Mutations
23
+ export const ADD_ADDRESS_MUTATION = `
24
+ mutation AddAddress($input: AddAddressInput!) {
25
+ addAddress(input: $input) {
26
+ success
27
+ message
28
+ address {
29
+ id
30
+ userId
31
+ street
32
+ city
33
+ state
34
+ zip
35
+ country
36
+ isDefault
37
+ label
38
+ createdAt
39
+ updatedAt
40
+ }
41
+ }
42
+ }
43
+ `;
44
+
45
+ export const UPDATE_ADDRESS_MUTATION = `
46
+ mutation UpdateAddress($id: ID!, $input: UpdateAddressInput!) {
47
+ updateAddress(id: $id, input: $input) {
48
+ success
49
+ message
50
+ address {
51
+ id
52
+ userId
53
+ street
54
+ city
55
+ state
56
+ zip
57
+ country
58
+ isDefault
59
+ label
60
+ createdAt
61
+ updatedAt
62
+ }
63
+ }
64
+ }
65
+ `;
66
+
67
+ export const DELETE_ADDRESS_MUTATION = `
68
+ mutation DeleteAddress($id: ID!) {
69
+ deleteAddress(id: $id) {
70
+ success
71
+ message
72
+ }
73
+ }
74
+ `;
75
+
76
+ export const SET_DEFAULT_ADDRESS_MUTATION = `
77
+ mutation SetDefaultAddress($id: ID!) {
78
+ setDefaultAddress(id: $id) {
79
+ success
80
+ message
81
+ address {
82
+ id
83
+ userId
84
+ street
85
+ city
86
+ state
87
+ zip
88
+ country
89
+ isDefault
90
+ label
91
+ createdAt
92
+ updatedAt
93
+ }
94
+ }
95
+ }
96
+ `;
@@ -0,0 +1,85 @@
1
+ export const GET_CATEGORIES_QUERY = `
2
+ query GetCategories($filter: CategoryFilterInput) {
3
+ categories(filter: $filter) {
4
+ success
5
+ message
6
+ data {
7
+ id
8
+ name
9
+ description
10
+ icon
11
+ slug
12
+ isActive
13
+ productCount
14
+ createdAt
15
+ updatedAt
16
+ }
17
+ count
18
+ }
19
+ }
20
+ `;
21
+
22
+ export const GET_CATEGORY_QUERY = `
23
+ query GetCategory($id: String!) {
24
+ category(id: $id) {
25
+ id
26
+ name
27
+ description
28
+ icon
29
+ slug
30
+ isActive
31
+ productCount
32
+ createdAt
33
+ updatedAt
34
+ }
35
+ }
36
+ `;
37
+
38
+ export const CREATE_CATEGORY_MUTATION = `
39
+ mutation CreateCategory($input: CategoryInput!) {
40
+ createCategory(input: $input) {
41
+ success
42
+ message
43
+ data {
44
+ id
45
+ name
46
+ description
47
+ icon
48
+ slug
49
+ isActive
50
+ productCount
51
+ createdAt
52
+ updatedAt
53
+ }
54
+ }
55
+ }
56
+ `;
57
+
58
+ export const UPDATE_CATEGORY_MUTATION = `
59
+ mutation UpdateCategory($id: ID!, $input: CategoryInput!) {
60
+ updateCategory(id: $id, input: $input) {
61
+ success
62
+ message
63
+ data {
64
+ id
65
+ name
66
+ description
67
+ icon
68
+ slug
69
+ isActive
70
+ productCount
71
+ createdAt
72
+ updatedAt
73
+ }
74
+ }
75
+ }
76
+ `;
77
+
78
+ export const DELETE_CATEGORY_MUTATION = `
79
+ mutation DeleteCategory($id: ID!) {
80
+ deleteCategory(id: $id) {
81
+ success
82
+ message
83
+ }
84
+ }
85
+ `;
@@ -0,0 +1,120 @@
1
+ export const GET_COUPONS_QUERY = `
2
+ query GetCoupons($page: Int, $limit: Int, $isActive: Boolean, $search: String) {
3
+ coupons(page: $page, limit: $limit, isActive: $isActive, search: $search) {
4
+ coupons {
5
+ id
6
+ code
7
+ description
8
+ discountType
9
+ discount
10
+ minPurchase
11
+ maxDiscount
12
+ validFrom
13
+ validTo
14
+ usageLimit
15
+ usageCount
16
+ isActive
17
+ createdAt
18
+ updatedAt
19
+ }
20
+ pagination {
21
+ page
22
+ limit
23
+ total
24
+ pages
25
+ }
26
+ }
27
+ }
28
+ `;
29
+
30
+ export const GET_COUPON_QUERY = `
31
+ query GetCoupon($id: ID!) {
32
+ coupon(id: $id) {
33
+ id
34
+ code
35
+ description
36
+ discountType
37
+ discount
38
+ minPurchase
39
+ maxDiscount
40
+ validFrom
41
+ validTo
42
+ usageLimit
43
+ usageCount
44
+ isActive
45
+ createdAt
46
+ updatedAt
47
+ }
48
+ }
49
+ `;
50
+
51
+ export const VALIDATE_COUPON_QUERY = `
52
+ query ValidateCoupon($code: String!, $orderTotal: Float!) {
53
+ validateCoupon(code: $code, orderTotal: $orderTotal) {
54
+ valid
55
+ discount
56
+ discountValue
57
+ finalTotal
58
+ discountType
59
+ message
60
+ code
61
+ }
62
+ }
63
+ `;
64
+
65
+ export const CREATE_COUPON_MUTATION = `
66
+ mutation CreateCoupon($input: CreateCouponInput!) {
67
+ createCoupon(input: $input) {
68
+ id
69
+ code
70
+ description
71
+ discountType
72
+ discount
73
+ minPurchase
74
+ maxDiscount
75
+ validFrom
76
+ validTo
77
+ usageLimit
78
+ usageCount
79
+ isActive
80
+ createdAt
81
+ updatedAt
82
+ }
83
+ }
84
+ `;
85
+
86
+ export const UPDATE_COUPON_MUTATION = `
87
+ mutation UpdateCoupon($id: ID!, $input: UpdateCouponInput!) {
88
+ updateCoupon(id: $id, input: $input) {
89
+ id
90
+ code
91
+ description
92
+ discountType
93
+ discount
94
+ minPurchase
95
+ maxDiscount
96
+ validFrom
97
+ validTo
98
+ usageLimit
99
+ usageCount
100
+ isActive
101
+ createdAt
102
+ updatedAt
103
+ }
104
+ }
105
+ `;
106
+
107
+ export const DELETE_COUPON_MUTATION = `
108
+ mutation DeleteCoupon($id: ID!) {
109
+ deleteCoupon(id: $id)
110
+ }
111
+ `;
112
+
113
+ export const TOGGLE_COUPON_STATUS_MUTATION = `
114
+ mutation ToggleCouponStatus($id: ID!) {
115
+ toggleCouponStatus(id: $id) {
116
+ id
117
+ isActive
118
+ }
119
+ }
120
+ `;
@@ -0,0 +1,35 @@
1
+ export const DASHBOARD_STATS_QUERY = `
2
+ query GetDashboardStats {
3
+ dashboardStats {
4
+ totalUsers
5
+ totalOrders
6
+ totalRevenue
7
+ pendingOrders
8
+ }
9
+ }
10
+ `;
11
+
12
+ export const SALES_ANALYTICS_QUERY = `
13
+ query GetSalesAnalytics($period: String!) {
14
+ salesAnalytics(period: $period) {
15
+ daily {
16
+ date
17
+ sales
18
+ orders
19
+ revenue
20
+ }
21
+ weekly {
22
+ date
23
+ sales
24
+ orders
25
+ revenue
26
+ }
27
+ monthly {
28
+ date
29
+ sales
30
+ orders
31
+ revenue
32
+ }
33
+ }
34
+ }
35
+ `;