@starasia/http 1.0.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/dist/index.d.ts +4 -0
- package/dist/index.es.js +139 -0
- package/dist/index.umd.js +1 -0
- package/dist/libs/http.d.ts +9 -0
- package/dist/libs/request.d.ts +4 -0
- package/dist/libs/types.d.ts +39 -0
- package/dist/libs/utils.d.ts +8 -0
- package/package.json +34 -0
package/dist/index.d.ts
ADDED
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const y = (e) => {
|
|
2
|
+
switch (e) {
|
|
3
|
+
case "url-encoded":
|
|
4
|
+
return "application/x-www-form-urlencoded";
|
|
5
|
+
case "json":
|
|
6
|
+
return "application/json";
|
|
7
|
+
default:
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
}, i = (e, t, a) => {
|
|
11
|
+
if (e !== "GET")
|
|
12
|
+
return t === "form-data" ? a : JSON.stringify(a);
|
|
13
|
+
};
|
|
14
|
+
function k(e) {
|
|
15
|
+
const t = new URLSearchParams(e), a = {};
|
|
16
|
+
for (const [r, n] of t)
|
|
17
|
+
a[r] = n;
|
|
18
|
+
return a;
|
|
19
|
+
}
|
|
20
|
+
function l(e) {
|
|
21
|
+
if (!e || typeof e != "object" || Array.isArray(e))
|
|
22
|
+
return "";
|
|
23
|
+
const t = new URLSearchParams();
|
|
24
|
+
return Object.keys(e).forEach((a) => {
|
|
25
|
+
const r = e[a];
|
|
26
|
+
(typeof r == "string" || typeof r == "number") && t.append(a, r.toString());
|
|
27
|
+
}), t.toString();
|
|
28
|
+
}
|
|
29
|
+
function T(e, t, a) {
|
|
30
|
+
if (!e) return "";
|
|
31
|
+
let r = t ? `${e}/${t}` : e;
|
|
32
|
+
if (a) {
|
|
33
|
+
const n = l(a);
|
|
34
|
+
r = `${r}${n ? `?${n}` : ""}`;
|
|
35
|
+
}
|
|
36
|
+
return r;
|
|
37
|
+
}
|
|
38
|
+
async function p(e) {
|
|
39
|
+
return typeof e == "function" ? await e() : e;
|
|
40
|
+
}
|
|
41
|
+
const h = {
|
|
42
|
+
contentType: "json",
|
|
43
|
+
path: "",
|
|
44
|
+
withAppAuth: !0,
|
|
45
|
+
responseContentType: "json"
|
|
46
|
+
};
|
|
47
|
+
async function w(e, t, a, r) {
|
|
48
|
+
let n = { ...h, ...r };
|
|
49
|
+
const s = y(n == null ? void 0 : n.contentType);
|
|
50
|
+
let o = {};
|
|
51
|
+
s && (o["Content-Type"] = s), n.bearerToken && typeof n.bearerToken == "string" && (o.Authorization = `Bearer ${n.bearerToken}`), n.headers && (o = {
|
|
52
|
+
...o,
|
|
53
|
+
...n.headers
|
|
54
|
+
});
|
|
55
|
+
const c = await fetch(e, {
|
|
56
|
+
method: t,
|
|
57
|
+
headers: o,
|
|
58
|
+
...(n == null ? void 0 : n.signal) && { signal: n.signal },
|
|
59
|
+
body: i(t, n.contentType, a)
|
|
60
|
+
});
|
|
61
|
+
let u;
|
|
62
|
+
if (!c.ok)
|
|
63
|
+
throw await c.json();
|
|
64
|
+
switch (n.responseContentType) {
|
|
65
|
+
case "json":
|
|
66
|
+
u = await c.json();
|
|
67
|
+
break;
|
|
68
|
+
case "text":
|
|
69
|
+
u = await c.text();
|
|
70
|
+
break;
|
|
71
|
+
case "blob":
|
|
72
|
+
u = await c.blob();
|
|
73
|
+
break;
|
|
74
|
+
default:
|
|
75
|
+
u = await c.json();
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
return u;
|
|
79
|
+
}
|
|
80
|
+
const f = async (e, t, a, r, n) => {
|
|
81
|
+
const s = await p(n), o = await p(r), c = T(
|
|
82
|
+
e,
|
|
83
|
+
(o == null ? void 0 : o.path) || (s == null ? void 0 : s.path) || "",
|
|
84
|
+
{
|
|
85
|
+
...s == null ? void 0 : s.metaParams,
|
|
86
|
+
...s == null ? void 0 : s.params,
|
|
87
|
+
...o == null ? void 0 : o.metaParams,
|
|
88
|
+
...o == null ? void 0 : o.params
|
|
89
|
+
}
|
|
90
|
+
), u = { ...s, ...o };
|
|
91
|
+
return w(c, t, a, u);
|
|
92
|
+
}, d = (e, t) => (a) => f(
|
|
93
|
+
e,
|
|
94
|
+
"GET",
|
|
95
|
+
void 0,
|
|
96
|
+
a,
|
|
97
|
+
t
|
|
98
|
+
), b = (e, t) => (a, r) => f(
|
|
99
|
+
e,
|
|
100
|
+
"POST",
|
|
101
|
+
a,
|
|
102
|
+
r,
|
|
103
|
+
t
|
|
104
|
+
), m = (e, t) => (a, r) => f(
|
|
105
|
+
e,
|
|
106
|
+
"PUT",
|
|
107
|
+
a,
|
|
108
|
+
r,
|
|
109
|
+
t
|
|
110
|
+
), g = (e, t) => (a, r) => f(
|
|
111
|
+
e,
|
|
112
|
+
"PATCH",
|
|
113
|
+
a,
|
|
114
|
+
r,
|
|
115
|
+
t
|
|
116
|
+
), j = (e, t) => (a) => f(
|
|
117
|
+
e,
|
|
118
|
+
"DELETE",
|
|
119
|
+
void 0,
|
|
120
|
+
a,
|
|
121
|
+
t
|
|
122
|
+
), P = {
|
|
123
|
+
get: d,
|
|
124
|
+
post: b,
|
|
125
|
+
put: m,
|
|
126
|
+
patch: g,
|
|
127
|
+
delete: j
|
|
128
|
+
};
|
|
129
|
+
export {
|
|
130
|
+
T as createEndpoint,
|
|
131
|
+
f as createRequest,
|
|
132
|
+
i as getBody,
|
|
133
|
+
y as getContentType,
|
|
134
|
+
P as http,
|
|
135
|
+
l as objectToSearchParams,
|
|
136
|
+
w as request,
|
|
137
|
+
p as resolveRequestOptions,
|
|
138
|
+
k as searchParamsToObject
|
|
139
|
+
};
|
|
@@ -0,0 +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}},y=(e,t,n)=>{if(e!=="GET")return t==="form-data"?n:JSON.stringify(n)};function b(e){const t=new URLSearchParams(e),n={};for(const[r,a]of t)n[r]=a;return n}function l(e){if(!e||typeof e!="object"||Array.isArray(e))return"";const t=new URLSearchParams;return Object.keys(e).forEach(n=>{const r=e[n];(typeof r=="string"||typeof r=="number")&&t.append(n,r.toString())}),t.toString()}function h(e,t,n){if(!e)return"";let r=t?`${e}/${t}`:e;if(n){const a=l(n);r=`${r}${a?`?${a}`:""}`}return r}async function p(e){return typeof e=="function"?await e():e}const m={contentType:"json",path:"",withAppAuth:!0,responseContentType:"json"};async function T(e,t,n,r){let a={...m,...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:y(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 p(a),o=await p(r),u=h(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)},w={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=h,s.createRequest=d,s.getBody=y,s.getContentType=i,s.http=w,s.objectToSearchParams=l,s.request=T,s.resolveRequestOptions=p,s.searchParamsToObject=b,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Option } from './types';
|
|
2
|
+
|
|
3
|
+
export declare const http: {
|
|
4
|
+
get: <Res = unknown, Req = unknown>(endpoint: string, initialOptions?: Option) => (options?: Option) => Promise<Res>;
|
|
5
|
+
post: <Res = unknown, Req = unknown>(endpoint: string, initialOptions?: Option) => (requestBody?: Req, options?: Option) => Promise<Res>;
|
|
6
|
+
put: <Res = unknown, Req = unknown>(endpoint: string, initialOptions?: Option) => (requestBody?: Req, options?: Option) => Promise<Res>;
|
|
7
|
+
patch: <Res = unknown, Req = unknown>(endpoint: string, initialOptions?: Option) => (requestBody?: Req, options?: Option) => Promise<Res>;
|
|
8
|
+
delete: <Res = unknown, Req = unknown>(endpoint: string, initialOptions?: Option) => (options?: Option) => Promise<Res>;
|
|
9
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Method, Option, RequestApiOptions } from './types';
|
|
2
|
+
|
|
3
|
+
export declare function request<Res = unknown, Req = unknown>(endpoint: string, method: Method, requestBody?: Req, requestOptions?: RequestApiOptions): Promise<Res>;
|
|
4
|
+
export declare const createRequest: <Res, Req>(endpoint: string, method: Method, body?: Req, requestOpts?: Option, initialOpts?: Option) => Promise<Res>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
2
|
+
export type ParamType = Record<string, string | number | undefined | null>;
|
|
3
|
+
export interface MetaParams {
|
|
4
|
+
page?: number;
|
|
5
|
+
perPage?: number;
|
|
6
|
+
status?: "active" | "inactive";
|
|
7
|
+
search?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface RequestApiOptions {
|
|
10
|
+
bearerToken?: string;
|
|
11
|
+
contentType?: "json" | "form-data" | "url-encoded";
|
|
12
|
+
headers?: HeadersInit;
|
|
13
|
+
signal?: RequestInit["signal"];
|
|
14
|
+
path?: string;
|
|
15
|
+
params?: ParamType;
|
|
16
|
+
metaParams?: MetaParams;
|
|
17
|
+
withAppAuth?: boolean;
|
|
18
|
+
responseContentType?: "json" | "text" | "blob" | "form-data" | "array-buffer" | "clone";
|
|
19
|
+
}
|
|
20
|
+
export interface MetaResponse {
|
|
21
|
+
currentPage: number;
|
|
22
|
+
perPage: number;
|
|
23
|
+
totalCurrentPage: number;
|
|
24
|
+
totalPage: number;
|
|
25
|
+
totalData: number;
|
|
26
|
+
rangeStart: number;
|
|
27
|
+
rangeEnd: number;
|
|
28
|
+
}
|
|
29
|
+
export interface ApiResponse<ResponseData = unknown> {
|
|
30
|
+
status?: string;
|
|
31
|
+
message?: string;
|
|
32
|
+
data?: ResponseData;
|
|
33
|
+
meta?: MetaResponse;
|
|
34
|
+
error?: {
|
|
35
|
+
code: string;
|
|
36
|
+
message: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export type Option = RequestApiOptions | (() => RequestApiOptions | Promise<RequestApiOptions>);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Method, Option, ParamType, RequestApiOptions } from './types';
|
|
2
|
+
|
|
3
|
+
export declare const getContentType: (t?: string) => "application/x-www-form-urlencoded" | "application/json" | undefined;
|
|
4
|
+
export declare const getBody: (m: Method, t: RequestApiOptions["contentType"], b: unknown) => unknown;
|
|
5
|
+
export declare function searchParamsToObject(searchParams: string): Record<string, string>;
|
|
6
|
+
export declare function objectToSearchParams(obj?: ParamType): string;
|
|
7
|
+
export declare function createEndpoint(endpoint: string, path?: string, params?: ParamType): string;
|
|
8
|
+
export declare function resolveRequestOptions(options?: Option): Promise<RequestApiOptions | undefined>;
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@starasia/http",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Client Http for starasia App",
|
|
5
|
+
"author": "Prawito Hudoro",
|
|
6
|
+
"main": "dist/index.umd.js",
|
|
7
|
+
"module": "dist/index.es.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=18.0.0"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "vite --config vite.config.ts --port 3000",
|
|
20
|
+
"build": "vite build --config vite.config.ts"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^20.14.8",
|
|
24
|
+
"@types/react": "^18.3.3",
|
|
25
|
+
"@types/react-dom": "^18.3.0",
|
|
26
|
+
"path": "^0.12.7",
|
|
27
|
+
"react": "^18.3.1",
|
|
28
|
+
"react-dom": "^18.3.1",
|
|
29
|
+
"typescript": "^5.5.2",
|
|
30
|
+
"url": "^0.11.3",
|
|
31
|
+
"vite": "^5.3.1",
|
|
32
|
+
"vite-plugin-dts": "^3.9.1"
|
|
33
|
+
}
|
|
34
|
+
}
|