@vesperjs/nuxt 0.3.3 → 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.
@@ -1,3 +1,5 @@
1
- export const createFetch = function () {
2
- return $fetch.create({})
1
+ import type { $Fetch } from 'ofetch'
2
+
3
+ export const createFetch = function (): $Fetch {
4
+ return $fetch.create({}) as $Fetch
3
5
  }
@@ -1,5 +1,6 @@
1
1
  import { useRequestFetch } from 'nuxt/app'
2
+ import type { $Fetch } from 'ofetch'
2
3
 
3
- export const createRequestFetch = function () {
4
- return useRequestFetch()
4
+ export const createRequestFetch = function (): $Fetch {
5
+ return useRequestFetch() as $Fetch
5
6
  }
@@ -1,15 +1,15 @@
1
1
  import { useRuntimeConfig } from 'nuxt/app'
2
2
 
3
- import { computed } from '@vue/reactivity'
4
-
5
- export const useApiConstants = function () {
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
 
@@ -1,4 +1,4 @@
1
- import type { FetchOptions, FetchError, FetchResponse } from 'ofetch'
1
+ import type { FetchContext, FetchOptions, FetchError, FetchResponse } from 'ofetch'
2
2
 
3
3
  import { useHttpHeaders } from './use-http-headers'
4
4
  import { useApiConstants } from './use-api-constants'
@@ -10,22 +10,26 @@ interface MutationAPIOptions {
10
10
  body?: Record<string, any> | FormData
11
11
  token?: string | null
12
12
  baseURL?: string | null
13
+ retry?: number | false
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ retryDelay?: number | ((context: FetchContext<any, 'json'>) => number)
16
+ retryStatusCodes?: number[]
17
+ timeout?: number
13
18
  onRequestError?: ({ error }: { error: Error }) => void
14
19
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
20
  onResponseError?: ({ response }: { response: FetchResponse<any> }) => void
16
21
  }
17
22
 
23
+ /**
24
+ *
25
+ * @param url
26
+ * @param options
27
+ * @returns
28
+ */
18
29
  // eslint-disable-next-line
19
30
  export const useMutationApi = async function <T = unknown, E = any>(
20
31
  url: string,
21
- {
22
- method,
23
- body = {},
24
- token = null,
25
- baseURL = null,
26
- onRequestError,
27
- onResponseError,
28
- }: MutationAPIOptions,
32
+ options: MutationAPIOptions,
29
33
  ): Promise<{
30
34
  token: string | null | undefined
31
35
  data: T | undefined
@@ -35,37 +39,45 @@ export const useMutationApi = async function <T = unknown, E = any>(
35
39
  const { commonHeaders } = useHttpHeaders()
36
40
  const { baseURL: baseUrl } = useApiConstants()
37
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
+
38
53
  const headers: Record<string, string> = commonHeaders.value
39
54
 
40
55
  const tokenRef = ref<string | null>()
41
56
 
42
- if (token) {
43
- headers.Authorization = `Bearer ${token}`
44
- }
57
+ if (token) headers.Authorization = `Bearer ${token}`
45
58
 
46
- const options: FetchOptions<'json'> = {
59
+ const mutOptions: FetchOptions<'json'> = {
47
60
  baseURL: baseURL ?? baseUrl.value,
48
61
  headers,
49
62
  method,
50
63
  }
51
64
 
52
- if (onRequestError) {
53
- options.onRequestError = onRequestError
54
- }
65
+ if (retry) mutOptions.retry = retry
66
+ if (retryDelay) mutOptions.retryDelay = retryDelay
67
+ if (retryStatusCodes) mutOptions.retryStatusCodes = retryStatusCodes
55
68
 
56
- if (onResponseError) {
57
- options.onResponseError = onResponseError
58
- }
69
+ if (timeout) mutOptions.timeout = timeout
59
70
 
60
- if (method == 'post' || method == 'put') {
61
- options.body = body
62
- }
71
+ if (onRequestError) mutOptions.onRequestError = onRequestError
72
+ if (onResponseError) mutOptions.onResponseError = onResponseError
73
+
74
+ if (method == 'post' || method == 'put') mutOptions.body = body
63
75
 
64
- options.onResponse = ({ response }: { response: FetchResponse<T> }) => {
76
+ mutOptions.onResponse = ({ response }: { response: FetchResponse<T> }) => {
65
77
  tokenRef.value = response.headers.get('Authorization')?.split(' ')[1] ?? token
66
78
  }
67
79
 
68
- const { data, error, pending } = await useOFetch<T, E>(url, options)
80
+ const { data, error, pending } = await useOFetch<T, E>(url, mutOptions)
69
81
 
70
82
  return { token: tokenRef.value, data, error, pending }
71
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 { 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
@@ -19,6 +34,11 @@ export type QueryAPIOptions = {
19
34
  token?: string | null
20
35
  baseURL?: string | null
21
36
  signal?: AbortSignal
37
+ retry?: number | false
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ retryDelay?: number | ((context: FetchContext<any, 'json'>) => number)
40
+ retryStatusCodes?: number[]
41
+ timeout?: number
22
42
  onRequestError?: ({ error }: { error: Error }) => void
23
43
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
44
  onResponseError?: ({ response }: { response: FetchResponse<any> }) => void
@@ -30,7 +50,22 @@ export type QueryAPIOptions = {
30
50
  export const useQueryApi = async function <T = unknown, E = any>(
31
51
  url: string,
32
52
  options?: QueryAPIOptions,
33
- ) {
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
+ > {
34
69
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
70
  const { $api } = useNuxtApp() as any
36
71
  const { commonHeaders } = useHttpHeaders()
@@ -45,12 +80,10 @@ export const useQueryApi = async function <T = unknown, E = any>(
45
80
  const fresh: boolean = options?.fresh ?? false
46
81
  const cache: boolean = options?.cache ?? true
47
82
 
48
- if (options?.token) {
49
- headers.Authorization = `Bearer ${options.token}`
50
- }
83
+ if (options?.token) headers.Authorization = `Bearer ${options.token}`
51
84
 
52
85
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
- const getOptions: FetchOptions<'json', any> = {
86
+ const queryOptions: FetchOptions<'json', any> = {
54
87
  baseURL: options?.baseURL ?? baseUrl.value,
55
88
  method: 'get',
56
89
  query: options?.query ?? {},
@@ -60,26 +93,23 @@ export const useQueryApi = async function <T = unknown, E = any>(
60
93
  },
61
94
  }
62
95
 
63
- if (options?.signal) {
64
- getOptions.signal = options.signal
65
- }
96
+ if (options?.retry) queryOptions.retry = options.retry
97
+ if (options?.retryDelay) queryOptions.retryDelay = options.retryDelay
98
+ if (options?.retryStatusCodes) queryOptions.retryStatusCodes = options.retryStatusCodes
66
99
 
67
- if (options?.onRequestError) {
68
- getOptions.onRequestError = options.onRequestError
69
- }
100
+ if (options?.timeout) queryOptions.timeout = options.timeout
70
101
 
71
- if (options?.onResponseError) {
72
- getOptions.onResponseError = options.onResponseError
73
- }
102
+ if (options?.signal) queryOptions.signal = options.signal
103
+
104
+ if (options?.onRequestError) queryOptions.onRequestError = options.onRequestError
105
+ if (options?.onResponseError) queryOptions.onResponseError = options.onResponseError
74
106
 
75
107
  if (cache) {
76
108
  const { data, error, refresh, pending } = await useAsyncData<T, E>(key, () =>
77
- $api(url, getOptions),
109
+ $api(url, queryOptions),
78
110
  )
79
111
 
80
- if (fresh) {
81
- await refresh()
82
- }
112
+ if (fresh) await refresh()
83
113
 
84
114
  return {
85
115
  token: tokenRef.value,
@@ -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, getOptions)
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,19 +2,26 @@ import { parse, format } from '@formkit/tempo'
2
2
 
3
3
  import { useLocale } from './use-locale'
4
4
 
5
- export const useDatetimeLocal = function () {
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
 
13
+ const fmtISO8601 = 'YYYY-MM-DDTHH:mm'
14
+
8
15
  const parseDT = (datetime: string, format: string): Date => {
9
16
  return parse(datetime, format, locale.value)
10
17
  }
11
18
 
12
19
  const toISO8601 = (datetime: string): string => {
13
- return format(parseDT(datetime, 'YYYY/MM/DD HH:mm'), 'YYYY-MM-DDTHH:mm', locale.value)
20
+ return format(parseDT(datetime, fmtDT), fmtISO8601, locale.value)
14
21
  }
15
22
 
16
23
  const fromISO8601 = (datetime: string): string => {
17
- return format(parseDT(datetime, 'YYYY-MM-DDTHH:mm'), 'YYYY/MM/DD HH:mm', locale.value)
24
+ return format(parseDT(datetime, fmtISO8601), fmtDT, locale.value)
18
25
  }
19
26
 
20
27
  const upDTL = (datetime: string | null): string | null => {
@@ -26,9 +33,7 @@ export const useDatetimeLocal = function () {
26
33
  }
27
34
 
28
35
  const formatHTML = (datetime: string | null, fmt: string): string => {
29
- return datetime
30
- ? format(parse(datetime, 'YYYY/MM/DD HH:mm', locale.value), fmt, locale.value)
31
- : ''
36
+ return datetime ? format(parse(datetime, fmtDT, locale.value), fmt, locale.value) : ''
32
37
  }
33
38
 
34
39
  return { upDTL, downDTL, toISO8601, formatHTML }
@@ -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
  }
@@ -1,6 +1,8 @@
1
1
  import { useState } from 'nuxt/app'
2
2
 
3
- export const useReferer = function () {
3
+ export const useReferer = function (): {
4
+ referers: Ref<Record<string, string>>
5
+ } {
4
6
  const referers = useState<Record<string, string>>('referers', () => {
5
7
  return {}
6
8
  })
@@ -17,10 +17,16 @@ interface TZOptions {
17
17
  value: string
18
18
  }
19
19
 
20
- export const useTimeZone = function () {
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
- const { toISO8601, formatHTML } = useDatetimeLocal()
29
+ const { toISO8601, formatHTML } = useDatetimeLocal(fmtDT)
24
30
 
25
31
  const serverTimeZone = runtimeConfig.public.timeZone as string
26
32
 
@@ -47,7 +53,7 @@ export const useTimeZone = function () {
47
53
  : datetime
48
54
  ? format({
49
55
  date: tzServerDate(datetime),
50
- format: 'YYYY/MM/DD HH:mm',
56
+ format: fmtDT,
51
57
  locale: locale.value,
52
58
  tz: timeZone.value.client,
53
59
  })
@@ -60,7 +66,7 @@ export const useTimeZone = function () {
60
66
  : datetime
61
67
  ? format({
62
68
  date: tzClientDate(datetime),
63
- format: 'YYYY/MM/DD HH:mm',
69
+ format: fmtDT,
64
70
  locale: locale.value,
65
71
  tz: timeZone.value.server,
66
72
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vesperjs/nuxt",
3
- "version": "0.3.3",
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.3.3",
28
- "eslint": "^10.2.1",
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",