@h3ravel/http 1.0.0 → 2.0.0
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/CHANGELOG.md +13 -0
- package/package.json +5 -5
- package/src/Middleware.ts +2 -2
- package/src/Resources/ApiResource.ts +12 -12
- package/src/Resources/JsonResource.ts +34 -34
- package/src/Response.ts +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @h3ravel/http
|
|
2
2
|
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- a27f452: chore: fix all linting issues.
|
|
8
|
+
- c906050: chore: migrate tests suite to jest
|
|
9
|
+
- Updated dependencies [8ceb2c1]
|
|
10
|
+
- Updated dependencies [a27f452]
|
|
11
|
+
- Updated dependencies [c906050]
|
|
12
|
+
- @h3ravel/core@0.4.0
|
|
13
|
+
- @h3ravel/shared@0.4.0
|
|
14
|
+
- @h3ravel/support@0.4.0
|
|
15
|
+
|
|
3
16
|
## 1.0.0
|
|
4
17
|
|
|
5
18
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h3ravel/http",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "HTTP kernel, middleware pipeline, request/response classes for H3ravel.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -11,11 +11,11 @@
|
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"h3": "^2.0.0-beta.1",
|
|
13
13
|
"srvx": "^0.8.2",
|
|
14
|
-
"@h3ravel/shared": "0.
|
|
15
|
-
"@h3ravel/support": "0.
|
|
14
|
+
"@h3ravel/shared": "0.4.0",
|
|
15
|
+
"@h3ravel/support": "0.4.0"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@h3ravel/core": "0.
|
|
18
|
+
"@h3ravel/core": "0.4.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"typescript": "^5.4.0"
|
|
@@ -26,6 +26,6 @@
|
|
|
26
26
|
"dev": "tsx watch src/index.ts",
|
|
27
27
|
"start": "node dist/index.js",
|
|
28
28
|
"lint": "eslint . --ext .ts",
|
|
29
|
-
"test": "
|
|
29
|
+
"test": "jest --passWithNoTests"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/src/Middleware.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HttpContext } from './Contracts/HttpContract'
|
|
2
|
-
import { IMiddleware } from '@h3ravel/shared'
|
|
1
|
+
import { HttpContext } from './Contracts/HttpContract'
|
|
2
|
+
import { IMiddleware } from '@h3ravel/shared'
|
|
3
3
|
|
|
4
4
|
export abstract class Middleware implements IMiddleware {
|
|
5
5
|
abstract handle (context: HttpContext, next: () => Promise<unknown>): Promise<unknown>
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { JsonResource, Resource } from
|
|
1
|
+
import { JsonResource, Resource } from './JsonResource'
|
|
2
2
|
|
|
3
|
-
import { H3Event } from
|
|
3
|
+
import { H3Event } from 'h3'
|
|
4
4
|
|
|
5
5
|
export function ApiResource (
|
|
6
6
|
instance: JsonResource
|
|
7
7
|
) {
|
|
8
8
|
return new Proxy(instance, {
|
|
9
9
|
get (target, prop, receiver) {
|
|
10
|
-
const value = Reflect.get(target, prop, receiver)
|
|
10
|
+
const value = Reflect.get(target, prop, receiver)
|
|
11
11
|
if (typeof value === 'function') {
|
|
12
12
|
// Intercept json, additional, and send methods
|
|
13
13
|
if (prop === 'json' || prop === 'additional') {
|
|
14
14
|
return (...args: any[]) => {
|
|
15
|
-
const result = value.apply(target, args)
|
|
15
|
+
const result = value.apply(target, args)
|
|
16
16
|
// Schedule checkSend after json or additional
|
|
17
|
-
setImmediate(() => target['checkSend']())
|
|
18
|
-
return result
|
|
19
|
-
}
|
|
17
|
+
setImmediate(() => target['checkSend']())
|
|
18
|
+
return result
|
|
19
|
+
}
|
|
20
20
|
} else if (prop === 'send') {
|
|
21
21
|
return (...args: any[]) => {
|
|
22
22
|
// Prevent checkSend from firing
|
|
23
|
-
target['shouldSend'] = false
|
|
23
|
+
target['shouldSend'] = false
|
|
24
24
|
|
|
25
|
-
return value.apply(target, args)
|
|
26
|
-
}
|
|
25
|
+
return value.apply(target, args)
|
|
26
|
+
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
return value
|
|
29
|
+
return value
|
|
30
30
|
},
|
|
31
|
-
})
|
|
31
|
+
})
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export default function BaseResource<R extends Resource> (
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventHandlerRequest, H3Event } from
|
|
1
|
+
import { EventHandlerRequest, H3Event } from 'h3'
|
|
2
2
|
|
|
3
3
|
export interface Resource {
|
|
4
4
|
[key: string]: any;
|
|
@@ -24,25 +24,25 @@ export class JsonResource<R extends Resource = any> {
|
|
|
24
24
|
/**
|
|
25
25
|
* The request instance
|
|
26
26
|
*/
|
|
27
|
-
request: H3Event<EventHandlerRequest>['req']
|
|
27
|
+
request: H3Event<EventHandlerRequest>['req']
|
|
28
28
|
/**
|
|
29
29
|
* The response instance
|
|
30
30
|
*/
|
|
31
|
-
response: H3Event['res']
|
|
31
|
+
response: H3Event['res']
|
|
32
32
|
/**
|
|
33
33
|
* The data to send to the client
|
|
34
34
|
*/
|
|
35
|
-
resource: R
|
|
35
|
+
resource: R
|
|
36
36
|
/**
|
|
37
37
|
* The final response data object
|
|
38
38
|
*/
|
|
39
39
|
body: BodyResource = {
|
|
40
40
|
data: {},
|
|
41
|
-
}
|
|
41
|
+
}
|
|
42
42
|
/**
|
|
43
43
|
* Flag to track if response should be sent automatically
|
|
44
44
|
*/
|
|
45
|
-
private shouldSend: boolean = false
|
|
45
|
+
private shouldSend: boolean = false
|
|
46
46
|
/**
|
|
47
47
|
* Flag to track if response has been sent
|
|
48
48
|
*/
|
|
@@ -60,9 +60,9 @@ export class JsonResource<R extends Resource = any> {
|
|
|
60
60
|
* @param rsc The data to send to the client
|
|
61
61
|
*/
|
|
62
62
|
constructor(protected event: H3Event, rsc: R) {
|
|
63
|
-
this.request = event.req
|
|
64
|
-
this.response = event.res
|
|
65
|
-
this.resource = rsc
|
|
63
|
+
this.request = event.req
|
|
64
|
+
this.response = event.res
|
|
65
|
+
this.resource = rsc
|
|
66
66
|
|
|
67
67
|
// Copy all properties from rsc to this, avoiding conflicts
|
|
68
68
|
for (const key of Object.keys(rsc)) {
|
|
@@ -72,9 +72,9 @@ export class JsonResource<R extends Resource = any> {
|
|
|
72
72
|
configurable: true,
|
|
73
73
|
get: () => this.resource[key],
|
|
74
74
|
set: (value) => {
|
|
75
|
-
(<any>this.resource)[key] = value
|
|
75
|
+
(<any>this.resource)[key] = value
|
|
76
76
|
},
|
|
77
|
-
})
|
|
77
|
+
})
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
}
|
|
@@ -94,43 +94,43 @@ export class JsonResource<R extends Resource = any> {
|
|
|
94
94
|
*/
|
|
95
95
|
json () {
|
|
96
96
|
// Indicate response should be sent automatically
|
|
97
|
-
this.shouldSend = true
|
|
97
|
+
this.shouldSend = true
|
|
98
98
|
|
|
99
99
|
// Set default status code
|
|
100
|
-
this.response.status = 200
|
|
100
|
+
this.response.status = 200
|
|
101
101
|
|
|
102
102
|
// Prepare body
|
|
103
103
|
const resource = this.data()
|
|
104
|
-
let data: Resource = Array.isArray(resource) ? [...resource] : { ...resource }
|
|
104
|
+
let data: Resource = Array.isArray(resource) ? [...resource] : { ...resource }
|
|
105
105
|
|
|
106
106
|
if (typeof data.data !== 'undefined') {
|
|
107
107
|
data = data.data
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
if (!Array.isArray(resource)) {
|
|
111
|
-
delete data.pagination
|
|
111
|
+
delete data.pagination
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
this.body = {
|
|
115
115
|
data,
|
|
116
|
-
}
|
|
116
|
+
}
|
|
117
117
|
|
|
118
118
|
// Set the pagination from the data() resource, if available
|
|
119
119
|
if (!Array.isArray(resource) && resource.pagination) {
|
|
120
120
|
const meta: BodyResource['meta'] = this.body.meta ?? {}
|
|
121
|
-
meta.pagination = resource.pagination
|
|
122
|
-
this.body.meta = meta
|
|
121
|
+
meta.pagination = resource.pagination
|
|
122
|
+
this.body.meta = meta
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
// If pagination is not available on the resource, then check and set it
|
|
126
126
|
// if it's available on the base resource.
|
|
127
127
|
if (this.resource.pagination && !this.body.meta?.pagination) {
|
|
128
128
|
const meta: BodyResource['meta'] = this.body.meta ?? {}
|
|
129
|
-
meta.pagination = this.resource.pagination
|
|
130
|
-
this.body.meta = meta
|
|
129
|
+
meta.pagination = this.resource.pagination
|
|
130
|
+
this.body.meta = meta
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
return this
|
|
133
|
+
return this
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
/**
|
|
@@ -141,18 +141,18 @@ export class JsonResource<R extends Resource = any> {
|
|
|
141
141
|
additional<X extends { [key: string]: any }> (data: X) {
|
|
142
142
|
|
|
143
143
|
// Allow automatic send after additional
|
|
144
|
-
this.shouldSend = true
|
|
144
|
+
this.shouldSend = true
|
|
145
145
|
|
|
146
146
|
// Merge data with body
|
|
147
|
-
delete data.data
|
|
148
|
-
delete data.pagination
|
|
147
|
+
delete data.data
|
|
148
|
+
delete data.pagination
|
|
149
149
|
|
|
150
150
|
this.body = {
|
|
151
151
|
...this.body,
|
|
152
152
|
...data,
|
|
153
|
-
}
|
|
153
|
+
}
|
|
154
154
|
|
|
155
|
-
return this
|
|
155
|
+
return this
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
/**
|
|
@@ -160,11 +160,11 @@ export class JsonResource<R extends Resource = any> {
|
|
|
160
160
|
* @returns this
|
|
161
161
|
*/
|
|
162
162
|
send () {
|
|
163
|
-
this.shouldSend = false
|
|
163
|
+
this.shouldSend = false // Prevent automatic send
|
|
164
164
|
if (!this.responseSent) {
|
|
165
|
-
this.#send()
|
|
165
|
+
this.#send()
|
|
166
166
|
}
|
|
167
|
-
return this
|
|
167
|
+
return this
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
/**
|
|
@@ -173,8 +173,8 @@ export class JsonResource<R extends Resource = any> {
|
|
|
173
173
|
* @returns this
|
|
174
174
|
*/
|
|
175
175
|
status (code: number) {
|
|
176
|
-
this.response.status = code
|
|
177
|
-
return this
|
|
176
|
+
this.response.status = code
|
|
177
|
+
return this
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
/**
|
|
@@ -183,10 +183,10 @@ export class JsonResource<R extends Resource = any> {
|
|
|
183
183
|
#send () {
|
|
184
184
|
if (!this.responseSent) {
|
|
185
185
|
this.event.context.
|
|
186
|
-
this.response.json(this.body)
|
|
186
|
+
this.response.json(this.body)
|
|
187
187
|
|
|
188
188
|
// Mark response as sent
|
|
189
|
-
this.responseSent = true
|
|
189
|
+
this.responseSent = true
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
192
|
|
|
@@ -195,7 +195,7 @@ export class JsonResource<R extends Resource = any> {
|
|
|
195
195
|
*/
|
|
196
196
|
private checkSend () {
|
|
197
197
|
if (this.shouldSend && !this.responseSent) {
|
|
198
|
-
this.#send()
|
|
198
|
+
this.#send()
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
201
|
}
|
package/src/Response.ts
CHANGED
|
@@ -39,7 +39,7 @@ export class Response implements IResponse {
|
|
|
39
39
|
* Send a JSON response.
|
|
40
40
|
*/
|
|
41
41
|
json<T = unknown> (data: T): T {
|
|
42
|
-
this.setHeader(
|
|
42
|
+
this.setHeader('content-type', 'application/json; charset=utf-8')
|
|
43
43
|
this.applyHeaders()
|
|
44
44
|
return data
|
|
45
45
|
}
|
|
@@ -48,9 +48,9 @@ export class Response implements IResponse {
|
|
|
48
48
|
* Send plain text.
|
|
49
49
|
*/
|
|
50
50
|
text (data: string): string {
|
|
51
|
-
this.setHeader(
|
|
51
|
+
this.setHeader('content-type', 'text/plain; charset=utf-8')
|
|
52
52
|
this.applyHeaders()
|
|
53
|
-
return data
|
|
53
|
+
return data
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|