@dargmuesli/nuxt-vio 21.0.10 → 22.0.0-beta.2

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/.config/lint.js CHANGED
@@ -24,7 +24,7 @@ export const VIO_ESLINT_CONFIG = [
24
24
  prettierConfiguration, // must be last
25
25
 
26
26
  {
27
- files: ['.config/**/*', 'server/**/*'],
27
+ files: ['.config/**/*', 'node/**/*', 'server/**/*'],
28
28
  rules: {
29
29
  'compat/compat': 'off',
30
30
  },
@@ -1,20 +1,25 @@
1
1
  export const useGetServiceHref = () => {
2
2
  const host = useHost()
3
3
  const runtimeConfig = useRuntimeConfig()
4
+ const isTesting = useIsTesting()
4
5
 
5
6
  return ({
6
7
  isSsr = true,
7
8
  name,
9
+ path,
8
10
  port,
9
11
  }: {
10
12
  isSsr?: boolean
11
- name?: string
13
+ name: string
14
+ path?: string
12
15
  port?: number
13
16
  }) =>
14
17
  getServiceHref({
15
18
  host,
16
19
  isSsr,
20
+ isTesting,
17
21
  name,
22
+ path,
18
23
  port,
19
24
  stagingHost: runtimeConfig.public.vio.stagingHost,
20
25
  })
@@ -28,3 +33,13 @@ export const useHost = () => {
28
33
 
29
34
  return host
30
35
  }
36
+
37
+ export const useServiceFetch = (
38
+ options: Parameters<ReturnType<typeof useGetServiceHref>>[0],
39
+ ) => {
40
+ const getServiceHref = useGetServiceHref()
41
+
42
+ return $fetch.create({
43
+ baseURL: getServiceHref(options),
44
+ })
45
+ }
@@ -1,7 +1,8 @@
1
- export const useStrapiFetch = () => {
2
- const getServiceHref = useGetServiceHref()
3
-
4
- return $fetch.create({
5
- baseURL: getServiceHref({ name: 'creal_strapi', port: 1337 }) + '/api',
1
+ export const useStrapiFetch = (
2
+ options?: Parameters<ReturnType<typeof useGetServiceHref>>[0],
3
+ ) =>
4
+ useServiceFetch({
5
+ ...(options ? options : {}),
6
+ name: options?.name || 'strapi',
7
+ path: options?.path || '/api',
6
8
  })
7
- }
@@ -1,6 +1,13 @@
1
- export const useIsTesting = () => {
2
- const cookie = useCookie(TESTING_COOKIE_NAME).value
1
+ export const useIsTesting = ({
2
+ isCookieEnabled = true,
3
+ }: { isCookieEnabled?: boolean } | undefined = {}) => {
3
4
  const runtimeConfig = useRuntimeConfig()
4
5
 
5
- return runtimeConfig.public.vio.isTesting || !!cookie
6
+ const isTestingByRuntimeConfig = runtimeConfig.public.vio.isTesting
7
+ if (isTestingByRuntimeConfig) return true
8
+
9
+ if (isCookieEnabled) {
10
+ const isTestingByCookie = !!useCookie(TESTING_COOKIE_NAME).value
11
+ if (isTestingByCookie) return true
12
+ }
6
13
  }
@@ -1,5 +1,5 @@
1
- export const getTimezone = () =>
2
- useNuxtApp().ssrContext?.event.context.$timezone ||
1
+ export const getTimeZone = () =>
2
+ useNuxtApp().ssrContext?.event.context.$timeZone ||
3
3
  useCookie(TIMEZONE_COOKIE_NAME, {
4
4
  httpOnly: false,
5
5
  sameSite: 'strict',
@@ -5,3 +5,4 @@ export const IS_IN_FRONTEND_DEVELOPMENT = !IS_IN_PRODUCTION && !IS_IN_STACK
5
5
  export const SITE_URL =
6
6
  process.env.NUXT_PUBLIC_I18N_BASE_URL ||
7
7
  `https://${process.env.HOST || 'app.localhost'}:${process.env.PORT || '3000'}`
8
+ export const SITE_URL_TYPED = new URL(SITE_URL)
package/package.json CHANGED
@@ -97,7 +97,7 @@
97
97
  "url": "git+https://github.com/dargmuesli/vio.git"
98
98
  },
99
99
  "type": "module",
100
- "version": "21.0.10",
100
+ "version": "22.0.0-beta.2",
101
101
  "scripts": {
102
102
  "build": "pnpm run build:node",
103
103
  "build:node": "NUXT_PUBLIC_I18N_BASE_URL=https://app.localhost:3001 nuxt build playground",
@@ -1,9 +1,10 @@
1
1
  export default defineEventHandler(async (event) => {
2
- event.context.$timezone = await getTimezone(event)
2
+ const isTesting = useIsTesting()
3
+ event.context.$timeZone = await getTimeZone({ event, isTesting })
3
4
  })
4
5
 
5
6
  declare module 'h3' {
6
7
  interface H3EventContext {
7
- $timezone?: string
8
+ $timeZone?: string
8
9
  }
9
10
  }
@@ -1,28 +1,41 @@
1
1
  import type { H3Event } from 'h3'
2
2
 
3
- export const getTimezone = async (event: H3Event) => {
4
- const timezoneBySsr = event.context.$timezone
3
+ export const getTimeZone = async ({
4
+ event,
5
+ isTesting,
6
+ }: {
7
+ event: H3Event
8
+ isTesting?: boolean
9
+ }) => {
10
+ const timeZoneBySsr = event.context.$timeZone
5
11
 
6
- if (timezoneBySsr) return timezoneBySsr
12
+ if (timeZoneBySsr) return timeZoneBySsr
7
13
 
8
- const timezoneByCookie = getCookie(event, TIMEZONE_COOKIE_NAME)
14
+ const timeZoneByCookie = getCookie(event, TIMEZONE_COOKIE_NAME)
9
15
 
10
- if (timezoneByCookie) return timezoneByCookie
16
+ if (timeZoneByCookie) return timeZoneByCookie
11
17
 
12
- const ip = getRequestIP(event, { xForwardedFor: true })
18
+ if (!isTesting) {
19
+ const ip = getRequestIP(event, { xForwardedFor: true })
13
20
 
14
- if (ip) {
15
- const timezoneByIpApi = await getTimezoneByIpApi(ip)
21
+ if (ip) {
22
+ const timeZoneByIpApi = await getTimeZoneByIpApi({ ip })
16
23
 
17
- if (timezoneByIpApi) return timezoneByIpApi
24
+ if (timeZoneByIpApi) return timeZoneByIpApi
25
+ }
18
26
  }
19
27
  }
20
28
 
21
- export const getTimezoneByIpApi = async (ip: string) => {
22
- if (isTestingServer()) return // TODO: mock
29
+ export const useTimeZone = async () => {
30
+ const event = useEvent()
31
+ const isTesting = useIsTesting()
23
32
 
33
+ return await getTimeZone({ event, isTesting })
34
+ }
35
+
36
+ export const getTimeZoneByIpApi = async ({ ip }: { ip: string }) => {
24
37
  const ipApiResult = await $fetch<{ timezone: string }>(
25
- `http://ip-api.com/json/${ip}`,
38
+ `http://geoip:8080/${ip}`,
26
39
  ).catch(() => {})
27
40
 
28
41
  if (ipApiResult) {
@@ -0,0 +1 @@
1
+ export const DARGSTACK_SECRET_UNUSED_THIRD_PARTY = 'UNSET THIRD PARTY SECRET'
@@ -1 +1,2 @@
1
+ export * from './dargstack'
1
2
  export * from './turnstile'
@@ -1,28 +1,35 @@
1
- export const useGetServiceHref = () => {
2
- const host = useHost()
1
+ import type { H3Event } from 'h3'
2
+
3
+ export const useGetServiceHref = ({ event }: { event?: H3Event } = {}) => {
4
+ const host = useHost({ event })
3
5
  const runtimeConfig = useRuntimeConfig()
6
+ const isTesting = useIsTesting()
4
7
 
5
8
  return ({
6
9
  isSsr = true,
7
10
  name,
11
+ path,
8
12
  port,
9
13
  }: {
10
14
  isSsr?: boolean
11
- name?: string
15
+ name: string
16
+ path?: string
12
17
  port?: number
13
18
  }) =>
14
19
  getServiceHref({
15
20
  host,
16
21
  isSsr,
22
+ isTesting,
17
23
  name,
24
+ path,
18
25
  port,
19
26
  stagingHost: runtimeConfig.public.vio.stagingHost,
20
- }).toString()
27
+ })
21
28
  }
22
29
 
23
- export const useHost = () => {
24
- const event = useEvent()
25
- const host = getHost(event)
30
+ export const useHost = ({ event }: { event?: H3Event } = {}) => {
31
+ const { siteUrlTyped: siteUrl } = useSiteUrl()
32
+ const host = event ? getHost(event) : siteUrl.host
26
33
 
27
34
  if (!host) throw new Error('Host is not given!')
28
35
 
@@ -1,13 +1,32 @@
1
1
  import type { H3Event } from 'h3'
2
2
 
3
- export const isTestingServer = (event?: H3Event) => {
4
- const isTestingByRuntimeConfig = useRuntimeConfig().public.vio.isTesting
3
+ export const useIsTesting = ({
4
+ isCookieEnabled = true,
5
+ }:
6
+ | {
7
+ isCookieEnabled?: boolean
8
+ }
9
+ | undefined = {}) => {
10
+ const event = useEvent()
11
+ const runtimeConfig = useRuntimeConfig()
5
12
 
13
+ return getIsTesting({ event, isCookieEnabled, runtimeConfig })
14
+ }
15
+
16
+ export const getIsTesting = ({
17
+ event,
18
+ isCookieEnabled,
19
+ runtimeConfig,
20
+ }: {
21
+ event?: H3Event
22
+ isCookieEnabled?: boolean
23
+ runtimeConfig: ReturnType<typeof useRuntimeConfig>
24
+ }) => {
25
+ const isTestingByRuntimeConfig = runtimeConfig.public.vio.isTesting
6
26
  if (isTestingByRuntimeConfig) return true
7
27
 
8
- if (event) {
28
+ if (isCookieEnabled && event) {
9
29
  const isTestingByCookie = !!getCookie(event, TESTING_COOKIE_NAME)
10
-
11
30
  if (isTestingByCookie) return true
12
31
  }
13
32
  }
@@ -3,6 +3,7 @@ import type { H3Event } from 'h3'
3
3
  import { computed, reactive } from 'vue'
4
4
  import type { Ref } from 'vue'
5
5
 
6
+ import { SITE_URL_TYPED } from '../../node/static'
6
7
  import type { ApiData, BackendError } from '../types/api'
7
8
 
8
9
  export const getApiDataDefault = (): ApiData =>
@@ -87,25 +88,41 @@ export const getHost = (event: H3Event) => {
87
88
  export const getServiceHref = ({
88
89
  host,
89
90
  isSsr = true,
91
+ isTesting,
90
92
  name,
93
+ path,
91
94
  port,
92
95
  stagingHost,
93
96
  }: {
94
- host: string
97
+ host?: string
95
98
  isSsr?: boolean
96
- name?: string
99
+ isTesting?: boolean
100
+ name: string
101
+ path?: string
97
102
  port?: number
98
103
  stagingHost?: string
99
104
  }) => {
100
- const nameSubdomain = name?.replaceAll('_', '-')
105
+ const nameSubdomain =
106
+ name !== VIO_SITE_NAME ? name?.replaceAll('_', '-') : undefined
101
107
  const nameSubdomainString = nameSubdomain ? `${nameSubdomain}.` : ''
102
108
  const portString = port ? `:${port}` : ''
109
+ const pathString = path ? `/${path.replace(/^\/+/, '')}` : ''
110
+
111
+ if (isTesting) {
112
+ return `${SITE_URL_TYPED.protocol}//${nameSubdomainString}${SITE_URL_TYPED.host}${pathString}`
113
+ }
103
114
 
104
115
  if (stagingHost) {
105
- return `https://${nameSubdomainString}${stagingHost}`
106
- } else if (isSsr && import.meta.server) {
107
- return `http://${name}${portString}`
108
- } else {
109
- return `https://${nameSubdomainString}${getRootHost(host)}`
116
+ return `https://${nameSubdomainString}${stagingHost}${pathString}`
117
+ }
118
+
119
+ if (import.meta.server && isSsr) {
120
+ return `http://${name}${portString}${pathString}`
110
121
  }
122
+
123
+ if (host) {
124
+ return `https://${nameSubdomainString}${host}${pathString}`
125
+ }
126
+
127
+ throw new Error('Could not get service href!')
111
128
  }