washday-sdk 0.0.23 → 0.0.24
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/babel.config.js +7 -7
- package/dist/api/index.js +8 -2
- package/dist/api/products/get.js +7 -0
- package/dist/api/products/post.js +7 -0
- package/dist/api/products/put.js +36 -0
- package/dist/api/stores/post.js +37 -0
- package/dist/utils/receipt/generateReceiptHTML.js +157 -0
- package/dist/utils/util.js +46 -0
- package/package.json +29 -28
- package/src/api/axiosInstance.ts +46 -46
- package/src/api/customers/get.ts +32 -32
- package/src/api/index.ts +37 -29
- package/src/api/products/get.ts +9 -0
- package/src/api/products/post.ts +11 -0
- package/src/api/products/put.ts +21 -0
- package/src/api/stores/get.ts +36 -36
- package/src/api/stores/post.ts +23 -0
- package/src/api/stores/put.ts +20 -20
- package/src/enum/index.ts +10 -10
- package/src/index.ts +4 -4
- package/src/interfaces/Api.ts +2 -2
- package/src/interfaces/Customer.ts +51 -51
- package/src/interfaces/Order.ts +95 -95
- package/src/interfaces/Permission.ts +24 -24
- package/src/interfaces/Product.ts +58 -58
- package/src/interfaces/Section.ts +14 -14
- package/src/interfaces/Store.ts +350 -350
- package/src/interfaces/StoreImage.ts +9 -9
- package/src/interfaces/User.ts +42 -42
- package/src/utils/index.ts +1 -1
- package/src/utils/orders/calculateOrderTotal.test.js +399 -399
- package/src/utils/orders/calculateOrderTotal.ts +60 -60
- package/src/utils/orders/calculateTotalTaxesIncluded.ts +75 -75
- package/src/utils/orders/calculateTotalTaxesOverPrice.ts +82 -82
- package/src/utils/orders/helpers.ts +73 -73
- package/src/utils/orders/index.ts +4 -4
- package/src/utils/receipt/generateReceiptHTML.ts +163 -0
- package/src/utils/util.ts +47 -0
- package/tsconfig.json +9 -9
package/babel.config.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
presets: [
|
|
3
|
-
['@babel/preset-env', {targets: {node: 'current'}}],
|
|
4
|
-
'@babel/preset-typescript',
|
|
5
|
-
],
|
|
6
|
-
};
|
|
7
|
-
|
|
1
|
+
module.exports = {
|
|
2
|
+
presets: [
|
|
3
|
+
['@babel/preset-env', {targets: {node: 'current'}}],
|
|
4
|
+
'@babel/preset-typescript',
|
|
5
|
+
],
|
|
6
|
+
};
|
|
7
|
+
|
|
8
8
|
|
package/dist/api/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const get_1 = require("./customers/get");
|
|
4
|
+
const put_1 = require("./products/put");
|
|
4
5
|
const get_2 = require("./stores/get");
|
|
5
|
-
const
|
|
6
|
+
const post_1 = require("./stores/post");
|
|
7
|
+
const put_2 = require("./stores/put");
|
|
6
8
|
const WashdayClient = function WashdayClient(apiToken) {
|
|
7
9
|
this.apiToken = apiToken;
|
|
8
10
|
WashdayClient.prototype.customers.apiToken = apiToken;
|
|
@@ -15,6 +17,10 @@ WashdayClient.prototype.customers = {
|
|
|
15
17
|
WashdayClient.prototype.stores = {
|
|
16
18
|
getStores: get_2.getStores,
|
|
17
19
|
getStoreById: get_2.getStoreById,
|
|
18
|
-
updateStoreById:
|
|
20
|
+
updateStoreById: put_2.updateStoreById,
|
|
21
|
+
createStoreImage: post_1.createStoreImage
|
|
22
|
+
};
|
|
23
|
+
WashdayClient.prototype.products = {
|
|
24
|
+
bulkUpdate: put_1.bulkUpdate,
|
|
19
25
|
};
|
|
20
26
|
exports.default = WashdayClient;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.bulkUpdate = void 0;
|
|
16
|
+
const axiosInstance_1 = __importDefault(require("../axiosInstance"));
|
|
17
|
+
const GET_SET_PRODUCTS = 'api/product';
|
|
18
|
+
const REQUEST_PARAMS = {
|
|
19
|
+
token: 'LOGGED_USER_TOKEN',
|
|
20
|
+
};
|
|
21
|
+
const bulkUpdate = function (storeId, data) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
try {
|
|
24
|
+
const config = {
|
|
25
|
+
headers: { Authorization: `Bearer ${this.apiToken}` }
|
|
26
|
+
};
|
|
27
|
+
const response = yield axiosInstance_1.default.put(`${GET_SET_PRODUCTS}/${storeId}/bulk`, data, config);
|
|
28
|
+
return response.data || {};
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
console.error('Error fetching getStoreById:', error);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
exports.bulkUpdate = bulkUpdate;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.createStoreImage = void 0;
|
|
16
|
+
const axiosInstance_1 = __importDefault(require("../axiosInstance"));
|
|
17
|
+
const GET_SET_STORES = 'api/store';
|
|
18
|
+
const GET_SET_STORE_IMAGE = 'api/store-image';
|
|
19
|
+
const REQUEST_PARAMS = {
|
|
20
|
+
token: 'LOGGED_USER_TOKEN',
|
|
21
|
+
};
|
|
22
|
+
const createStoreImage = function (data) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
try {
|
|
25
|
+
const config = {
|
|
26
|
+
headers: { Authorization: `Bearer ${this.apiToken}`, 'Content-Type': 'multipart/form-data' }
|
|
27
|
+
};
|
|
28
|
+
const response = yield axiosInstance_1.default.post(`${GET_SET_STORE_IMAGE}`, data, config);
|
|
29
|
+
return response;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error('Error fetching createStoreImage:', error);
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
exports.createStoreImage = createStoreImage;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import moment from "moment";
|
|
3
|
+
// import { formatMoneyString, truncateOrFill } from "../util";
|
|
4
|
+
// export const generateReceiptThermalPrinter = (order, storeSettings, ticketStructure, charsPerLine = 32) => {
|
|
5
|
+
// //THIS STRING IS FORMED WITH THE EXPECTED USE OF react-native-thermal-receipt-printer PACKAGE
|
|
6
|
+
// const PAPER_LINE_LENGTH = charsPerLine;
|
|
7
|
+
// const orderProducts = order?.products || [];
|
|
8
|
+
// const orderBuyAndGerProducts = order?.buyAndGetProducts || [];
|
|
9
|
+
// const orderAllProducts = [...orderProducts, ...orderBuyAndGerProducts];
|
|
10
|
+
// console.log('PAPER_LINE_LENGTH', PAPER_LINE_LENGTH);
|
|
11
|
+
// const paymentLinesTotal =
|
|
12
|
+
// order.paymentLines?.reduce((prev, next) => {
|
|
13
|
+
// return prev + (next?.amountPaid || 0);
|
|
14
|
+
// }, 0) || 0;
|
|
15
|
+
// let headers = `
|
|
16
|
+
// ${ticketStructure.showStoreName ? `<CM>${storeSettings.name}</CM>` : ''}
|
|
17
|
+
// ${ticketStructure.showStorePhone ? `Tel: ${storeSettings.phone}` : ''}
|
|
18
|
+
// ${'-'.repeat(PAPER_LINE_LENGTH)}
|
|
19
|
+
// ${ticketStructure.showEmisionDate
|
|
20
|
+
// ? `Fecha emision: ${moment(order?.createdDate).format('DD/MM/YYYY')}`
|
|
21
|
+
// : ''
|
|
22
|
+
// }
|
|
23
|
+
// ${ticketStructure.showEmisionTime
|
|
24
|
+
// ? `Hora emision: ${moment(order?.createdDate).format('hh:mm a')}`
|
|
25
|
+
// : ''
|
|
26
|
+
// }
|
|
27
|
+
// ${ticketStructure.showCreatedByName ? `Atendio: ${order.createdBy.name}` : ''}
|
|
28
|
+
// Pedido: WD-${order.sequence}
|
|
29
|
+
// ${ticketStructure.showCustomerName ? `Cliente: ${order.customer.name}` : ''}
|
|
30
|
+
// ${ticketStructure.showDeliveryDate
|
|
31
|
+
// ? `Entrega: ${moment(order?.deliveryInfo?.date).format('DD/MM/YYYY')} ${ticketStructure.showDeliveryTime
|
|
32
|
+
// ? moment(order?.deliveryInfo?.fromTime).format('HH:mm')
|
|
33
|
+
// : ''
|
|
34
|
+
// }`
|
|
35
|
+
// : ''
|
|
36
|
+
// }
|
|
37
|
+
// ${ticketStructure.showTotalPieces ? `Piezas: ${orderAllProducts?.reduce((prev, curr) => {
|
|
38
|
+
// return prev + +curr.pieces * curr.quantity;
|
|
39
|
+
// }, 0)}` : ''}
|
|
40
|
+
// ${ticketStructure.showOrderNotes && order.notes ? `${order.notes}` : ''}
|
|
41
|
+
// `;
|
|
42
|
+
// let middle = '';
|
|
43
|
+
// //Add order products
|
|
44
|
+
// orderAllProducts.forEach(prod => {
|
|
45
|
+
// //CHARS PER LINE: 32 in 58MM PAPER
|
|
46
|
+
// const quantityFixed = truncateOrFill(
|
|
47
|
+
// `x${prod.quantity.toString()}`,
|
|
48
|
+
// 3,
|
|
49
|
+
// 'right',
|
|
50
|
+
// );
|
|
51
|
+
// const prodNameFixed = truncateOrFill(
|
|
52
|
+
// prod.name,
|
|
53
|
+
// PAPER_LINE_LENGTH - 3 - 7 - 7 - 3,
|
|
54
|
+
// 'right',
|
|
55
|
+
// );
|
|
56
|
+
// const unitPriceFixed = truncateOrFill(
|
|
57
|
+
// formatMoneyString(
|
|
58
|
+
// (order?.express ? +prod.expressPrice : +prod.price) +
|
|
59
|
+
// (+prod.extraAmount / +prod.quantity || 0),
|
|
60
|
+
// ),
|
|
61
|
+
// 7,
|
|
62
|
+
// 'left',
|
|
63
|
+
// );
|
|
64
|
+
// const totalPriceFixed = truncateOrFill(
|
|
65
|
+
// formatMoneyString(
|
|
66
|
+
// (order?.express ? +prod.expressPrice : +prod.price) * +prod.quantity +
|
|
67
|
+
// (+prod.extraAmount || 0),
|
|
68
|
+
// ),
|
|
69
|
+
// 7,
|
|
70
|
+
// 'left',
|
|
71
|
+
// );
|
|
72
|
+
// middle =
|
|
73
|
+
// middle +
|
|
74
|
+
// `${quantityFixed} ${prodNameFixed} ${unitPriceFixed} ${totalPriceFixed}\n`;
|
|
75
|
+
// });
|
|
76
|
+
// let footer = '';
|
|
77
|
+
// footer =
|
|
78
|
+
// footer +
|
|
79
|
+
// `\n${truncateOrFill(
|
|
80
|
+
// 'Subtotal: ' +
|
|
81
|
+
// formatMoneyString(order.productTotalWithoutDiscount || 0),
|
|
82
|
+
// PAPER_LINE_LENGTH,
|
|
83
|
+
// 'left',
|
|
84
|
+
// )}`;
|
|
85
|
+
// if (ticketStructure.showDiscounts) {
|
|
86
|
+
// footer =
|
|
87
|
+
// footer +
|
|
88
|
+
// `\n${truncateOrFill(
|
|
89
|
+
// 'Descuento: ' + formatMoneyString(order.totalDiscountAmount || 0),
|
|
90
|
+
// PAPER_LINE_LENGTH,
|
|
91
|
+
// 'left',
|
|
92
|
+
// )}`;
|
|
93
|
+
// }
|
|
94
|
+
// if (ticketStructure.showCredit) {
|
|
95
|
+
// footer =
|
|
96
|
+
// footer +
|
|
97
|
+
// `\n${truncateOrFill(
|
|
98
|
+
// 'Credito: ' + formatMoneyString(order.creditApplied || 0),
|
|
99
|
+
// PAPER_LINE_LENGTH,
|
|
100
|
+
// 'left',
|
|
101
|
+
// )}`;
|
|
102
|
+
// }
|
|
103
|
+
// if (ticketStructure.showTaxes) {
|
|
104
|
+
// footer =
|
|
105
|
+
// footer +
|
|
106
|
+
// `\n${truncateOrFill(
|
|
107
|
+
// 'Impuestos: ' + formatMoneyString(order.taxesTotal || 0),
|
|
108
|
+
// PAPER_LINE_LENGTH,
|
|
109
|
+
// 'left',
|
|
110
|
+
// )}`;
|
|
111
|
+
// }
|
|
112
|
+
// footer =
|
|
113
|
+
// footer +
|
|
114
|
+
// `${order.delivery || order.pickup
|
|
115
|
+
// ? `\n${truncateOrFill(
|
|
116
|
+
// 'Servicio a domicilio: ' +
|
|
117
|
+
// formatMoneyString(order.shippingServiceTotal || 0),
|
|
118
|
+
// PAPER_LINE_LENGTH,
|
|
119
|
+
// 'left',
|
|
120
|
+
// )}`
|
|
121
|
+
// : ''
|
|
122
|
+
// }`;
|
|
123
|
+
// footer =
|
|
124
|
+
// footer +
|
|
125
|
+
// '\n' +
|
|
126
|
+
// `${truncateOrFill(
|
|
127
|
+
// 'Total: ' + formatMoneyString(order.total || 0),
|
|
128
|
+
// PAPER_LINE_LENGTH,
|
|
129
|
+
// 'left',
|
|
130
|
+
// )}`;
|
|
131
|
+
// footer =
|
|
132
|
+
// footer +
|
|
133
|
+
// '\n' +
|
|
134
|
+
// `${truncateOrFill(
|
|
135
|
+
// 'Pago: ' + formatMoneyString(paymentLinesTotal),
|
|
136
|
+
// PAPER_LINE_LENGTH,
|
|
137
|
+
// 'left',
|
|
138
|
+
// )}`;
|
|
139
|
+
// footer =
|
|
140
|
+
// footer +
|
|
141
|
+
// '\n' +
|
|
142
|
+
// `${truncateOrFill(
|
|
143
|
+
// 'Debe: ' + ((order.total || 0) - paymentLinesTotal).toFixed(2),
|
|
144
|
+
// PAPER_LINE_LENGTH,
|
|
145
|
+
// 'left',
|
|
146
|
+
// )}`;
|
|
147
|
+
// footer = footer + `\n${'-'.repeat(PAPER_LINE_LENGTH)}\n`;
|
|
148
|
+
// // footer = footer + `${ticketStructure.showPaymentMethod ? `<C>${translatePaymentType(order.paymentMethod)}</C>\n` : ''}`
|
|
149
|
+
// footer =
|
|
150
|
+
// footer +
|
|
151
|
+
// `${ticketStructure.showLegend
|
|
152
|
+
// ? `<C>${ticketStructure.ticketLegendText}</C>`
|
|
153
|
+
// : ''
|
|
154
|
+
// }`;
|
|
155
|
+
// const ticket = headers + '\n' + middle + '\n' + footer;
|
|
156
|
+
// return ticket;
|
|
157
|
+
// }
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// export function truncateOrFill(str: string, n: number, side: string = 'right') {
|
|
3
|
+
// let needSpacesQty = n - str.length;
|
|
4
|
+
// let whiteSpace = '';
|
|
5
|
+
// if (needSpacesQty > 0) {
|
|
6
|
+
// for (let i = 0; i < needSpacesQty; i++) {
|
|
7
|
+
// whiteSpace += ' ';
|
|
8
|
+
// }
|
|
9
|
+
// }
|
|
10
|
+
// return str.length > n
|
|
11
|
+
// ? str.substr(0, n - 3) + '...'
|
|
12
|
+
// : side === 'right'
|
|
13
|
+
// ? str + whiteSpace
|
|
14
|
+
// : whiteSpace + str;
|
|
15
|
+
// }
|
|
16
|
+
// export const formatMoneyString = (
|
|
17
|
+
// amount: string | number = 0,
|
|
18
|
+
// decimalCount: number = 2,
|
|
19
|
+
// decimal = '.',
|
|
20
|
+
// thousands = ',',
|
|
21
|
+
// currencySymbol = '$',
|
|
22
|
+
// ) => {
|
|
23
|
+
// try {
|
|
24
|
+
// decimalCount = Math.abs(decimalCount);
|
|
25
|
+
// decimalCount = isNaN(decimalCount) ? 2 : decimalCount;
|
|
26
|
+
// const negativeSign = amount < 0 ? '-' : '';
|
|
27
|
+
// let i = parseInt(
|
|
28
|
+
// (amount = Math.abs(Number(amount) || 0).toFixed(decimalCount)),
|
|
29
|
+
// ).toString();
|
|
30
|
+
// let j = i.length > 3 ? i.length % 3 : 0;
|
|
31
|
+
// return (
|
|
32
|
+
// currencySymbol +
|
|
33
|
+
// negativeSign +
|
|
34
|
+
// (j ? i.substr(0, j) + thousands : '') +
|
|
35
|
+
// i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousands) +
|
|
36
|
+
// (decimalCount
|
|
37
|
+
// ? decimal +
|
|
38
|
+
// Math.abs(amount - i)
|
|
39
|
+
// .toFixed(decimalCount)
|
|
40
|
+
// .slice(2)
|
|
41
|
+
// : '')
|
|
42
|
+
// );
|
|
43
|
+
// } catch (e) {
|
|
44
|
+
// console.log(e);
|
|
45
|
+
// }
|
|
46
|
+
// };
|
package/package.json
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "washday-sdk",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Washday utilities functions and API",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "jest",
|
|
8
|
-
"build": "tsc",
|
|
9
|
-
"publish": "npm run build && npm publish --access public"
|
|
10
|
-
},
|
|
11
|
-
"keywords": [
|
|
12
|
-
"washday"
|
|
13
|
-
],
|
|
14
|
-
"author": "Washday",
|
|
15
|
-
"license": "ISC",
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"@babel/core": "^7.24.4",
|
|
18
|
-
"@babel/preset-env": "^7.24.4",
|
|
19
|
-
"@babel/preset-typescript": "^7.24.1",
|
|
20
|
-
"babel-jest": "^29.7.0",
|
|
21
|
-
"jest": "^29.7.0",
|
|
22
|
-
"typescript": "^5.4.4"
|
|
23
|
-
},
|
|
24
|
-
"dependencies": {
|
|
25
|
-
"axios": "^1.6.8",
|
|
26
|
-
"joi": "^17.12.3"
|
|
27
|
-
|
|
28
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "washday-sdk",
|
|
3
|
+
"version": "0.0.24",
|
|
4
|
+
"description": "Washday utilities functions and API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "jest",
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"publish": "npm run build && npm publish --access public"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"washday"
|
|
13
|
+
],
|
|
14
|
+
"author": "Washday",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@babel/core": "^7.24.4",
|
|
18
|
+
"@babel/preset-env": "^7.24.4",
|
|
19
|
+
"@babel/preset-typescript": "^7.24.1",
|
|
20
|
+
"babel-jest": "^29.7.0",
|
|
21
|
+
"jest": "^29.7.0",
|
|
22
|
+
"typescript": "^5.4.4"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"axios": "^1.6.8",
|
|
26
|
+
"joi": "^17.12.3",
|
|
27
|
+
"moment": "^2.30.1"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/api/axiosInstance.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
-
|
|
3
|
-
// Define the type for the Axios instance
|
|
4
|
-
let axiosInstance: AxiosInstance | null = null;
|
|
5
|
-
|
|
6
|
-
const BASE_URL: string = 'https://washday-backend.herokuapp.com/';
|
|
7
|
-
|
|
8
|
-
// Function to create or return the singleton instance
|
|
9
|
-
const getAxiosInstance = (): AxiosInstance => {
|
|
10
|
-
if (!axiosInstance) {
|
|
11
|
-
axiosInstance = axios.create({
|
|
12
|
-
baseURL: BASE_URL,
|
|
13
|
-
headers: {
|
|
14
|
-
'Content-Type': 'application/json',
|
|
15
|
-
// Add any default headers here
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// Add interceptor to set token and other options for every request
|
|
20
|
-
axiosInstance.interceptors.request.use(
|
|
21
|
-
(config) => {
|
|
22
|
-
const { token, contentType = 'application/json', responseType = 'json', contentDisposition } = config.params || {};
|
|
23
|
-
const { Authorization } = config.headers || {};
|
|
24
|
-
if (Authorization || token) {
|
|
25
|
-
config.headers.Authorization = Authorization || `Bearer ${token}`;
|
|
26
|
-
}
|
|
27
|
-
if (contentType) {
|
|
28
|
-
config.headers['Content-Type'] = contentType;
|
|
29
|
-
}
|
|
30
|
-
if (responseType) {
|
|
31
|
-
config.responseType = responseType;
|
|
32
|
-
}
|
|
33
|
-
if (contentDisposition) {
|
|
34
|
-
config.headers['Content-Disposition'] = contentDisposition;
|
|
35
|
-
}
|
|
36
|
-
return config;
|
|
37
|
-
},
|
|
38
|
-
(error) => {
|
|
39
|
-
return Promise.reject(error);
|
|
40
|
-
}
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
return axiosInstance;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export default getAxiosInstance();
|
|
1
|
+
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
|
|
3
|
+
// Define the type for the Axios instance
|
|
4
|
+
let axiosInstance: AxiosInstance | null = null;
|
|
5
|
+
|
|
6
|
+
const BASE_URL: string = 'https://washday-backend.herokuapp.com/';
|
|
7
|
+
|
|
8
|
+
// Function to create or return the singleton instance
|
|
9
|
+
const getAxiosInstance = (): AxiosInstance => {
|
|
10
|
+
if (!axiosInstance) {
|
|
11
|
+
axiosInstance = axios.create({
|
|
12
|
+
baseURL: BASE_URL,
|
|
13
|
+
headers: {
|
|
14
|
+
'Content-Type': 'application/json',
|
|
15
|
+
// Add any default headers here
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Add interceptor to set token and other options for every request
|
|
20
|
+
axiosInstance.interceptors.request.use(
|
|
21
|
+
(config) => {
|
|
22
|
+
const { token, contentType = 'application/json', responseType = 'json', contentDisposition } = config.params || {};
|
|
23
|
+
const { Authorization } = config.headers || {};
|
|
24
|
+
if (Authorization || token) {
|
|
25
|
+
config.headers.Authorization = Authorization || `Bearer ${token}`;
|
|
26
|
+
}
|
|
27
|
+
if (contentType) {
|
|
28
|
+
config.headers['Content-Type'] = contentType;
|
|
29
|
+
}
|
|
30
|
+
if (responseType) {
|
|
31
|
+
config.responseType = responseType;
|
|
32
|
+
}
|
|
33
|
+
if (contentDisposition) {
|
|
34
|
+
config.headers['Content-Disposition'] = contentDisposition;
|
|
35
|
+
}
|
|
36
|
+
return config;
|
|
37
|
+
},
|
|
38
|
+
(error) => {
|
|
39
|
+
return Promise.reject(error);
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
return axiosInstance;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default getAxiosInstance();
|
package/src/api/customers/get.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import { WashdayClientInstance } from "../../interfaces/Api";
|
|
2
|
-
import { ICustomer } from "../../interfaces/Customer";
|
|
3
|
-
import { IOrderInfo } from "../../interfaces/Order";
|
|
4
|
-
import axiosInstance from "../axiosInstance";
|
|
5
|
-
|
|
6
|
-
const GET_SET_CUSTOMERS = 'api/customer';
|
|
7
|
-
const REQUEST_PARAMS = {
|
|
8
|
-
token: 'LOGGED_USER_TOKEN',
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export const getCustomerById = async function (this: WashdayClientInstance, customerId: string): Promise<{ customer: ICustomer, orderInfo: IOrderInfo }> {
|
|
13
|
-
try {
|
|
14
|
-
REQUEST_PARAMS.token = this.apiToken;
|
|
15
|
-
const response = await axiosInstance.get(`${GET_SET_CUSTOMERS}/${customerId}`, { params: { ...REQUEST_PARAMS } });
|
|
16
|
-
return response.data;
|
|
17
|
-
} catch (error) {
|
|
18
|
-
console.error('Error fetching customer:', error);
|
|
19
|
-
throw error;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const getCustomerHighlightsById = async function (this: WashdayClientInstance, customerId: string): Promise<{ customer: ICustomer, orderInfo: IOrderInfo }> {
|
|
24
|
-
try {
|
|
25
|
-
REQUEST_PARAMS.token = this.apiToken;
|
|
26
|
-
const response = await axiosInstance.get(`${GET_SET_CUSTOMERS}/${customerId}/highlights`, { params: { ...REQUEST_PARAMS } });
|
|
27
|
-
return response.data;
|
|
28
|
-
} catch (error) {
|
|
29
|
-
console.error('Error fetching getCustomerHighlightsById:', error);
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
1
|
+
import { WashdayClientInstance } from "../../interfaces/Api";
|
|
2
|
+
import { ICustomer } from "../../interfaces/Customer";
|
|
3
|
+
import { IOrderInfo } from "../../interfaces/Order";
|
|
4
|
+
import axiosInstance from "../axiosInstance";
|
|
5
|
+
|
|
6
|
+
const GET_SET_CUSTOMERS = 'api/customer';
|
|
7
|
+
const REQUEST_PARAMS = {
|
|
8
|
+
token: 'LOGGED_USER_TOKEN',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export const getCustomerById = async function (this: WashdayClientInstance, customerId: string): Promise<{ customer: ICustomer, orderInfo: IOrderInfo }> {
|
|
13
|
+
try {
|
|
14
|
+
REQUEST_PARAMS.token = this.apiToken;
|
|
15
|
+
const response = await axiosInstance.get(`${GET_SET_CUSTOMERS}/${customerId}`, { params: { ...REQUEST_PARAMS } });
|
|
16
|
+
return response.data;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.error('Error fetching customer:', error);
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const getCustomerHighlightsById = async function (this: WashdayClientInstance, customerId: string): Promise<{ customer: ICustomer, orderInfo: IOrderInfo }> {
|
|
24
|
+
try {
|
|
25
|
+
REQUEST_PARAMS.token = this.apiToken;
|
|
26
|
+
const response = await axiosInstance.get(`${GET_SET_CUSTOMERS}/${customerId}/highlights`, { params: { ...REQUEST_PARAMS } });
|
|
27
|
+
return response.data;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error('Error fetching getCustomerHighlightsById:', error);
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
};
|
package/src/api/index.ts
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
|
-
import { WashdayClientInstance } from "../interfaces/Api";
|
|
2
|
-
import { getCustomerById, getCustomerHighlightsById } from "./customers/get";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
import { WashdayClientInstance } from "../interfaces/Api";
|
|
2
|
+
import { getCustomerById, getCustomerHighlightsById } from "./customers/get";
|
|
3
|
+
import { bulkUpdate } from "./products/put";
|
|
4
|
+
import { getStoreById, getStores } from "./stores/get";
|
|
5
|
+
import { createStoreImage } from "./stores/post";
|
|
6
|
+
import { updateStoreById } from "./stores/put";
|
|
7
|
+
|
|
8
|
+
type WashdayClientConstructor = {
|
|
9
|
+
new(apiToken: string): {
|
|
10
|
+
apiToken: string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const WashdayClient: WashdayClientConstructor = function WashdayClient(this: WashdayClientInstance, apiToken: string) {
|
|
15
|
+
this.apiToken = apiToken;
|
|
16
|
+
WashdayClient.prototype.customers.apiToken = apiToken;
|
|
17
|
+
WashdayClient.prototype.stores.apiToken = apiToken;
|
|
18
|
+
} as any;
|
|
19
|
+
|
|
20
|
+
WashdayClient.prototype.customers = {
|
|
21
|
+
getCustomerById: getCustomerById,
|
|
22
|
+
getCustomerHighlightsById: getCustomerHighlightsById
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
WashdayClient.prototype.stores = {
|
|
26
|
+
getStores: getStores,
|
|
27
|
+
getStoreById: getStoreById,
|
|
28
|
+
updateStoreById: updateStoreById,
|
|
29
|
+
createStoreImage: createStoreImage
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
WashdayClient.prototype.products = {
|
|
34
|
+
bulkUpdate: bulkUpdate,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export default WashdayClient;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { WashdayClientInstance } from "../../interfaces/Api";
|
|
2
|
+
import { IStore } from "../../interfaces/Store";
|
|
3
|
+
import axiosInstance from "../axiosInstance";
|
|
4
|
+
|
|
5
|
+
export const GET_SET_PRODUCTS = 'api/product';
|
|
6
|
+
const REQUEST_PARAMS = {
|
|
7
|
+
token: 'LOGGED_USER_TOKEN',
|
|
8
|
+
};
|
|
9
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { WashdayClientInstance } from "../../interfaces/Api";
|
|
2
|
+
import { IStore } from "../../interfaces/Store";
|
|
3
|
+
import axiosInstance from "../axiosInstance";
|
|
4
|
+
|
|
5
|
+
const GET_SET_STORES = 'api/store';
|
|
6
|
+
const GET_SET_STORE_IMAGE = 'api/store-image';
|
|
7
|
+
|
|
8
|
+
const REQUEST_PARAMS = {
|
|
9
|
+
token: 'LOGGED_USER_TOKEN',
|
|
10
|
+
};
|
|
11
|
+
|