@things-factory/shell 8.0.0 → 8.0.8

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.
Files changed (29) hide show
  1. package/config/config.development.js +1 -1
  2. package/config/config.production.js +1 -14
  3. package/dist-server/middlewares/domain-middleware.js +3 -3
  4. package/dist-server/middlewares/domain-middleware.js.map +1 -1
  5. package/dist-server/service/domain/domain-query.d.ts +4 -3
  6. package/dist-server/service/domain/domain-query.js +27 -15
  7. package/dist-server/service/domain/domain-query.js.map +1 -1
  8. package/dist-server/service/domain/domain-types.d.ts +2 -0
  9. package/dist-server/service/domain/domain-types.js +8 -0
  10. package/dist-server/service/domain/domain-types.js.map +1 -1
  11. package/dist-server/service/domain/domain.js +4 -38
  12. package/dist-server/service/domain/domain.js.map +1 -1
  13. package/dist-server/tsconfig.tsbuildinfo +1 -1
  14. package/dist-server/typeorm/json5-transform.js +2 -2
  15. package/dist-server/typeorm/json5-transform.js.map +1 -1
  16. package/dist-server/utils/get-domain.d.ts +3 -10
  17. package/dist-server/utils/get-domain.js +52 -71
  18. package/dist-server/utils/get-domain.js.map +1 -1
  19. package/dist-server/utils/get-query-builder-from-list-params.d.ts +2 -2
  20. package/dist-server/utils/get-query-builder-from-list-params.js +5 -5
  21. package/dist-server/utils/get-query-builder-from-list-params.js.map +1 -1
  22. package/package.json +4 -4
  23. package/server/middlewares/domain-middleware.ts +3 -3
  24. package/server/service/domain/domain-query.ts +11 -4
  25. package/server/service/domain/domain-types.ts +6 -0
  26. package/server/service/domain/domain.ts +4 -39
  27. package/server/typeorm/json5-transform.ts +2 -2
  28. package/server/utils/get-domain.ts +60 -73
  29. package/server/utils/get-query-builder-from-list-params.ts +3 -3
@@ -7,17 +7,11 @@ import {
7
7
  Entity,
8
8
  Index,
9
9
  UpdateDateColumn,
10
- DeleteDateColumn
10
+ DeleteDateColumn,
11
+ PrimaryGeneratedColumn
11
12
  } from 'typeorm'
12
13
  import { ObjectType, Directive, Field, ID } from 'type-graphql'
13
- import { config } from '@things-factory/env'
14
- import { ScalarObject } from '../common-types'
15
-
16
- const numericTypes = ['int', 'int2', 'int4', 'int8', 'integer', 'tinyint', 'smallint', 'mediumint', 'bigint']
17
- const generatedStrategy = ['uuid', 'rowid', 'increment', 'identity']
18
- const domainPrimaryOption = config.get('domainPrimaryOption')
19
- const domainPrimaryType = domainPrimaryOption?.type
20
- const domainPrimaryStrategy = domainPrimaryOption?.strategy
14
+ import { ScalarObject } from '../common-types/scalar-object.js'
21
15
 
22
16
  export type IPList = {
23
17
  whitelist?: string[]
@@ -36,37 +30,8 @@ export type IPList = {
36
30
  })
37
31
  @ObjectType()
38
32
  export class Domain {
33
+ @PrimaryGeneratedColumn('uuid')
39
34
  @Field(type => ID)
40
- @Column(
41
- domainPrimaryOption
42
- ? {
43
- type: domainPrimaryType,
44
- primary: true,
45
- transformer: {
46
- //from(value: DatabaseType): EntityType
47
- from: value => {
48
- return value
49
- },
50
- //to(value: EntityType): DatabaseType
51
- to: value => {
52
- if (!value) {
53
- value = 0
54
- }
55
- if (numericTypes.indexOf(domainPrimaryType) >= 0) {
56
- return parseInt(value)
57
- } else {
58
- return value
59
- }
60
- }
61
- },
62
- generated: generatedStrategy.indexOf(domainPrimaryStrategy) >= 0 ? domainPrimaryStrategy : false
63
- }
64
- : {
65
- type: 'uuid',
66
- primary: true,
67
- generated: 'uuid'
68
- }
69
- )
70
35
  readonly id: string
71
36
 
72
37
  @Field()
@@ -8,7 +8,7 @@ export const json5Transformer: ValueTransformer = {
8
8
  * @returns {string} - The stringified JSON5 representation of the entityValue.
9
9
  */
10
10
  to(entityValue: any) {
11
- return json5.stringify(entityValue)
11
+ return entityValue === null || entityValue === undefined ? null : json5.stringify(entityValue)
12
12
  },
13
13
 
14
14
  /**
@@ -18,7 +18,7 @@ export const json5Transformer: ValueTransformer = {
18
18
  */
19
19
  from(databaseValue: string) {
20
20
  try {
21
- return json5.parse(databaseValue)
21
+ return databaseValue === null ? null : json5.parse(databaseValue)
22
22
  } finally {
23
23
  return databaseValue
24
24
  }
@@ -1,3 +1,4 @@
1
+ import { In } from 'typeorm'
1
2
  import { URL } from 'url'
2
3
 
3
4
  import { config } from '@things-factory/env'
@@ -6,10 +7,9 @@ import { getPathInfo } from '@things-factory/utils'
6
7
  import { getRepository } from '../initializers/database'
7
8
  import { Domain } from '../service/domain/domain'
8
9
 
9
- const useVirtualHostBasedDomain = !!config.get('useVirtualHostBasedDomain')
10
10
  const protocol: string = config.get('protocol')
11
11
  const fixed = config.get('subdomain')
12
- const subdomainOffset = config.getNumber('subdomainOffset', 2)
12
+ const domainTypes = config.get('domainTypes') || ['domain']
13
13
 
14
14
  /**
15
15
  * Creates a URL based on the given context and path.
@@ -51,18 +51,6 @@ export function getUrlFromContext(context, path = '') {
51
51
  return url
52
52
  }
53
53
 
54
- /**
55
- * Extracts subdomains from the Host header.
56
- *
57
- * @param context {Object} An object containing the current request context information.
58
- * @returns {string[]} An array of extracted subdomains.
59
- */
60
- function getSubdomainsFromHost(context: any) {
61
- const { request } = context
62
- var subdomains = request.headers.host.split('.').reverse()
63
- return subdomains.slice(subdomainOffset)
64
- }
65
-
66
54
  /**
67
55
  * Extracts a subdomain from the path.
68
56
  *
@@ -72,9 +60,9 @@ function getSubdomainsFromHost(context: any) {
72
60
  function getSubdomainFromPath(context: any) {
73
61
  var { path } = context
74
62
 
75
- var domain = getPathInfo(path || '')?.domain
76
- if (domain) {
77
- return domain
63
+ var subdomain = getPathInfo(path || '', domainTypes)?.subdomain
64
+ if (subdomain) {
65
+ return subdomain
78
66
  }
79
67
 
80
68
  var {
@@ -83,20 +71,32 @@ function getSubdomainFromPath(context: any) {
83
71
 
84
72
  if (referer) {
85
73
  var { pathname } = new URL(referer)
86
- return getPathInfo(pathname || '')?.domain
74
+ return getPathInfo(pathname || '', domainTypes)?.subdomain
87
75
  }
88
76
  }
89
77
 
90
78
  /**
91
- * Extracts a subdomain based on virtual host.
79
+ * Extracts the subdomain type from the given context's path or referer header.
92
80
  *
93
- * @param context {Object} An object containing the current request context information.
94
- * @returns {string} The extracted subdomain.
81
+ * @param context - The context object containing path and header information.
82
+ * @returns The subdomain type if found, otherwise undefined.
95
83
  */
96
- function getSubdomainFromVhost(context: any) {
97
- const subdomain = (context.subdomains || getSubdomainsFromHost(context)).slice(-1)[0]
84
+ function getSubdomainTypeFromPath(context: any) {
85
+ var { path } = context
98
86
 
99
- return subdomain
87
+ var prefix = getPathInfo(path || '', domainTypes)?.prefix
88
+ if (prefix) {
89
+ return prefix
90
+ }
91
+
92
+ var {
93
+ header: { referer }
94
+ } = context
95
+
96
+ if (referer) {
97
+ var { pathname } = new URL(referer)
98
+ return getPathInfo(pathname || '', domainTypes)?.prefix
99
+ }
100
100
  }
101
101
 
102
102
  /**
@@ -106,7 +106,11 @@ function getSubdomainFromVhost(context: any) {
106
106
  * @returns {string} The extracted subdomain.
107
107
  */
108
108
  function getSubdomainFromURL(context) {
109
- return fixed || (useVirtualHostBasedDomain ? getSubdomainFromVhost(context) : getSubdomainFromPath(context))
109
+ return fixed || getSubdomainFromPath(context)
110
+ }
111
+
112
+ function getSubdomainTypeFromURL(context) {
113
+ return fixed ? '' : getSubdomainTypeFromPath(context)
110
114
  }
111
115
 
112
116
  /**
@@ -119,21 +123,21 @@ export async function getDomainFromURL(context: any): Promise<Domain> {
119
123
  const { header } = context
120
124
 
121
125
  const subdomain = header['x-things-factory-domain'] || getSubdomainFromURL(context)
126
+ const extType = header['x-things-factory-type'] || getSubdomainTypeFromURL(context)
122
127
 
123
128
  if (subdomain) {
124
- return await getRepository(Domain).findOne({ where: { subdomain }, cache: true })
125
- }
126
- }
127
-
128
- /**
129
- * Extracts the cookie domain from the hostname.
130
- *
131
- * @param hostname {string} The hostname.
132
- * @returns {string} The extracted cookie domain.
133
- */
134
- export function getCookieDomainFromHostname(hostname) {
135
- if (useVirtualHostBasedDomain) {
136
- return hostname.split('.').slice(-subdomainOffset).join('.')
129
+ return await getRepository(Domain).findOne({
130
+ where:
131
+ fixed || extType == 'domain' || !extType
132
+ ? {
133
+ subdomain
134
+ }
135
+ : {
136
+ subdomain,
137
+ extType
138
+ },
139
+ cache: true
140
+ })
137
141
  }
138
142
  }
139
143
 
@@ -143,42 +147,37 @@ export function getCookieDomainFromHostname(hostname) {
143
147
  * @param subdomain {string} The subdomain.
144
148
  * @returns {string} The generated context path.
145
149
  */
146
- export function getContextPath(subdomain) {
147
- return fixed || useVirtualHostBasedDomain ? '' : '/domain/' + subdomain
150
+ export function getContextPath(domain: Domain) {
151
+ const type = domain?.extType || 'domain'
152
+ return fixed || !domain ? '' : `${type}/${domain?.subdomain}/`
148
153
  }
149
154
 
150
155
  /**
151
156
  * Generates a redirection path considering the subdomain.
152
157
  *
153
158
  * @param context {Object} An object containing the current request context information.
154
- * @param subdomain {string} The subdomain.
159
+ * @param subdomain {Domain} The target domain.
155
160
  * @param redirectTo {string} The path to redirect to (optional).
156
161
  * @returns {string} The generated redirection path.
157
162
  */
158
- export function getRedirectSubdomainPath(context, subdomain, redirectTo = '/') {
159
- if (fixed) {
163
+ export function getRedirectSubdomainPath(context, domain: Partial<Domain>, redirectTo: string = '/') {
164
+ if (fixed || !domain) {
160
165
  return redirectTo || '/'
161
166
  }
162
167
 
168
+ const type = domain.extType || 'domain'
169
+ const subdomain = domain.subdomain || fixed
170
+
163
171
  var parsed = getUrlFromContext(context, redirectTo)
164
172
  var { hostname, pathname } = parsed
165
173
 
166
- if (useVirtualHostBasedDomain) {
167
- const splitHost = hostname.split('.').reverse()
168
- splitHost[subdomainOffset] = subdomain
169
- parsed.hostname = splitHost
170
- .reverse()
171
- .filter(a => a)
172
- .join('.')
173
- } else {
174
- const contextPath = `/domain/${subdomain}`
175
- const match = pathname.match(/^\/domain\/([^\/]+)/)
174
+ const contextPath = `/${type}/${subdomain}`
175
+ const match = pathname.match(new RegExp(`^/${type}/([^\/]+)`))
176
176
 
177
- if (match) {
178
- parsed.pathname = pathname.replace(match[0], contextPath)
179
- } else {
180
- parsed.pathname = `${contextPath}${pathname}`
181
- }
177
+ if (match) {
178
+ parsed.pathname = pathname.replace(match[0], contextPath)
179
+ } else {
180
+ parsed.pathname = `${contextPath}${pathname}`
182
181
  }
183
182
 
184
183
  return parsed.toString()
@@ -197,13 +196,9 @@ export function findSubdomainFromPath(context, path) {
197
196
  }
198
197
 
199
198
  var parsed = getUrlFromContext(context, path)
200
- var { hostname, pathname } = parsed
199
+ var { pathname } = parsed
201
200
 
202
- if (useVirtualHostBasedDomain) {
203
- return hostname.split('.').reverse()[subdomainOffset]
204
- }
205
-
206
- const match = pathname.match(/^\/domain\/([^\/]+)/)
201
+ const match = pathname.match(new RegExp(`^/${getSubdomainTypeFromURL(context)}/([^\/]+)`))
207
202
  return match && match[1]
208
203
  }
209
204
 
@@ -214,13 +209,5 @@ export function findSubdomainFromPath(context, path) {
214
209
  * @returns {string} The generated site root path.
215
210
  */
216
211
  export function getSiteRootPath(context) {
217
- if (useVirtualHostBasedDomain) {
218
- var { protocol, host } = context
219
- protocol = protocol.replace(':', '')
220
-
221
- let domainname = host.split('.').slice(-subdomainOffset).join('.')
222
- return protocol + '://' + domainname + '/'
223
- } else {
224
- return '/'
225
- }
212
+ return '/'
226
213
  }
@@ -1,7 +1,7 @@
1
1
  import { Brackets, EntityMetadata, Repository, SelectQueryBuilder, WhereExpressionBuilder } from 'typeorm'
2
- import { RelationMetadata } from 'typeorm/metadata/RelationMetadata'
3
- import { Filter, Sorting, Pagination, ListParam, InheritedValueType } from '../service/common-types/list-param'
4
- import { Domain } from '../service/domain/domain'
2
+ import { RelationMetadata } from 'typeorm/metadata/RelationMetadata.js'
3
+ import { Filter, Sorting, Pagination, ListParam, InheritedValueType } from '../service/common-types/list-param.js'
4
+ import { Domain } from '../service/domain/domain.js'
5
5
 
6
6
  /**
7
7
  * Creates a TypeORM SelectQueryBuilder based on the provided parameters.