@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 CHANGED
@@ -1,15 +1,51 @@
1
- # @buntal/http
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
- To install dependencies:
6
+ <br/>
4
7
 
5
- ```bash
6
- bun install
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> &nbsp;&middot; &nbsp;</span>
19
+ <a href="https://buntaljs.org/docs" target="_blank">
20
+ Docs
21
+ </a>
22
+ <span> &nbsp;&middot; &nbsp;</span>
23
+ <a href="https://github.com/sponsors/mgilangjanuar" target="_blank">
24
+ Sponsor &hearts;
25
+ </a>
26
+ </section>
27
+
28
+ <br/>
8
29
 
9
- To run:
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 run index.ts
34
+ bun create buntal my-app
13
35
  ```
14
36
 
15
- This project was created using `bun init` in bun v1.2.16. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
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
@@ -1,5 +1,5 @@
1
- import type { Req } from '../../handler/main/request'
2
- import type { Res } from '../../handler/main/response'
1
+ import type { Req } from './request'
2
+ import type { Res } from './response'
3
3
 
4
4
  export type CookieOptions = {
5
5
  maxAge?: number
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 BaseReq {
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 { Res as BaseRes } from '@buntal/handler'
1
+ import type { BodyInit } from 'bun'
2
2
  import { cookie, type CookieOptions } from './cookie'
3
3
 
4
- export class Res extends BaseRes {
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)
@@ -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'
@@ -0,0 +1,9 @@
1
+ import type { Req, Res } from '../app'
2
+
3
+ export type AtomicHandler<
4
+ P = Record<string, string>,
5
+ T = unknown,
6
+ R = Response | void | undefined | Promise<Response | void | undefined>
7
+ > = {
8
+ (req: Req<P, T>, res: Res): R
9
+ }
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
@@ -0,0 +1,8 @@
1
+ export const ALLOWED_METHODS = [
2
+ 'GET',
3
+ 'POST',
4
+ 'PUT',
5
+ 'PATCH',
6
+ 'DELETE',
7
+ 'OPTIONS'
8
+ ] as const
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buntal/http",
3
- "version": "0.0.3",
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"