Skip to content

Commit 2d78fce

Browse files
app.server null fixed
1 parent 48ab45e commit 2d78fce

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

bun.lockb

1.59 KB
Binary file not shown.

example/basic.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Elysia } from "elysia"
2+
import { swagger } from "@elysiajs/swagger"
3+
4+
import { ip } from "../src"
5+
import { Server } from "bun"
6+
7+
let server: Server | null
8+
9+
const app = new Elysia()
10+
.use(swagger())
11+
.use(ip())
12+
.get("/", ({ ip }) => ({ hello: ip }))
13+
.listen(3000, () => {
14+
console.log("🦊 Swagger is active at: http://localhost:3000/swagger")
15+
})
16+
17+
server = app.server

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"ip"
2828
],
2929
"scripts": {
30+
"dev": "bun run --hot example/basic.ts",
3031
"build": "rm -rf dist && tsc --project tsconfig.esm.json",
3132
"lint": "eslint . --ext .ts"
3233
},
@@ -36,10 +37,13 @@
3637
"@typescript-eslint/eslint-plugin": "^7.8.0",
3738
"@typescript-eslint/parser": "^7.8.0",
3839
"elysia": "^1.0.16",
39-
"eslint": "^9.1.1",
40+
"eslint": "^9.2.0",
4041
"typescript": "^5.4.5"
4142
},
4243
"peerDependencies": {
4344
"elysia": ">= 1.0.9"
45+
},
46+
"dependencies": {
47+
"@elysiajs/swagger": "^1.0.5"
4448
}
4549
}

src/services/plugin.ts

+23-21
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ export const plugin = (userOptions?: Partial<Options>) => (app: Elysia) => {
99
...userOptions,
1010
}
1111

12-
return new Elysia({
13-
name: "elysia-ip",
14-
}).derive({ as: "global" }, ({ request }): { ip: string } => {
15-
if (!options.headersOnly && globalThis.Bun) {
16-
const socketAddress = options.injectServer(app)?.requestIP(request)
17-
if (socketAddress) return { ip: socketAddress.address }
18-
else {
19-
console.log(
20-
`Elysia server is not initialized. Make sure to call Elyisa.listen()`,
21-
)
22-
console.log(`use injectServer to inject Server instance`)
12+
return app.use(
13+
new Elysia({
14+
name: "elysia-ip",
15+
}).derive({ as: "global" }, ({ request }): { ip: string } => {
16+
if (!options.headersOnly && globalThis.Bun) {
17+
const socketAddress = options.injectServer(app)?.requestIP(request)
18+
if (socketAddress) return { ip: socketAddress.address }
19+
else {
20+
console.log(
21+
`Elysia server is not initialized. Make sure to call Elyisa.listen()`,
22+
)
23+
console.log(`use injectServer to inject Server instance`)
24+
}
2325
}
24-
}
25-
return {
26-
ip:
27-
getIP(request.headers, options.checkHeaders) ||
28-
(function () {
29-
console.log("Failed to get ip from header!")
30-
return ""
31-
})(),
32-
}
33-
})
26+
return {
27+
ip:
28+
getIP(request.headers, options.checkHeaders) ||
29+
(function () {
30+
console.log("Failed to get ip from header!")
31+
return ""
32+
})(),
33+
}
34+
}),
35+
)
3436
}

0 commit comments

Comments
 (0)