Skip to content

Commit

Permalink
🔧 fix: can't property is removed when calling deepMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Sep 20, 2023
1 parent 7c51647 commit 9065474
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.7.1 - 20 Sep 2023
Bug Fix:
- Class property is removed when calling deepMerge

# 0.7.0 - 20 Sep 2023
Feature:
- rewrite type
Expand Down
33 changes: 24 additions & 9 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { Elysia, t } from '../src'
import { Elysia } from '../src'

const one = new Elysia({ name: 'one' }).onRequest(() => console.log('Hello, one!'))
const two = new Elysia().use(one)
class Test {
readonly name = 'test'

const three = new Elysia()
.use(one)
.use(two)
.get('/hello', () => 'Hello, world!')
.listen(3000)
public foo() {
return this.name
}
}

// three.handle(new Request('http://localhost:3000/hello'))
const test = new Test()

console.log(test)

export const ctx = new Elysia().decorate('test', test)

const app = new Elysia()
.use(ctx)
.get('/', ({ test }) => {
console.log(test)
console.log(test.foo())
})
.listen(3002)

console.log(`app is listening on ${app.server?.hostname}:${app.server?.port}`)

app.handle(new Request('http://localhost:3002/'))
6 changes: 4 additions & 2 deletions example/trace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Elysia } from '../src'

const logs = []
const sleep = (time = 1000) =>
new Promise((resolve) => setTimeout(resolve, time))

const app = new Elysia()
.trace(async ({ beforeHandle, request, response }) => {
Expand All @@ -10,13 +11,14 @@ const app = new Elysia()

console.log(name, 'took', (await end) - start, 'ms')
}

console.log('beforeHandle took', (await end) - start)
})
.get('/', () => 'Hi', {
beforeHandle: [
function setup() {},
async function delay() {
await new Promise((resolve) => setTimeout(resolve, 1000))
await sleep()
}
]
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "0.7.0",
"version": "0.7.1",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
22 changes: 13 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const isObject = (item: any): item is Object =>
const isClass = (v: Object) =>
typeof v === 'function' && /^\s*class\s+/.test(v.toString())

export const mergeDeep = <const A extends Object, const B extends Object>(
export const mergeDeep = <
const A extends Record<string, any>,
const B extends Record<string, any>
>(
target: A,
source: B,
{
Expand All @@ -24,31 +27,32 @@ export const mergeDeep = <const A extends Object, const B extends Object>(
skipKeys?: string[]
} = {}
): A & B => {
const output: Record<any, any> = Object.assign({}, target)

if (isObject(target) && isObject(source))
for (const [key, value] of Object.entries(source)) {
if (skipKeys?.includes(key)) continue

if (!isObject(value)) {
output[key] = value
target[key as keyof typeof target] = value
continue
}

if (!(key in target)) {
output[key] = value
target[key as keyof typeof target] = value
continue
}

if (key in target && isClass(value)) {
output[key] = value
if (isClass(value)) {
target[key as keyof typeof target] = value
continue
}

output[key] = mergeDeep((target as any)[key] as any, value)
target[key as keyof typeof target] = mergeDeep(
(target as any)[key] as any,
value
)
}

return output as A & B
return target as A & B
}

export const mergeCookie = <const A extends Object, const B extends Object>(
Expand Down

0 comments on commit 9065474

Please sign in to comment.