@zuzjs/core 0.1.1 → 0.1.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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +70 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -25,3 +25,4 @@ export declare const createElems: (list: ElementNode[]) => Node[];
|
|
|
25
25
|
export declare const getPlatform: () => "MAC" | "WIN" | "UNKNOWN";
|
|
26
26
|
export declare const findParent: (node: HTMLElement, classes: string[], depth?: number) => HTMLElement | null;
|
|
27
27
|
export declare const ucfirst: (str: string) => string;
|
|
28
|
+
export declare const withRest: (uri: string, data: object, timeout: number | undefined, fd: object, progress?: Function, bearer?: string) => Promise<unknown>;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Hashids from "hashids";
|
|
2
2
|
import { nanoid } from "nanoid";
|
|
3
3
|
import Cookies from 'js-cookie';
|
|
4
|
+
import axios from 'axios';
|
|
4
5
|
const _Hashids = new Hashids('', 4);
|
|
5
6
|
export const setCookie = (key, value, expiry, host) => Cookies.set(key, value, { expires: expiry || 7, domain: host || window.location.host });
|
|
6
7
|
export const getCookie = (key) => key == `` ? Cookies.get() : Cookies.get(key) || null;
|
|
@@ -73,3 +74,72 @@ export const ucfirst = (str) => {
|
|
|
73
74
|
return str = str.trim(),
|
|
74
75
|
str.charAt(0).toUpperCase() + str.substring(1);
|
|
75
76
|
};
|
|
77
|
+
const buildFormData = (data) => {
|
|
78
|
+
var formData = new FormData();
|
|
79
|
+
var _data = Cookies.get();
|
|
80
|
+
Object.keys(_data).map(k => formData.append(k, _data[k]));
|
|
81
|
+
Object.keys(data).filter(x => x != 'files').map(k => formData.append(k, data[k]));
|
|
82
|
+
if ('files' in data)
|
|
83
|
+
[data['files']].map((f) => formData.append('files[]', f));
|
|
84
|
+
return formData;
|
|
85
|
+
};
|
|
86
|
+
export const withRest = async (uri, data, timeout = 60, fd, progress, bearer = `__ha`) => {
|
|
87
|
+
var Bearer = getCookie(bearer) || `${uuid(8)}^${uuid(8)}`;
|
|
88
|
+
var isWindow = typeof window !== 'undefined';
|
|
89
|
+
var cancelToken = null;
|
|
90
|
+
if (isWindow) {
|
|
91
|
+
window.__restToken = axios.CancelToken.source();
|
|
92
|
+
cancelToken = window.__restToken?.token;
|
|
93
|
+
}
|
|
94
|
+
if (fd) {
|
|
95
|
+
return new Promise((resolve, reject) => {
|
|
96
|
+
axios({
|
|
97
|
+
method: "post",
|
|
98
|
+
url: uri,
|
|
99
|
+
data: buildFormData(data),
|
|
100
|
+
timeout: 1000 * timeout,
|
|
101
|
+
cancelToken: cancelToken,
|
|
102
|
+
headers: {
|
|
103
|
+
'Content-Type': 'multipart/form-data',
|
|
104
|
+
'Authorization': `Bearer ${Bearer}`
|
|
105
|
+
},
|
|
106
|
+
onUploadProgress: ev => {
|
|
107
|
+
//TODO: Add progress
|
|
108
|
+
// if(progress) progress(ev.)
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
.then(resp => {
|
|
112
|
+
if (resp.data && "kind" in resp.data) {
|
|
113
|
+
resolve(resp.data);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
reject(resp.data);
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
.catch(err => reject(err));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return new Promise((resolve, reject) => {
|
|
123
|
+
axios.post(uri, {
|
|
124
|
+
...Cookies.get(),
|
|
125
|
+
...data,
|
|
126
|
+
__ustmp: new Date().getTime() / 1000
|
|
127
|
+
}, {
|
|
128
|
+
timeout: 1000 * timeout,
|
|
129
|
+
headers: {
|
|
130
|
+
'Content-Type': 'application/json',
|
|
131
|
+
'Authorization': `Bearer ${Bearer}`
|
|
132
|
+
},
|
|
133
|
+
cancelToken: cancelToken
|
|
134
|
+
})
|
|
135
|
+
.then(resp => {
|
|
136
|
+
if (resp.data && "kind" in resp.data) {
|
|
137
|
+
resolve(resp.data);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
reject(resp.data);
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
.catch(err => reject(err));
|
|
144
|
+
});
|
|
145
|
+
};
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|