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

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,6 +1,7 @@
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,
@@ -8,12 +9,13 @@ export const useGetServiceHref = () => {
8
9
  port,
9
10
  }: {
10
11
  isSsr?: boolean
11
- name?: string
12
+ name: string
12
13
  port?: number
13
14
  }) =>
14
15
  getServiceHref({
15
16
  host,
16
17
  isSsr,
18
+ isTesting,
17
19
  name,
18
20
  port,
19
21
  stagingHost: runtimeConfig.public.vio.stagingHost,
@@ -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.1",
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) {
@@ -1,6 +1,9 @@
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,
@@ -8,21 +11,22 @@ export const useGetServiceHref = () => {
8
11
  port,
9
12
  }: {
10
13
  isSsr?: boolean
11
- name?: string
14
+ name: string
12
15
  port?: number
13
16
  }) =>
14
17
  getServiceHref({
15
18
  host,
16
19
  isSsr,
20
+ isTesting,
17
21
  name,
18
22
  port,
19
23
  stagingHost: runtimeConfig.public.vio.stagingHost,
20
- }).toString()
24
+ })
21
25
  }
22
26
 
23
- export const useHost = () => {
24
- const event = useEvent()
25
- const host = getHost(event)
27
+ export const useHost = ({ event }: { event?: H3Event } = {}) => {
28
+ const { siteUrlTyped: siteUrl } = useSiteUrl()
29
+ const host = event ? getHost(event) : siteUrl.host
26
30
 
27
31
  if (!host) throw new Error('Host is not given!')
28
32
 
@@ -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,38 @@ export const getHost = (event: H3Event) => {
87
88
  export const getServiceHref = ({
88
89
  host,
89
90
  isSsr = true,
91
+ isTesting,
90
92
  name,
91
93
  port,
92
94
  stagingHost,
93
95
  }: {
94
- host: string
96
+ host?: string
95
97
  isSsr?: boolean
96
- name?: string
98
+ isTesting?: boolean
99
+ name: string
97
100
  port?: number
98
101
  stagingHost?: string
99
102
  }) => {
100
- const nameSubdomain = name?.replaceAll('_', '-')
103
+ const nameSubdomain =
104
+ name !== VIO_SITE_NAME ? name?.replaceAll('_', '-') : undefined
101
105
  const nameSubdomainString = nameSubdomain ? `${nameSubdomain}.` : ''
102
106
  const portString = port ? `:${port}` : ''
103
107
 
108
+ if (isTesting) {
109
+ return `${SITE_URL_TYPED.protocol}//${nameSubdomainString}${SITE_URL_TYPED.host}`
110
+ }
111
+
104
112
  if (stagingHost) {
105
113
  return `https://${nameSubdomainString}${stagingHost}`
106
- } else if (isSsr && import.meta.server) {
114
+ }
115
+
116
+ if (import.meta.server && isSsr) {
107
117
  return `http://${name}${portString}`
108
- } else {
109
- return `https://${nameSubdomainString}${getRootHost(host)}`
110
118
  }
119
+
120
+ if (host) {
121
+ return `https://${nameSubdomainString}${host}`
122
+ }
123
+
124
+ throw new Error('Could not get service href!')
111
125
  }