File tree 2 files changed +49
-5
lines changed
2 files changed +49
-5
lines changed Original file line number Diff line number Diff line change 10
10
import proxyAddr from 'proxy-addr'
11
11
import string from '@poppinss/utils/string'
12
12
import type { ServerConfig } from './types/server.js'
13
+ import lodash from '@poppinss/utils/lodash'
13
14
14
- type UserDefinedServerConfig = Partial <
15
+ type DeepPartial < T > = T extends object
16
+ ? {
17
+ [ P in keyof T ] ?: DeepPartial < T [ P ] >
18
+ }
19
+ : T
20
+
21
+ type UserDefinedServerConfig = DeepPartial <
15
22
Omit < ServerConfig , 'trustProxy' > & {
16
23
trustProxy : ( ( address : string , distance : number ) => boolean ) | boolean | string
17
24
}
@@ -23,7 +30,7 @@ type UserDefinedServerConfig = Partial<
23
30
export function defineConfig ( config : UserDefinedServerConfig ) : ServerConfig {
24
31
const { trustProxy, ...rest } = config
25
32
26
- const normalizedConfig = {
33
+ const defaults = {
27
34
allowMethodSpoofing : false ,
28
35
trustProxy : proxyAddr . compile ( 'loopback' ) ,
29
36
subdomainOffset : 2 ,
@@ -53,8 +60,9 @@ export function defineConfig(config: UserDefinedServerConfig): ServerConfig {
53
60
skipNulls : false ,
54
61
} ,
55
62
} ,
56
- ...rest ,
57
- }
63
+ } satisfies ServerConfig
64
+
65
+ const normalizedConfig : ServerConfig = lodash . merge ( { } , defaults , rest )
58
66
59
67
/**
60
68
* Normalizing maxAge property on cookies to be a number in
Original file line number Diff line number Diff line change @@ -54,7 +54,7 @@ test.group('Define config', () => {
54
54
assert . typeOf ( config . trustProxy , 'function' )
55
55
} )
56
56
57
- test ( 'comfile trustProxy config when string' , ( { assert } ) => {
57
+ test ( 'compile trustProxy config when string' , ( { assert } ) => {
58
58
const config = defineConfig ( { trustProxy : 'loopback' } )
59
59
60
60
assert . typeOf ( config . trustProxy , 'function' )
@@ -65,4 +65,40 @@ test.group('Define config', () => {
65
65
const config = defineConfig ( { trustProxy : fn } )
66
66
assert . strictEqual ( config . trustProxy , fn )
67
67
} )
68
+
69
+ test ( 'deep merge user values with defaults' , ( { assert } ) => {
70
+ const config = defineConfig ( {
71
+ cookie : {
72
+ httpOnly : false ,
73
+ } ,
74
+ qs : {
75
+ parse : {
76
+ comma : false ,
77
+ } ,
78
+ } ,
79
+ } )
80
+
81
+ assert . deepEqual ( config . cookie , {
82
+ httpOnly : false ,
83
+ path : '/' ,
84
+ maxAge : 7200 ,
85
+ sameSite : 'lax' ,
86
+ secure : true ,
87
+ } )
88
+ assert . deepEqual ( config . qs , {
89
+ parse : {
90
+ depth : 5 ,
91
+ parameterLimit : 1000 ,
92
+ allowSparse : false ,
93
+ arrayLimit : 20 ,
94
+ comma : false ,
95
+ } ,
96
+ stringify : {
97
+ encode : true ,
98
+ encodeValuesOnly : false ,
99
+ arrayFormat : 'indices' as const ,
100
+ skipNulls : false ,
101
+ } ,
102
+ } )
103
+ } )
68
104
} )
You can’t perform that action at this time.
0 commit comments