tuijs-util 2.0.3 → 2.0.5

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/src/lib/http.ts CHANGED
@@ -1,90 +1,90 @@
1
- import { checkUrl } from './check.js';
2
-
3
- /**
4
- * Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
5
- */
6
- export function urlAddHttp(url: string): string | undefined {
7
- if (url === null || !checkUrl(url)) {
8
- throw new Error(`Invalid input.`);
9
- };
10
- if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
11
- url = "http://" + url
12
- }
13
- return url;
14
- }
15
-
16
- /**
17
- * Adds 'https://' if valid URL and 'http://' or 'https://' is missing.
18
- */
19
- export function urlAddHttps(url: string): string | undefined {
20
- if (url === null || !checkUrl(url)) {
21
- throw new Error(`Invalid input.`);
22
- };
23
- if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
24
- url = "https://" + url
25
- }
26
- return url;
27
- }
28
-
29
- /**
30
- * Sends GET request to specified URL
31
- */
32
- export async function reqGet(url: string, signal: AbortSignal | null = null) {
33
- if (!checkUrl(url)) {
34
- throw new Error(`Invalid URL`);
35
- }
36
- const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
37
- return await fetch(url, options);
38
- }
39
-
40
- /**
41
- * Collects JSON data from a given url
42
- */
43
- export async function reqGetJson(url: string, signal: AbortSignal | null = null) {
44
- const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
45
- const res = await fetch(url, options);
46
- return await res.json();
47
- }
48
-
49
- /**
50
- * Collects text data from a given url
51
- */
52
- export async function reqGetText(url: string, signal: AbortSignal | null = null) {
53
- const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
54
- const res = await fetch(url, options);
55
- return await res.text();
56
- }
57
-
58
- /**
59
- * Sends POST request to specified URL, which contains JSON data in the body.
60
- */
61
- export async function reqPostJson(url: string, data: object, signal: AbortSignal | null = null) {
62
- const options = signal ? {
63
- method: 'POST',
64
- headers: { 'Content-Type': 'application/json' },
65
- body: JSON.stringify(data),
66
- signal
67
- } : {
68
- method: 'POST',
69
- headers: { 'Content-Type': 'application/json' },
70
- body: JSON.stringify(data)
71
- };
72
- const res = await fetch(url, options);
73
- return res;
74
- }
75
-
76
- /**
77
- * Sends POST request to specified URL, which contains FormData in the body.
78
- */
79
- export async function reqPostForm(url: string, data: FormData, signal: AbortSignal | null = null) {
80
- const options = signal ? {
81
- method: 'POST',
82
- body: data,
83
- signal
84
- } : {
85
- method: 'POST',
86
- body: data
87
- };
88
- const res = await fetch(url, options);
89
- return res;
90
- }
1
+ import { checkUrl } from './check.js';
2
+
3
+ /**
4
+ * Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
5
+ */
6
+ export function urlAddHttp(url: string): string | undefined {
7
+ if (url === null || !checkUrl(url)) {
8
+ throw new Error(`Invalid input.`);
9
+ };
10
+ if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
11
+ url = "http://" + url
12
+ }
13
+ return url;
14
+ }
15
+
16
+ /**
17
+ * Adds 'https://' if valid URL and 'http://' or 'https://' is missing.
18
+ */
19
+ export function urlAddHttps(url: string): string | undefined {
20
+ if (url === null || !checkUrl(url)) {
21
+ throw new Error(`Invalid input.`);
22
+ };
23
+ if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
24
+ url = "https://" + url
25
+ }
26
+ return url;
27
+ }
28
+
29
+ /**
30
+ * Sends GET request to specified URL
31
+ */
32
+ export async function reqGet(url: string, signal: AbortSignal | null = null) {
33
+ if (!checkUrl(url)) {
34
+ throw new Error(`Invalid URL`);
35
+ }
36
+ const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
37
+ return await fetch(url, options);
38
+ }
39
+
40
+ /**
41
+ * Collects JSON data from a given url
42
+ */
43
+ export async function reqGetJson(url: string, signal: AbortSignal | null = null) {
44
+ const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
45
+ const res = await fetch(url, options);
46
+ return await res.json();
47
+ }
48
+
49
+ /**
50
+ * Collects text data from a given url
51
+ */
52
+ export async function reqGetText(url: string, signal: AbortSignal | null = null) {
53
+ const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
54
+ const res = await fetch(url, options);
55
+ return await res.text();
56
+ }
57
+
58
+ /**
59
+ * Sends POST request to specified URL, which contains JSON data in the body.
60
+ */
61
+ export async function reqPostJson(url: string, data: object, signal: AbortSignal | null = null) {
62
+ const options = signal ? {
63
+ method: 'POST',
64
+ headers: { 'Content-Type': 'application/json' },
65
+ body: JSON.stringify(data),
66
+ signal
67
+ } : {
68
+ method: 'POST',
69
+ headers: { 'Content-Type': 'application/json' },
70
+ body: JSON.stringify(data)
71
+ };
72
+ const res = await fetch(url, options);
73
+ return res;
74
+ }
75
+
76
+ /**
77
+ * Sends POST request to specified URL, which contains FormData in the body.
78
+ */
79
+ export async function reqPostForm(url: string, data: FormData, signal: AbortSignal | null = null) {
80
+ const options = signal ? {
81
+ method: 'POST',
82
+ body: data,
83
+ signal
84
+ } : {
85
+ method: 'POST',
86
+ body: data
87
+ };
88
+ const res = await fetch(url, options);
89
+ return res;
90
+ }