@zuzjs/core 0.1.1 → 0.1.3

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 CHANGED
@@ -25,3 +25,22 @@ 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
+ /**
29
+ * HTTP Requests
30
+ */
31
+ export declare const withRest: (uri: string, data: object, timeout: number | undefined, fd: object, progress?: Function, bearer?: string) => Promise<unknown>;
32
+ /**
33
+ * Extends Object.prototype adding,
34
+ * @function isNull
35
+ * @function equals
36
+ * @function isString
37
+ * @function isNumber
38
+ * @function isObject
39
+ * @function isArray
40
+ * @function isEmpty
41
+ * @function isNotEmpty
42
+ * @function isEmail
43
+ * @function isUrl
44
+ *
45
+ */
46
+ export declare const extendGlobals: () => void;
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,106 @@ 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
+ /**
87
+ * HTTP Requests
88
+ */
89
+ export const withRest = async (uri, data, timeout = 60, fd, progress, bearer = `__ha`) => {
90
+ var Bearer = getCookie(bearer) || `${uuid(8)}^${uuid(8)}`;
91
+ var isWindow = typeof window !== 'undefined';
92
+ var cancelToken = null;
93
+ if (isWindow) {
94
+ window.__restToken = axios.CancelToken.source();
95
+ cancelToken = window.__restToken?.token;
96
+ }
97
+ if (fd) {
98
+ return new Promise((resolve, reject) => {
99
+ axios({
100
+ method: "post",
101
+ url: uri,
102
+ data: buildFormData(data),
103
+ timeout: 1000 * timeout,
104
+ cancelToken: cancelToken,
105
+ headers: {
106
+ 'Content-Type': 'multipart/form-data',
107
+ 'Authorization': `Bearer ${Bearer}`
108
+ },
109
+ onUploadProgress: ev => {
110
+ //TODO: Add progress
111
+ // if(progress) progress(ev.)
112
+ }
113
+ })
114
+ .then(resp => {
115
+ if (resp.data && "kind" in resp.data) {
116
+ resolve(resp.data);
117
+ }
118
+ else {
119
+ reject(resp.data);
120
+ }
121
+ })
122
+ .catch(err => reject(err));
123
+ });
124
+ }
125
+ return new Promise((resolve, reject) => {
126
+ axios.post(uri, {
127
+ ...Cookies.get(),
128
+ ...data,
129
+ __ustmp: new Date().getTime() / 1000
130
+ }, {
131
+ timeout: 1000 * timeout,
132
+ headers: {
133
+ 'Content-Type': 'application/json',
134
+ 'Authorization': `Bearer ${Bearer}`
135
+ },
136
+ cancelToken: cancelToken
137
+ })
138
+ .then(resp => {
139
+ if (resp.data && "kind" in resp.data) {
140
+ resolve(resp.data);
141
+ }
142
+ else {
143
+ reject(resp.data);
144
+ }
145
+ })
146
+ .catch(err => reject(err));
147
+ });
148
+ };
149
+ /**
150
+ * Extends Object.prototype adding,
151
+ * @function isNull
152
+ * @function equals
153
+ * @function isString
154
+ * @function isNumber
155
+ * @function isObject
156
+ * @function isArray
157
+ * @function isEmpty
158
+ * @function isNotEmpty
159
+ * @function isEmail
160
+ * @function isUrl
161
+ *
162
+ */
163
+ export const extendGlobals = () => {
164
+ Object.prototype.isNull = function () { return this === null; };
165
+ Object.prototype.equals = function (v) { return this === v; };
166
+ Object.prototype.isString = function () { return typeof this == `string`; };
167
+ Object.prototype.isNumber = function () { return /^[+-]?\d+(\.\d+)?$/.test(this); };
168
+ Object.prototype.isObject = function () { return typeof this == `object` && !Array.isArray(this) && this !== null; };
169
+ Object.prototype.isArray = function () { return Array.isArray(this); };
170
+ Object.prototype.isEmpty = function () {
171
+ if (Array.isArray(this))
172
+ return this.length === 0;
173
+ else if (`object` === typeof this)
174
+ return Object.keys(this).length == 0;
175
+ else
176
+ return this == "" || this.length == 0;
177
+ };
178
+ };
179
+ extendGlobals();
@@ -0,0 +1,18 @@
1
+ declare global {
2
+ interface Window {
3
+ __restToken: any;
4
+ }
5
+ interface Object {
6
+ isNull(): boolean;
7
+ equals(v: any): boolean;
8
+ isString(): boolean;
9
+ isNumber(): boolean;
10
+ isObject(): boolean;
11
+ isArray(): boolean;
12
+ isEmpty(): boolean;
13
+ isNotEmpty(v: any): boolean;
14
+ isEmail(v: any): boolean;
15
+ isUrl(v: any): boolean;
16
+ }
17
+ }
18
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuzjs/core",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "keywords": [
5
5
  "react",
6
6
  "zuz",
@@ -18,16 +18,17 @@
18
18
  "dist"
19
19
  ],
20
20
  "scripts": {
21
- "dev": "tsc -d -w -p tsconfig.json"
21
+ "dev": "tsc -d -w -p tsconfig.json",
22
+ "build": "tsc -b tsconfig.json"
22
23
  },
23
24
  "engines": {
24
25
  "node": ">=18.17.0"
25
26
  },
26
27
  "dependencies": {
27
- "axios": "1.6.5",
28
+ "axios": "1.6.8",
28
29
  "hashids": "2.3.0",
29
30
  "js-cookie": "3.0.5",
30
- "nanoid": "5.0.4",
31
- "prettier": "3.2.4"
31
+ "nanoid": "5.0.7",
32
+ "prettier": "3.2.5"
32
33
  }
33
34
  }