@vesperjs/nuxt 0.4.0 → 0.6.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
 
@@ -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 options: FetchOptions<'json'> = {
59
+ const mutOptions: FetchOptions<'json'> = {
54
60
  baseURL: baseURL ?? baseUrl.value,
55
61
  headers,
56
62
  method,
57
63
  }
58
64
 
59
- if (retry) options.retry = retry
60
- if (retryDelay) options.retryDelay = retryDelay
61
- if (retryStatusCodes) options.retryStatusCodes = retryStatusCodes
65
+ if (retry) mutOptions.retry = retry
66
+ if (retryDelay) mutOptions.retryDelay = retryDelay
67
+ if (retryStatusCodes) mutOptions.retryStatusCodes = retryStatusCodes
62
68
 
63
- if (timeout) options.timeout = timeout
69
+ if (timeout) mutOptions.timeout = timeout
64
70
 
65
- if (onRequestError) options.onRequestError = onRequestError
66
- if (onResponseError) options.onResponseError = onResponseError
71
+ if (onRequestError) mutOptions.onRequestError = onRequestError
72
+ if (onResponseError) mutOptions.onResponseError = onResponseError
67
73
 
68
- if (method == 'post' || method == 'put') options.body = body
74
+ if (method == 'post' || method == 'put') mutOptions.body = body
69
75
 
70
- options.onResponse = ({ response }: { response: FetchResponse<T> }) => {
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, options)
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,14 +8,32 @@ 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
14
29
  }
15
30
 
16
31
  export type QueryAPIOptions = {
32
+ method?: 'get' | 'query'
17
33
  key?: MaybeRefOrGetter<string>
18
34
  query?: SearchParams
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ body?: Record<string, any> | FormData
19
37
  token?: string | null
20
38
  baseURL?: string | null
21
39
  signal?: AbortSignal
@@ -35,7 +53,22 @@ export type QueryAPIOptions = {
35
53
  export const useQueryApi = async function <T = unknown, E = any>(
36
54
  url: string,
37
55
  options?: QueryAPIOptions,
38
- ) {
56
+ ): Promise<
57
+ | {
58
+ token: string | null | undefined
59
+ data: PickFrom<T, KeysOf<T>> | undefined
60
+ error: (E extends Error | NuxtError<unknown> ? E : NuxtError<E>) | undefined
61
+ refresh: (opts?: never) => Promise<void>
62
+ pending: boolean
63
+ }
64
+ | {
65
+ token: string | null | undefined
66
+ data: T | undefined
67
+ error: FetchError<E> | undefined
68
+ pending: boolean
69
+ refresh?: undefined
70
+ }
71
+ > {
39
72
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
73
  const { $api } = useNuxtApp() as any
41
74
  const { commonHeaders } = useHttpHeaders()
@@ -53,30 +86,43 @@ export const useQueryApi = async function <T = unknown, E = any>(
53
86
  if (options?.token) headers.Authorization = `Bearer ${options.token}`
54
87
 
55
88
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
- const getOptions: FetchOptions<'json', any> = {
89
+ const queryOptions: FetchOptions<'json', any> = {
57
90
  baseURL: options?.baseURL ?? baseUrl.value,
58
- method: 'get',
59
- query: options?.query ?? {},
60
91
  headers,
61
92
  onResponse({ response }: { response: FetchResponse<T> }) {
62
93
  tokenRef.value = response.headers.get('Authorization')?.split(' ')[1] ?? options?.token
63
94
  },
64
95
  }
65
96
 
66
- if (options?.retry) getOptions.retry = options.retry
67
- if (options?.retryDelay) getOptions.retryDelay = options.retryDelay
68
- if (options?.retryStatusCodes) getOptions.retryStatusCodes = options.retryStatusCodes
97
+ if (options?.method) {
98
+ queryOptions.method = options.method
99
+
100
+ if (options.method === 'get') queryOptions.query = options.query ?? {}
101
+ if (options.method === 'query') queryOptions.body = options.body ?? {}
102
+ } else {
103
+ if (options?.query) {
104
+ queryOptions.method = 'get'
105
+ queryOptions.query = options.query
106
+ } else if (options?.body) {
107
+ queryOptions.method = 'query'
108
+ queryOptions.body = options.body
109
+ }
110
+ }
111
+
112
+ if (options?.retry) queryOptions.retry = options.retry
113
+ if (options?.retryDelay) queryOptions.retryDelay = options.retryDelay
114
+ if (options?.retryStatusCodes) queryOptions.retryStatusCodes = options.retryStatusCodes
69
115
 
70
- if (options?.timeout) getOptions.timeout = options.timeout
116
+ if (options?.timeout) queryOptions.timeout = options.timeout
71
117
 
72
- if (options?.signal) getOptions.signal = options.signal
118
+ if (options?.signal) queryOptions.signal = options.signal
73
119
 
74
- if (options?.onRequestError) getOptions.onRequestError = options.onRequestError
75
- if (options?.onResponseError) getOptions.onResponseError = options.onResponseError
120
+ if (options?.onRequestError) queryOptions.onRequestError = options.onRequestError
121
+ if (options?.onResponseError) queryOptions.onResponseError = options.onResponseError
76
122
 
77
123
  if (cache) {
78
124
  const { data, error, refresh, pending } = await useAsyncData<T, E>(key, () =>
79
- $api(url, getOptions),
125
+ $api(url, queryOptions),
80
126
  )
81
127
 
82
128
  if (fresh) await refresh()
@@ -89,7 +135,7 @@ export const useQueryApi = async function <T = unknown, E = any>(
89
135
  pending: pending.value,
90
136
  }
91
137
  } else {
92
- const { data, error, pending } = await useOFetch<T, E>(url, getOptions)
138
+ const { data, error, pending } = await useOFetch<T, E>(url, queryOptions)
93
139
 
94
140
  return { token: tokenRef.value, data, error, pending }
95
141
  }
@@ -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
  }
@@ -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,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.4.0",
3
+ "version": "0.6.0",
4
4
  "keywords": [
5
5
  "nuxt",
6
6
  "vue.js"
@@ -24,16 +24,16 @@
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.4.0",
28
- "eslint": "^10.2.1",
27
+ "@vesperjs/shared": "0.6.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",
32
- "oxfmt": "^0.47.0",
32
+ "oxfmt": "^0.48.0",
33
33
  "typescript": "^6.0.3"
34
34
  },
35
35
  "peerDependencies": {
36
- "vue": "^3.5.33 || ^3.6.0-beta.10"
36
+ "vue": "^3.5.34 || ^3.6.0-beta.10"
37
37
  },
38
38
  "scripts": {
39
39
  "dev": "nuxi dev .playground",