exnet-routing 1.1.6 → 1.1.8

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.
@@ -27,31 +27,27 @@ exports.apiFetch = {
27
27
  fetch: async (urlInput, method, body, searchParams, queryParams) => {
28
28
  try {
29
29
  let url = (0, exports.apiUrlBuilder)(urlInput, baseUrl, host);
30
- // Remplacer les paramètres dynamiques dans l'URL
31
30
  if (queryParams) {
32
- Object.entries(queryParams).forEach(([key, value]) => {
33
- if (value !== undefined && value !== null) {
34
- const placeholder = `:${key}`;
35
- if (url.includes(placeholder)) {
36
- url = url.replace(placeholder, encodeURIComponent(String(value)));
37
- }
38
- }
39
- });
31
+ const queryString = Object.entries(queryParams)
32
+ .filter(([_, value]) => value !== undefined && value !== null)
33
+ .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
34
+ .join('&');
35
+ url += (url.includes('?') ? '&' : '?') + queryString;
40
36
  }
41
- // Ajouter les paramètres de recherche
42
37
  if (searchParams) {
43
- const params = new URLSearchParams();
44
- if (typeof searchParams === "object") {
45
- Object.entries(searchParams).forEach(([key, value]) => {
46
- if (value !== undefined && value !== null) {
47
- params.append(key, String(value));
48
- }
49
- });
50
- }
51
- const searchString = params.toString();
52
- if (searchString) {
53
- url += (url.includes("?") ? "&" : "?") + searchString;
54
- }
38
+ const searchParamsString = typeof searchParams === "string"
39
+ ? searchParams
40
+ : Object.entries(searchParams)
41
+ .filter(([_, value]) => value !== undefined && value !== null)
42
+ .map(([key, value]) => {
43
+ const encodedKey = encodeURIComponent(key);
44
+ const encodedValue = Array.isArray(value) || typeof value === "object"
45
+ ? encodeURIComponent(JSON.stringify(value))
46
+ : encodeURIComponent(String(value !== null && value !== void 0 ? value : ""));
47
+ return `${encodedKey}=${encodedValue}`;
48
+ })
49
+ .join("&");
50
+ url += (url.includes('?') ? '&' : '?') + searchParamsString;
55
51
  }
56
52
  const res = await (0, axios_1.default)({
57
53
  headers: {
@@ -83,59 +79,45 @@ const isSubRoute = (value) => {
83
79
  return !(0, exports.isRoute)(value);
84
80
  };
85
81
  exports.isSubRoute = isSubRoute;
82
+ const createRouteFunction = (route, apiFetch) => {
83
+ const createFetchFunction = (paramTypes) => {
84
+ return async (...args) => {
85
+ const params = paramTypes.reduce((acc, type, index) => {
86
+ acc[type] = args[index] || null;
87
+ return acc;
88
+ }, {});
89
+ return await apiFetch.fetch(route.url, route.method, params.body, params.searchParams, params.queryParams);
90
+ };
91
+ };
92
+ if (route.body) {
93
+ if (route.searchParams) {
94
+ return route.params
95
+ ? createFetchFunction(['body', 'searchParams', 'queryParams'])
96
+ : createFetchFunction(['body', 'searchParams']);
97
+ }
98
+ return route.params
99
+ ? createFetchFunction(['body', 'queryParams'])
100
+ : createFetchFunction(['body']);
101
+ }
102
+ if (route.searchParams) {
103
+ return route.params
104
+ ? createFetchFunction(['searchParams', 'queryParams'])
105
+ : createFetchFunction(['searchParams']);
106
+ }
107
+ return route.params
108
+ ? createFetchFunction(['queryParams'])
109
+ : createFetchFunction([]);
110
+ };
86
111
  const parseApiRoute = (key, subRoute, result, hostApi, basePath, headers) => {
87
112
  var _a;
88
113
  exports.apiFetch.setHostApi(hostApi);
89
114
  exports.apiFetch.setBaseUrl(basePath);
90
115
  exports.apiFetch.setHeaders(headers);
91
116
  if ((0, exports.isRoute)(subRoute) && key) {
92
- const route = subRoute;
93
- if (route.body) {
94
- if (route.searchParams) {
95
- if (route.params) {
96
- ;
97
- result = async (body, searchParams, queryParams) => await exports.apiFetch.fetch(route.url, route.method, body, searchParams, queryParams);
98
- }
99
- else {
100
- ;
101
- result = async (body, searchParams) => await exports.apiFetch.fetch(route.url, route.method, body, searchParams);
102
- }
103
- }
104
- else {
105
- if (route.params) {
106
- ;
107
- result = async (body, queryParams) => await exports.apiFetch.fetch(route.url, route.method, body, undefined, queryParams);
108
- }
109
- else {
110
- ;
111
- result = async (body) => await exports.apiFetch.fetch(route.url, route.method, body);
112
- }
113
- }
114
- }
115
- else if (route.searchParams) {
116
- if (route.params) {
117
- ;
118
- result = async (searchParams, queryParams) => await exports.apiFetch.fetch(route.url, route.method, undefined, searchParams, queryParams);
119
- }
120
- else {
121
- ;
122
- result = async (searchParams) => await exports.apiFetch.fetch(route.url, route.method, undefined, searchParams);
123
- }
124
- }
125
- else {
126
- if (route.params) {
127
- ;
128
- result = async (queryParams) => await exports.apiFetch.fetch(route.url, route.method, undefined, undefined, queryParams);
129
- }
130
- else {
131
- ;
132
- result = async () => await exports.apiFetch.fetch(route.url, route.method);
133
- }
134
- }
117
+ result = createRouteFunction(subRoute, exports.apiFetch);
135
118
  }
136
119
  else if ((0, exports.isSubRoute)(subRoute)) {
137
120
  for (const [key, value] of Object.entries(subRoute)) {
138
- ;
139
121
  result[key] = (0, exports.parseApiRoute)(key, value, ((_a = result[key]) !== null && _a !== void 0 ? _a : {}), hostApi, basePath, headers);
140
122
  }
141
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exnet-routing",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [