@h3ravel/shared 0.8.0 → 0.10.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.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - feat: hide RouterEnd from itslelf to prevent unintentional chaining.
8
+
9
+ ## 0.9.0
10
+
11
+ ### Minor Changes
12
+
13
+ - refactor!: make route definition more similar to the way it is handled in laravel.
14
+
3
15
  ## 0.8.0
4
16
 
5
17
  ### Minor Changes
package/dist/index.d.cts CHANGED
@@ -189,6 +189,7 @@ interface IResponse {
189
189
  getEvent<K extends DotNestedKeys<H3Event>>(key: K): DotNestedValue<H3Event, K>;
190
190
  }
191
191
 
192
+ type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'apiResource' | 'name' | 'group' | 'route';
192
193
  /**
193
194
  * Interface for the Router contract, defining methods for HTTP routing.
194
195
  */
@@ -196,46 +197,42 @@ interface IRouter {
196
197
  /**
197
198
  * Registers a GET route.
198
199
  * @param path - The route path.
199
- * @param handler - The handler function or controller class.
200
- * @param methodName - Optional controller method name.
200
+ * @param definition - The handler function or [controller class, method] array.
201
201
  * @param name - Optional route name.
202
202
  * @param middleware - Optional middleware array.
203
203
  */
204
- get(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
204
+ get(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
205
205
  /**
206
206
  * Registers a POST route.
207
207
  * @param path - The route path.
208
- * @param handler - The handler function or controller class.
209
- * @param methodName - Optional controller method name.
208
+ * @param definition - The handler function or [controller class, method] array.
210
209
  * @param name - Optional route name.
211
210
  * @param middleware - Optional middleware array.
212
211
  */
213
- post(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
212
+ post(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
214
213
  /**
215
214
  * Registers a PUT route.
216
215
  * @param path - The route path.
217
- * @param handler - The handler function or controller class.
218
- * @param methodName - Optional controller method name.
216
+ * @param definition - The handler function or [controller class, method] array.
219
217
  * @param name - Optional route name.
220
218
  * @param middleware - Optional middleware array.
221
219
  */
222
- put(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
220
+ put(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
223
221
  /**
224
222
  * Registers a DELETE route.
225
223
  * @param path - The route path.
226
- * @param handler - The handler function or controller class.
227
- * @param methodName - Optional controller method name.
224
+ * @param definition - The handler function or [controller class, method] array.
228
225
  * @param name - Optional route name.
229
226
  * @param middleware - Optional middleware array.
230
227
  */
231
- delete(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
228
+ delete(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
232
229
  /**
233
230
  * Registers an API resource with standard CRUD routes.
234
231
  * @param path - The base path for the resource.
235
232
  * @param controller - The controller class handling the resource.
236
233
  * @param middleware - Optional middleware array.
237
234
  */
238
- apiResource(path: string, controller: new (app: IApplication) => IController, middleware?: IMiddleware[]): void;
235
+ apiResource(path: string, controller: new (app: IApplication) => IController, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
239
236
  /**
240
237
  * Generates a URL for a named route.
241
238
  * @param name - The name of the route.
@@ -243,6 +240,12 @@ interface IRouter {
243
240
  * @returns The generated URL or undefined if the route is not found.
244
241
  */
245
242
  route(name: string, params?: Record<string, string>): string | undefined;
243
+ /**
244
+ * Set the name of the current route
245
+ *
246
+ * @param name
247
+ */
248
+ name(name: string): this;
246
249
  /**
247
250
  * Groups routes with shared prefix or middleware.
248
251
  * @param options - Configuration for prefix or middleware.
@@ -251,14 +254,14 @@ interface IRouter {
251
254
  group(options: {
252
255
  prefix?: string;
253
256
  middleware?: EventHandler[];
254
- }, callback: () => void): void;
257
+ }, callback: () => void): this;
255
258
  /**
256
259
  * Registers middleware for a specific path.
257
260
  * @param path - The path to apply the middleware.
258
261
  * @param handler - The middleware handler.
259
262
  * @param opts - Optional middleware options.
260
263
  */
261
- middleware(path: string, handler: Middleware, opts?: MiddlewareOptions): void;
264
+ middleware(path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;
262
265
  }
263
266
  interface HttpContext {
264
267
  request: IRequest;
@@ -287,4 +290,4 @@ interface IMiddleware {
287
290
  handle(context: HttpContext, next: () => Promise<any>): Promise<any>;
288
291
  }
289
292
 
290
- export type { DotFlatten, DotNestedKeys, DotNestedValue, EventHandler, HttpContext, IApplication, IController, IMiddleware, IPathName, IRequest, IResponse, IRouter, IServiceProvider };
293
+ export type { DotFlatten, DotNestedKeys, DotNestedValue, EventHandler, HttpContext, IApplication, IController, IMiddleware, IPathName, IRequest, IResponse, IRouter, IServiceProvider, RouterEnd };
package/dist/index.d.ts CHANGED
@@ -189,6 +189,7 @@ interface IResponse {
189
189
  getEvent<K extends DotNestedKeys<H3Event>>(key: K): DotNestedValue<H3Event, K>;
190
190
  }
191
191
 
192
+ type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'apiResource' | 'name' | 'group' | 'route';
192
193
  /**
193
194
  * Interface for the Router contract, defining methods for HTTP routing.
194
195
  */
@@ -196,46 +197,42 @@ interface IRouter {
196
197
  /**
197
198
  * Registers a GET route.
198
199
  * @param path - The route path.
199
- * @param handler - The handler function or controller class.
200
- * @param methodName - Optional controller method name.
200
+ * @param definition - The handler function or [controller class, method] array.
201
201
  * @param name - Optional route name.
202
202
  * @param middleware - Optional middleware array.
203
203
  */
204
- get(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
204
+ get(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
205
205
  /**
206
206
  * Registers a POST route.
207
207
  * @param path - The route path.
208
- * @param handler - The handler function or controller class.
209
- * @param methodName - Optional controller method name.
208
+ * @param definition - The handler function or [controller class, method] array.
210
209
  * @param name - Optional route name.
211
210
  * @param middleware - Optional middleware array.
212
211
  */
213
- post(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
212
+ post(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
214
213
  /**
215
214
  * Registers a PUT route.
216
215
  * @param path - The route path.
217
- * @param handler - The handler function or controller class.
218
- * @param methodName - Optional controller method name.
216
+ * @param definition - The handler function or [controller class, method] array.
219
217
  * @param name - Optional route name.
220
218
  * @param middleware - Optional middleware array.
221
219
  */
222
- put(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
220
+ put(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
223
221
  /**
224
222
  * Registers a DELETE route.
225
223
  * @param path - The route path.
226
- * @param handler - The handler function or controller class.
227
- * @param methodName - Optional controller method name.
224
+ * @param definition - The handler function or [controller class, method] array.
228
225
  * @param name - Optional route name.
229
226
  * @param middleware - Optional middleware array.
230
227
  */
231
- delete(path: string, handler: EventHandler | (new (...args: any[]) => IController), methodName?: string, name?: string, middleware?: IMiddleware[]): void;
228
+ delete(path: string, definition: EventHandler | [(new (...args: any[]) => IController), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
232
229
  /**
233
230
  * Registers an API resource with standard CRUD routes.
234
231
  * @param path - The base path for the resource.
235
232
  * @param controller - The controller class handling the resource.
236
233
  * @param middleware - Optional middleware array.
237
234
  */
238
- apiResource(path: string, controller: new (app: IApplication) => IController, middleware?: IMiddleware[]): void;
235
+ apiResource(path: string, controller: new (app: IApplication) => IController, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
239
236
  /**
240
237
  * Generates a URL for a named route.
241
238
  * @param name - The name of the route.
@@ -243,6 +240,12 @@ interface IRouter {
243
240
  * @returns The generated URL or undefined if the route is not found.
244
241
  */
245
242
  route(name: string, params?: Record<string, string>): string | undefined;
243
+ /**
244
+ * Set the name of the current route
245
+ *
246
+ * @param name
247
+ */
248
+ name(name: string): this;
246
249
  /**
247
250
  * Groups routes with shared prefix or middleware.
248
251
  * @param options - Configuration for prefix or middleware.
@@ -251,14 +254,14 @@ interface IRouter {
251
254
  group(options: {
252
255
  prefix?: string;
253
256
  middleware?: EventHandler[];
254
- }, callback: () => void): void;
257
+ }, callback: () => void): this;
255
258
  /**
256
259
  * Registers middleware for a specific path.
257
260
  * @param path - The path to apply the middleware.
258
261
  * @param handler - The middleware handler.
259
262
  * @param opts - Optional middleware options.
260
263
  */
261
- middleware(path: string, handler: Middleware, opts?: MiddlewareOptions): void;
264
+ middleware(path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;
262
265
  }
263
266
  interface HttpContext {
264
267
  request: IRequest;
@@ -287,4 +290,4 @@ interface IMiddleware {
287
290
  handle(context: HttpContext, next: () => Promise<any>): Promise<any>;
288
291
  }
289
292
 
290
- export type { DotFlatten, DotNestedKeys, DotNestedValue, EventHandler, HttpContext, IApplication, IController, IMiddleware, IPathName, IRequest, IResponse, IRouter, IServiceProvider };
293
+ export type { DotFlatten, DotNestedKeys, DotNestedValue, EventHandler, HttpContext, IApplication, IController, IMiddleware, IPathName, IRequest, IResponse, IRouter, IServiceProvider, RouterEnd };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/shared",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Shared Utilities.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -4,6 +4,8 @@ import { IApplication } from './IApplication'
4
4
  import { IRequest } from './IRequest'
5
5
  import { IResponse } from './IResponse'
6
6
 
7
+ export type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'apiResource' | 'name' | 'group' | 'route';
8
+
7
9
  /**
8
10
  * Interface for the Router contract, defining methods for HTTP routing.
9
11
  */
@@ -11,66 +13,58 @@ export interface IRouter {
11
13
  /**
12
14
  * Registers a GET route.
13
15
  * @param path - The route path.
14
- * @param handler - The handler function or controller class.
15
- * @param methodName - Optional controller method name.
16
+ * @param definition - The handler function or [controller class, method] array.
16
17
  * @param name - Optional route name.
17
18
  * @param middleware - Optional middleware array.
18
19
  */
19
20
  get (
20
21
  path: string,
21
- handler: EventHandler | (new (...args: any[]) => IController),
22
- methodName?: string,
22
+ definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
23
23
  name?: string,
24
24
  middleware?: IMiddleware[]
25
- ): void;
25
+ ): Omit<this, RouterEnd>;
26
26
 
27
27
  /**
28
28
  * Registers a POST route.
29
29
  * @param path - The route path.
30
- * @param handler - The handler function or controller class.
31
- * @param methodName - Optional controller method name.
30
+ * @param definition - The handler function or [controller class, method] array.
32
31
  * @param name - Optional route name.
33
32
  * @param middleware - Optional middleware array.
34
33
  */
35
34
  post (
36
35
  path: string,
37
- handler: EventHandler | (new (...args: any[]) => IController),
38
- methodName?: string,
36
+ definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
39
37
  name?: string,
40
38
  middleware?: IMiddleware[]
41
- ): void;
39
+ ): Omit<this, RouterEnd>;
42
40
 
43
41
  /**
44
42
  * Registers a PUT route.
45
43
  * @param path - The route path.
46
- * @param handler - The handler function or controller class.
47
- * @param methodName - Optional controller method name.
44
+ * @param definition - The handler function or [controller class, method] array.
48
45
  * @param name - Optional route name.
49
46
  * @param middleware - Optional middleware array.
50
47
  */
51
48
  put (
52
49
  path: string,
53
- handler: EventHandler | (new (...args: any[]) => IController),
54
- methodName?: string,
50
+ definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
55
51
  name?: string,
56
52
  middleware?: IMiddleware[]
57
- ): void;
53
+ ): Omit<this, RouterEnd>;
58
54
 
59
55
  /**
60
56
  * Registers a DELETE route.
61
57
  * @param path - The route path.
62
- * @param handler - The handler function or controller class.
63
- * @param methodName - Optional controller method name.
58
+ * @param definition - The handler function or [controller class, method] array.
64
59
  * @param name - Optional route name.
65
60
  * @param middleware - Optional middleware array.
66
61
  */
67
62
  delete (
68
63
  path: string,
69
- handler: EventHandler | (new (...args: any[]) => IController),
70
- methodName?: string,
64
+ definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
71
65
  name?: string,
72
66
  middleware?: IMiddleware[]
73
- ): void;
67
+ ): Omit<this, RouterEnd>;
74
68
 
75
69
  /**
76
70
  * Registers an API resource with standard CRUD routes.
@@ -82,7 +76,7 @@ export interface IRouter {
82
76
  path: string,
83
77
  controller: new (app: IApplication) => IController,
84
78
  middleware?: IMiddleware[]
85
- ): void;
79
+ ): Omit<this, RouterEnd>;
86
80
 
87
81
  /**
88
82
  * Generates a URL for a named route.
@@ -92,12 +86,20 @@ export interface IRouter {
92
86
  */
93
87
  route (name: string, params?: Record<string, string>): string | undefined;
94
88
 
89
+
90
+ /**
91
+ * Set the name of the current route
92
+ *
93
+ * @param name
94
+ */
95
+ name (name: string): this
96
+
95
97
  /**
96
98
  * Groups routes with shared prefix or middleware.
97
99
  * @param options - Configuration for prefix or middleware.
98
100
  * @param callback - Callback function defining grouped routes.
99
101
  */
100
- group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => void): void;
102
+ group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => void): this;
101
103
 
102
104
  /**
103
105
  * Registers middleware for a specific path.
@@ -105,7 +107,7 @@ export interface IRouter {
105
107
  * @param handler - The middleware handler.
106
108
  * @param opts - Optional middleware options.
107
109
  */
108
- middleware (path: string, handler: Middleware, opts?: MiddlewareOptions): void;
110
+ middleware (path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;
109
111
  }
110
112
 
111
113
  export interface HttpContext {