@starasia/http 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.
- package/dist/index.es.js +55 -53
- package/dist/index.umd.js +1 -1
- package/dist/libs/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const i = (e) => {
|
|
2
2
|
switch (e) {
|
|
3
3
|
case "url-encoded":
|
|
4
4
|
return "application/x-www-form-urlencoded";
|
|
@@ -7,35 +7,37 @@ const y = (e) => {
|
|
|
7
7
|
default:
|
|
8
8
|
return;
|
|
9
9
|
}
|
|
10
|
-
},
|
|
10
|
+
}, p = (e, t, r) => {
|
|
11
11
|
if (e !== "GET")
|
|
12
|
-
return t === "form-data" ?
|
|
12
|
+
return t === "form-data" ? r : JSON.stringify(r);
|
|
13
13
|
};
|
|
14
|
-
function
|
|
15
|
-
const t = new URLSearchParams(e),
|
|
16
|
-
for (const [
|
|
17
|
-
|
|
18
|
-
return
|
|
14
|
+
function j(e) {
|
|
15
|
+
const t = new URLSearchParams(e), r = {};
|
|
16
|
+
for (const [n, a] of t)
|
|
17
|
+
r[n] = a;
|
|
18
|
+
return r;
|
|
19
19
|
}
|
|
20
|
-
function
|
|
20
|
+
function y(e) {
|
|
21
21
|
if (!e || typeof e != "object" || Array.isArray(e))
|
|
22
22
|
return "";
|
|
23
23
|
const t = new URLSearchParams();
|
|
24
|
-
return Object.keys(e).forEach((
|
|
25
|
-
const
|
|
26
|
-
|
|
24
|
+
return Object.keys(e).forEach((r) => {
|
|
25
|
+
const n = e[r];
|
|
26
|
+
n != null && (Array.isArray(n) ? n.forEach((a) => {
|
|
27
|
+
a != null && t.append(r, String(a));
|
|
28
|
+
}) : t.append(r, String(n)));
|
|
27
29
|
}), t.toString();
|
|
28
30
|
}
|
|
29
|
-
function
|
|
31
|
+
function d(e, t, r) {
|
|
30
32
|
if (!e) return "";
|
|
31
|
-
let
|
|
32
|
-
if (
|
|
33
|
-
const
|
|
34
|
-
|
|
33
|
+
let n = t ? `${e}/${t}` : e;
|
|
34
|
+
if (r) {
|
|
35
|
+
const a = y(r);
|
|
36
|
+
n = `${n}${a ? `?${a}` : ""}`;
|
|
35
37
|
}
|
|
36
|
-
return
|
|
38
|
+
return n;
|
|
37
39
|
}
|
|
38
|
-
async function
|
|
40
|
+
async function l(e) {
|
|
39
41
|
return typeof e == "function" ? await e() : e;
|
|
40
42
|
}
|
|
41
43
|
const h = {
|
|
@@ -44,24 +46,24 @@ const h = {
|
|
|
44
46
|
withAppAuth: !0,
|
|
45
47
|
responseContentType: "json"
|
|
46
48
|
};
|
|
47
|
-
async function
|
|
48
|
-
let
|
|
49
|
-
const s =
|
|
49
|
+
async function T(e, t, r, n) {
|
|
50
|
+
let a = { ...h, ...n };
|
|
51
|
+
const s = i(a == null ? void 0 : a.contentType);
|
|
50
52
|
let o = {};
|
|
51
|
-
s && (o["Content-Type"] = s),
|
|
53
|
+
s && (o["Content-Type"] = s), a.bearerToken && typeof a.bearerToken == "string" && (o.Authorization = `Bearer ${a.bearerToken}`), a.headers && (o = {
|
|
52
54
|
...o,
|
|
53
|
-
...
|
|
55
|
+
...a.headers
|
|
54
56
|
});
|
|
55
57
|
const c = await fetch(e, {
|
|
56
58
|
method: t,
|
|
57
59
|
headers: o,
|
|
58
|
-
...(
|
|
59
|
-
body:
|
|
60
|
+
...(a == null ? void 0 : a.signal) && { signal: a.signal },
|
|
61
|
+
body: p(t, a.contentType, r)
|
|
60
62
|
});
|
|
61
63
|
let u;
|
|
62
64
|
if (!c.ok)
|
|
63
65
|
throw await c.json();
|
|
64
|
-
switch (
|
|
66
|
+
switch (a.responseContentType) {
|
|
65
67
|
case "json":
|
|
66
68
|
u = await c.json();
|
|
67
69
|
break;
|
|
@@ -77,8 +79,8 @@ async function w(e, t, a, r) {
|
|
|
77
79
|
}
|
|
78
80
|
return u;
|
|
79
81
|
}
|
|
80
|
-
const f = async (e, t,
|
|
81
|
-
const s = await
|
|
82
|
+
const f = async (e, t, r, n, a) => {
|
|
83
|
+
const s = await l(a), o = await l(n), c = d(
|
|
82
84
|
e,
|
|
83
85
|
(o == null ? void 0 : o.path) || (s == null ? void 0 : s.path) || "",
|
|
84
86
|
{
|
|
@@ -88,52 +90,52 @@ const f = async (e, t, a, r, n) => {
|
|
|
88
90
|
...o == null ? void 0 : o.params
|
|
89
91
|
}
|
|
90
92
|
), u = { ...s, ...o };
|
|
91
|
-
return
|
|
92
|
-
},
|
|
93
|
+
return T(c, t, r, u);
|
|
94
|
+
}, w = (e, t) => (r) => f(
|
|
93
95
|
e,
|
|
94
96
|
"GET",
|
|
95
97
|
void 0,
|
|
96
|
-
|
|
98
|
+
r,
|
|
97
99
|
t
|
|
98
|
-
), b = (e, t) => (
|
|
100
|
+
), b = (e, t) => (r, n) => f(
|
|
99
101
|
e,
|
|
100
102
|
"POST",
|
|
101
|
-
a,
|
|
102
103
|
r,
|
|
104
|
+
n,
|
|
103
105
|
t
|
|
104
|
-
),
|
|
106
|
+
), g = (e, t) => (r, n) => f(
|
|
105
107
|
e,
|
|
106
108
|
"PUT",
|
|
107
|
-
a,
|
|
108
109
|
r,
|
|
110
|
+
n,
|
|
109
111
|
t
|
|
110
|
-
),
|
|
112
|
+
), m = (e, t) => (r, n) => f(
|
|
111
113
|
e,
|
|
112
114
|
"PATCH",
|
|
113
|
-
a,
|
|
114
115
|
r,
|
|
116
|
+
n,
|
|
115
117
|
t
|
|
116
|
-
),
|
|
118
|
+
), v = (e, t) => (r) => f(
|
|
117
119
|
e,
|
|
118
120
|
"DELETE",
|
|
119
121
|
void 0,
|
|
120
|
-
|
|
122
|
+
r,
|
|
121
123
|
t
|
|
122
|
-
),
|
|
123
|
-
get:
|
|
124
|
+
), A = {
|
|
125
|
+
get: w,
|
|
124
126
|
post: b,
|
|
125
|
-
put:
|
|
126
|
-
patch:
|
|
127
|
-
delete:
|
|
127
|
+
put: g,
|
|
128
|
+
patch: m,
|
|
129
|
+
delete: v
|
|
128
130
|
};
|
|
129
131
|
export {
|
|
130
|
-
|
|
132
|
+
d as createEndpoint,
|
|
131
133
|
f as createRequest,
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
134
|
+
p as getBody,
|
|
135
|
+
i as getContentType,
|
|
136
|
+
A as http,
|
|
137
|
+
y as objectToSearchParams,
|
|
138
|
+
T as request,
|
|
139
|
+
l as resolveRequestOptions,
|
|
140
|
+
j as searchParamsToObject
|
|
139
141
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s["@starasia/http"]={}))})(this,function(s){"use strict";const i=e=>{switch(e){case"url-encoded":return"application/x-www-form-urlencoded";case"json":return"application/json";default:return}},
|
|
1
|
+
(function(s,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s["@starasia/http"]={}))})(this,function(s){"use strict";const i=e=>{switch(e){case"url-encoded":return"application/x-www-form-urlencoded";case"json":return"application/json";default:return}},p=(e,t,n)=>{if(e!=="GET")return t==="form-data"?n:JSON.stringify(n)};function w(e){const t=new URLSearchParams(e),n={};for(const[r,a]of t)n[r]=a;return n}function h(e){if(!e||typeof e!="object"||Array.isArray(e))return"";const t=new URLSearchParams;return Object.keys(e).forEach(n=>{const r=e[n];r!=null&&(Array.isArray(r)?r.forEach(a=>{a!=null&&t.append(n,String(a))}):t.append(n,String(r)))}),t.toString()}function y(e,t,n){if(!e)return"";let r=t?`${e}/${t}`:e;if(n){const a=h(n);r=`${r}${a?`?${a}`:""}`}return r}async function l(e){return typeof e=="function"?await e():e}const b={contentType:"json",path:"",withAppAuth:!0,responseContentType:"json"};async function T(e,t,n,r){let a={...b,...r};const c=i(a==null?void 0:a.contentType);let o={};c&&(o["Content-Type"]=c),a.bearerToken&&typeof a.bearerToken=="string"&&(o.Authorization=`Bearer ${a.bearerToken}`),a.headers&&(o={...o,...a.headers});const u=await fetch(e,{method:t,headers:o,...(a==null?void 0:a.signal)&&{signal:a.signal},body:p(t,a.contentType,n)});let f;if(!u.ok)throw await u.json();switch(a.responseContentType){case"json":f=await u.json();break;case"text":f=await u.text();break;case"blob":f=await u.blob();break;default:f=await u.json();break}return f}const d=async(e,t,n,r,a)=>{const c=await l(a),o=await l(r),u=y(e,(o==null?void 0:o.path)||(c==null?void 0:c.path)||"",{...c==null?void 0:c.metaParams,...c==null?void 0:c.params,...o==null?void 0:o.metaParams,...o==null?void 0:o.params}),f={...c,...o};return T(u,t,n,f)},g={get:(e,t)=>n=>d(e,"GET",void 0,n,t),post:(e,t)=>(n,r)=>d(e,"POST",n,r,t),put:(e,t)=>(n,r)=>d(e,"PUT",n,r,t),patch:(e,t)=>(n,r)=>d(e,"PATCH",n,r,t),delete:(e,t)=>n=>d(e,"DELETE",void 0,n,t)};s.createEndpoint=y,s.createRequest=d,s.getBody=p,s.getContentType=i,s.http=g,s.objectToSearchParams=h,s.request=T,s.resolveRequestOptions=l,s.searchParamsToObject=w,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/dist/libs/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
2
|
-
export type ParamType = Record<string, string | number | undefined | null
|
|
2
|
+
export type ParamType = Record<string, string | number | boolean | null | undefined | Array<string | number | boolean | null>>;
|
|
3
3
|
export interface MetaParams {
|
|
4
4
|
page?: number;
|
|
5
5
|
perPage?: number;
|