badak 1.0.0 → 1.0.2
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/.mocharc.json +9 -0
- package/CHANGELOG.md +0 -0
- package/README-KR.md +157 -46
- package/README.md +246 -3
- package/build/badak.d.ts +38 -38
- package/build/{badak.js → cjs/badak.js} +982 -949
- package/build/cjs/badak.js.map +1 -0
- package/build/cjs/constants.js +35 -0
- package/build/cjs/constants.js.map +1 -0
- package/build/cjs/index.js +8 -0
- package/build/cjs/index.js.map +1 -0
- package/build/{interfaces.js → cjs/interfaces.js} +2 -2
- package/build/{interfaces.js.map → cjs/interfaces.js.map} +1 -1
- package/build/cjs/package.json +1 -0
- package/build/{util.js → cjs/util.js} +184 -165
- package/build/cjs/util.js.map +1 -0
- package/build/constants.d.ts +25 -6
- package/build/esm/badak.js +969 -0
- package/build/esm/badak.js.map +1 -0
- package/build/esm/constants.js +35 -0
- package/build/esm/constants.js.map +1 -0
- package/build/esm/index.js +8 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/interfaces.js +3 -0
- package/build/esm/interfaces.js.map +1 -0
- package/build/esm/package.json +1 -0
- package/build/esm/util.js +199 -0
- package/build/esm/util.js.map +1 -0
- package/build/{public_api.d.ts → index.d.ts} +3 -3
- package/build/interfaces.d.ts +54 -53
- package/build/scripts/add-js-extensions.js +46 -0
- package/build/scripts/add-js-extensions.js.map +1 -0
- package/build/util.d.ts +10 -8
- package/eslint.config.mjs +101 -0
- package/package.json +28 -24
- package/sample/index.ts +3 -3
- package/scripts/add-js-extensions.ts +59 -0
- package/spec/badak.spec.ts +397 -3628
- package/spec/config.spec.ts +707 -0
- package/spec/middleware.spec.ts +63 -57
- package/spec/route.spec.ts +2134 -0
- package/spec/setSPARoot.spec.ts +259 -0
- package/spec/static.spec.ts +386 -0
- package/spec/test-util.ts +37 -0
- package/src/badak.ts +256 -187
- package/src/constants.ts +29 -2
- package/src/{public_api.ts → index.ts} +1 -1
- package/src/interfaces.ts +16 -12
- package/src/util.ts +43 -20
- package/tsconfig.cjs.json +8 -0
- package/tsconfig.esm.json +8 -0
- package/tsconfig.json +9 -8
- package/tsconfig.scripts.json +15 -0
- package/tsconfig.types.json +11 -0
- package/.eslintrc.json +0 -81
- package/build/badak.js.map +0 -1
- package/build/constants.js +0 -11
- package/build/constants.js.map +0 -1
- package/build/public_api.js +0 -8
- package/build/public_api.js.map +0 -1
- package/build/util.js.map +0 -1
- package/scripts/cp_static_files.js +0 -29
- package/tsconfig.spec.json +0 -14
package/.mocharc.json
ADDED
package/CHANGELOG.md
ADDED
|
File without changes
|
package/README-KR.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
|
|
5
5
|
# badak
|
|
6
6
|
|
|
7
7
|
badak은 Promise, TypeScript 기반으로 개발된 백엔드 프레임워크입니다.
|
|
8
8
|
|
|
9
|
-
> badak은 TypeScript로 작성되었으며 JavaScript 코드로 컴파일되어 배포됩니다.
|
|
9
|
+
> badak은 TypeScript로 작성되었으며 JavaScript 코드로 컴파일되어 배포됩니다.
|
|
10
|
+
> 따라서 TypeScript 문법으로 사용할 수도 있고 JavaScript 문법으로 사용할 수도 있습니다.
|
|
10
11
|
|
|
11
12
|
## 설치방법
|
|
12
13
|
|
|
@@ -14,10 +15,8 @@ badak은 npm으로 배포되며 npm/yarn 으로 설치할 수 있습니다.
|
|
|
14
15
|
|
|
15
16
|
```bash
|
|
16
17
|
npm install badak
|
|
17
|
-
|
|
18
18
|
# 또는
|
|
19
|
-
|
|
20
|
-
yarn install badak
|
|
19
|
+
yarn add badak
|
|
21
20
|
```
|
|
22
21
|
|
|
23
22
|
## 사용방법
|
|
@@ -25,26 +24,25 @@ yarn install badak
|
|
|
25
24
|
```typescript
|
|
26
25
|
import { Badak } from 'badak';
|
|
27
26
|
|
|
28
|
-
const rule
|
|
29
|
-
users
|
|
30
|
-
GET
|
|
31
|
-
//
|
|
27
|
+
const rule: RouteRule = {
|
|
28
|
+
users: {
|
|
29
|
+
GET: () => {
|
|
30
|
+
// 사용자 목록을 반환합니다.
|
|
32
31
|
return {
|
|
33
|
-
list
|
|
32
|
+
list: ['han', 'kim', 'lee']
|
|
34
33
|
}
|
|
35
34
|
},
|
|
36
|
-
POST
|
|
37
|
-
//
|
|
38
|
-
|
|
35
|
+
POST: (user: User) => {
|
|
36
|
+
// 사용자를 추가합니다.
|
|
39
37
|
return {
|
|
40
|
-
result
|
|
38
|
+
result: 'ok'
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
}
|
|
44
42
|
};
|
|
45
43
|
|
|
46
44
|
|
|
47
|
-
const app
|
|
45
|
+
const app: Badak = new Badak();
|
|
48
46
|
await app.route(routeRule);
|
|
49
47
|
await app.listen(3000);
|
|
50
48
|
```
|
|
@@ -56,80 +54,193 @@ await app.listen(3000);
|
|
|
56
54
|
badak 인스턴스를 생성합니다. 생성자로 옵션을 전달할 수 있습니다.
|
|
57
55
|
|
|
58
56
|
```typescript
|
|
59
|
-
const app
|
|
57
|
+
const app: Badak = new Badak();
|
|
60
58
|
|
|
61
|
-
const appWithLog
|
|
62
|
-
catchErrorLog
|
|
59
|
+
const appWithLog: Badak = new Badak({
|
|
60
|
+
catchErrorLog: true
|
|
63
61
|
});
|
|
64
62
|
```
|
|
65
63
|
|
|
66
|
-
### `auth()
|
|
64
|
+
### `auth(fnc: MiddlewareFunction): Promise<void>`
|
|
65
|
+
|
|
66
|
+
인증용 미들웨어 함수를 지정합니다. 인증 함수는 하나만 지정할 수 있습니다.
|
|
67
|
+
인증 미들웨어로 등록된 함수는 `before()` 이후에, 라우팅 함수가 실행되기 전에 실행됩니다.
|
|
68
|
+
인증 미들웨어 함수에서 오류가 발생하면 `401 Unauthorized` 에러가 발생합니다.
|
|
69
|
+
|
|
70
|
+
### `before(fnc: MiddlewareFunction): Promise<void>`
|
|
71
|
+
|
|
72
|
+
라우팅 함수가 실행되기 전에 실행할 미들웨어 함수를 등록합니다.
|
|
73
|
+
미들웨어 함수는 여러개를 지정할 수 있으며, 지정한 순서와 관계없이 병렬로 실행됩니다.
|
|
74
|
+
|
|
75
|
+
### `after(fnc: MiddlewareFunction): Promise<void>`
|
|
76
|
+
|
|
77
|
+
라우팅 함수가 실행된 후에 실행할 미들웨어 함수를 등록합니다.
|
|
78
|
+
미들웨어 함수는 여러개를 지정할 수 있으며, 지정한 순서와 관계없이 병렬로 실행됩니다.
|
|
79
|
+
|
|
80
|
+
### `config(key: string, value: unknown): Promise<void>`
|
|
81
|
+
|
|
82
|
+
badak 앱이 실행되는 동작을 조정합니다.
|
|
83
|
+
|
|
84
|
+
### `listen(port: number): Promise<void>`
|
|
85
|
+
|
|
86
|
+
서버 응답을 시작합니다.
|
|
87
|
+
|
|
88
|
+
### `isRunning(): boolean`
|
|
89
|
+
|
|
90
|
+
서버가 실행중인지 확인하고 결과값을 반환합니다.
|
|
91
|
+
초기 상태는 `false` 이며, `listen()` 함수가 실행된 이후에 `true`가 되고, `stop()`으로 종료한 후에는 `false`가 됩니다.
|
|
92
|
+
|
|
93
|
+
### `getHttpServer(): Server | undefined`
|
|
67
94
|
|
|
68
|
-
|
|
95
|
+
HTTP 서버 인스턴스를 반환합니다.
|
|
96
|
+
Badak 내부 멤버에 접근할 때 사용합니다.
|
|
69
97
|
|
|
70
|
-
### `
|
|
98
|
+
### `stop(): Promise<void>`
|
|
71
99
|
|
|
72
|
-
|
|
100
|
+
HTTP 서버를 정지합니다.
|
|
73
101
|
|
|
74
|
-
### `
|
|
102
|
+
### `route(rule: RouteRule): Promise<void>`
|
|
75
103
|
|
|
76
|
-
|
|
104
|
+
라우팅 함수를 등록합니다.
|
|
77
105
|
|
|
78
|
-
### `
|
|
106
|
+
### `get(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>`
|
|
79
107
|
|
|
80
|
-
|
|
108
|
+
`GET` 메서드 요청에 반응하는 라우팅 함수를 등록합니다.
|
|
81
109
|
|
|
82
|
-
### `
|
|
110
|
+
### `post(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>`
|
|
83
111
|
|
|
84
|
-
|
|
112
|
+
`POST` 메서드 요청에 반응하는 라우팅 함수를 등록합니다.
|
|
85
113
|
|
|
86
|
-
### `
|
|
114
|
+
### `put(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>`
|
|
87
115
|
|
|
88
|
-
|
|
116
|
+
`PUT` 메서드 요청에 반응하는 라우팅 함수를 등록합니다.
|
|
89
117
|
|
|
90
|
-
### `delete(address
|
|
118
|
+
### `delete(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>`
|
|
91
119
|
|
|
92
|
-
|
|
120
|
+
`DELETE` 메서드 요청에 반응하는 라우팅 함수를 등록합니다.
|
|
93
121
|
|
|
94
|
-
### `
|
|
122
|
+
### `static(uri: string, path: string): Promise<void>`
|
|
123
|
+
|
|
124
|
+
정적 파일로 제공할 주소와 파일 경로를 연결합니다.
|
|
125
|
+
|
|
126
|
+
### `setSPARoot(uri: string, path: string): Promise<void>`
|
|
127
|
+
|
|
128
|
+
SPA 앱으로 사용할 주소와 파일 경로를 연결합니다.
|
|
129
|
+
지정된 폴더 안에 있는 파일은 정적으로 제공되며, 캐싱됩니다.
|
|
95
130
|
|
|
96
131
|
## 인터페이스
|
|
97
132
|
|
|
133
|
+
### `MiddlewareFunction`
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
export type MiddlewareFunction = (req: IncomingMessage, res: ServerResponse, responseBody?: unknown) => void | Promise<void>;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`before()`, `after()`에 등록하는 함수입니다.
|
|
140
|
+
미들웨어 함수는 라우팅 함수에 전달되는 인자를 변경할 수 없습니다.
|
|
141
|
+
|
|
142
|
+
### `RouteFunction`
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
export type RouteFunction = (param: any, req: IncomingMessage, res: ServerResponse) => unknown | Promise<unknown>;
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
HTTP 요청에 반응할 라우팅 함수를 등록합니다.
|
|
149
|
+
라우팅 함수가 반환한 값은 HTTP 응답으로 반환됩니다.
|
|
150
|
+
|
|
98
151
|
### `BadakOption`
|
|
99
152
|
|
|
100
|
-
|
|
153
|
+
badak 앱의 동작 방식을 지정합니다.
|
|
101
154
|
|
|
102
|
-
#### `catchErrorLog
|
|
155
|
+
#### `catchErrorLog: boolean`
|
|
103
156
|
|
|
104
157
|
에러가 발생하면 콘솔에 로그를 출력합니다.
|
|
105
158
|
|
|
106
159
|
기본값은 `true` 입니다.
|
|
107
160
|
|
|
108
|
-
#### `preventError
|
|
161
|
+
#### `preventError: boolean`
|
|
109
162
|
|
|
110
|
-
에러가 발생하면 node로 전달합니다.
|
|
163
|
+
에러가 발생하면 node로 전달합니다.
|
|
164
|
+
`false` 값을 지정하면 에러를 전달하지 않고 무시합니다.
|
|
111
165
|
|
|
112
166
|
기본값은 `true` 입니다.
|
|
113
167
|
|
|
114
|
-
#### `defaultMethod
|
|
168
|
+
#### `defaultMethod: METHOD | undefined`
|
|
115
169
|
|
|
116
|
-
라우팅에 사용할 기본 HTTP 메서드 타입을 지정합니다.
|
|
170
|
+
라우팅에 사용할 기본 HTTP 메서드 타입을 지정합니다.
|
|
171
|
+
이 값을 지정하면 `Badak.route()` 메서드를 사용할 때 HTTP 메서드 지정을 생략할 수 있습니다.
|
|
117
172
|
|
|
118
173
|
- 지정 전 :
|
|
119
174
|
|
|
120
175
|
```typescript
|
|
121
|
-
{
|
|
122
|
-
|
|
123
|
-
GET
|
|
176
|
+
app.route({
|
|
177
|
+
users: {
|
|
178
|
+
GET: getUserList
|
|
124
179
|
}
|
|
125
|
-
}
|
|
180
|
+
});
|
|
126
181
|
```
|
|
127
182
|
|
|
128
183
|
- `app.config('defaultMethod', 'GET')` 지정 후 :
|
|
129
184
|
|
|
130
185
|
```typescript
|
|
131
|
-
{
|
|
132
|
-
|
|
133
|
-
}
|
|
186
|
+
app.route({
|
|
187
|
+
users: getUserList
|
|
188
|
+
});
|
|
134
189
|
```
|
|
135
190
|
|
|
191
|
+
#### `parseNumber: boolean`
|
|
192
|
+
|
|
193
|
+
인자로 전달되는 문자열 형태의 숫자를 `number` 타입으로 변환합니다.
|
|
194
|
+
|
|
195
|
+
기본값은 `false` 입니다.
|
|
196
|
+
|
|
197
|
+
#### `parseDate: boolean`
|
|
198
|
+
|
|
199
|
+
인자로 전달되는 문자열 형태의 날짜를 `Date` 타입으로 변환합니다.
|
|
200
|
+
|
|
201
|
+
기본값은 `false` 입니다.
|
|
202
|
+
|
|
203
|
+
### `RouteOption`
|
|
204
|
+
|
|
205
|
+
라우팅 함수의 동작을 결정합니다.
|
|
206
|
+
|
|
207
|
+
#### `auth: boolean`
|
|
208
|
+
|
|
209
|
+
`false`로 지정하면 이 라우팅 함수가 실행될 때 `auth()` 함수를 실행하지 않습니다.
|
|
210
|
+
|
|
211
|
+
기본값은 `true` 입니다.
|
|
212
|
+
|
|
213
|
+
## 특수 라우팅
|
|
214
|
+
|
|
215
|
+
`route()`나 `get()`, `post()`, `put()`, `delete()` 함수의 경로에 특수 라우팅을 사용할 수 있습니다.
|
|
216
|
+
|
|
217
|
+
### `:` 라우팅
|
|
218
|
+
|
|
219
|
+
`/:userId`로 라우팅 경로를 지정하고 `/user1`로 HTTP 요청을 보내면, 요청이 매칭되며 라우팅 함수의 인자로 `{ userId: 'user1' }`이 전달됩니다.
|
|
220
|
+
|
|
221
|
+
### `?` 라우팅
|
|
222
|
+
|
|
223
|
+
`/users?`로 라우팅 경로를 지정하면 `/user`와 `/users` 경로를 매칭할 수 있습니다.
|
|
224
|
+
|
|
225
|
+
### `+` 라우팅
|
|
226
|
+
|
|
227
|
+
`/user1+`로 라우팅 경로를 지정하면 `/user1`과 `/user11` 등의 경로를 매칭할 수 있습니다.
|
|
228
|
+
|
|
229
|
+
### `*` 라우팅
|
|
230
|
+
|
|
231
|
+
`/user1*`로 라우팅 경로를 지정하면 `/user`와 `/user1`, `/user11` 등의 경로를 매칭할 수 있습니다.
|
|
232
|
+
|
|
233
|
+
## 라우팅 인자
|
|
234
|
+
|
|
235
|
+
### 쿼리 스트링
|
|
236
|
+
|
|
237
|
+
`GET` 메서드를 요청하는 경우 쿼리 스트링 인자를 파싱해서 라우팅 함수로 전달합니다.
|
|
238
|
+
|
|
239
|
+
아래 예제는 모두 `/path` 경로와 매칭되며 전달되는 인자는 각각 이렇습니다.
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
/path? # {}
|
|
243
|
+
/path?key # { key: null }
|
|
244
|
+
/path?key= # { key: '' }
|
|
245
|
+
/path?key=value # { key: 'value' }
|
|
246
|
+
```
|
package/README.md
CHANGED
|
@@ -1,9 +1,252 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[한국어](https://github.com/han41858/badak/blob/main/README-KR.md)
|
|
6
6
|
|
|
7
7
|
# badak
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
badak is a backend framework developed based on Promises and TypeScript.
|
|
10
|
+
|
|
11
|
+
> badak is written in TypeScript and is compiled and distributed as JavaScript code.
|
|
12
|
+
> So, it can be used in TypeScript syntax or JavaScript syntax.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
badak is distributed with npm and can be installed with npm/yarn.
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install badak
|
|
20
|
+
# or
|
|
21
|
+
yarn add badak
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { Badak } from 'badak';
|
|
28
|
+
|
|
29
|
+
const rule: RouteRule = {
|
|
30
|
+
users: {
|
|
31
|
+
GET: () => {
|
|
32
|
+
// returns a list of users.
|
|
33
|
+
return {
|
|
34
|
+
list: ['han', 'kim', 'lee']
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
POST: (user: User) => {
|
|
38
|
+
// add a user.
|
|
39
|
+
return {
|
|
40
|
+
result: 'ok'
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const app: Badak = new Badak();
|
|
48
|
+
await app.route(routeRule);
|
|
49
|
+
await app.listen(3000);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Methods
|
|
53
|
+
|
|
54
|
+
### `new Badak(option?: Partial<BadakOption>)`
|
|
55
|
+
|
|
56
|
+
Create a badak instance. You can pass options to the constructor.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
const app: Badak = new Badak();
|
|
60
|
+
|
|
61
|
+
const appWithLog: Badak = new Badak({
|
|
62
|
+
catchErrorLog: true
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### `auth(fnc: MiddlewareFunction): Promise<void>`
|
|
67
|
+
|
|
68
|
+
Set a middleware function for authentication. Only one authentication function can be specified.
|
|
69
|
+
The registered function is executed after `before()` and before the routing function is executed.
|
|
70
|
+
If an error occurs in this middleware function, a `401 Unauthorized` error occurs.
|
|
71
|
+
|
|
72
|
+
### `before(fnc: MiddlewareFunction): Promise<void>`
|
|
73
|
+
|
|
74
|
+
Registers a middleware function to run **before** the routing function is executed.
|
|
75
|
+
Multiple middleware functions can be specified,
|
|
76
|
+
and they are executed in parallel regardless of the order in which they are specified.
|
|
77
|
+
|
|
78
|
+
### `after(fnc: MiddlewareFunction): Promise<void>`
|
|
79
|
+
|
|
80
|
+
Registers a middleware function to run **after** the routing function is executed.
|
|
81
|
+
Multiple middleware functions can be specified,
|
|
82
|
+
and they are executed in parallel regardless of the order in which they are specified.
|
|
83
|
+
|
|
84
|
+
### `config(key: string, value: unknown): Promise<void>`
|
|
85
|
+
|
|
86
|
+
Controls how badak apps run.
|
|
87
|
+
|
|
88
|
+
### `listen(port: number): Promise<void>`
|
|
89
|
+
|
|
90
|
+
The server starts responding.
|
|
91
|
+
|
|
92
|
+
### `isRunning(): boolean`
|
|
93
|
+
|
|
94
|
+
Checks if the server is running and returns the results.
|
|
95
|
+
The initial state is `false`, becomes `true` after the `listen()` function is executed,
|
|
96
|
+
and becomes `false` after terminating with `stop()`.
|
|
97
|
+
|
|
98
|
+
### `getHttpServer(): Server | undefined`
|
|
99
|
+
|
|
100
|
+
Returns the HTTP server instance.
|
|
101
|
+
Used to access badak internal members.
|
|
102
|
+
|
|
103
|
+
### `stop(): Promise<void>`
|
|
104
|
+
|
|
105
|
+
Stop the HTTP server.
|
|
106
|
+
|
|
107
|
+
### `route(rule: RouteRule): Promise<void>`
|
|
108
|
+
|
|
109
|
+
Register a routing function.
|
|
110
|
+
|
|
111
|
+
### `get(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>`
|
|
112
|
+
|
|
113
|
+
Registers a routing function that responds to `GET` method requests.
|
|
114
|
+
|
|
115
|
+
### `post(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>`
|
|
116
|
+
|
|
117
|
+
Registers a routing function that responds to `POST` method requests.
|
|
118
|
+
|
|
119
|
+
### `put(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>`
|
|
120
|
+
|
|
121
|
+
Registers a routing function that responds to `PUT` method requests.
|
|
122
|
+
|
|
123
|
+
### `delete(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>`
|
|
124
|
+
|
|
125
|
+
Registers a routing function that responds to `DELETE` method requests.
|
|
126
|
+
|
|
127
|
+
### `static(uri: string, path: string): Promise<void>`
|
|
128
|
+
|
|
129
|
+
Concatenate the address and file path to be provided as a static file.
|
|
130
|
+
|
|
131
|
+
### `setSPARoot(uri: string, path: string): Promise<void>`
|
|
132
|
+
|
|
133
|
+
Connect the address and file path to be used with the SPA app.
|
|
134
|
+
Files within the specified folder are served statically and cached.
|
|
135
|
+
|
|
136
|
+
## 인터페이스
|
|
137
|
+
|
|
138
|
+
### `MiddlewareFunction`
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
export type MiddlewareFunction = (req: IncomingMessage, res: ServerResponse, responseBody?: unknown) => void | Promise<void>;
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
This is a function registered in `before()` and `after()`.
|
|
145
|
+
Middleware functions cannot change the arguments passed to the routing function.
|
|
146
|
+
|
|
147
|
+
### `RouteFunction`
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
export type RouteFunction = (param: any, req: IncomingMessage, res: ServerResponse) => unknown | Promise<unknown>;
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Register a routing function to respond to HTTP requests.
|
|
154
|
+
The value returned by the routing function is returned as an HTTP response.
|
|
155
|
+
|
|
156
|
+
### `BadakOption`
|
|
157
|
+
|
|
158
|
+
Specifies how the badak app should behave.
|
|
159
|
+
|
|
160
|
+
#### `catchErrorLog: boolean`
|
|
161
|
+
|
|
162
|
+
If an error occurs, a log is output to the console.
|
|
163
|
+
|
|
164
|
+
The default is `true`.
|
|
165
|
+
|
|
166
|
+
#### `preventError: boolean`
|
|
167
|
+
|
|
168
|
+
If an error occurs, it is transmitted to the node.
|
|
169
|
+
If you specify a value of `false`, the error will not be passed and will be ignored.
|
|
170
|
+
|
|
171
|
+
The default is `true`.
|
|
172
|
+
|
|
173
|
+
#### `defaultMethod: METHOD | undefined`
|
|
174
|
+
|
|
175
|
+
Specifies the default HTTP method type to use for routing.
|
|
176
|
+
Specifying this value allows you to omit specifying the HTTP method when using the `Badak.route()` method.
|
|
177
|
+
|
|
178
|
+
- before :
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
app.route({
|
|
182
|
+
users: {
|
|
183
|
+
GET: getUserList
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
- after `app.config('defaultMethod', 'GET')` :
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
app.route({
|
|
192
|
+
users: getUserList
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### `parseNumber: boolean`
|
|
197
|
+
|
|
198
|
+
Converts the string-type number passed as an argument to the `number` type.
|
|
199
|
+
|
|
200
|
+
The default is `false`.
|
|
201
|
+
|
|
202
|
+
#### `parseDate: boolean`
|
|
203
|
+
|
|
204
|
+
Converts the date in string format passed as an argument to the `Date` type.
|
|
205
|
+
|
|
206
|
+
The default is `false`.
|
|
207
|
+
|
|
208
|
+
### `RouteOption`
|
|
209
|
+
|
|
210
|
+
Determines the behavior of routing functions.
|
|
211
|
+
|
|
212
|
+
#### `auth: boolean`
|
|
213
|
+
|
|
214
|
+
If specified as `false`, the `auth()` function will not be executed when this routing function is executed.
|
|
215
|
+
|
|
216
|
+
The default is `true`.
|
|
217
|
+
|
|
218
|
+
## Special Routing
|
|
219
|
+
|
|
220
|
+
Special routing can be used on the routes of the `route()`, `get()`, `post()`, `put()`, and `delete()` functions.
|
|
221
|
+
|
|
222
|
+
### `:` routing
|
|
223
|
+
|
|
224
|
+
If you specify a routing path with `/:userId` and send an HTTP request to `/user1`,
|
|
225
|
+
the request is matched and `{ userId: 'user1' }` is passed as an argument to the routing function.
|
|
226
|
+
|
|
227
|
+
### `?` routing
|
|
228
|
+
|
|
229
|
+
If you specify the routing path as `/users?`, you can match the `/user` and `/users` paths.
|
|
230
|
+
|
|
231
|
+
### `+` routing
|
|
232
|
+
|
|
233
|
+
If you specify the routing path as `/user1+`, you can match paths such as `/user1` and `/user11`.
|
|
234
|
+
|
|
235
|
+
### `*` routing
|
|
236
|
+
|
|
237
|
+
If you specify a routing path as `/user1*`, you can match paths such as `/user`, `/user1`, and `/user11`.
|
|
238
|
+
|
|
239
|
+
## Routing Param
|
|
240
|
+
|
|
241
|
+
### query string
|
|
242
|
+
|
|
243
|
+
When requesting the `GET` method, the query string argument is parsed and passed to the routing function.
|
|
244
|
+
|
|
245
|
+
The examples below all match the `/path` path, and the arguments passed are as follows.
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
/path? # {}
|
|
249
|
+
/path?key # { key: null }
|
|
250
|
+
/path?key= # { key: '' }
|
|
251
|
+
/path?key=value # { key: 'value' }
|
|
252
|
+
```
|
package/build/badak.d.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { Server } from 'net';
|
|
3
|
-
import { BadakOption, MiddlewareFunction, RouteFunction, RouteOption, RouteRule } from './interfaces';
|
|
4
|
-
export declare class Badak {
|
|
5
|
-
private _http;
|
|
6
|
-
private _authFnc;
|
|
7
|
-
private _middlewaresBefore;
|
|
8
|
-
private _middlewaresAfter;
|
|
9
|
-
private _routeRules;
|
|
10
|
-
private _staticRules;
|
|
11
|
-
private _staticCache;
|
|
12
|
-
private _spaRoot;
|
|
13
|
-
private _config;
|
|
14
|
-
constructor(option?: Partial<BadakOption>);
|
|
15
|
-
private _refineRouteRule;
|
|
16
|
-
private getUriKeyArr;
|
|
17
|
-
private _checkUriDuplication;
|
|
18
|
-
private _assignRule;
|
|
19
|
-
auth(fnc: MiddlewareFunction): Promise<void>;
|
|
20
|
-
before(middleware: MiddlewareFunction): Promise<void>;
|
|
21
|
-
after(middleware: MiddlewareFunction): Promise<void>;
|
|
22
|
-
config(key: string, value: unknown): Promise<void>;
|
|
23
|
-
private _routeAbbrValidator;
|
|
24
|
-
get(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>;
|
|
25
|
-
post(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>;
|
|
26
|
-
put(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>;
|
|
27
|
-
delete(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>;
|
|
28
|
-
private _paramConverter;
|
|
29
|
-
private _paramParser;
|
|
30
|
-
private _static;
|
|
31
|
-
static(uri: string, path: string): Promise<void>;
|
|
32
|
-
route(rule: RouteRule): Promise<void>;
|
|
33
|
-
setSPARoot(uri: string, path: string): Promise<void>;
|
|
34
|
-
listen(port: number): Promise<void>;
|
|
35
|
-
isRunning(): boolean;
|
|
36
|
-
getHttpServer(): Server | undefined;
|
|
37
|
-
stop(): Promise<void>;
|
|
38
|
-
}
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Server } from 'node:net';
|
|
3
|
+
import { BadakOption, MiddlewareFunction, RouteFunction, RouteOption, RouteRule } from './interfaces';
|
|
4
|
+
export declare class Badak {
|
|
5
|
+
private _http;
|
|
6
|
+
private _authFnc;
|
|
7
|
+
private _middlewaresBefore;
|
|
8
|
+
private _middlewaresAfter;
|
|
9
|
+
private _routeRules;
|
|
10
|
+
private _staticRules;
|
|
11
|
+
private _staticCache;
|
|
12
|
+
private _spaRoot;
|
|
13
|
+
private _config;
|
|
14
|
+
constructor(option?: Partial<BadakOption>);
|
|
15
|
+
private _refineRouteRule;
|
|
16
|
+
private getUriKeyArr;
|
|
17
|
+
private _checkUriDuplication;
|
|
18
|
+
private _assignRule;
|
|
19
|
+
auth(fnc: MiddlewareFunction): Promise<void>;
|
|
20
|
+
before(middleware: MiddlewareFunction): Promise<void>;
|
|
21
|
+
after(middleware: MiddlewareFunction): Promise<void>;
|
|
22
|
+
config(key: string, value: unknown): Promise<void>;
|
|
23
|
+
private _routeAbbrValidator;
|
|
24
|
+
get(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>;
|
|
25
|
+
post(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>;
|
|
26
|
+
put(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>;
|
|
27
|
+
delete(address: string, fnc: RouteFunction, option?: RouteOption): Promise<void>;
|
|
28
|
+
private _paramConverter;
|
|
29
|
+
private _paramParser;
|
|
30
|
+
private _static;
|
|
31
|
+
static(uri: string, path: string): Promise<void>;
|
|
32
|
+
route(rule: RouteRule): Promise<void>;
|
|
33
|
+
setSPARoot(uri: string, path: string): Promise<void>;
|
|
34
|
+
listen(port: number): Promise<void>;
|
|
35
|
+
isRunning(): boolean;
|
|
36
|
+
getHttpServer(): Server | undefined;
|
|
37
|
+
stop(): Promise<void>;
|
|
38
|
+
}
|