@server/next 0.22.1 → 0.23.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.
Files changed (2) hide show
  1. package/index.d.ts +48 -14
  2. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -18,12 +18,34 @@ type ServerOptions = {
18
18
  auth?: string | { type: string; provider: string };
19
19
  };
20
20
 
21
- type Context = {
21
+ type ExtractPathParams<Path extends string> =
22
+ Path extends `${string}:${infer Param}/${infer Rest}`
23
+ ? Param | ExtractPathParams<`/${Rest}`>
24
+ : Path extends `${string}:${infer Param}`
25
+ ? Param
26
+ : never;
27
+
28
+ type ParamsToObject<Params extends string> = {
29
+ [K in Params as K extends `${infer Key}?`
30
+ ? Key
31
+ : K]: K extends `${infer Key}?` ? string | undefined : string;
32
+ };
33
+
34
+ type PathToParams<Path extends string> = ParamsToObject<
35
+ ExtractPathParams<Path>
36
+ >;
37
+
38
+ type Simplify<T> = T extends object ? { [K in keyof T]: T[K] } : T;
39
+
40
+ type Context<Path extends string = string> = {
22
41
  method: Method;
23
42
  headers: { [key: string]: string | string[] };
24
43
  cookies: { [key: string]: any };
25
44
  body?: any;
26
- url: URL & { params: {}; query: {} };
45
+ url: URL & {
46
+ params: Simplify<PathToParams<Path>>; // Simplify here
47
+ query: {};
48
+ };
27
49
  options: ServerOptions;
28
50
  };
29
51
 
@@ -57,28 +79,40 @@ type InlineReply =
57
79
  | number
58
80
  | void;
59
81
 
60
- type Middleware = (ctx: Context) => InlineReply;
61
-
62
- type Router = {};
82
+ type Middleware<Path extends string = string> = (
83
+ ctx: Context<Path>,
84
+ ) => InlineReply;
85
+
86
+ declare interface Router {
87
+ get<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
88
+ head<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
89
+ post<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
90
+ put<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
91
+ patch<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
92
+ del<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
93
+ options<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
94
+ }
63
95
 
64
96
  declare interface Server {
65
97
  (options?: ServerOptions): this;
66
98
 
67
- socket(path: string, ...middleware: Middleware[]): this;
68
- get(path: string, ...middleware: Middleware[]): this;
69
- head(path: string, ...middleware: Middleware[]): this;
70
- post(path: string, ...middleware: Middleware[]): this;
71
- put(path: string, ...middleware: Middleware[]): this;
72
- patch(path: string, ...middleware: Middleware[]): this;
73
- del(path: string, ...middleware: Middleware[]): this;
74
- options(path: string, ...middleware: Middleware[]): this;
99
+ socket(path: string, ...middle: Middleware[]): this;
100
+
101
+ get<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
102
+ head<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
103
+ post<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
104
+ put<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
105
+ patch<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
106
+ del<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
107
+ options<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
75
108
 
76
- use(...middleware: Middleware[]): this;
109
+ use(...middle: Middleware[]): this;
77
110
  router(router: Router): this;
78
111
  }
79
112
 
80
113
  type headers = (obj?: Headers) => any;
81
114
 
82
115
  declare const server: Server;
116
+ export const router: Router;
83
117
  export const headers: headers;
84
118
  export default server;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.22.1",
3
+ "version": "0.23.0",
4
4
  "description": "A fully-fledged web server with routing, file uploads, sessions, static files, schema validation, websockets, testing, etc.",
5
5
  "homepage": "https://server-js.com/",
6
6
  "repository": "https://github.com/franciscop/server-next.git",