sysone-api-mapper 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.
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "sysone-api-mapper",
3
+ "version": "1.0.0",
4
+ "description": "To make it easy for you to get started with GitLab, here's a list of recommended next steps.",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "start": "node src/server.js",
8
+ "dev": "nodemon src/server.js",
9
+ "build": "webpack --config config/webpack.prod.js",
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [],
13
+ "author": "",
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "axios": "^1.6.0",
17
+ "cors": "^2.8.5",
18
+ "dotenv": "^16.0.3",
19
+ "express": "^4.21.2"
20
+ },
21
+ "devDependencies": {
22
+ "jest": "^29.7.0",
23
+ "nodemon": "^2.0.22"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://gitlab.sysone.com/sysone-cloud-ready/sales-platform/api-mapper/-/tree/develop?ref_type=heads"
28
+ },
29
+ "private": false
30
+ }
package/services.js ADDED
@@ -0,0 +1,105 @@
1
+ import { AxiosError } from "axios";
2
+ import axiosInstance from "./axiosInstance";
3
+ import { methods, services } from "./servicesData";
4
+ import {
5
+ openNotificationWithIcon,
6
+ TYPE,
7
+ } from "./src/components/notificationToast";
8
+
9
+ const handleApiError = (service, e) => {
10
+ try {
11
+ const errorToDisplay = {
12
+ title: "",
13
+ errors: [],
14
+ };
15
+
16
+ if (!(e instanceof AxiosError)) {
17
+ errorToDisplay.title = e.message;
18
+ } else {
19
+ const error = e.response;
20
+ const statusError = error.status;
21
+ const modulesInfo = JSON.parse(localStorage.getItem("modules_info"));
22
+
23
+ var translationInfo = modulesInfo
24
+ .find((m) => m.code === service.module)
25
+ ?.services?.find(
26
+ (s) =>
27
+ s.name.toUpperCase() === service.serviceName.toUpperCase() &&
28
+ s.httpMethod.toUpperCase() === service.method.toUpperCase()
29
+ )
30
+ ?.errors.find((e) => e.httpStatus === statusError);
31
+
32
+ errorToDisplay.title = translationInfo?.content || error.data.message;
33
+
34
+ const subErrorTranslations = error.data.errorMessages?.map((err) => {
35
+ const translatedSubError = translationInfo?.subErrors?.find(
36
+ (s) => s.errorType === err.fieldError && s.fieldName === err.fieldName
37
+ );
38
+ return translatedSubError?.content || err?.error;
39
+ });
40
+ errorToDisplay.errors = subErrorTranslations || [];
41
+ }
42
+
43
+ openNotificationWithIcon(
44
+ TYPE.ERROR,
45
+ "",
46
+ errorToDisplay.title,
47
+ errorToDisplay.errors
48
+ );
49
+ } catch (e) {
50
+ console.error(e);
51
+ throw e;
52
+ }
53
+ };
54
+
55
+ const getUrl = (serviceUrl, routeParams) => {
56
+ if (!routeParams) return serviceUrl;
57
+ return serviceUrl.replace(/{(\d+)}/g, function (match, routeParamNumber) {
58
+ return routeParams[routeParamNumber]
59
+ ? routeParams[routeParamNumber]
60
+ : match;
61
+ });
62
+ };
63
+
64
+ export const formatNumberES = (n, d = 0) => {
65
+ n = new Intl.NumberFormat("de-DE").format(parseFloat(n)?.toFixed(d));
66
+ if (d > 0) {
67
+ const decimals = n.indexOf(",") > -1 ? n.length - 1 - n.indexOf(",") : 0;
68
+
69
+ n = decimals == 0 ? n + "," + "0".repeat(d) : n + "0".repeat(d - decimals);
70
+ }
71
+ return n;
72
+ };
73
+
74
+ export const apiCall = async (endpoint, routeParams, params = null) => {
75
+ const service = services.find((s) => s.endpoint === endpoint);
76
+ try {
77
+ if (!service) throw new Error("Service does not exists");
78
+
79
+ const url = getUrl(service.url, routeParams);
80
+
81
+ let response;
82
+ switch (service.method) {
83
+ case methods.GET:
84
+ response = await axiosInstance.get(url, { params });
85
+ return response.data;
86
+ case methods.POST:
87
+ response = await axiosInstance.post(url, params);
88
+ return response.data;
89
+ case methods.PUT:
90
+ response = await axiosInstance.put(url, params);
91
+ return response.data;
92
+ case methods.DELETE:
93
+ response = await axiosInstance.delete(url, params);
94
+ return response.data;
95
+ default:
96
+ response = null;
97
+ throw new Error(
98
+ "Request error: Method not allowed. Only use GET, POST, PUT, DELETE"
99
+ );
100
+ }
101
+ } catch (e) {
102
+ handleApiError(service, e);
103
+ throw e;
104
+ }
105
+ };