@stacksjs/api 0.59.11 → 0.61.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.js CHANGED
@@ -160,7 +160,8 @@ var withoutTrailingSlash = function(input = "", respectQueryAndFragment) {
160
160
  fragment = input.slice(fragmentIndex);
161
161
  }
162
162
  const [s0, ...s] = path.split("?");
163
- return (s0.slice(0, -1) || "/") + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
163
+ const cleanPath = s0.endsWith("/") ? s0.slice(0, -1) : s0;
164
+ return (cleanPath || "/") + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
164
165
  };
165
166
  var withTrailingSlash = function(input = "", respectQueryAndFragment) {
166
167
  if (!respectQueryAndFragment) {
@@ -280,7 +281,7 @@ var TRAILING_SLASH_RE = /\/$|\/\?|\/#/;
280
281
  var JOIN_LEADING_SLASH_RE = /^\.?\//;
281
282
  var protocolRelative = Symbol.for("ufo:protocolRelative");
282
283
 
283
- // ../../../../node_modules/ofetch/dist/shared/ofetch.00501375.mjs
284
+ // ../../../../node_modules/ofetch/dist/shared/ofetch.37386b05.mjs
284
285
  var createFetchError = function(ctx) {
285
286
  const errorMessage = ctx.error?.message || ctx.error?.toString() || "";
286
287
  const method = ctx.request?.method || ctx.options?.method || "GET";
@@ -394,8 +395,7 @@ var createFetch = function(globalOptions = {}) {
394
395
  }
395
396
  return $fetchRaw(context.request, {
396
397
  ...context.options,
397
- retry: retries - 1,
398
- timeout: context.options.timeout
398
+ retry: retries - 1
399
399
  });
400
400
  }
401
401
  }
@@ -443,9 +443,10 @@ var createFetch = function(globalOptions = {}) {
443
443
  }
444
444
  }
445
445
  }
446
+ let abortTimeout;
446
447
  if (!context.options.signal && context.options.timeout) {
447
448
  const controller = new AbortController;
448
- setTimeout(() => controller.abort(), context.options.timeout);
449
+ abortTimeout = setTimeout(() => controller.abort(), context.options.timeout);
449
450
  context.options.signal = controller.signal;
450
451
  }
451
452
  try {
@@ -456,6 +457,10 @@ var createFetch = function(globalOptions = {}) {
456
457
  await context.options.onRequestError(context);
457
458
  }
458
459
  return await onError(context);
460
+ } finally {
461
+ if (abortTimeout) {
462
+ clearTimeout(abortTimeout);
463
+ }
459
464
  }
460
465
  const hasBody = context.response.body && !nullBodyResponses.has(context.response.status) && context.options.method !== "HEAD";
461
466
  if (hasBody) {
@@ -554,86 +559,91 @@ var AbortController = _globalThis.AbortController;
554
559
  var ofetch = createFetch({ fetch, Headers, AbortController });
555
560
 
556
561
  // src/index.ts
557
- async function useHttpFetch(endpoint = "") {
558
- let baseURL = "/";
559
- if (endpoint)
560
- baseURL = endpoint;
561
- async function post(url, params) {
562
- const headers = { Accept: "application/json" };
563
- if (token.value)
564
- headers.Authorization = `Bearer ${token.value}`;
565
- const parameters = {
566
- ...params,
567
- ...{
568
- headers,
569
- parseResponse: JSON.parse,
570
- method: "POST",
571
- baseURL
572
- }
573
- };
574
- loading.value = true;
575
- try {
576
- const result = await ofetch(url, parameters);
577
- loading.value = false;
578
- return result;
579
- } catch (err) {
580
- loading.value = false;
581
- throw err;
582
- }
583
- }
584
- async function get(url, params) {
585
- const headers = { Accept: "application/json" };
586
- if (token.value)
587
- headers.Authorization = `Bearer ${token.value}`;
588
- const parameters = {
589
- ...params,
590
- ...{
591
- headers,
592
- parseResponse: JSON.parse,
593
- method: "GET",
594
- baseURL
595
- }
596
- };
597
- return await ofetch(url, parameters);
598
- }
599
- async function patch(url, params) {
600
- const headers = { Accept: "application/json" };
601
- if (token.value)
602
- headers.Authorization = `Bearer ${token.value}`;
603
- const parameters = {
604
- ...params,
605
- ...{
606
- headers,
607
- parseResponse: JSON.parse,
608
- method: "PATCH",
609
- baseURL
610
- }
611
- };
612
- loading.value = true;
613
- return await ofetch(url, parameters);
614
- }
615
- async function destroy(url, params) {
616
- const headers = { Accept: "application/json" };
617
- if (token.value)
618
- headers.Authorization = `Bearer ${token.value}`;
619
- const parameters = {
620
- ...params,
621
- ...{
622
- headers,
623
- method: "DELETE",
624
- baseURL
625
- }
626
- };
627
- loading.value = true;
628
- return await ofetch(url, parameters);
629
- }
630
- function setToken(authToken) {
631
- token.value = authToken;
562
+ async function post(url, params) {
563
+ const headers = { Accept: "application/json" };
564
+ if (token.value)
565
+ headers.Authorization = `Bearer ${token.value}`;
566
+ const parameters = {
567
+ ...params,
568
+ ...{
569
+ headers,
570
+ parseResponse: JSON.parse,
571
+ method: "POST",
572
+ baseURL
573
+ }
574
+ };
575
+ loading.value = true;
576
+ try {
577
+ const result = await ofetch(url, parameters);
578
+ loading.value = false;
579
+ return result;
580
+ } catch (err) {
581
+ loading.value = false;
582
+ throw err;
632
583
  }
633
- return { post, get, patch, destroy, baseURL, loading, token, setToken };
634
584
  }
585
+ async function get(url, params) {
586
+ const headers = { Accept: "application/json" };
587
+ if (token.value)
588
+ headers.Authorization = `Bearer ${token.value}`;
589
+ const parameters = {
590
+ ...params,
591
+ ...{
592
+ headers,
593
+ parseResponse: JSON.parse,
594
+ method: "GET",
595
+ baseURL
596
+ }
597
+ };
598
+ return await ofetch(url, parameters);
599
+ }
600
+ async function patch(url, params) {
601
+ const headers = { Accept: "application/json" };
602
+ if (token.value)
603
+ headers.Authorization = `Bearer ${token.value}`;
604
+ const parameters = {
605
+ ...params,
606
+ ...{
607
+ headers,
608
+ parseResponse: JSON.parse,
609
+ method: "PATCH",
610
+ baseURL
611
+ }
612
+ };
613
+ loading.value = true;
614
+ return await ofetch(url, parameters);
615
+ }
616
+ async function destroy(url, params) {
617
+ const headers = { Accept: "application/json" };
618
+ if (token.value)
619
+ headers.Authorization = `Bearer ${token.value}`;
620
+ const parameters = {
621
+ ...params,
622
+ ...{
623
+ headers,
624
+ method: "DELETE",
625
+ baseURL
626
+ }
627
+ };
628
+ loading.value = true;
629
+ return await ofetch(url, parameters);
630
+ }
631
+ var setToken = function(authToken) {
632
+ token.value = authToken;
633
+ };
635
634
  var loading = ref(false);
636
635
  var token = ref("");
636
+ var baseURL = "/";
637
+ var Fetch = {
638
+ post,
639
+ get,
640
+ patch,
641
+ destroy,
642
+ baseURL,
643
+ loading,
644
+ token,
645
+ setToken
646
+ };
637
647
  export {
638
- useHttpFetch
648
+ Fetch
639
649
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/api",
3
3
  "type": "module",
4
- "version": "0.59.11",
4
+ "version": "0.61.0",
5
5
  "description": "The Stacks array utilities.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
@@ -53,7 +53,7 @@
53
53
  },
54
54
  "dependencies": {
55
55
  "@stacksjs/utils": "latest",
56
- "ofetch": "^1.3.3"
56
+ "ofetch": "^1.3.4"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@stacksjs/development": "latest"
package/src/index.ts CHANGED
@@ -1,115 +1,149 @@
1
1
  import { ofetch } from 'ofetch'
2
- import type { FetchOptions } from 'ofetch'
3
2
 
4
3
  interface Params {
5
4
  [key: string]: any // Replace 'any' with more specific types if possible
6
5
  }
7
6
 
7
+ interface FetchRequestHeaders {
8
+ 'Content-Type'?: string
9
+ Authorization?: string
10
+ Accept?: string
11
+ 'User-Agent'?: string
12
+ Referer?: string
13
+ Origin?: string
14
+ 'Cache-Control'?: string
15
+ Pragma?: string
16
+ Cookie?: string
17
+ 'If-Modified-Since'?: string
18
+ 'If-None-Match'?: string
19
+ 'X-Requested-With'?: string
20
+ 'Accept-Encoding'?: string
21
+ 'Accept-Language'?: string
22
+ [key: string]: string | undefined // To allow additional headers
23
+ }
24
+
25
+ interface FetchRequestParams {
26
+ headers?: FetchRequestHeaders
27
+ method?: string
28
+ baseURL?: string
29
+ body?: any // Optional, for request bodies like in POST or PUT requests
30
+ mode?: RequestMode // Optional, e.g., 'cors', 'no-cors', 'same-origin'
31
+ credentials?: RequestCredentials // Optional, e.g., 'include', 'same-origin', 'omit'
32
+ cache?: RequestCache // Optional, e.g., 'default', 'no-store', 'reload'
33
+ redirect?: RequestRedirect // Optional, e.g., 'follow', 'manual', 'error'
34
+ referrerPolicy?: ReferrerPolicy // Optional, e.g., 'no-referrer', 'origin'
35
+ integrity?: string // Optional, for subresource integrity
36
+ keepalive?: boolean // Optional, for whether the request should outlive the page
37
+ signal?: AbortSignal // Optional, to abort the request
38
+ [key: string]: any // To allow additional parameters
39
+ }
40
+
8
41
  type FetchResponse = string | Blob | ArrayBuffer | ReadableStream<Uint8Array>
9
42
 
10
43
  const loading = ref(false)
11
44
  const token = ref('')
45
+ const baseURL = '/'
12
46
 
13
- export async function useHttpFetch(endpoint = '') {
14
- let baseURL = '/'
15
-
16
- if (endpoint)
17
- baseURL = endpoint
47
+ async function post(url: string, params?: Params): Promise<any> {
48
+ const headers: FetchRequestHeaders = { Accept: 'application/json' }
18
49
 
19
- async function post(url: string, params?: Params): Promise<any> {
20
- const headers: any = { Accept: 'application/json' }
50
+ if (token.value) headers.Authorization = `Bearer ${token.value}`
21
51
 
22
- if (token.value)
23
- headers.Authorization = `Bearer ${token.value}`
24
-
25
- const parameters: FetchOptions = {
26
- ...params,
27
- ...{
28
- headers,
29
- parseResponse: JSON.parse,
30
- method: 'POST',
31
- baseURL,
32
- },
33
- }
52
+ const parameters: FetchRequestParams = {
53
+ ...params,
54
+ ...{
55
+ headers,
56
+ parseResponse: JSON.parse,
57
+ method: 'POST',
58
+ baseURL,
59
+ },
60
+ }
34
61
 
35
- loading.value = true
62
+ loading.value = true
36
63
 
37
- try {
38
- const result: string | FetchResponse | Blob | ArrayBuffer | ReadableStream<Uint8Array> = await ofetch(url, parameters)
64
+ try {
65
+ const result: string | FetchResponse | Blob | ArrayBuffer | ReadableStream<Uint8Array> = await ofetch(
66
+ url,
67
+ parameters,
68
+ )
39
69
 
40
- loading.value = false
41
- return result
42
- }
43
- catch (err: any) {
44
- loading.value = false
70
+ loading.value = false
71
+ return result
72
+ } catch (err: any) {
73
+ loading.value = false
45
74
 
46
- throw err
47
- }
75
+ throw err
48
76
  }
77
+ }
49
78
 
50
- async function get(url: string, params?: Params): Promise<any> {
51
- const headers: any = { Accept: 'application/json' }
52
-
53
- if (token.value)
54
- headers.Authorization = `Bearer ${token.value}`
79
+ async function get(url: string, params?: Params): Promise<any> {
80
+ const headers: FetchRequestHeaders = { Accept: 'application/json' }
55
81
 
56
- const parameters: FetchOptions = {
57
- ...params,
58
- ...{
59
- headers,
60
- parseResponse: JSON.parse,
61
- method: 'GET',
62
- baseURL,
63
- },
64
- }
82
+ if (token.value) headers.Authorization = `Bearer ${token.value}`
65
83
 
66
- return await ofetch(url, parameters) as FetchResponse
84
+ const parameters: FetchRequestParams = {
85
+ ...params,
86
+ ...{
87
+ headers,
88
+ parseResponse: JSON.parse,
89
+ method: 'GET',
90
+ baseURL,
91
+ },
67
92
  }
68
93
 
69
- async function patch(url: string, params?: Params): Promise<any> {
70
- const headers: any = { Accept: 'application/json' }
71
-
72
- if (token.value)
73
- headers.Authorization = `Bearer ${token.value}`
94
+ return (await ofetch(url, parameters)) as FetchResponse
95
+ }
74
96
 
75
- const parameters: FetchOptions = {
76
- ...params,
77
- ...{
78
- headers,
79
- parseResponse: JSON.parse,
80
- method: 'PATCH',
81
- baseURL,
82
- },
83
- }
97
+ async function patch(url: string, params?: Params): Promise<any> {
98
+ const headers: FetchRequestHeaders = { Accept: 'application/json' }
84
99
 
85
- loading.value = true
100
+ if (token.value) headers.Authorization = `Bearer ${token.value}`
86
101
 
87
- return await ofetch(url, parameters) as FetchResponse
102
+ const parameters: FetchRequestParams = {
103
+ ...params,
104
+ ...{
105
+ headers,
106
+ parseResponse: JSON.parse,
107
+ method: 'PATCH',
108
+ baseURL,
109
+ },
88
110
  }
89
111
 
90
- async function destroy(url: string, params?: Params): Promise<any> {
91
- const headers: any = { Accept: 'application/json' }
112
+ loading.value = true
92
113
 
93
- if (token.value)
94
- headers.Authorization = `Bearer ${token.value}`
114
+ return (await ofetch(url, parameters)) as FetchResponse
115
+ }
95
116
 
96
- const parameters: FetchOptions = {
97
- ...params,
98
- ...{
99
- headers,
100
- method: 'DELETE',
101
- baseURL,
102
- },
103
- }
117
+ async function destroy(url: string, params?: Params): Promise<any> {
118
+ const headers: FetchRequestHeaders = { Accept: 'application/json' }
104
119
 
105
- loading.value = true
120
+ if (token.value) headers.Authorization = `Bearer ${token.value}`
106
121
 
107
- return await ofetch(url, parameters) as FetchResponse
122
+ const parameters: FetchRequestParams = {
123
+ ...params,
124
+ ...{
125
+ headers,
126
+ method: 'DELETE',
127
+ baseURL,
128
+ },
108
129
  }
109
130
 
110
- function setToken(authToken: string) {
111
- token.value = authToken
112
- }
131
+ loading.value = true
132
+
133
+ return (await ofetch(url, parameters)) as FetchResponse
134
+ }
135
+
136
+ function setToken(authToken: string) {
137
+ token.value = authToken
138
+ }
113
139
 
114
- return { post, get, patch, destroy, baseURL, loading, token, setToken }
140
+ export const Fetch = {
141
+ post,
142
+ get,
143
+ patch,
144
+ destroy,
145
+ baseURL,
146
+ loading,
147
+ token,
148
+ setToken,
115
149
  }
package/dist/index.d.ts DELETED
@@ -1,14 +0,0 @@
1
- interface Params {
2
- [key: string]: any;
3
- }
4
- export declare function useHttpFetch(endpoint?: string): Promise<{
5
- post: (url: string, params?: Params) => Promise<any>;
6
- get: (url: string, params?: Params) => Promise<any>;
7
- patch: (url: string, params?: Params) => Promise<any>;
8
- destroy: (url: string, params?: Params) => Promise<any>;
9
- baseURL: string;
10
- loading: any;
11
- token: any;
12
- setToken: (authToken: string) => void;
13
- }>;
14
- export {};