@buntal/http 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/cookie.ts +2 -2
- package/app/request.ts +5 -2
- package/app/response.ts +57 -2
- package/handler/index.ts +18 -0
- package/handler/types.ts +9 -0
- package/index.ts +8 -1
- package/lib/const.ts +8 -0
- package/package.json +1 -4
package/app/cookie.ts
CHANGED
package/app/request.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { Req as BaseReq } from '@buntal/handler'
|
|
2
1
|
import { cookie } from './cookie'
|
|
3
2
|
|
|
4
|
-
export class Req<P = Record<string, string>, T = unknown> extends
|
|
3
|
+
export class Req<P = Record<string, string>, T = unknown> extends Request {
|
|
4
|
+
public params: P = {} as P
|
|
5
|
+
public query?: Record<string, string>
|
|
6
|
+
public context?: T
|
|
7
|
+
|
|
5
8
|
get cookies() {
|
|
6
9
|
return cookie.getAll(this)
|
|
7
10
|
}
|
package/app/response.ts
CHANGED
|
@@ -1,7 +1,62 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { BodyInit } from 'bun'
|
|
2
2
|
import { cookie, type CookieOptions } from './cookie'
|
|
3
3
|
|
|
4
|
-
export class Res
|
|
4
|
+
export class Res {
|
|
5
|
+
private options: {
|
|
6
|
+
status: number
|
|
7
|
+
headers: Record<string, string>
|
|
8
|
+
} = {
|
|
9
|
+
status: 200,
|
|
10
|
+
headers: {
|
|
11
|
+
'X-Powered-By': 'Buntal v0.0.1'
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
constructor() {}
|
|
16
|
+
|
|
17
|
+
status(status: number) {
|
|
18
|
+
this.options.status = status
|
|
19
|
+
return this
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
headers(headers: Record<string, string>) {
|
|
23
|
+
this.options.headers = {
|
|
24
|
+
...(this.options.headers || {}),
|
|
25
|
+
...headers
|
|
26
|
+
}
|
|
27
|
+
return this
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
redirect(url: string, status = 302) {
|
|
31
|
+
this.status(status)
|
|
32
|
+
this.headers({
|
|
33
|
+
location: url
|
|
34
|
+
})
|
|
35
|
+
return this.send()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
send(data?: BodyInit) {
|
|
39
|
+
return new Response(data, this.options)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
json(data: unknown) {
|
|
43
|
+
return this.headers({
|
|
44
|
+
'content-type': 'application/json'
|
|
45
|
+
}).send(JSON.stringify(data))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
html(data: string | ReadableStream<Uint8Array>) {
|
|
49
|
+
return this.headers({
|
|
50
|
+
'content-type': 'text/html'
|
|
51
|
+
}).send(data)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
text(data: string) {
|
|
55
|
+
return this.headers({
|
|
56
|
+
'content-type': 'text/plain'
|
|
57
|
+
}).send(data)
|
|
58
|
+
}
|
|
59
|
+
|
|
5
60
|
cookie(name: string, value?: string | null, options?: CookieOptions) {
|
|
6
61
|
if (value) {
|
|
7
62
|
cookie.set(this, name, value, options)
|
package/handler/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Req, Res } from '../app'
|
|
2
|
+
import type { AtomicHandler } from './types'
|
|
3
|
+
|
|
4
|
+
export const h = <P = Record<string, string>, T = unknown>(
|
|
5
|
+
...handlers: AtomicHandler<P, T>[]
|
|
6
|
+
): AtomicHandler<P, T, Response | Promise<Response>> => {
|
|
7
|
+
return async (req: Req<P, T>, res: Res) => {
|
|
8
|
+
for (const handler of handlers) {
|
|
9
|
+
const result = await handler(req, res)
|
|
10
|
+
if (result instanceof Response) {
|
|
11
|
+
return result
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return res.status(204).send()
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export * from './types'
|
package/handler/types.ts
ADDED
package/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { ALLOWED_METHODS, h, Res, type AtomicHandler, type Req } from '@buntal/handler'
|
|
2
1
|
import type { WebSocketHandler } from 'bun'
|
|
2
|
+
import { Req, Res } from './app'
|
|
3
|
+
import { h, type AtomicHandler } from './handler'
|
|
4
|
+
import type { ALLOWED_METHODS } from './lib/const'
|
|
3
5
|
import { buildRouter } from './router'
|
|
4
6
|
|
|
5
7
|
type Config = {
|
|
@@ -178,3 +180,8 @@ export class Http {
|
|
|
178
180
|
}
|
|
179
181
|
}
|
|
180
182
|
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
export * from './app'
|
|
186
|
+
export * from './router'
|
|
187
|
+
export * from './handler'
|
package/lib/const.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buntal/http",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"module": "index.ts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"devDependencies": {
|
|
@@ -9,9 +9,6 @@
|
|
|
9
9
|
"peerDependencies": {
|
|
10
10
|
"typescript": "^5"
|
|
11
11
|
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@buntal/handler": "latest"
|
|
14
|
-
},
|
|
15
12
|
"repository": {
|
|
16
13
|
"type": "git",
|
|
17
14
|
"url": "https://github.com/mgilangjanuar/buntal.git"
|