@buntal/http 0.0.3 → 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 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 = {
@@ -182,3 +184,4 @@ export class Http {
182
184
 
183
185
  export * from './app'
184
186
  export * from './router'
187
+ 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.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"