@vesperjs/nuxt 0.3.2 → 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'
@@ -9,6 +9,12 @@ interface MutationAPIOptions {
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
10
  body?: Record<string, any> | FormData
11
11
  token?: string | null
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
12
18
  onRequestError?: ({ error }: { error: Error }) => void
13
19
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
20
  onResponseError?: ({ response }: { response: FetchResponse<any> }) => void
@@ -17,7 +23,18 @@ interface MutationAPIOptions {
17
23
  // eslint-disable-next-line
18
24
  export const useMutationApi = async function <T = unknown, E = any>(
19
25
  url: string,
20
- { method, body = {}, token = null, onRequestError, onResponseError }: MutationAPIOptions,
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,
21
38
  ): Promise<{
22
39
  token: string | null | undefined
23
40
  data: T | undefined
@@ -25,33 +42,30 @@ export const useMutationApi = async function <T = unknown, E = any>(
25
42
  pending: boolean
26
43
  }> {
27
44
  const { commonHeaders } = useHttpHeaders()
28
- const { baseURL } = useApiConstants()
45
+ const { baseURL: baseUrl } = useApiConstants()
29
46
 
30
47
  const headers: Record<string, string> = commonHeaders.value
31
48
 
32
49
  const tokenRef = ref<string | null>()
33
50
 
34
- if (token) {
35
- headers.Authorization = `Bearer ${token}`
36
- }
51
+ if (token) headers.Authorization = `Bearer ${token}`
37
52
 
38
53
  const options: FetchOptions<'json'> = {
39
- baseURL: baseURL.value,
54
+ baseURL: baseURL ?? baseUrl.value,
40
55
  headers,
41
56
  method,
42
57
  }
43
58
 
44
- if (onRequestError) {
45
- options.onRequestError = onRequestError
46
- }
59
+ if (retry) options.retry = retry
60
+ if (retryDelay) options.retryDelay = retryDelay
61
+ if (retryStatusCodes) options.retryStatusCodes = retryStatusCodes
47
62
 
48
- if (onResponseError) {
49
- options.onResponseError = onResponseError
50
- }
63
+ if (timeout) options.timeout = timeout
51
64
 
52
- if (method == 'post' || method == 'put') {
53
- options.body = body
54
- }
65
+ if (onRequestError) options.onRequestError = onRequestError
66
+ if (onResponseError) options.onResponseError = onResponseError
67
+
68
+ if (method == 'post' || method == 'put') options.body = body
55
69
 
56
70
  options.onResponse = ({ response }: { response: FetchResponse<T> }) => {
57
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
 
@@ -17,7 +17,13 @@ export type QueryAPIOptions = {
17
17
  key?: MaybeRefOrGetter<string>
18
18
  query?: SearchParams
19
19
  token?: string | null
20
+ baseURL?: string | null
20
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
21
27
  onRequestError?: ({ error }: { error: Error }) => void
22
28
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
29
  onResponseError?: ({ response }: { response: FetchResponse<any> }) => void
@@ -33,7 +39,7 @@ export const useQueryApi = async function <T = unknown, E = any>(
33
39
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
40
  const { $api } = useNuxtApp() as any
35
41
  const { commonHeaders } = useHttpHeaders()
36
- const { baseURL } = useApiConstants()
42
+ const { baseURL: baseUrl } = useApiConstants()
37
43
 
38
44
  const key = options?.key ?? url
39
45
 
@@ -44,13 +50,11 @@ export const useQueryApi = async function <T = unknown, E = any>(
44
50
  const fresh: boolean = options?.fresh ?? false
45
51
  const cache: boolean = options?.cache ?? true
46
52
 
47
- if (options?.token) {
48
- headers.Authorization = `Bearer ${options.token}`
49
- }
53
+ if (options?.token) headers.Authorization = `Bearer ${options.token}`
50
54
 
51
55
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
56
  const getOptions: FetchOptions<'json', any> = {
53
- baseURL: baseURL.value,
57
+ baseURL: options?.baseURL ?? baseUrl.value,
54
58
  method: 'get',
55
59
  query: options?.query ?? {},
56
60
  headers,
@@ -59,26 +63,23 @@ export const useQueryApi = async function <T = unknown, E = any>(
59
63
  },
60
64
  }
61
65
 
62
- if (options?.signal) {
63
- getOptions.signal = options.signal
64
- }
66
+ if (options?.retry) getOptions.retry = options.retry
67
+ if (options?.retryDelay) getOptions.retryDelay = options.retryDelay
68
+ if (options?.retryStatusCodes) getOptions.retryStatusCodes = options.retryStatusCodes
65
69
 
66
- if (options?.onRequestError) {
67
- getOptions.onRequestError = options.onRequestError
68
- }
70
+ if (options?.timeout) getOptions.timeout = options.timeout
69
71
 
70
- if (options?.onResponseError) {
71
- getOptions.onResponseError = options.onResponseError
72
- }
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
73
76
 
74
77
  if (cache) {
75
78
  const { data, error, refresh, pending } = await useAsyncData<T, E>(key, () =>
76
79
  $api(url, getOptions),
77
80
  )
78
81
 
79
- if (fresh) {
80
- await refresh()
81
- }
82
+ if (fresh) await refresh()
82
83
 
83
84
  return {
84
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.2",
3
+ "version": "0.4.0",
4
4
  "keywords": [
5
5
  "nuxt",
6
6
  "vue.js"
@@ -22,14 +22,14 @@
22
22
  "devDependencies": {
23
23
  "@formkit/tempo": "^1.0.0",
24
24
  "@nuxt/eslint": "1.15.2",
25
- "@nuxtjs/i18n": "^10.2.4",
25
+ "@nuxtjs/i18n": "^10.3.0",
26
26
  "@types/node": "^24.12.2",
27
- "@vesperjs/shared": "0.3.2",
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",
31
31
  "ofetch": "^1.5.1",
32
- "oxfmt": "^0.46.0",
32
+ "oxfmt": "^0.47.0",
33
33
  "typescript": "^6.0.3"
34
34
  },
35
35
  "peerDependencies": {