sysone-api-mapper 1.0.0 → 1.0.2

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.
@@ -1,38 +1,38 @@
1
- const path = require("path");
2
- const pkg = require("../package.json");
3
- const nodeExternals = require("webpack-node-externals");
4
- module.exports = {
5
- mode: "development",
6
- entry: "./index.js",
7
- output: {
8
- path: path.resolve(__dirname, "dist"),
9
- filename: "index.js",
10
- libraryTarget: "commonjs2", //<- to which version are we compiling js
11
- },
12
- resolve: {
13
- extensions: [".tsx", ".ts", ".jsx", ".js", ".json"],
14
- },
15
- module: {
16
- rules: [
17
- {
18
- test: /\.(js|jsx)$/,
19
- exclude: /node_modules/,
20
- use: {
21
- loader: "babel-loader",
22
- },
23
- },
24
- {
25
- test: /\.css$/i,
26
- use: ["style-loader", "css-loader"],
27
- },
28
- {
29
- test: /\.svg$/i,
30
- use: {
31
- loader: "svg-url-loader",
32
- },
33
- },
34
- ],
35
- },
36
- target: "node",
37
- externals: [nodeExternals()],
38
- };
1
+ const path = require("path");
2
+ const pkg = require("../package.json");
3
+ const nodeExternals = require("webpack-node-externals");
4
+ module.exports = {
5
+ mode: "development",
6
+ entry: "./index.js",
7
+ output: {
8
+ path: path.resolve(__dirname, "dist"),
9
+ filename: "index.js",
10
+ libraryTarget: "commonjs2", //<- to which version are we compiling js
11
+ },
12
+ resolve: {
13
+ extensions: [".tsx", ".ts", ".jsx", ".js", ".json"],
14
+ },
15
+ module: {
16
+ rules: [
17
+ {
18
+ test: /\.(js|jsx)$/,
19
+ exclude: /node_modules/,
20
+ use: {
21
+ loader: "babel-loader",
22
+ },
23
+ },
24
+ {
25
+ test: /\.css$/i,
26
+ use: ["style-loader", "css-loader"],
27
+ },
28
+ {
29
+ test: /\.svg$/i,
30
+ use: {
31
+ loader: "svg-url-loader",
32
+ },
33
+ },
34
+ ],
35
+ },
36
+ target: "node",
37
+ externals: [nodeExternals()],
38
+ };
@@ -1,41 +1,41 @@
1
- const path = require("path"); // <-get absolute location for saving
2
- const pkg = require("../package.json");
3
- const nodeExternals = require("webpack-node-externals");
4
- module.exports = {
5
- mode: "production",
6
- entry: "./index.js",
7
- output: {
8
- path: path.resolve(__dirname, "dist"),
9
- filename: "index.js",
10
- libraryTarget: "commonjs2",
11
- },
12
- resolve: {
13
- extensions: [".tsx", ".ts", ".jsx", ".js", ".json"],
14
- alias: {
15
- react: path.resolve(__dirname, "./node_modules/react"),
16
- },
17
- },
18
- module: {
19
- rules: [
20
- {
21
- test: /\.(js|jsx)$/,
22
- exclude: /node_modules/,
23
- use: {
24
- loader: "babel-loader",
25
- },
26
- },
27
- {
28
- test: /\.css$/i,
29
- use: ["style-loader", "css-loader"],
30
- },
31
- {
32
- test: /\.svg$/i,
33
- use: {
34
- loader: "svg-url-loader",
35
- },
36
- },
37
- ],
38
- },
39
- target: "node",
40
- externals: [nodeExternals()],
41
- };
1
+ const path = require("path"); // <-get absolute location for saving
2
+ const pkg = require("../package.json");
3
+ const nodeExternals = require("webpack-node-externals");
4
+ module.exports = {
5
+ mode: "production",
6
+ entry: "./index.js",
7
+ output: {
8
+ path: path.resolve(__dirname, "dist"),
9
+ filename: "index.js",
10
+ libraryTarget: "commonjs2",
11
+ },
12
+ resolve: {
13
+ extensions: [".tsx", ".ts", ".jsx", ".js", ".json"],
14
+ alias: {
15
+ react: path.resolve(__dirname, "./node_modules/react"),
16
+ },
17
+ },
18
+ module: {
19
+ rules: [
20
+ {
21
+ test: /\.(js|jsx)$/,
22
+ exclude: /node_modules/,
23
+ use: {
24
+ loader: "babel-loader",
25
+ },
26
+ },
27
+ {
28
+ test: /\.css$/i,
29
+ use: ["style-loader", "css-loader"],
30
+ },
31
+ {
32
+ test: /\.svg$/i,
33
+ use: {
34
+ loader: "svg-url-loader",
35
+ },
36
+ },
37
+ ],
38
+ },
39
+ target: "node",
40
+ externals: [nodeExternals()],
41
+ };
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { apiCall } from "./services";
2
- import { endpoints } from "./servicesData";
3
- import axiosInstance from "./axiosInstance";
4
-
5
- export { apiCall, endpoints, axiosInstance };
1
+ import { apiCall } from "./services";
2
+ import { endpoints } from "./servicesData";
3
+ import axiosInstance from "./axiosInstance";
4
+
5
+ export { apiCall, endpoints, axiosInstance };
package/package.json CHANGED
@@ -1,30 +1,39 @@
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
- }
1
+ {
2
+ "name": "sysone-api-mapper",
3
+ "version": "1.0.2",
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
+ "webpack": "^5.57.1",
21
+ "webpack-cli": "^5.0.1",
22
+ "webpack-node-externals": "^3.0.0",
23
+ "@babel/core": "^7.21.0",
24
+ "@babel/plugin-proposal-class-properties": "^7.18.6",
25
+ "@babel/preset-env": "^7.20.2",
26
+ "@babel/preset-react": "^7.18.6",
27
+ "antd": "^4.21.4",
28
+ "babel-loader": "^9.1.2"
29
+ },
30
+ "devDependencies": {
31
+ "jest": "^29.7.0",
32
+ "nodemon": "^2.0.22"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://gitlab.sysone.com/sysone-cloud-ready/sales-platform/api-mapper/-/tree/develop?ref_type=heads"
37
+ },
38
+ "private": false
39
+ }
package/services.js CHANGED
@@ -1,105 +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
- };
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
+ };