@dargmuesli/nuxt-vio 22.0.6 → 23.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.
@@ -4,23 +4,22 @@ export const useGetServiceHref = () => {
4
4
  const isTesting = useIsTesting()
5
5
 
6
6
  return ({
7
- isSsr = true,
7
+ allowInternal = true,
8
8
  name,
9
9
  path,
10
- port,
11
10
  }: {
12
- isSsr?: boolean
13
- name: string
11
+ allowInternal?: boolean
12
+ name: ServiceName
14
13
  path?: string
15
- port?: number
16
14
  }) =>
17
15
  getServiceHref({
16
+ allowInternal,
18
17
  host,
19
- isSsr,
18
+ isServer: import.meta.server,
20
19
  isTesting,
21
20
  name,
22
21
  path,
23
- port,
22
+ services: runtimeConfig.public.vio.services,
24
23
  stagingHost: runtimeConfig.public.vio.stagingHost,
25
24
  })
26
25
  }
@@ -5,5 +5,4 @@ export const useStrapiFetch = (
5
5
  ...(options ? options : {}),
6
6
  name: options?.name || 'strapi',
7
7
  path: options?.path || '/api',
8
- port: options?.port || 1337,
9
8
  })
@@ -0,0 +1,89 @@
1
+ import type { H3Event } from 'h3'
2
+
3
+ import { SERVICES } from './services'
4
+ import type { Service, ServiceName } from './services'
5
+ import { SITE_URL_TYPED } from './static'
6
+
7
+ export const getHost = (event: H3Event) => {
8
+ const host = event.node.req.headers.host
9
+
10
+ if (!host) throw new Error('Host header is not given!')
11
+
12
+ return host
13
+ }
14
+
15
+ export const getRootHost = (host: string) => {
16
+ const hostParts = host.split('.')
17
+ const hostPartsLast = hostParts[hostParts.length - 1]
18
+
19
+ if (hostParts.length === 1) return hostParts[0]
20
+
21
+ // only a single subdomain directly on bare localhost collapses to the root; e.g. app.localhost must keep its "app" label
22
+ if (
23
+ hostParts.length === 2 &&
24
+ hostPartsLast &&
25
+ /^localhost(:[0-9]+)?$/.test(hostPartsLast)
26
+ )
27
+ return hostPartsLast
28
+
29
+ return `${hostParts[hostParts.length - 2]}.${hostPartsLast}`
30
+ }
31
+
32
+ export const getServiceHref = ({
33
+ allowInternal = true,
34
+ host,
35
+ isServer,
36
+ isTesting,
37
+ name,
38
+ path,
39
+ protocol = SITE_URL_TYPED.protocol,
40
+ services = SERVICES,
41
+ stagingHost,
42
+ }: {
43
+ allowInternal?: boolean
44
+ host?: string
45
+ isServer: boolean
46
+ isTesting?: boolean
47
+ name: ServiceName
48
+ path?: string
49
+ protocol?: string
50
+ services?: Record<string, Service>
51
+ stagingHost?: string
52
+ }) => {
53
+ const service = services[name]
54
+
55
+ if (!service) throw new Error(`Service "${name}" is not registered!`)
56
+
57
+ const { hasSubdomain, port } = service
58
+ const nameSubdomainString = `${name.replaceAll('_', '-')}.`
59
+ const pathString = path ? `/${path.replace(/^\/+/, '')}` : ''
60
+
61
+ const assertHasSubdomain = () => {
62
+ if (!hasSubdomain)
63
+ throw new Error(`Service "${name}" has no public subdomain!`)
64
+ }
65
+
66
+ if (isTesting) {
67
+ assertHasSubdomain()
68
+
69
+ return `${protocol}//${nameSubdomainString}${SITE_URL_TYPED.host}${pathString}`
70
+ }
71
+
72
+ if (stagingHost) {
73
+ assertHasSubdomain()
74
+
75
+ return `${protocol}//${nameSubdomainString}${stagingHost}${pathString}`
76
+ }
77
+
78
+ if (isServer && allowInternal) {
79
+ return `http://${name}:${port}${pathString}`
80
+ }
81
+
82
+ if (host) {
83
+ assertHasSubdomain()
84
+
85
+ return `${protocol}//${nameSubdomainString}${getRootHost(host)}${pathString}`
86
+ }
87
+
88
+ throw new Error('Could not get service href!')
89
+ }
@@ -0,0 +1,11 @@
1
+ export type Service = {
2
+ hasSubdomain: boolean
3
+ port: number
4
+ }
5
+
6
+ export const SERVICES = {
7
+ strapi: { hasSubdomain: true, port: 1337 },
8
+ } as const satisfies Record<string, Service>
9
+
10
+ // The open string branch accepts services registered by extending projects through VIO_NUXT_BASE_CONFIG's `services` option, while still keeping autocomplete for the built-in names.
11
+ export type ServiceName = keyof typeof SERVICES | (string & {})
@@ -1,11 +1,15 @@
1
1
  import type { defineNuxtConfig } from 'nuxt/config'
2
2
 
3
+ import type { Service } from '../services'
4
+ import { SERVICES } from '../services'
3
5
  import { IS_IN_FRONTEND_DEVELOPMENT } from './'
4
6
 
5
7
  export const VIO_NUXT_BASE_CONFIG = ({
8
+ services,
6
9
  siteName,
7
10
  stagingHost,
8
11
  }: {
12
+ services?: Record<string, Service>
9
13
  siteName: string
10
14
  stagingHost?: string
11
15
  }) =>
@@ -18,6 +22,7 @@ export const VIO_NUXT_BASE_CONFIG = ({
18
22
  runtimeConfig: {
19
23
  public: {
20
24
  vio: {
25
+ services: { ...SERVICES, ...services },
21
26
  stagingHost: IS_IN_FRONTEND_DEVELOPMENT ? stagingHost : undefined,
22
27
  },
23
28
  },
package/package.json CHANGED
@@ -101,7 +101,7 @@
101
101
  "url": "git+https://github.com/dargmuesli/vio.git"
102
102
  },
103
103
  "type": "module",
104
- "version": "22.0.6",
104
+ "version": "23.0.0-beta.2",
105
105
  "scripts": {
106
106
  "build": "pnpm run build:node",
107
107
  "build:node": "NUXT_PUBLIC_I18N_BASE_URL=https://app.localhost:3001 nuxt build playground",
@@ -6,23 +6,22 @@ export const useGetServiceHref = ({ event }: { event?: H3Event } = {}) => {
6
6
  const isTesting = useIsTesting()
7
7
 
8
8
  return ({
9
- isSsr = true,
9
+ allowInternal = true,
10
10
  name,
11
11
  path,
12
- port,
13
12
  }: {
14
- isSsr?: boolean
15
- name: string
13
+ allowInternal?: boolean
14
+ name: ServiceName
16
15
  path?: string
17
- port?: number
18
16
  }) =>
19
17
  getServiceHref({
18
+ allowInternal,
20
19
  host,
21
- isSsr,
20
+ isServer: import.meta.server,
22
21
  isTesting,
23
22
  name,
24
23
  path,
25
- port,
24
+ services: runtimeConfig.public.vio.services,
26
25
  stagingHost: runtimeConfig.public.vio.stagingHost,
27
26
  })
28
27
  }
@@ -1,11 +1,11 @@
1
1
  import type { CombinedError } from '@urql/core'
2
- import type { H3Event } from 'h3'
3
2
  import { computed, reactive } from 'vue'
4
3
  import type { Ref } from 'vue'
5
4
 
6
- import { SITE_URL_TYPED } from '../../node/static'
7
5
  import type { ApiData, BackendError } from '../types/api'
8
6
 
7
+ export { getHost, getRootHost, getServiceHref } from '../../node/networking'
8
+
9
9
  export const getApiDataDefault = (): ApiData =>
10
10
  computed(() =>
11
11
  reactive({
@@ -64,70 +64,3 @@ export const getCombinedErrorMessages = (
64
64
 
65
65
  return errorMessages
66
66
  }
67
-
68
- export const getRootHost = (host: string) => {
69
- const hostParts = host.split('.')
70
- const hostPartsLast = hostParts[hostParts.length - 1]
71
-
72
- if (hostParts.length === 1) return hostParts[0]
73
-
74
- // only a single subdomain directly on bare localhost collapses to the root; e.g. app.localhost must keep its "app" label
75
- if (
76
- hostParts.length === 2 &&
77
- hostPartsLast &&
78
- /^localhost(:[0-9]+)?$/.test(hostPartsLast)
79
- )
80
- return hostPartsLast
81
-
82
- return `${hostParts[hostParts.length - 2]}.${hostPartsLast}`
83
- }
84
-
85
- export const getHost = (event: H3Event) => {
86
- const host = event.node.req.headers.host
87
-
88
- if (!host) throw new Error('Host header is not given!')
89
-
90
- return host
91
- }
92
-
93
- export const getServiceHref = ({
94
- host,
95
- isSsr = true,
96
- isTesting,
97
- name,
98
- path,
99
- port,
100
- stagingHost,
101
- }: {
102
- host?: string
103
- isSsr?: boolean
104
- isTesting?: boolean
105
- name: string
106
- path?: string
107
- port?: number
108
- stagingHost?: string
109
- }) => {
110
- const nameSubdomain =
111
- name !== VIO_SITE_NAME ? name?.replaceAll('_', '-') : undefined
112
- const nameSubdomainString = nameSubdomain ? `${nameSubdomain}.` : ''
113
- const portString = port ? `:${port}` : ''
114
- const pathString = path ? `/${path.replace(/^\/+/, '')}` : ''
115
-
116
- if (isTesting) {
117
- return `${SITE_URL_TYPED.protocol}//${nameSubdomainString}${SITE_URL_TYPED.host}${pathString}`
118
- }
119
-
120
- if (stagingHost) {
121
- return `https://${nameSubdomainString}${stagingHost}${pathString}`
122
- }
123
-
124
- if (import.meta.server && isSsr) {
125
- return `http://${name}${portString}${pathString}`
126
- }
127
-
128
- if (host) {
129
- return `https://${nameSubdomainString}${getRootHost(host)}${pathString}`
130
- }
131
-
132
- throw new Error('Could not get service href!')
133
- }
@@ -0,0 +1,2 @@
1
+ export { SERVICES } from '../../node/services'
2
+ export type { Service, ServiceName } from '../../node/services'