@vesperjs/nuxt 0.3.3 → 0.4.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,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,6 +10,11 @@ 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
@@ -23,6 +28,10 @@ export const useMutationApi = async function <T = unknown, E = any>(
23
28
  body = {},
24
29
  token = null,
25
30
  baseURL = null,
31
+ retry,
32
+ retryDelay,
33
+ retryStatusCodes,
34
+ timeout,
26
35
  onRequestError,
27
36
  onResponseError,
28
37
  }: MutationAPIOptions,
@@ -39,9 +48,7 @@ export const useMutationApi = async function <T = unknown, E = any>(
39
48
 
40
49
  const tokenRef = ref<string | null>()
41
50
 
42
- if (token) {
43
- headers.Authorization = `Bearer ${token}`
44
- }
51
+ if (token) headers.Authorization = `Bearer ${token}`
45
52
 
46
53
  const options: FetchOptions<'json'> = {
47
54
  baseURL: baseURL ?? baseUrl.value,
@@ -49,17 +56,16 @@ export const useMutationApi = async function <T = unknown, E = any>(
49
56
  method,
50
57
  }
51
58
 
52
- if (onRequestError) {
53
- options.onRequestError = onRequestError
54
- }
59
+ if (retry) options.retry = retry
60
+ if (retryDelay) options.retryDelay = retryDelay
61
+ if (retryStatusCodes) options.retryStatusCodes = retryStatusCodes
55
62
 
56
- if (onResponseError) {
57
- options.onResponseError = onResponseError
58
- }
63
+ if (timeout) options.timeout = timeout
59
64
 
60
- if (method == 'post' || method == 'put') {
61
- options.body = body
62
- }
65
+ if (onRequestError) options.onRequestError = onRequestError
66
+ if (onResponseError) options.onResponseError = onResponseError
67
+
68
+ if (method == 'post' || method == 'put') options.body = body
63
69
 
64
70
  options.onResponse = ({ response }: { response: FetchResponse<T> }) => {
65
71
  tokenRef.value = response.headers.get('Authorization')?.split(' ')[1] ?? token
@@ -1,6 +1,6 @@
1
1
  import { useAsyncData, useNuxtApp } from 'nuxt/app'
2
2
 
3
- import type { FetchOptions, FetchResponse } from 'ofetch'
3
+ import type { FetchContext, FetchOptions, FetchResponse } from 'ofetch'
4
4
 
5
5
  import { ref } from '@vue/reactivity'
6
6
 
@@ -19,6 +19,11 @@ export type QueryAPIOptions = {
19
19
  token?: string | null
20
20
  baseURL?: string | null
21
21
  signal?: AbortSignal
22
+ retry?: number | false
23
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
+ retryDelay?: number | ((context: FetchContext<any, 'json'>) => number)
25
+ retryStatusCodes?: number[]
26
+ timeout?: number
22
27
  onRequestError?: ({ error }: { error: Error }) => void
23
28
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
29
  onResponseError?: ({ response }: { response: FetchResponse<any> }) => void
@@ -45,9 +50,7 @@ export const useQueryApi = async function <T = unknown, E = any>(
45
50
  const fresh: boolean = options?.fresh ?? false
46
51
  const cache: boolean = options?.cache ?? true
47
52
 
48
- if (options?.token) {
49
- headers.Authorization = `Bearer ${options.token}`
50
- }
53
+ if (options?.token) headers.Authorization = `Bearer ${options.token}`
51
54
 
52
55
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
56
  const getOptions: FetchOptions<'json', any> = {
@@ -60,26 +63,23 @@ export const useQueryApi = async function <T = unknown, E = any>(
60
63
  },
61
64
  }
62
65
 
63
- if (options?.signal) {
64
- getOptions.signal = options.signal
65
- }
66
+ if (options?.retry) getOptions.retry = options.retry
67
+ if (options?.retryDelay) getOptions.retryDelay = options.retryDelay
68
+ if (options?.retryStatusCodes) getOptions.retryStatusCodes = options.retryStatusCodes
66
69
 
67
- if (options?.onRequestError) {
68
- getOptions.onRequestError = options.onRequestError
69
- }
70
+ if (options?.timeout) getOptions.timeout = options.timeout
70
71
 
71
- if (options?.onResponseError) {
72
- getOptions.onResponseError = options.onResponseError
73
- }
72
+ if (options?.signal) getOptions.signal = options.signal
73
+
74
+ if (options?.onRequestError) getOptions.onRequestError = options.onRequestError
75
+ if (options?.onResponseError) getOptions.onResponseError = options.onResponseError
74
76
 
75
77
  if (cache) {
76
78
  const { data, error, refresh, pending } = await useAsyncData<T, E>(key, () =>
77
79
  $api(url, getOptions),
78
80
  )
79
81
 
80
- if (fresh) {
81
- await refresh()
82
- }
82
+ if (fresh) await refresh()
83
83
 
84
84
  return {
85
85
  token: tokenRef.value,
@@ -2,19 +2,21 @@ 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
6
  const { locale } = useLocale()
7
7
 
8
+ const fmtISO8601 = 'YYYY-MM-DDTHH:mm'
9
+
8
10
  const parseDT = (datetime: string, format: string): Date => {
9
11
  return parse(datetime, format, locale.value)
10
12
  }
11
13
 
12
14
  const toISO8601 = (datetime: string): string => {
13
- return format(parseDT(datetime, 'YYYY/MM/DD HH:mm'), 'YYYY-MM-DDTHH:mm', locale.value)
15
+ return format(parseDT(datetime, fmtDT), fmtISO8601, locale.value)
14
16
  }
15
17
 
16
18
  const fromISO8601 = (datetime: string): string => {
17
- return format(parseDT(datetime, 'YYYY-MM-DDTHH:mm'), 'YYYY/MM/DD HH:mm', locale.value)
19
+ return format(parseDT(datetime, fmtISO8601), fmtDT, locale.value)
18
20
  }
19
21
 
20
22
  const upDTL = (datetime: string | null): string | null => {
@@ -26,9 +28,7 @@ export const useDatetimeLocal = function () {
26
28
  }
27
29
 
28
30
  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
- : ''
31
+ return datetime ? format(parse(datetime, fmtDT, locale.value), fmt, locale.value) : ''
32
32
  }
33
33
 
34
34
  return { upDTL, downDTL, toISO8601, formatHTML }
@@ -17,10 +17,10 @@ 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
21
  const runtimeConfig = useRuntimeConfig()
22
22
  const { locale } = useLocale()
23
- const { toISO8601, formatHTML } = useDatetimeLocal()
23
+ const { toISO8601, formatHTML } = useDatetimeLocal(fmtDT)
24
24
 
25
25
  const serverTimeZone = runtimeConfig.public.timeZone as string
26
26
 
@@ -47,7 +47,7 @@ export const useTimeZone = function () {
47
47
  : datetime
48
48
  ? format({
49
49
  date: tzServerDate(datetime),
50
- format: 'YYYY/MM/DD HH:mm',
50
+ format: fmtDT,
51
51
  locale: locale.value,
52
52
  tz: timeZone.value.client,
53
53
  })
@@ -60,7 +60,7 @@ export const useTimeZone = function () {
60
60
  : datetime
61
61
  ? format({
62
62
  date: tzClientDate(datetime),
63
- format: 'YYYY/MM/DD HH:mm',
63
+ format: fmtDT,
64
64
  locale: locale.value,
65
65
  tz: timeZone.value.server,
66
66
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vesperjs/nuxt",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "keywords": [
5
5
  "nuxt",
6
6
  "vue.js"
@@ -24,7 +24,7 @@
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",
27
+ "@vesperjs/shared": "0.4.0",
28
28
  "eslint": "^10.2.1",
29
29
  "eslint-typegen": "2.3.1",
30
30
  "nuxt": "^4.3.1",