@vesperjs/nuxt 0.4.0 → 0.5.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/app/composables/backend/api/create-fetch.ts +4 -2
- package/app/composables/backend/api/create-request-fetch.ts +3 -2
- package/app/composables/backend/api/use-api-constants.ts +4 -4
- package/app/composables/backend/api/use-http-headers.ts +3 -1
- package/app/composables/backend/api/use-mutation-api.ts +28 -22
- package/app/composables/backend/api/use-ofetch.ts +5 -1
- package/app/composables/backend/api/use-query-api.ts +43 -13
- package/app/composables/use-datetime-local.ts +6 -1
- package/app/composables/use-locale.ts +6 -1
- package/app/composables/use-more-scroll.ts +10 -1
- package/app/composables/use-referer.ts +3 -1
- package/app/composables/use-time-zone.ts +7 -1
- package/package.json +3 -3
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { useRuntimeConfig } from 'nuxt/app'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
export const useApiConstants = function (): {
|
|
4
|
+
baseURL: ComputedRef<string>
|
|
5
|
+
} {
|
|
6
6
|
const runtimeConfig = useRuntimeConfig()
|
|
7
7
|
|
|
8
8
|
const baseOrigin = computed<string | undefined>(() => {
|
|
9
9
|
if (import.meta.client) {
|
|
10
10
|
return runtimeConfig.public.backendApiOrigin as string
|
|
11
11
|
} else if (import.meta.server) {
|
|
12
|
-
return runtimeConfig.backendApiOrigin as string
|
|
12
|
+
return (runtimeConfig.backendApiOrigin ?? runtimeConfig.public.backendApiOrigin) as string
|
|
13
13
|
}
|
|
14
14
|
})
|
|
15
15
|
|
|
@@ -3,7 +3,9 @@ import { computed, type Ref } from '@vue/reactivity'
|
|
|
3
3
|
import { useLocale } from '../../use-locale'
|
|
4
4
|
import { useTimeZone } from '../../use-time-zone'
|
|
5
5
|
|
|
6
|
-
export const useHttpHeaders = function () {
|
|
6
|
+
export const useHttpHeaders = function (): {
|
|
7
|
+
commonHeaders: Ref<Record<string, string>, Record<string, string>>
|
|
8
|
+
} {
|
|
7
9
|
const { locale } = useLocale()
|
|
8
10
|
const { timeZone } = useTimeZone()
|
|
9
11
|
|
|
@@ -20,21 +20,16 @@ interface MutationAPIOptions {
|
|
|
20
20
|
onResponseError?: ({ response }: { response: FetchResponse<any> }) => void
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @param url
|
|
26
|
+
* @param options
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
23
29
|
// eslint-disable-next-line
|
|
24
30
|
export const useMutationApi = async function <T = unknown, E = any>(
|
|
25
31
|
url: string,
|
|
26
|
-
|
|
27
|
-
method,
|
|
28
|
-
body = {},
|
|
29
|
-
token = null,
|
|
30
|
-
baseURL = null,
|
|
31
|
-
retry,
|
|
32
|
-
retryDelay,
|
|
33
|
-
retryStatusCodes,
|
|
34
|
-
timeout,
|
|
35
|
-
onRequestError,
|
|
36
|
-
onResponseError,
|
|
37
|
-
}: MutationAPIOptions,
|
|
32
|
+
options: MutationAPIOptions,
|
|
38
33
|
): Promise<{
|
|
39
34
|
token: string | null | undefined
|
|
40
35
|
data: T | undefined
|
|
@@ -44,34 +39,45 @@ export const useMutationApi = async function <T = unknown, E = any>(
|
|
|
44
39
|
const { commonHeaders } = useHttpHeaders()
|
|
45
40
|
const { baseURL: baseUrl } = useApiConstants()
|
|
46
41
|
|
|
42
|
+
const method = options.method
|
|
43
|
+
const body = options.body ?? {}
|
|
44
|
+
const token = options.token ?? null
|
|
45
|
+
const baseURL = options.baseURL ?? null
|
|
46
|
+
const retry = options.retry
|
|
47
|
+
const retryDelay = options.retryDelay
|
|
48
|
+
const retryStatusCodes = options.retryStatusCodes
|
|
49
|
+
const timeout = options.timeout
|
|
50
|
+
const onRequestError = options.onRequestError
|
|
51
|
+
const onResponseError = options.onResponseError
|
|
52
|
+
|
|
47
53
|
const headers: Record<string, string> = commonHeaders.value
|
|
48
54
|
|
|
49
55
|
const tokenRef = ref<string | null>()
|
|
50
56
|
|
|
51
57
|
if (token) headers.Authorization = `Bearer ${token}`
|
|
52
58
|
|
|
53
|
-
const
|
|
59
|
+
const mutOptions: FetchOptions<'json'> = {
|
|
54
60
|
baseURL: baseURL ?? baseUrl.value,
|
|
55
61
|
headers,
|
|
56
62
|
method,
|
|
57
63
|
}
|
|
58
64
|
|
|
59
|
-
if (retry)
|
|
60
|
-
if (retryDelay)
|
|
61
|
-
if (retryStatusCodes)
|
|
65
|
+
if (retry) mutOptions.retry = retry
|
|
66
|
+
if (retryDelay) mutOptions.retryDelay = retryDelay
|
|
67
|
+
if (retryStatusCodes) mutOptions.retryStatusCodes = retryStatusCodes
|
|
62
68
|
|
|
63
|
-
if (timeout)
|
|
69
|
+
if (timeout) mutOptions.timeout = timeout
|
|
64
70
|
|
|
65
|
-
if (onRequestError)
|
|
66
|
-
if (onResponseError)
|
|
71
|
+
if (onRequestError) mutOptions.onRequestError = onRequestError
|
|
72
|
+
if (onResponseError) mutOptions.onResponseError = onResponseError
|
|
67
73
|
|
|
68
|
-
if (method == 'post' || method == 'put')
|
|
74
|
+
if (method == 'post' || method == 'put') mutOptions.body = body
|
|
69
75
|
|
|
70
|
-
|
|
76
|
+
mutOptions.onResponse = ({ response }: { response: FetchResponse<T> }) => {
|
|
71
77
|
tokenRef.value = response.headers.get('Authorization')?.split(' ')[1] ?? token
|
|
72
78
|
}
|
|
73
79
|
|
|
74
|
-
const { data, error, pending } = await useOFetch<T, E>(url,
|
|
80
|
+
const { data, error, pending } = await useOFetch<T, E>(url, mutOptions)
|
|
75
81
|
|
|
76
82
|
return { token: tokenRef.value, data, error, pending }
|
|
77
83
|
}
|
|
@@ -4,7 +4,11 @@ import type { $Fetch, FetchOptions, FetchError } from 'ofetch'
|
|
|
4
4
|
export const useOFetch = async function <T = unknown, E = any>(
|
|
5
5
|
url: string,
|
|
6
6
|
options?: FetchOptions<'json'>,
|
|
7
|
-
) {
|
|
7
|
+
): Promise<{
|
|
8
|
+
data: T | undefined
|
|
9
|
+
error: FetchError<E> | undefined
|
|
10
|
+
pending: boolean
|
|
11
|
+
}> {
|
|
8
12
|
const { $api } = useNuxtApp()
|
|
9
13
|
|
|
10
14
|
const pending = ref(true)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useAsyncData, useNuxtApp } from 'nuxt/app'
|
|
1
|
+
import { useAsyncData, useNuxtApp, type NuxtError } from 'nuxt/app'
|
|
2
2
|
|
|
3
|
-
import type { FetchContext, FetchOptions, FetchResponse } from 'ofetch'
|
|
3
|
+
import type { FetchContext, FetchError, FetchOptions, FetchResponse } from 'ofetch'
|
|
4
4
|
|
|
5
5
|
import { ref } from '@vue/reactivity'
|
|
6
6
|
|
|
@@ -8,6 +8,21 @@ import { useHttpHeaders } from './use-http-headers'
|
|
|
8
8
|
import { useApiConstants } from './use-api-constants'
|
|
9
9
|
import { useOFetch } from './use-ofetch'
|
|
10
10
|
|
|
11
|
+
type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>
|
|
12
|
+
|
|
13
|
+
type PickFrom<T, K extends Array<string>> =
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
T extends Array<any>
|
|
16
|
+
? T
|
|
17
|
+
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
T extends Record<string, any>
|
|
19
|
+
? keyof T extends K[number]
|
|
20
|
+
? T
|
|
21
|
+
: K[number] extends never
|
|
22
|
+
? T
|
|
23
|
+
: Pick<T, K[number]>
|
|
24
|
+
: T
|
|
25
|
+
|
|
11
26
|
interface SearchParams {
|
|
12
27
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
28
|
[key: string]: any
|
|
@@ -35,7 +50,22 @@ export type QueryAPIOptions = {
|
|
|
35
50
|
export const useQueryApi = async function <T = unknown, E = any>(
|
|
36
51
|
url: string,
|
|
37
52
|
options?: QueryAPIOptions,
|
|
38
|
-
)
|
|
53
|
+
): Promise<
|
|
54
|
+
| {
|
|
55
|
+
token: string | null | undefined
|
|
56
|
+
data: PickFrom<T, KeysOf<T>> | undefined
|
|
57
|
+
error: (E extends Error | NuxtError<unknown> ? E : NuxtError<E>) | undefined
|
|
58
|
+
refresh: (opts?: never) => Promise<void>
|
|
59
|
+
pending: boolean
|
|
60
|
+
}
|
|
61
|
+
| {
|
|
62
|
+
token: string | null | undefined
|
|
63
|
+
data: T | undefined
|
|
64
|
+
error: FetchError<E> | undefined
|
|
65
|
+
pending: boolean
|
|
66
|
+
refresh?: undefined
|
|
67
|
+
}
|
|
68
|
+
> {
|
|
39
69
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
40
70
|
const { $api } = useNuxtApp() as any
|
|
41
71
|
const { commonHeaders } = useHttpHeaders()
|
|
@@ -53,7 +83,7 @@ export const useQueryApi = async function <T = unknown, E = any>(
|
|
|
53
83
|
if (options?.token) headers.Authorization = `Bearer ${options.token}`
|
|
54
84
|
|
|
55
85
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
-
const
|
|
86
|
+
const queryOptions: FetchOptions<'json', any> = {
|
|
57
87
|
baseURL: options?.baseURL ?? baseUrl.value,
|
|
58
88
|
method: 'get',
|
|
59
89
|
query: options?.query ?? {},
|
|
@@ -63,20 +93,20 @@ export const useQueryApi = async function <T = unknown, E = any>(
|
|
|
63
93
|
},
|
|
64
94
|
}
|
|
65
95
|
|
|
66
|
-
if (options?.retry)
|
|
67
|
-
if (options?.retryDelay)
|
|
68
|
-
if (options?.retryStatusCodes)
|
|
96
|
+
if (options?.retry) queryOptions.retry = options.retry
|
|
97
|
+
if (options?.retryDelay) queryOptions.retryDelay = options.retryDelay
|
|
98
|
+
if (options?.retryStatusCodes) queryOptions.retryStatusCodes = options.retryStatusCodes
|
|
69
99
|
|
|
70
|
-
if (options?.timeout)
|
|
100
|
+
if (options?.timeout) queryOptions.timeout = options.timeout
|
|
71
101
|
|
|
72
|
-
if (options?.signal)
|
|
102
|
+
if (options?.signal) queryOptions.signal = options.signal
|
|
73
103
|
|
|
74
|
-
if (options?.onRequestError)
|
|
75
|
-
if (options?.onResponseError)
|
|
104
|
+
if (options?.onRequestError) queryOptions.onRequestError = options.onRequestError
|
|
105
|
+
if (options?.onResponseError) queryOptions.onResponseError = options.onResponseError
|
|
76
106
|
|
|
77
107
|
if (cache) {
|
|
78
108
|
const { data, error, refresh, pending } = await useAsyncData<T, E>(key, () =>
|
|
79
|
-
$api(url,
|
|
109
|
+
$api(url, queryOptions),
|
|
80
110
|
)
|
|
81
111
|
|
|
82
112
|
if (fresh) await refresh()
|
|
@@ -89,7 +119,7 @@ export const useQueryApi = async function <T = unknown, E = any>(
|
|
|
89
119
|
pending: pending.value,
|
|
90
120
|
}
|
|
91
121
|
} else {
|
|
92
|
-
const { data, error, pending } = await useOFetch<T, E>(url,
|
|
122
|
+
const { data, error, pending } = await useOFetch<T, E>(url, queryOptions)
|
|
93
123
|
|
|
94
124
|
return { token: tokenRef.value, data, error, pending }
|
|
95
125
|
}
|
|
@@ -2,7 +2,12 @@ import { parse, format } from '@formkit/tempo'
|
|
|
2
2
|
|
|
3
3
|
import { useLocale } from './use-locale'
|
|
4
4
|
|
|
5
|
-
export const useDatetimeLocal = function (fmtDT = 'YYYY/MM/DD HH:mm') {
|
|
5
|
+
export const useDatetimeLocal = function (fmtDT = 'YYYY/MM/DD HH:mm'): {
|
|
6
|
+
upDTL: (datetime: string | null) => string | null
|
|
7
|
+
downDTL: (datetime: string | null) => string
|
|
8
|
+
toISO8601: (datetime: string) => string
|
|
9
|
+
formatHTML: (datetime: string | null, fmt: string) => string
|
|
10
|
+
} {
|
|
6
11
|
const { locale } = useLocale()
|
|
7
12
|
|
|
8
13
|
const fmtISO8601 = 'YYYY-MM-DDTHH:mm'
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { useNuxtApp } from 'nuxt/app'
|
|
2
2
|
import { useBrowserLocale } from '#i18n'
|
|
3
3
|
|
|
4
|
-
export const useLocale = function () {
|
|
4
|
+
export const useLocale = function (): {
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
+
locale: any
|
|
7
|
+
autodetect: () => void
|
|
8
|
+
} {
|
|
5
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
10
|
const { $i18n } = useNuxtApp() as any
|
|
7
11
|
const { locale, availableLocales, fallbackLocale } = $i18n
|
|
12
|
+
|
|
8
13
|
const autodetect = (): void => {
|
|
9
14
|
const browserLocale: string | null = useBrowserLocale()?.split('-')[0] ?? null
|
|
10
15
|
// console.log(browserLocale)
|
|
@@ -8,7 +8,16 @@ export const useMoreScroll = function ({
|
|
|
8
8
|
key?: string | null
|
|
9
9
|
page: number
|
|
10
10
|
pages: number
|
|
11
|
-
}) {
|
|
11
|
+
}): {
|
|
12
|
+
currentPage: Ref<number>
|
|
13
|
+
pagePrev: Ref<boolean>
|
|
14
|
+
pageNext: Ref<boolean>
|
|
15
|
+
minPage: Ref<number>
|
|
16
|
+
maxPage: Ref<number>
|
|
17
|
+
init: () => void
|
|
18
|
+
decrement: () => void
|
|
19
|
+
increment: () => void
|
|
20
|
+
} {
|
|
12
21
|
const toFirstUpper = (str: string): string => {
|
|
13
22
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
14
23
|
}
|
|
@@ -17,7 +17,13 @@ interface TZOptions {
|
|
|
17
17
|
value: string
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export const useTimeZone = function (fmtDT = 'YYYY/MM/DD HH:mm') {
|
|
20
|
+
export const useTimeZone = function (fmtDT = 'YYYY/MM/DD HH:mm'): {
|
|
21
|
+
timeZone: ComputedRef<TimeZone>
|
|
22
|
+
tzOptions: ComputedRef<TZOptions[]>
|
|
23
|
+
upTZ: (datetime: string | null) => string
|
|
24
|
+
downTZ: (datetime: string | null) => string
|
|
25
|
+
formatHtmlTZ: (datetime: string | null, fmt: string) => string
|
|
26
|
+
} {
|
|
21
27
|
const runtimeConfig = useRuntimeConfig()
|
|
22
28
|
const { locale } = useLocale()
|
|
23
29
|
const { toISO8601, formatHTML } = useDatetimeLocal(fmtDT)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vesperjs/nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"nuxt",
|
|
6
6
|
"vue.js"
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"@nuxt/eslint": "1.15.2",
|
|
25
25
|
"@nuxtjs/i18n": "^10.3.0",
|
|
26
26
|
"@types/node": "^24.12.2",
|
|
27
|
-
"@vesperjs/shared": "0.
|
|
28
|
-
"eslint": "^10.
|
|
27
|
+
"@vesperjs/shared": "0.5.0",
|
|
28
|
+
"eslint": "^10.3.0",
|
|
29
29
|
"eslint-typegen": "2.3.1",
|
|
30
30
|
"nuxt": "^4.3.1",
|
|
31
31
|
"ofetch": "^1.5.1",
|