@acrool/react-fetcher 0.0.8 → 0.0.10-alpha.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/README.md
CHANGED
|
@@ -131,6 +131,49 @@ logout();
|
|
|
131
131
|
|
|
132
132
|
---
|
|
133
133
|
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
### Options queryString
|
|
137
|
+
|
|
138
|
+
(.net not support)
|
|
139
|
+
default: `?statusIds[]=1&statusIds[]=2`
|
|
140
|
+
|
|
141
|
+
change to: `?statusIds=1&statusIds=2`
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
export const baseQueryWithAxios: BaseQueryFn<IQuery> = async (query, BaseQueryApi, extraOptions) => {
|
|
145
|
+
await mutex.waitForUnlock();
|
|
146
|
+
try {
|
|
147
|
+
const {url, method, fetchOptions, ...args} = query;
|
|
148
|
+
const data = await createRestFulFetcher(axiosInstance, {url, method}, getContentTypeWithMethod)({
|
|
149
|
+
...args,
|
|
150
|
+
fetchOptions: {
|
|
151
|
+
...fetchOptions,
|
|
152
|
+
paramsSerializer: (params) => {
|
|
153
|
+
return qs.stringify(params, {indices: false});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
data,
|
|
160
|
+
meta: {}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
} catch (error: any) {
|
|
164
|
+
dialog.error(error.message, {code: error.code});
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
error: {
|
|
168
|
+
code: error.code || 500,
|
|
169
|
+
message: error.message
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
|
|
134
177
|
## License
|
|
135
178
|
|
|
136
179
|
MIT © [Acrool](https://github.com/acrool) & [Imagine](https://github.com/imagine10255)
|
|
@@ -94,6 +94,20 @@ const createGraphQLFetcher = (axiosInstance, query, options) => {
|
|
|
94
94
|
return res.data.data;
|
|
95
95
|
};
|
|
96
96
|
};
|
|
97
|
+
var ERequestContentType = /* @__PURE__ */ ((ERequestContentType2) => {
|
|
98
|
+
ERequestContentType2["formData"] = "multipart/form-data";
|
|
99
|
+
ERequestContentType2["formUrlDecode"] = "application/x-www-form-urlencoded";
|
|
100
|
+
ERequestContentType2["json"] = "application/json";
|
|
101
|
+
return ERequestContentType2;
|
|
102
|
+
})(ERequestContentType || {});
|
|
103
|
+
var ERequestMethod = /* @__PURE__ */ ((ERequestMethod2) => {
|
|
104
|
+
ERequestMethod2["GET"] = "GET";
|
|
105
|
+
ERequestMethod2["POST"] = "POST";
|
|
106
|
+
ERequestMethod2["PUT"] = "PUT";
|
|
107
|
+
ERequestMethod2["DELETE"] = "DELETE";
|
|
108
|
+
ERequestMethod2["PATCH"] = "PATCH";
|
|
109
|
+
return ERequestMethod2;
|
|
110
|
+
})(ERequestMethod || {});
|
|
97
111
|
function $(t2) {
|
|
98
112
|
const n = new FormData(), e2 = (s2, r2 = "") => {
|
|
99
113
|
for (const [a2, o] of Object.entries(s2)) {
|
|
@@ -110,27 +124,10 @@ function $(t2) {
|
|
|
110
124
|
};
|
|
111
125
|
return e2(t2), n;
|
|
112
126
|
}
|
|
113
|
-
var ERequestContentType = /* @__PURE__ */ ((ERequestContentType2) => {
|
|
114
|
-
ERequestContentType2["formData"] = "multipart/form-data";
|
|
115
|
-
ERequestContentType2["formUrlDecode"] = "application/x-www-form-urlencoded";
|
|
116
|
-
ERequestContentType2["json"] = "application/json";
|
|
117
|
-
return ERequestContentType2;
|
|
118
|
-
})(ERequestContentType || {});
|
|
119
|
-
var ERequestMethod = /* @__PURE__ */ ((ERequestMethod2) => {
|
|
120
|
-
ERequestMethod2["GET"] = "GET";
|
|
121
|
-
ERequestMethod2["POST"] = "POST";
|
|
122
|
-
ERequestMethod2["PUT"] = "PUT";
|
|
123
|
-
ERequestMethod2["DELETE"] = "DELETE";
|
|
124
|
-
ERequestMethod2["PATCH"] = "PATCH";
|
|
125
|
-
return ERequestMethod2;
|
|
126
|
-
})(ERequestMethod || {});
|
|
127
127
|
const getDataWithContentType = (contentType, data = {}) => {
|
|
128
|
-
if ([ERequestContentType.formData
|
|
128
|
+
if ([ERequestContentType.formData].includes(contentType)) return $(data);
|
|
129
129
|
return JSON.stringify(data);
|
|
130
130
|
};
|
|
131
|
-
const getContentTypeWithMethod = (method) => {
|
|
132
|
-
return ERequestContentType.json;
|
|
133
|
-
};
|
|
134
131
|
const createRestFulFetcher = (axiosInstance, document2, options) => {
|
|
135
132
|
return async (args) => {
|
|
136
133
|
const method = (document2 == null ? void 0 : document2.method) || "";
|
|
@@ -143,8 +140,10 @@ const createRestFulFetcher = (axiosInstance, document2, options) => {
|
|
|
143
140
|
}
|
|
144
141
|
const body = typeof args === "object" && args !== null && "body" in args ? args.body : void 0;
|
|
145
142
|
const params = typeof args === "object" && args !== null && "params" in args ? args.params : void 0;
|
|
146
|
-
|
|
147
|
-
|
|
143
|
+
let contentType = ERequestContentType.json;
|
|
144
|
+
if (headers == null ? void 0 : headers.contentType) contentType = headers.contentType;
|
|
145
|
+
else if (options == null ? void 0 : options.contentTypeResolver) contentType = options.contentTypeResolver(method.toUpperCase());
|
|
146
|
+
else if (document2.contentType) contentType = document2.contentType;
|
|
148
147
|
const config = {
|
|
149
148
|
url: document2.url,
|
|
150
149
|
method,
|