Skip to content

Commit 7b30a59

Browse files
committed
feat: make http config deep partial
1 parent 24565b2 commit 7b30a59

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/define_config.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010
import proxyAddr from 'proxy-addr'
1111
import string from '@poppinss/utils/string'
1212
import type { ServerConfig } from './types/server.js'
13+
import lodash from '@poppinss/utils/lodash'
1314

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<
1522
Omit<ServerConfig, 'trustProxy'> & {
1623
trustProxy: ((address: string, distance: number) => boolean) | boolean | string
1724
}
@@ -23,7 +30,7 @@ type UserDefinedServerConfig = Partial<
2330
export function defineConfig(config: UserDefinedServerConfig): ServerConfig {
2431
const { trustProxy, ...rest } = config
2532

26-
const normalizedConfig = {
33+
const defaults = {
2734
allowMethodSpoofing: false,
2835
trustProxy: proxyAddr.compile('loopback'),
2936
subdomainOffset: 2,
@@ -53,8 +60,9 @@ export function defineConfig(config: UserDefinedServerConfig): ServerConfig {
5360
skipNulls: false,
5461
},
5562
},
56-
...rest,
57-
}
63+
} satisfies ServerConfig
64+
65+
const normalizedConfig: ServerConfig = lodash.merge({}, defaults, rest)
5866

5967
/**
6068
* Normalizing maxAge property on cookies to be a number in

tests/define_config.spec.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test.group('Define config', () => {
5454
assert.typeOf(config.trustProxy, 'function')
5555
})
5656

57-
test('comfile trustProxy config when string', ({ assert }) => {
57+
test('compile trustProxy config when string', ({ assert }) => {
5858
const config = defineConfig({ trustProxy: 'loopback' })
5959

6060
assert.typeOf(config.trustProxy, 'function')
@@ -65,4 +65,40 @@ test.group('Define config', () => {
6565
const config = defineConfig({ trustProxy: fn })
6666
assert.strictEqual(config.trustProxy, fn)
6767
})
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+
})
68104
})

0 commit comments

Comments
 (0)