@h3ravel/http 8.0.2 → 8.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/package.json +7 -4
- package/CHANGELOG.md +0 -257
- package/src/Contracts/HttpContract.ts +0 -3
- package/src/Middleware/LogRequests.ts +0 -10
- package/src/Middleware.ts +0 -6
- package/src/Providers/HttpServiceProvider.ts +0 -24
- package/src/Request.ts +0 -78
- package/src/Resources/ApiResource.ts +0 -39
- package/src/Resources/JsonResource.ts +0 -201
- package/src/Response.ts +0 -92
- package/src/index.ts +0 -12
- package/tests/.gitkeep +0 -0
- package/tsconfig.json +0 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h3ravel/http",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.4",
|
|
4
4
|
"description": "HTTP kernel, middleware pipeline, request/response classes for H3ravel.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
"types": "./dist/index.d.ts"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
16
19
|
"publishConfig": {
|
|
17
20
|
"access": "public"
|
|
18
21
|
},
|
|
@@ -35,11 +38,11 @@
|
|
|
35
38
|
"dependencies": {
|
|
36
39
|
"h3": "^2.0.0-beta.1",
|
|
37
40
|
"srvx": "^0.8.2",
|
|
38
|
-
"@h3ravel/support": "^0.8.
|
|
39
|
-
"@h3ravel/shared": "^0.16.
|
|
41
|
+
"@h3ravel/support": "^0.8.2",
|
|
42
|
+
"@h3ravel/shared": "^0.16.4"
|
|
40
43
|
},
|
|
41
44
|
"peerDependencies": {
|
|
42
|
-
"@h3ravel/core": "^1.4.
|
|
45
|
+
"@h3ravel/core": "^1.4.2"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
48
|
"typescript": "^5.4.0"
|
package/CHANGELOG.md
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
# @h3ravel/http
|
|
2
|
-
|
|
3
|
-
## 8.0.2
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- fix: nothing at all
|
|
8
|
-
|
|
9
|
-
## 8.0.0
|
|
10
|
-
|
|
11
|
-
### Minor Changes
|
|
12
|
-
|
|
13
|
-
- 79f4045: feat: add add exports to package.json
|
|
14
|
-
|
|
15
|
-
### Patch Changes
|
|
16
|
-
|
|
17
|
-
- Updated dependencies [79f4045]
|
|
18
|
-
- @h3ravel/support@0.8.0
|
|
19
|
-
- @h3ravel/shared@0.16.0
|
|
20
|
-
- @h3ravel/core@1.4.0
|
|
21
|
-
|
|
22
|
-
## 7.0.0
|
|
23
|
-
|
|
24
|
-
### Patch Changes
|
|
25
|
-
|
|
26
|
-
- Updated dependencies
|
|
27
|
-
- @h3ravel/core@1.3.0
|
|
28
|
-
- @h3ravel/shared@0.15.4
|
|
29
|
-
|
|
30
|
-
## 6.0.2
|
|
31
|
-
|
|
32
|
-
### Patch Changes
|
|
33
|
-
|
|
34
|
-
- feat: add homepage and repository to all packages.
|
|
35
|
-
- Updated dependencies
|
|
36
|
-
- @h3ravel/core@1.2.2
|
|
37
|
-
- @h3ravel/shared@0.15.2
|
|
38
|
-
- @h3ravel/support@0.7.5
|
|
39
|
-
|
|
40
|
-
## 6.0.1
|
|
41
|
-
|
|
42
|
-
### Patch Changes
|
|
43
|
-
|
|
44
|
-
- chore: update readme accros all packages
|
|
45
|
-
- Updated dependencies
|
|
46
|
-
- @h3ravel/core@1.2.1
|
|
47
|
-
- @h3ravel/shared@0.15.1
|
|
48
|
-
- @h3ravel/support@0.7.4
|
|
49
|
-
|
|
50
|
-
## 6.0.0
|
|
51
|
-
|
|
52
|
-
### Patch Changes
|
|
53
|
-
|
|
54
|
-
- Updated dependencies [d07ff49]
|
|
55
|
-
- @h3ravel/shared@0.15.0
|
|
56
|
-
- @h3ravel/core@1.2.0
|
|
57
|
-
- @h3ravel/support@0.7.3
|
|
58
|
-
|
|
59
|
-
## 5.0.5
|
|
60
|
-
|
|
61
|
-
### Patch Changes
|
|
62
|
-
|
|
63
|
-
- db0dd70: chore: revert to old workspace core dependency
|
|
64
|
-
|
|
65
|
-
## 5.0.4
|
|
66
|
-
|
|
67
|
-
### Patch Changes
|
|
68
|
-
|
|
69
|
-
- chore: remove core as a queue dependency, leave as peer
|
|
70
|
-
|
|
71
|
-
## 5.0.3
|
|
72
|
-
|
|
73
|
-
### Patch Changes
|
|
74
|
-
|
|
75
|
-
- version: explicitly set the latest version as pearDependency in requiring packages.
|
|
76
|
-
|
|
77
|
-
## 5.0.2
|
|
78
|
-
|
|
79
|
-
### Patch Changes
|
|
80
|
-
|
|
81
|
-
- chore: require the latest dependencies from the framework
|
|
82
|
-
- Updated dependencies
|
|
83
|
-
- @h3ravel/support@0.7.3
|
|
84
|
-
- @h3ravel/core@1.1.2
|
|
85
|
-
|
|
86
|
-
## 5.0.1
|
|
87
|
-
|
|
88
|
-
### Patch Changes
|
|
89
|
-
|
|
90
|
-
- Updated dependencies
|
|
91
|
-
- @h3ravel/shared@0.14.0
|
|
92
|
-
- @h3ravel/core@1.1.1
|
|
93
|
-
- @h3ravel/support@0.7.2
|
|
94
|
-
|
|
95
|
-
## 5.0.0
|
|
96
|
-
|
|
97
|
-
### Patch Changes
|
|
98
|
-
|
|
99
|
-
- Updated dependencies [6e249fe]
|
|
100
|
-
- @h3ravel/shared@0.13.0
|
|
101
|
-
- @h3ravel/core@1.1.0
|
|
102
|
-
- @h3ravel/support@0.7.2
|
|
103
|
-
|
|
104
|
-
## 4.2.2
|
|
105
|
-
|
|
106
|
-
### Patch Changes
|
|
107
|
-
|
|
108
|
-
- chore: regularize all interfaces.
|
|
109
|
-
- Updated dependencies
|
|
110
|
-
- @h3ravel/core@1.0.9
|
|
111
|
-
- @h3ravel/shared@0.12.1
|
|
112
|
-
- @h3ravel/support@0.7.2
|
|
113
|
-
|
|
114
|
-
## 4.2.1
|
|
115
|
-
|
|
116
|
-
### Patch Changes
|
|
117
|
-
|
|
118
|
-
- fix: use the Application class directly as the app property of the Request and Response classes.
|
|
119
|
-
|
|
120
|
-
## 4.2.0
|
|
121
|
-
|
|
122
|
-
### Minor Changes
|
|
123
|
-
|
|
124
|
-
- feat: add the current app instance to the Request and Response object
|
|
125
|
-
|
|
126
|
-
### Patch Changes
|
|
127
|
-
|
|
128
|
-
- Updated dependencies
|
|
129
|
-
- @h3ravel/shared@0.12.0
|
|
130
|
-
- @h3ravel/core@1.0.8
|
|
131
|
-
- @h3ravel/support@0.7.1
|
|
132
|
-
|
|
133
|
-
## 4.1.3
|
|
134
|
-
|
|
135
|
-
### Patch Changes
|
|
136
|
-
|
|
137
|
-
- Updated dependencies
|
|
138
|
-
- @h3ravel/shared@0.11.0
|
|
139
|
-
- @h3ravel/core@1.0.7
|
|
140
|
-
- @h3ravel/support@0.7.1
|
|
141
|
-
|
|
142
|
-
## 4.1.2
|
|
143
|
-
|
|
144
|
-
### Patch Changes
|
|
145
|
-
|
|
146
|
-
- Updated dependencies
|
|
147
|
-
- @h3ravel/shared@0.10.0
|
|
148
|
-
- @h3ravel/core@1.0.6
|
|
149
|
-
- @h3ravel/support@0.7.1
|
|
150
|
-
|
|
151
|
-
## 4.1.1
|
|
152
|
-
|
|
153
|
-
### Patch Changes
|
|
154
|
-
|
|
155
|
-
- Updated dependencies
|
|
156
|
-
- @h3ravel/shared@0.9.0
|
|
157
|
-
- @h3ravel/core@1.0.5
|
|
158
|
-
- @h3ravel/support@0.7.1
|
|
159
|
-
|
|
160
|
-
## 4.1.0
|
|
161
|
-
|
|
162
|
-
### Minor Changes
|
|
163
|
-
|
|
164
|
-
- feat: convert Request params and query from a method to a property and add Request headers
|
|
165
|
-
|
|
166
|
-
### Patch Changes
|
|
167
|
-
|
|
168
|
-
- Updated dependencies
|
|
169
|
-
- @h3ravel/shared@0.8.0
|
|
170
|
-
- @h3ravel/core@1.0.4
|
|
171
|
-
- @h3ravel/support@0.7.1
|
|
172
|
-
|
|
173
|
-
## 4.0.3
|
|
174
|
-
|
|
175
|
-
### Patch Changes
|
|
176
|
-
|
|
177
|
-
- chore: add download count to readme
|
|
178
|
-
- Updated dependencies
|
|
179
|
-
- @h3ravel/core@1.0.3
|
|
180
|
-
- @h3ravel/shared@0.7.1
|
|
181
|
-
- @h3ravel/support@0.7.1
|
|
182
|
-
|
|
183
|
-
## 4.0.2
|
|
184
|
-
|
|
185
|
-
### Patch Changes
|
|
186
|
-
|
|
187
|
-
- Updated dependencies [b0d1b7c]
|
|
188
|
-
- Updated dependencies [ce51a92]
|
|
189
|
-
- @h3ravel/support@0.7.0
|
|
190
|
-
- @h3ravel/shared@0.7.0
|
|
191
|
-
- @h3ravel/core@1.0.2
|
|
192
|
-
|
|
193
|
-
## 4.0.0
|
|
194
|
-
|
|
195
|
-
### Minor Changes
|
|
196
|
-
|
|
197
|
-
- b40caeb: feat: make service providers sortable and unique while only loading the core providers by default.
|
|
198
|
-
Service providers are no longer loaded by default, asides the ones provided by @h3ravel/core
|
|
199
|
-
Service provides are sorted by an optional order and priority property.
|
|
200
|
-
|
|
201
|
-
### Patch Changes
|
|
202
|
-
|
|
203
|
-
- Updated dependencies [b40caeb]
|
|
204
|
-
- @h3ravel/core@1.0.0
|
|
205
|
-
- @h3ravel/shared@0.6.0
|
|
206
|
-
- @h3ravel/support@0.6.0
|
|
207
|
-
|
|
208
|
-
## 3.0.0
|
|
209
|
-
|
|
210
|
-
### Minor Changes
|
|
211
|
-
|
|
212
|
-
- rebuild all dependencies
|
|
213
|
-
|
|
214
|
-
### Patch Changes
|
|
215
|
-
|
|
216
|
-
- Updated dependencies
|
|
217
|
-
- @h3ravel/core@0.5.0
|
|
218
|
-
- @h3ravel/shared@0.5.0
|
|
219
|
-
- @h3ravel/support@0.5.0
|
|
220
|
-
|
|
221
|
-
## 2.0.0
|
|
222
|
-
|
|
223
|
-
### Patch Changes
|
|
224
|
-
|
|
225
|
-
- a27f452: chore: fix all linting issues.
|
|
226
|
-
- c906050: chore: migrate tests suite to jest
|
|
227
|
-
- Updated dependencies [8ceb2c1]
|
|
228
|
-
- Updated dependencies [a27f452]
|
|
229
|
-
- Updated dependencies [c906050]
|
|
230
|
-
- @h3ravel/core@0.4.0
|
|
231
|
-
- @h3ravel/shared@0.4.0
|
|
232
|
-
- @h3ravel/support@0.4.0
|
|
233
|
-
|
|
234
|
-
## 1.0.0
|
|
235
|
-
|
|
236
|
-
### Minor Changes
|
|
237
|
-
|
|
238
|
-
- 3ff97bf: refactor: add a shared package to be extended by others to avoid cyclic dependency issues.
|
|
239
|
-
|
|
240
|
-
### Patch Changes
|
|
241
|
-
|
|
242
|
-
- Updated dependencies [3ff97bf]
|
|
243
|
-
- @h3ravel/core@0.3.0
|
|
244
|
-
- @h3ravel/shared@0.3.0
|
|
245
|
-
- @h3ravel/support@0.3.0
|
|
246
|
-
|
|
247
|
-
## 0.2.0
|
|
248
|
-
|
|
249
|
-
### Minor Changes
|
|
250
|
-
|
|
251
|
-
- aea734f: Fix all known bugs and improved interdependecy between packages.
|
|
252
|
-
|
|
253
|
-
### Patch Changes
|
|
254
|
-
|
|
255
|
-
- Updated dependencies [aea734f]
|
|
256
|
-
- @h3ravel/core@0.2.0
|
|
257
|
-
- @h3ravel/support@0.2.0
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { HttpContext } from '@h3ravel/shared'
|
|
2
|
-
import { Middleware } from '../Middleware'
|
|
3
|
-
|
|
4
|
-
export class LogRequests extends Middleware {
|
|
5
|
-
async handle ({ request }: HttpContext, next: () => Promise<unknown>): Promise<unknown> {
|
|
6
|
-
const url = request.getEvent('url')
|
|
7
|
-
console.log(`[${request.getEvent('method')}] ${url.pathname + url.search}`)
|
|
8
|
-
return next()
|
|
9
|
-
}
|
|
10
|
-
}
|
package/src/Middleware.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { H3, serve } from 'h3'
|
|
2
|
-
|
|
3
|
-
import { ServiceProvider } from '@h3ravel/core'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Sets up HTTP kernel and request lifecycle.
|
|
7
|
-
*
|
|
8
|
-
* Register Request, Response, and Middleware classes.
|
|
9
|
-
* Configure global middleware stack.
|
|
10
|
-
* Boot HTTP kernel.
|
|
11
|
-
*
|
|
12
|
-
* Auto-Registered
|
|
13
|
-
*/
|
|
14
|
-
export class HttpServiceProvider extends ServiceProvider {
|
|
15
|
-
public static priority = 998;
|
|
16
|
-
|
|
17
|
-
register () {
|
|
18
|
-
this.app.singleton('http.app', () => {
|
|
19
|
-
return new H3()
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
this.app.singleton('http.serve', () => serve)
|
|
23
|
-
}
|
|
24
|
-
}
|
package/src/Request.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { getQuery, getRouterParams, readBody, type H3Event } from 'h3'
|
|
2
|
-
import { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'
|
|
3
|
-
import type { ResponseHeaderMap, TypedHeaders } from 'fetchdts'
|
|
4
|
-
import { IRequest } from '@h3ravel/shared'
|
|
5
|
-
import { Application } from '@h3ravel/core'
|
|
6
|
-
|
|
7
|
-
export class Request implements IRequest {
|
|
8
|
-
/**
|
|
9
|
-
* Gets route parameters.
|
|
10
|
-
* @returns An object containing route parameters.
|
|
11
|
-
*/
|
|
12
|
-
readonly params: NonNullable<H3Event["context"]["params"]>
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Gets query parameters.
|
|
16
|
-
* @returns An object containing query parameters.
|
|
17
|
-
*/
|
|
18
|
-
readonly query: Record<string, string>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Gets the request headers.
|
|
22
|
-
* @returns An object containing request headers.
|
|
23
|
-
*/
|
|
24
|
-
readonly headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The current H3 H3Event instance
|
|
28
|
-
*/
|
|
29
|
-
private readonly event: H3Event
|
|
30
|
-
|
|
31
|
-
constructor(
|
|
32
|
-
event: H3Event,
|
|
33
|
-
/**
|
|
34
|
-
* The current app instance
|
|
35
|
-
*/
|
|
36
|
-
public app: Application
|
|
37
|
-
) {
|
|
38
|
-
this.event = event
|
|
39
|
-
this.query = getQuery(this.event)
|
|
40
|
-
this.params = getRouterParams(this.event)
|
|
41
|
-
this.headers = this.event.req.headers
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Get all input data (query + body).
|
|
46
|
-
*/
|
|
47
|
-
async all<T = Record<string, unknown>> (): Promise<T> {
|
|
48
|
-
let data = {
|
|
49
|
-
...getRouterParams(this.event),
|
|
50
|
-
...getQuery(this.event),
|
|
51
|
-
} as T
|
|
52
|
-
|
|
53
|
-
if (this.event.req.method === 'POST') {
|
|
54
|
-
data = Object.assign({}, data, Object.fromEntries((await this.event.req.formData()).entries()))
|
|
55
|
-
} else if (this.event.req.method === 'PUT') {
|
|
56
|
-
data = <never>Object.fromEntries(Object.entries(<never>await readBody(this.event)))
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return data
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Get a single input field from query or body.
|
|
64
|
-
*/
|
|
65
|
-
async input<T = unknown> (key: string, defaultValue?: T): Promise<T> {
|
|
66
|
-
const data = await this.all<Record<string, T>>()
|
|
67
|
-
return (data[key] ?? defaultValue) as T
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Get the base event
|
|
72
|
-
*/
|
|
73
|
-
getEvent (): H3Event
|
|
74
|
-
getEvent<K extends DotNestedKeys<H3Event>> (key: K): DotNestedValue<H3Event, K>
|
|
75
|
-
getEvent<K extends DotNestedKeys<H3Event>> (key?: K): any {
|
|
76
|
-
return safeDot(this.event, key)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { JsonResource, Resource } from './JsonResource'
|
|
2
|
-
|
|
3
|
-
import { H3Event } from 'h3'
|
|
4
|
-
|
|
5
|
-
export function ApiResource (
|
|
6
|
-
instance: JsonResource
|
|
7
|
-
) {
|
|
8
|
-
return new Proxy(instance, {
|
|
9
|
-
get (target, prop, receiver) {
|
|
10
|
-
const value = Reflect.get(target, prop, receiver)
|
|
11
|
-
if (typeof value === 'function') {
|
|
12
|
-
// Intercept json, additional, and send methods
|
|
13
|
-
if (prop === 'json' || prop === 'additional') {
|
|
14
|
-
return (...args: any[]) => {
|
|
15
|
-
const result = value.apply(target, args)
|
|
16
|
-
// Schedule checkSend after json or additional
|
|
17
|
-
setImmediate(() => target['checkSend']())
|
|
18
|
-
return result
|
|
19
|
-
}
|
|
20
|
-
} else if (prop === 'send') {
|
|
21
|
-
return (...args: any[]) => {
|
|
22
|
-
// Prevent checkSend from firing
|
|
23
|
-
target['shouldSend'] = false
|
|
24
|
-
|
|
25
|
-
return value.apply(target, args)
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return value
|
|
30
|
-
},
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export default function BaseResource<R extends Resource> (
|
|
35
|
-
evt: H3Event,
|
|
36
|
-
rsc: R
|
|
37
|
-
) {
|
|
38
|
-
return ApiResource(new JsonResource<R>(evt, rsc))
|
|
39
|
-
}
|
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import { EventHandlerRequest, H3Event } from 'h3'
|
|
2
|
-
|
|
3
|
-
export interface Resource {
|
|
4
|
-
[key: string]: any;
|
|
5
|
-
pagination?: {
|
|
6
|
-
from?: number | undefined;
|
|
7
|
-
to?: number | undefined;
|
|
8
|
-
perPage?: number | undefined;
|
|
9
|
-
total?: number | undefined;
|
|
10
|
-
} | undefined;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type BodyResource = Resource & {
|
|
14
|
-
data: Omit<Resource, 'pagination'>,
|
|
15
|
-
meta?: {
|
|
16
|
-
pagination?: Resource['pagination']
|
|
17
|
-
} | undefined;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Class to render API resource
|
|
22
|
-
*/
|
|
23
|
-
export class JsonResource<R extends Resource = any> {
|
|
24
|
-
/**
|
|
25
|
-
* The request instance
|
|
26
|
-
*/
|
|
27
|
-
request: H3Event<EventHandlerRequest>['req']
|
|
28
|
-
/**
|
|
29
|
-
* The response instance
|
|
30
|
-
*/
|
|
31
|
-
response: H3Event['res']
|
|
32
|
-
/**
|
|
33
|
-
* The data to send to the client
|
|
34
|
-
*/
|
|
35
|
-
resource: R
|
|
36
|
-
/**
|
|
37
|
-
* The final response data object
|
|
38
|
-
*/
|
|
39
|
-
body: BodyResource = {
|
|
40
|
-
data: {},
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Flag to track if response should be sent automatically
|
|
44
|
-
*/
|
|
45
|
-
private shouldSend: boolean = false
|
|
46
|
-
/**
|
|
47
|
-
* Flag to track if response has been sent
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
private responseSent: boolean = false;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Declare that this includes R's properties
|
|
54
|
-
*/
|
|
55
|
-
[key: string]: any;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* @param req The request instance
|
|
59
|
-
* @param res The response instance
|
|
60
|
-
* @param rsc The data to send to the client
|
|
61
|
-
*/
|
|
62
|
-
constructor(protected event: H3Event, rsc: R) {
|
|
63
|
-
this.request = event.req
|
|
64
|
-
this.response = event.res
|
|
65
|
-
this.resource = rsc
|
|
66
|
-
|
|
67
|
-
// Copy all properties from rsc to this, avoiding conflicts
|
|
68
|
-
for (const key of Object.keys(rsc)) {
|
|
69
|
-
if (!(key in this)) {
|
|
70
|
-
Object.defineProperty(this, key, {
|
|
71
|
-
enumerable: true,
|
|
72
|
-
configurable: true,
|
|
73
|
-
get: () => this.resource[key],
|
|
74
|
-
set: (value) => {
|
|
75
|
-
(<any>this.resource)[key] = value
|
|
76
|
-
},
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Return the data in the expected format
|
|
84
|
-
*
|
|
85
|
-
* @returns
|
|
86
|
-
*/
|
|
87
|
-
data (): Resource {
|
|
88
|
-
return this.resource
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Build the response object
|
|
93
|
-
* @returns this
|
|
94
|
-
*/
|
|
95
|
-
json () {
|
|
96
|
-
// Indicate response should be sent automatically
|
|
97
|
-
this.shouldSend = true
|
|
98
|
-
|
|
99
|
-
// Set default status code
|
|
100
|
-
this.response.status = 200
|
|
101
|
-
|
|
102
|
-
// Prepare body
|
|
103
|
-
const resource = this.data()
|
|
104
|
-
let data: Resource = Array.isArray(resource) ? [...resource] : { ...resource }
|
|
105
|
-
|
|
106
|
-
if (typeof data.data !== 'undefined') {
|
|
107
|
-
data = data.data
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (!Array.isArray(resource)) {
|
|
111
|
-
delete data.pagination
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
this.body = {
|
|
115
|
-
data,
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Set the pagination from the data() resource, if available
|
|
119
|
-
if (!Array.isArray(resource) && resource.pagination) {
|
|
120
|
-
const meta: BodyResource['meta'] = this.body.meta ?? {}
|
|
121
|
-
meta.pagination = resource.pagination
|
|
122
|
-
this.body.meta = meta
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// If pagination is not available on the resource, then check and set it
|
|
126
|
-
// if it's available on the base resource.
|
|
127
|
-
if (this.resource.pagination && !this.body.meta?.pagination) {
|
|
128
|
-
const meta: BodyResource['meta'] = this.body.meta ?? {}
|
|
129
|
-
meta.pagination = this.resource.pagination
|
|
130
|
-
this.body.meta = meta
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return this
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Add context data to the response object
|
|
138
|
-
* @param data Context data
|
|
139
|
-
* @returns this
|
|
140
|
-
*/
|
|
141
|
-
additional<X extends { [key: string]: any }> (data: X) {
|
|
142
|
-
|
|
143
|
-
// Allow automatic send after additional
|
|
144
|
-
this.shouldSend = true
|
|
145
|
-
|
|
146
|
-
// Merge data with body
|
|
147
|
-
delete data.data
|
|
148
|
-
delete data.pagination
|
|
149
|
-
|
|
150
|
-
this.body = {
|
|
151
|
-
...this.body,
|
|
152
|
-
...data,
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return this
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Send the output to the client
|
|
160
|
-
* @returns this
|
|
161
|
-
*/
|
|
162
|
-
send () {
|
|
163
|
-
this.shouldSend = false // Prevent automatic send
|
|
164
|
-
if (!this.responseSent) {
|
|
165
|
-
this.#send()
|
|
166
|
-
}
|
|
167
|
-
return this
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Set the status code for this response
|
|
172
|
-
* @param code Status code
|
|
173
|
-
* @returns this
|
|
174
|
-
*/
|
|
175
|
-
status (code: number) {
|
|
176
|
-
this.response.status = code
|
|
177
|
-
return this
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Private method to send the response
|
|
182
|
-
*/
|
|
183
|
-
#send () {
|
|
184
|
-
if (!this.responseSent) {
|
|
185
|
-
this.event.context.
|
|
186
|
-
this.response.json(this.body)
|
|
187
|
-
|
|
188
|
-
// Mark response as sent
|
|
189
|
-
this.responseSent = true
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Check if send should be triggered automatically
|
|
195
|
-
*/
|
|
196
|
-
private checkSend () {
|
|
197
|
-
if (this.shouldSend && !this.responseSent) {
|
|
198
|
-
this.#send()
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
package/src/Response.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'
|
|
2
|
-
import { html, redirect, } from 'h3'
|
|
3
|
-
|
|
4
|
-
import { Application } from '@h3ravel/core'
|
|
5
|
-
import type { H3Event } from 'h3'
|
|
6
|
-
import { IResponse } from '@h3ravel/shared'
|
|
7
|
-
|
|
8
|
-
export class Response implements IResponse {
|
|
9
|
-
/**
|
|
10
|
-
* The current H3 H3Event instance
|
|
11
|
-
*/
|
|
12
|
-
private readonly event: H3Event
|
|
13
|
-
|
|
14
|
-
private statusCode: number = 200
|
|
15
|
-
private headers: Record<string, string> = {}
|
|
16
|
-
|
|
17
|
-
constructor(
|
|
18
|
-
event: H3Event,
|
|
19
|
-
/**
|
|
20
|
-
* The current app instance
|
|
21
|
-
*/
|
|
22
|
-
public app: Application
|
|
23
|
-
) {
|
|
24
|
-
this.event = event
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Set HTTP status code.
|
|
29
|
-
*/
|
|
30
|
-
setStatusCode (code: number): this {
|
|
31
|
-
this.statusCode = code
|
|
32
|
-
this.event.res.status = code
|
|
33
|
-
return this
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Set a header.
|
|
38
|
-
*/
|
|
39
|
-
setHeader (name: string, value: string): this {
|
|
40
|
-
this.headers[name] = value
|
|
41
|
-
return this
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
html (content: string): string {
|
|
45
|
-
this.applyHeaders()
|
|
46
|
-
return html(this.event, content)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Send a JSON response.
|
|
51
|
-
*/
|
|
52
|
-
json<T = unknown> (data: T): T {
|
|
53
|
-
this.setHeader('content-type', 'application/json; charset=utf-8')
|
|
54
|
-
this.applyHeaders()
|
|
55
|
-
return data
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Send plain text.
|
|
60
|
-
*/
|
|
61
|
-
text (data: string): string {
|
|
62
|
-
this.setHeader('content-type', 'text/plain; charset=utf-8')
|
|
63
|
-
this.applyHeaders()
|
|
64
|
-
return data
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Redirect to another URL.
|
|
69
|
-
*/
|
|
70
|
-
redirect (url: string, status = 302): string {
|
|
71
|
-
this.setStatusCode(status)
|
|
72
|
-
return redirect(this.event, url, this.statusCode)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Apply headers before sending response.
|
|
77
|
-
*/
|
|
78
|
-
private applyHeaders (): void {
|
|
79
|
-
Object.entries(this.headers).forEach(([key, value]) => {
|
|
80
|
-
this.event.res.headers.set(key, value)
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Get the base event
|
|
86
|
-
*/
|
|
87
|
-
getEvent (): H3Event
|
|
88
|
-
getEvent<K extends DotNestedKeys<H3Event>> (key: K): DotNestedValue<H3Event, K>
|
|
89
|
-
getEvent<K extends DotNestedKeys<H3Event>> (key?: K): any {
|
|
90
|
-
return safeDot(this.event, key)
|
|
91
|
-
}
|
|
92
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Automatically generated by barrelsby.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export * from './Middleware';
|
|
6
|
-
export * from './Request';
|
|
7
|
-
export * from './Response';
|
|
8
|
-
export * from './Contracts/HttpContract';
|
|
9
|
-
export * from './Middleware/LogRequests';
|
|
10
|
-
export * from './Providers/HttpServiceProvider';
|
|
11
|
-
export * from './Resources/ApiResource';
|
|
12
|
-
export * from './Resources/JsonResource';
|
package/tests/.gitkeep
DELETED
|
File without changes
|