@h3ravel/http 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +371 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +199 -0
- package/dist/index.d.ts +199 -0
- package/dist/index.js +340 -0
- package/dist/index.js.map +1 -0
- package/package.json +28 -0
- package/src/Contracts/ControllerContracts.ts +13 -0
- package/src/Contracts/HttpContract.ts +7 -0
- package/src/Middleware/LogRequests.ts +9 -0
- package/src/Middleware.ts +5 -0
- package/src/Providers/HttpServiceProvider.ts +22 -0
- package/src/Request.ts +59 -0
- package/src/Resources/ApiResource.ts +39 -0
- package/src/Resources/JsonResource.ts +201 -0
- package/src/Response.ts +80 -0
- package/src/index.ts +13 -0
- package/tests/.gitkeep +0 -0
- package/tsconfig.json +8 -0
- package/vite.config.ts +9 -0
package/src/Request.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { getQuery, getRouterParams, readBody, type H3Event } from 'h3'
|
|
2
|
+
import { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'
|
|
3
|
+
|
|
4
|
+
export class Request {
|
|
5
|
+
private readonly event: H3Event
|
|
6
|
+
|
|
7
|
+
constructor(event: H3Event) {
|
|
8
|
+
this.event = event
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get all input data (query + body).
|
|
13
|
+
*/
|
|
14
|
+
async all<T = Record<string, unknown>> (): Promise<T> {
|
|
15
|
+
let data = {
|
|
16
|
+
...getRouterParams(this.event),
|
|
17
|
+
...getQuery(this.event),
|
|
18
|
+
} as T
|
|
19
|
+
|
|
20
|
+
if (this.event.req.method === 'POST') {
|
|
21
|
+
data = Object.assign({}, data, Object.fromEntries((await this.event.req.formData()).entries()))
|
|
22
|
+
} else if (this.event.req.method === 'PUT') {
|
|
23
|
+
data = <never>Object.fromEntries(Object.entries(<never>await readBody(this.event)))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return data
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Get a single input field from query or body.
|
|
31
|
+
*/
|
|
32
|
+
async input<T = unknown> (key: string, defaultValue?: T): Promise<T> {
|
|
33
|
+
const data = await this.all<Record<string, T>>()
|
|
34
|
+
return (data[key] ?? defaultValue) as T
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get route parameters.
|
|
39
|
+
*/
|
|
40
|
+
params<T = Record<string, string>> (): T {
|
|
41
|
+
return getRouterParams(this.event) as T
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get query parameters.
|
|
46
|
+
*/
|
|
47
|
+
query<T = Record<string, string>> (): T {
|
|
48
|
+
return getQuery(this.event) as T
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get the base event
|
|
53
|
+
*/
|
|
54
|
+
getEvent (): H3Event
|
|
55
|
+
getEvent<K extends DotNestedKeys<H3Event>> (key: K): DotNestedValue<H3Event, K>
|
|
56
|
+
getEvent<K extends DotNestedKeys<H3Event>> (key?: K): any {
|
|
57
|
+
return safeDot(this.event, key)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
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(private 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
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'
|
|
2
|
+
import { html, redirect, } from 'h3'
|
|
3
|
+
|
|
4
|
+
import type { H3Event } from 'h3'
|
|
5
|
+
|
|
6
|
+
export class Response {
|
|
7
|
+
private readonly event: H3Event
|
|
8
|
+
private statusCode: number = 200
|
|
9
|
+
private headers: Record<string, string> = {}
|
|
10
|
+
|
|
11
|
+
constructor(event: H3Event) {
|
|
12
|
+
this.event = event
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Set HTTP status code.
|
|
17
|
+
*/
|
|
18
|
+
setStatusCode (code: number): this {
|
|
19
|
+
this.statusCode = code
|
|
20
|
+
this.event.res.status = code
|
|
21
|
+
return this
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Set a header.
|
|
26
|
+
*/
|
|
27
|
+
setHeader (name: string, value: string): this {
|
|
28
|
+
this.headers[name] = value
|
|
29
|
+
return this
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
html (content: string): string {
|
|
33
|
+
this.applyHeaders()
|
|
34
|
+
return html(this.event, content)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Send a JSON response.
|
|
39
|
+
*/
|
|
40
|
+
json<T = unknown> (data: T): T {
|
|
41
|
+
this.setHeader("content-type", "application/json; charset=utf-8")
|
|
42
|
+
this.applyHeaders()
|
|
43
|
+
return data
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Send plain text.
|
|
48
|
+
*/
|
|
49
|
+
text (data: string): string {
|
|
50
|
+
this.setHeader("content-type", "text/plain; charset=utf-8")
|
|
51
|
+
this.applyHeaders()
|
|
52
|
+
return data;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Redirect to another URL.
|
|
57
|
+
*/
|
|
58
|
+
redirect (url: string, status = 302): string {
|
|
59
|
+
this.setStatusCode(status)
|
|
60
|
+
return redirect(this.event, url, this.statusCode)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Apply headers before sending response.
|
|
65
|
+
*/
|
|
66
|
+
private applyHeaders (): void {
|
|
67
|
+
Object.entries(this.headers).forEach(([key, value]) => {
|
|
68
|
+
this.event.res.headers.set(key, value)
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get the base event
|
|
74
|
+
*/
|
|
75
|
+
getEvent (): H3Event
|
|
76
|
+
getEvent<K extends DotNestedKeys<H3Event>> (key: K): DotNestedValue<H3Event, K>
|
|
77
|
+
getEvent<K extends DotNestedKeys<H3Event>> (key?: K): any {
|
|
78
|
+
return safeDot(this.event, key)
|
|
79
|
+
}
|
|
80
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
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/ControllerContracts';
|
|
9
|
+
export * from './Contracts/HttpContract';
|
|
10
|
+
export * from './Middleware/LogRequests';
|
|
11
|
+
export * from './Providers/HttpServiceProvider';
|
|
12
|
+
export * from './Resources/ApiResource';
|
|
13
|
+
export * from './Resources/JsonResource';
|
package/tests/.gitkeep
ADDED
|
File without changes
|
package/tsconfig.json
ADDED