Skip to content

Commit 1b582e0

Browse files
chore: wip
1 parent 88afa26 commit 1b582e0

File tree

2 files changed

+74
-102
lines changed

2 files changed

+74
-102
lines changed
Lines changed: 67 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,85 @@
1-
import { ofetch } from 'ofetch'
2-
import { ref } from 'vue'
1+
import { type FetchOptions, ofetch } from 'ofetch'
32

43
interface Params {
54
[key: string]: any // Replace 'any' with more specific types if possible
65
}
76

8-
interface FetchRequestHeaders {
9-
'Content-Type'?: string
10-
Authorization?: string
11-
Accept?: string
12-
'User-Agent'?: string
13-
Referer?: string
14-
Origin?: string
15-
'Cache-Control'?: string
16-
Pragma?: string
17-
Cookie?: string
18-
'If-Modified-Since'?: string
19-
'If-None-Match'?: string
20-
'X-Requested-With'?: string
21-
'Accept-Encoding'?: string
22-
'Accept-Language'?: string
23-
[key: string]: string | undefined // To allow additional headers
24-
}
25-
26-
interface FetchRequestParams {
27-
headers?: FetchRequestHeaders
28-
method?: string
29-
baseURL?: string
30-
body?: any // Optional, for request bodies like in POST or PUT requests
31-
mode?: RequestMode // Optional, e.g., 'cors', 'no-cors', 'same-origin'
32-
credentials?: RequestCredentials // Optional, e.g., 'include', 'same-origin', 'omit'
33-
cache?: RequestCache // Optional, e.g., 'default', 'no-store', 'reload'
34-
redirect?: RequestRedirect // Optional, e.g., 'follow', 'manual', 'error'
35-
referrerPolicy?: ReferrerPolicy // Optional, e.g., 'no-referrer', 'origin'
36-
integrity?: string // Optional, for subresource integrity
37-
keepalive?: boolean // Optional, for whether the request should outlive the page
38-
signal?: AbortSignal // Optional, to abort the request
39-
[key: string]: any // To allow additional parameters
7+
interface ApiFetch {
8+
get(url: string, params?: Params): Promise<FetchResponse>
9+
post(url: string, params?: Params): Promise<FetchResponse>
10+
destroy(url: string, params?: Params): Promise<FetchResponse>
11+
patch(url: string, params?: Params): Promise<FetchResponse>
12+
put(url: string, params?: Params): Promise<FetchResponse>
13+
setToken(authToken: string): void
14+
baseURL: '/' | string
15+
loading: boolean
16+
token: string
4017
}
4118

4219
type FetchResponse = string | Blob | ArrayBuffer | ReadableStream<Uint8Array>
4320

44-
const loading = ref(false)
45-
const token = ref('')
21+
let loading = false
22+
let token = ''
4623
const baseURL = '/'
4724

48-
async function post(url: string, params?: Params): Promise<any> {
49-
const headers: FetchRequestHeaders = { Accept: 'application/json' }
25+
async function get(url: string, params?: Params, headers?: Headers): Promise<FetchResponse> {
26+
if (headers) if (token) headers.set('Authorization', `Bearer ${token}`)
5027

51-
if (token.value) headers.Authorization = `Bearer ${token.value}`
28+
return await ofetch(url, { method: 'GET', baseURL, params, headers })
29+
}
5230

53-
const parameters: FetchRequestParams = {
54-
...params,
55-
...{
56-
headers,
57-
parseResponse: JSON.parse,
58-
method: 'POST',
59-
baseURL,
60-
},
61-
}
31+
async function post(url: string, params?: Params, headers?: Headers): Promise<any> {
32+
if (headers) if (token) headers.set('Authorization', `Bearer ${token}`)
6233

63-
loading.value = true
34+
loading = true
6435

6536
try {
66-
const result: string | FetchResponse | Blob | ArrayBuffer | ReadableStream<Uint8Array> = await ofetch(
67-
url,
68-
parameters,
69-
)
37+
const result: string | FetchResponse | Blob | ArrayBuffer | ReadableStream<Uint8Array> = await ofetch(url, {
38+
method: 'POST',
39+
baseURL,
40+
params,
41+
headers,
42+
})
7043

71-
loading.value = false
44+
loading = false
7245
return result
7346
} catch (err: any) {
74-
loading.value = false
47+
loading = false
7548

7649
throw err
7750
}
7851
}
7952

80-
async function get(url: string, params?: Params): Promise<any> {
81-
const headers: FetchRequestHeaders = { Accept: 'application/json' }
53+
async function patch(url: string, params?: Params, headers?: Headers): Promise<FetchResponse> {
54+
if (headers) if (token) headers.set('Authorization', `Bearer ${token}`)
8255

83-
if (token.value) headers.Authorization = `Bearer ${token.value}`
56+
loading = true
8457

85-
const parameters: FetchRequestParams = {
86-
...params,
87-
...{
88-
headers,
89-
parseResponse: JSON.parse,
90-
method: 'GET',
91-
baseURL,
92-
},
93-
}
94-
95-
return (await ofetch(url, parameters)) as FetchResponse
58+
return await ofetch(url, {
59+
method: 'PATCH',
60+
baseURL,
61+
params,
62+
headers,
63+
})
9664
}
9765

98-
async function patch(url: string, params?: Params): Promise<any> {
99-
const headers: FetchRequestHeaders = { Accept: 'application/json' }
100-
101-
if (token.value) headers.Authorization = `Bearer ${token.value}`
102-
103-
const parameters: FetchRequestParams = {
104-
...params,
105-
...{
106-
headers,
107-
parseResponse: JSON.parse,
108-
method: 'PATCH',
109-
baseURL,
110-
},
111-
}
66+
async function put(url: string, params?: Params, headers?: Headers): Promise<FetchResponse> {
67+
if (headers) if (token) headers.set('Authorization', `Bearer ${token}`)
11268

113-
loading.value = true
69+
loading = true
11470

115-
return (await ofetch(url, parameters)) as FetchResponse
71+
return await ofetch(url, {
72+
method: 'PUT',
73+
baseURL,
74+
params,
75+
headers,
76+
})
11677
}
11778

118-
async function destroy(url: string, params?: Params): Promise<any> {
119-
const headers: FetchRequestHeaders = { Accept: 'application/json' }
120-
121-
if (token.value) headers.Authorization = `Bearer ${token.value}`
79+
async function destroy(url: string, params?: Params, headers?: Headers): Promise<FetchResponse> {
80+
if (headers) if (token) headers.set('Authorization', `Bearer ${token}`)
12281

123-
const parameters: FetchRequestParams = {
82+
const parameters = {
12483
...params,
12584
...{
12685
headers,
@@ -129,22 +88,28 @@ async function destroy(url: string, params?: Params): Promise<any> {
12988
},
13089
}
13190

132-
loading.value = true
91+
loading = true
13392

134-
return (await ofetch(url, parameters)) as FetchResponse
93+
return await ofetch(url, {
94+
method: 'DELETE',
95+
baseURL,
96+
params,
97+
headers,
98+
})
13599
}
136100

137101
function setToken(authToken: string): void {
138-
token.value = authToken
102+
token = authToken
139103
}
140104

141-
export const Fetch = {
142-
post,
143-
get,
144-
patch,
145-
destroy,
146-
baseURL,
147-
loading,
148-
token,
149-
setToken,
105+
export const Fetch: ApiFetch = {
106+
get: get,
107+
post: post,
108+
patch: patch,
109+
put: put,
110+
destroy: destroy,
111+
baseURL: baseURL,
112+
token: token,
113+
setToken: setToken,
114+
loading: loading,
150115
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { dynamoDb } from 'dynamodb-tooling'
2+
3+
async function launch(): Promise<void> {
4+
await dynamoDb.launch()
5+
}
6+
7+
await launch()

0 commit comments

Comments
 (0)