@h3ravel/shared 0.7.1 → 0.9.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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @h3ravel/shared
2
2
 
3
+ ## 0.9.0
4
+
5
+ ### Minor Changes
6
+
7
+ - refactor!: make route definition more similar to the way it is handled in laravel.
8
+
9
+ ## 0.8.0
10
+
11
+ ### Minor Changes
12
+
13
+ - feat: convert Request params and query from a method to a property and add Request headers
14
+
3
15
  ## 0.7.1
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { H3Event, Middleware, MiddlewareOptions } from 'h3';
2
+ import { TypedHeaders, ResponseHeaderMap } from 'fetchdts';
2
3
 
3
4
  interface IServiceProvider {
4
5
  /**
@@ -117,12 +118,17 @@ interface IRequest {
117
118
  * Gets route parameters.
118
119
  * @returns An object containing route parameters.
119
120
  */
120
- params<T = Record<string, string>>(): T;
121
+ params: NonNullable<H3Event["context"]["params"]>;
121
122
  /**
122
123
  * Gets query parameters.
123
124
  * @returns An object containing query parameters.
124
125
  */
125
- query<T = Record<string, string>>(): T;
126
+ query: Record<string, any>;
127
+ /**
128
+ * Gets the request headers.
129
+ * @returns An object containing request headers.
130
+ */
131
+ headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>;
126
132
  /**
127
133
  * Gets the underlying event object or a specific property of it.
128
134
  * @param key - Optional key to access a nested property of the event.
@@ -190,46 +196,42 @@ interface IRouter {
190
196
  /**
191
197
  * Registers a GET route.
192
198
  * @param path - The route path.
193
- * @param handler - The handler function or controller class.
194
- * @param methodName - Optional controller method name.
199
+ * @param definition - The handler function or [controller class, method] array.
195
200
  * @param name - Optional route name.
196
201
  * @param middleware - Optional middleware array.
197
202
  */
198
- get(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
203
+ get(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): this;
199
204
  /**
200
205
  * Registers a POST route.
201
206
  * @param path - The route path.
202
- * @param handler - The handler function or controller class.
203
- * @param methodName - Optional controller method name.
207
+ * @param definition - The handler function or [controller class, method] array.
204
208
  * @param name - Optional route name.
205
209
  * @param middleware - Optional middleware array.
206
210
  */
207
- post(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
211
+ post(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): this;
208
212
  /**
209
213
  * Registers a PUT route.
210
214
  * @param path - The route path.
211
- * @param handler - The handler function or controller class.
212
- * @param methodName - Optional controller method name.
215
+ * @param definition - The handler function or [controller class, method] array.
213
216
  * @param name - Optional route name.
214
217
  * @param middleware - Optional middleware array.
215
218
  */
216
- put(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
219
+ put(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): this;
217
220
  /**
218
221
  * Registers a DELETE route.
219
222
  * @param path - The route path.
220
- * @param handler - The handler function or controller class.
221
- * @param methodName - Optional controller method name.
223
+ * @param definition - The handler function or [controller class, method] array.
222
224
  * @param name - Optional route name.
223
225
  * @param middleware - Optional middleware array.
224
226
  */
225
- delete(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
227
+ delete(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): this;
226
228
  /**
227
229
  * Registers an API resource with standard CRUD routes.
228
230
  * @param path - The base path for the resource.
229
231
  * @param controller - The controller class handling the resource.
230
232
  * @param middleware - Optional middleware array.
231
233
  */
232
- apiResource(path: string, controller: new (app: IApplication) => IController, middleware?: IMiddleware[]): void;
234
+ apiResource(path: string, controller: new (app: IApplication) => IController, middleware?: IMiddleware[]): this;
233
235
  /**
234
236
  * Generates a URL for a named route.
235
237
  * @param name - The name of the route.
@@ -237,6 +239,12 @@ interface IRouter {
237
239
  * @returns The generated URL or undefined if the route is not found.
238
240
  */
239
241
  route(name: string, params?: Record<string, string>): string | undefined;
242
+ /**
243
+ * Set the name of the current route
244
+ *
245
+ * @param name
246
+ */
247
+ name(name: string): this;
240
248
  /**
241
249
  * Groups routes with shared prefix or middleware.
242
250
  * @param options - Configuration for prefix or middleware.
@@ -245,14 +253,14 @@ interface IRouter {
245
253
  group(options: {
246
254
  prefix?: string;
247
255
  middleware?: EventHandler[];
248
- }, callback: () => void): void;
256
+ }, callback: () => void): this;
249
257
  /**
250
258
  * Registers middleware for a specific path.
251
259
  * @param path - The path to apply the middleware.
252
260
  * @param handler - The middleware handler.
253
261
  * @param opts - Optional middleware options.
254
262
  */
255
- middleware(path: string, handler: Middleware, opts?: MiddlewareOptions): void;
263
+ middleware(path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;
256
264
  }
257
265
  interface HttpContext {
258
266
  request: IRequest;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { H3Event, Middleware, MiddlewareOptions } from 'h3';
2
+ import { TypedHeaders, ResponseHeaderMap } from 'fetchdts';
2
3
 
3
4
  interface IServiceProvider {
4
5
  /**
@@ -117,12 +118,17 @@ interface IRequest {
117
118
  * Gets route parameters.
118
119
  * @returns An object containing route parameters.
119
120
  */
120
- params<T = Record<string, string>>(): T;
121
+ params: NonNullable<H3Event["context"]["params"]>;
121
122
  /**
122
123
  * Gets query parameters.
123
124
  * @returns An object containing query parameters.
124
125
  */
125
- query<T = Record<string, string>>(): T;
126
+ query: Record<string, any>;
127
+ /**
128
+ * Gets the request headers.
129
+ * @returns An object containing request headers.
130
+ */
131
+ headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>;
126
132
  /**
127
133
  * Gets the underlying event object or a specific property of it.
128
134
  * @param key - Optional key to access a nested property of the event.
@@ -190,46 +196,42 @@ interface IRouter {
190
196
  /**
191
197
  * Registers a GET route.
192
198
  * @param path - The route path.
193
- * @param handler - The handler function or controller class.
194
- * @param methodName - Optional controller method name.
199
+ * @param definition - The handler function or [controller class, method] array.
195
200
  * @param name - Optional route name.
196
201
  * @param middleware - Optional middleware array.
197
202
  */
198
- get(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
203
+ get(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): this;
199
204
  /**
200
205
  * Registers a POST route.
201
206
  * @param path - The route path.
202
- * @param handler - The handler function or controller class.
203
- * @param methodName - Optional controller method name.
207
+ * @param definition - The handler function or [controller class, method] array.
204
208
  * @param name - Optional route name.
205
209
  * @param middleware - Optional middleware array.
206
210
  */
207
- post(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
211
+ post(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): this;
208
212
  /**
209
213
  * Registers a PUT route.
210
214
  * @param path - The route path.
211
- * @param handler - The handler function or controller class.
212
- * @param methodName - Optional controller method name.
215
+ * @param definition - The handler function or [controller class, method] array.
213
216
  * @param name - Optional route name.
214
217
  * @param middleware - Optional middleware array.
215
218
  */
216
- put(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
219
+ put(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): this;
217
220
  /**
218
221
  * Registers a DELETE route.
219
222
  * @param path - The route path.
220
- * @param handler - The handler function or controller class.
221
- * @param methodName - Optional controller method name.
223
+ * @param definition - The handler function or [controller class, method] array.
222
224
  * @param name - Optional route name.
223
225
  * @param middleware - Optional middleware array.
224
226
  */
225
- delete(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
227
+ delete(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): this;
226
228
  /**
227
229
  * Registers an API resource with standard CRUD routes.
228
230
  * @param path - The base path for the resource.
229
231
  * @param controller - The controller class handling the resource.
230
232
  * @param middleware - Optional middleware array.
231
233
  */
232
- apiResource(path: string, controller: new (app: IApplication) => IController, middleware?: IMiddleware[]): void;
234
+ apiResource(path: string, controller: new (app: IApplication) => IController, middleware?: IMiddleware[]): this;
233
235
  /**
234
236
  * Generates a URL for a named route.
235
237
  * @param name - The name of the route.
@@ -237,6 +239,12 @@ interface IRouter {
237
239
  * @returns The generated URL or undefined if the route is not found.
238
240
  */
239
241
  route(name: string, params?: Record<string, string>): string | undefined;
242
+ /**
243
+ * Set the name of the current route
244
+ *
245
+ * @param name
246
+ */
247
+ name(name: string): this;
240
248
  /**
241
249
  * Groups routes with shared prefix or middleware.
242
250
  * @param options - Configuration for prefix or middleware.
@@ -245,14 +253,14 @@ interface IRouter {
245
253
  group(options: {
246
254
  prefix?: string;
247
255
  middleware?: EventHandler[];
248
- }, callback: () => void): void;
256
+ }, callback: () => void): this;
249
257
  /**
250
258
  * Registers middleware for a specific path.
251
259
  * @param path - The path to apply the middleware.
252
260
  * @param handler - The middleware handler.
253
261
  * @param opts - Optional middleware options.
254
262
  */
255
- middleware(path: string, handler: Middleware, opts?: MiddlewareOptions): void;
263
+ middleware(path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;
256
264
  }
257
265
  interface HttpContext {
258
266
  request: IRequest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/shared",
3
- "version": "0.7.1",
3
+ "version": "0.9.0",
4
4
  "description": "Shared Utilities.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -21,6 +21,11 @@
21
21
  "dependencies": {
22
22
  "h3": "^2.0.0-beta.1"
23
23
  },
24
+ "devDependencies": {
25
+ "fetchdts": "^0.1.6",
26
+ "install": "^0.13.0",
27
+ "pnpm": "^10.14.0"
28
+ },
24
29
  "scripts": {
25
30
  "build": "tsup",
26
31
  "barrel": "barrelsby --directory src --delete --singleQuotes",
@@ -11,66 +11,58 @@ export interface IRouter {
11
11
  /**
12
12
  * Registers a GET route.
13
13
  * @param path - The route path.
14
- * @param handler - The handler function or controller class.
15
- * @param methodName - Optional controller method name.
14
+ * @param definition - The handler function or [controller class, method] array.
16
15
  * @param name - Optional route name.
17
16
  * @param middleware - Optional middleware array.
18
17
  */
19
18
  get (
20
19
  path: string,
21
- handler: EventHandler | (new (...args: any[]) => IController),
22
- methodName?: string,
20
+ definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
23
21
  name?: string,
24
22
  middleware?: IMiddleware[]
25
- ): void;
23
+ ): this;
26
24
 
27
25
  /**
28
26
  * Registers a POST route.
29
27
  * @param path - The route path.
30
- * @param handler - The handler function or controller class.
31
- * @param methodName - Optional controller method name.
28
+ * @param definition - The handler function or [controller class, method] array.
32
29
  * @param name - Optional route name.
33
30
  * @param middleware - Optional middleware array.
34
31
  */
35
32
  post (
36
33
  path: string,
37
- handler: EventHandler | (new (...args: any[]) => IController),
38
- methodName?: string,
34
+ definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
39
35
  name?: string,
40
36
  middleware?: IMiddleware[]
41
- ): void;
37
+ ): this;
42
38
 
43
39
  /**
44
40
  * Registers a PUT route.
45
41
  * @param path - The route path.
46
- * @param handler - The handler function or controller class.
47
- * @param methodName - Optional controller method name.
42
+ * @param definition - The handler function or [controller class, method] array.
48
43
  * @param name - Optional route name.
49
44
  * @param middleware - Optional middleware array.
50
45
  */
51
46
  put (
52
47
  path: string,
53
- handler: EventHandler | (new (...args: any[]) => IController),
54
- methodName?: string,
48
+ definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
55
49
  name?: string,
56
50
  middleware?: IMiddleware[]
57
- ): void;
51
+ ): this;
58
52
 
59
53
  /**
60
54
  * Registers a DELETE route.
61
55
  * @param path - The route path.
62
- * @param handler - The handler function or controller class.
63
- * @param methodName - Optional controller method name.
56
+ * @param definition - The handler function or [controller class, method] array.
64
57
  * @param name - Optional route name.
65
58
  * @param middleware - Optional middleware array.
66
59
  */
67
60
  delete (
68
61
  path: string,
69
- handler: EventHandler | (new (...args: any[]) => IController),
70
- methodName?: string,
62
+ definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
71
63
  name?: string,
72
64
  middleware?: IMiddleware[]
73
- ): void;
65
+ ): this;
74
66
 
75
67
  /**
76
68
  * Registers an API resource with standard CRUD routes.
@@ -82,7 +74,7 @@ export interface IRouter {
82
74
  path: string,
83
75
  controller: new (app: IApplication) => IController,
84
76
  middleware?: IMiddleware[]
85
- ): void;
77
+ ): this;
86
78
 
87
79
  /**
88
80
  * Generates a URL for a named route.
@@ -92,12 +84,20 @@ export interface IRouter {
92
84
  */
93
85
  route (name: string, params?: Record<string, string>): string | undefined;
94
86
 
87
+
88
+ /**
89
+ * Set the name of the current route
90
+ *
91
+ * @param name
92
+ */
93
+ name (name: string): this
94
+
95
95
  /**
96
96
  * Groups routes with shared prefix or middleware.
97
97
  * @param options - Configuration for prefix or middleware.
98
98
  * @param callback - Callback function defining grouped routes.
99
99
  */
100
- group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => void): void;
100
+ group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => void): this;
101
101
 
102
102
  /**
103
103
  * Registers middleware for a specific path.
@@ -105,7 +105,7 @@ export interface IRouter {
105
105
  * @param handler - The middleware handler.
106
106
  * @param opts - Optional middleware options.
107
107
  */
108
- middleware (path: string, handler: Middleware, opts?: MiddlewareOptions): void;
108
+ middleware (path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;
109
109
  }
110
110
 
111
111
  export interface HttpContext {
@@ -1,4 +1,5 @@
1
1
  import { DotNestedKeys, DotNestedValue } from './ObjContract'
2
+ import type { ResponseHeaderMap, TypedHeaders } from 'fetchdts'
2
3
 
3
4
  import type { H3Event } from 'h3'
4
5
 
@@ -24,13 +25,19 @@ export interface IRequest {
24
25
  * Gets route parameters.
25
26
  * @returns An object containing route parameters.
26
27
  */
27
- params<T = Record<string, string>> (): T;
28
+ params: NonNullable<H3Event["context"]["params"]>;
28
29
 
29
30
  /**
30
31
  * Gets query parameters.
31
32
  * @returns An object containing query parameters.
32
33
  */
33
- query<T = Record<string, string>> (): T;
34
+ query: Record<string, any>;
35
+
36
+ /**
37
+ * Gets the request headers.
38
+ * @returns An object containing request headers.
39
+ */
40
+ headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>;
34
41
 
35
42
  /**
36
43
  * Gets the underlying event object or a specific property of it.