mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 07:05:11 +00:00
fix(nuxt): allow updating appConfig
with non-iterable objects (#28773)
This commit is contained in:
parent
2d9e1442d8
commit
3c4949acfb
@ -11,6 +11,15 @@ type DeepPartial<T> = T extends Function ? T : T extends Record<string, any> ? {
|
||||
// Workaround for vite HMR with virtual modules
|
||||
export const _getAppConfig = () => __appConfig as AppConfig
|
||||
|
||||
function isPojoOrArray (val: unknown): val is object {
|
||||
return (
|
||||
Array.isArray(val) ||
|
||||
(!!val &&
|
||||
typeof val === 'object' &&
|
||||
val.constructor?.name === 'Object')
|
||||
)
|
||||
}
|
||||
|
||||
function deepDelete (obj: any, newObj: any) {
|
||||
for (const key in obj) {
|
||||
const val = newObj[key]
|
||||
@ -18,7 +27,7 @@ function deepDelete (obj: any, newObj: any) {
|
||||
delete (obj as any)[key]
|
||||
}
|
||||
|
||||
if (val !== null && typeof val === 'object') {
|
||||
if (isPojoOrArray(val)) {
|
||||
deepDelete(obj[key], newObj[key])
|
||||
}
|
||||
}
|
||||
@ -27,7 +36,7 @@ function deepDelete (obj: any, newObj: any) {
|
||||
function deepAssign (obj: any, newObj: any) {
|
||||
for (const key in newObj) {
|
||||
const val = newObj[key]
|
||||
if (val !== null && typeof val === 'object') {
|
||||
if (isPojoOrArray(val)) {
|
||||
const defaultVal = Array.isArray(val) ? [] : {}
|
||||
obj[key] = obj[key] || defaultVal
|
||||
deepAssign(obj[key], val)
|
||||
|
@ -33,23 +33,33 @@ registerEndpoint('/api/test', defineEventHandler(event => ({
|
||||
describe('app config', () => {
|
||||
it('can be updated', () => {
|
||||
const appConfig = useAppConfig()
|
||||
expect(appConfig).toMatchInlineSnapshot(`
|
||||
{
|
||||
"nuxt": {},
|
||||
}
|
||||
`)
|
||||
updateAppConfig({
|
||||
expect(appConfig).toStrictEqual({ nuxt: {} })
|
||||
|
||||
type UpdateAppConfig = Parameters<typeof updateAppConfig>[0]
|
||||
|
||||
const initConfig: UpdateAppConfig = {
|
||||
new: 'value',
|
||||
nuxt: { nested: 42 },
|
||||
regExp: /foo/g,
|
||||
date: new Date(1111, 11, 11),
|
||||
arr: [1, 2, 3],
|
||||
}
|
||||
updateAppConfig(initConfig)
|
||||
expect(appConfig).toStrictEqual(initConfig)
|
||||
|
||||
const newConfig: UpdateAppConfig = {
|
||||
nuxt: { anotherNested: 24 },
|
||||
regExp: /bar/g,
|
||||
date: new Date(2222, 12, 12),
|
||||
arr: [4, 5],
|
||||
}
|
||||
updateAppConfig(newConfig)
|
||||
expect(appConfig).toStrictEqual({
|
||||
...initConfig,
|
||||
...newConfig,
|
||||
nuxt: { ...initConfig.nuxt, ...newConfig.nuxt },
|
||||
arr: [4, 5, 3],
|
||||
})
|
||||
expect(appConfig).toMatchInlineSnapshot(`
|
||||
{
|
||||
"new": "value",
|
||||
"nuxt": {
|
||||
"nested": 42,
|
||||
},
|
||||
}
|
||||
`)
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user