@buntal/http 0.0.3 → 0.0.5
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/README.md +44 -8
- 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 +4 -2
- package/lib/const.ts +8 -0
- package/package.json +1 -4
package/README.md
CHANGED
|
@@ -1,15 +1,51 @@
|
|
|
1
|
-
|
|
1
|
+
<section align="center">
|
|
2
|
+
<img align="top" src="https://media.tenor.com/yjOrdcOkLPUAAAAj/green-dot.gif" width="22px" height="22px" />
|
|
3
|
+
<span>Early Development Stage</span>
|
|
4
|
+
<section>
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
<br/>
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
<section>
|
|
9
|
+
<img src="https://github.com/mgilangjanuar/buntal/raw/main/banner.png" alt="Buntal JS"/>
|
|
10
|
+
</section>
|
|
11
|
+
|
|
12
|
+
<br/>
|
|
13
|
+
|
|
14
|
+
<section align="center">
|
|
15
|
+
<a href="https://buntaljs.org" target="_blank">
|
|
16
|
+
Website
|
|
17
|
+
</a>
|
|
18
|
+
<span> · </span>
|
|
19
|
+
<a href="https://buntaljs.org/docs" target="_blank">
|
|
20
|
+
Docs
|
|
21
|
+
</a>
|
|
22
|
+
<span> · </span>
|
|
23
|
+
<a href="https://github.com/sponsors/mgilangjanuar" target="_blank">
|
|
24
|
+
Sponsor ♥
|
|
25
|
+
</a>
|
|
26
|
+
</section>
|
|
27
|
+
|
|
28
|
+
<br/>
|
|
8
29
|
|
|
9
|
-
|
|
30
|
+
<section align="left" markdown="1">
|
|
31
|
+
<p>Ultra-lightweight type-safe modern full-stack web framework with TypeScript, React and Bun. Create HTTP servers and/or web apps without unnecessary bloatware.</p>
|
|
10
32
|
|
|
11
33
|
```bash
|
|
12
|
-
bun
|
|
34
|
+
bun create buntal my-app
|
|
13
35
|
```
|
|
14
36
|
|
|
15
|
-
|
|
37
|
+
## Features
|
|
38
|
+
|
|
39
|
+
- **Blazing Fast**: Built on Bun, the fastest JavaScript runtime.
|
|
40
|
+
- **HTTP Server**: Create type-safe HTTP servers with Bun's native HTTP server.
|
|
41
|
+
- **File-based Routing**: Define routes using file structure, similar to Next.js.
|
|
42
|
+
- **SSR**: Server-side rendering for dynamic content.
|
|
43
|
+
- **SPA**: Single Page Application support with Bun's bundler.
|
|
44
|
+
- More to come!
|
|
45
|
+
|
|
46
|
+
View more examples [here](/examples).
|
|
47
|
+
|
|
48
|
+
> [!WARNING]
|
|
49
|
+
> NEVER use Buntal JS in production! Unless you are a pufferfish 🐡
|
|
50
|
+
|
|
51
|
+
</section>
|
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 = {
|
|
@@ -179,6 +181,6 @@ export class Http {
|
|
|
179
181
|
}
|
|
180
182
|
}
|
|
181
183
|
|
|
182
|
-
|
|
183
184
|
export * from './app'
|
|
184
185
|
export * from './router'
|
|
186
|
+
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.5",
|
|
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"
|