expediate 0.0.3 → 1.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.
@@ -0,0 +1,224 @@
1
+ import * as http from 'http';
2
+ /** A key-value map of arbitrary string values. */
3
+ type StringMap = Record<string, string>;
4
+ /**
5
+ * Extended HTTP incoming message that carries parsed routing metadata.
6
+ * Augments the standard `http.IncomingMessage` with fields populated by
7
+ * the router before any middleware is invoked.
8
+ */
9
+ interface RouterRequest extends http.IncomingMessage {
10
+ /** The original, unmodified URL string from the HTTP request. */
11
+ originalUrl: string;
12
+ /**
13
+ * The current path being matched. Prefix-style layers (`use`)
14
+ * rewrite this field after matching so that nested routers only see the
15
+ * remaining suffix. Exact-method layers (`all`, `get`, `post`, etc.) leave it
16
+ * untouched so that chained middlewares sharing the same path each match.
17
+ */
18
+ path: string;
19
+ /**
20
+ * Aggregated parameters from all sources.
21
+ * URL query parameters are merged first; named route parameters (from both
22
+ * plain-string and RegExp patterns) override them when a route matches.
23
+ */
24
+ params: StringMap;
25
+ /**
26
+ * Structured query buckets:
27
+ * - `url` — parameters parsed from the query string.
28
+ * - `route` — named parameters captured from the route pattern.
29
+ */
30
+ queries: {
31
+ url?: StringMap;
32
+ route?: StringMap;
33
+ };
34
+ /** Parsed cookies sent with the request. */
35
+ cookies: StringMap;
36
+ }
37
+ /**
38
+ * Extended HTTP server response with convenience helpers.
39
+ * Augments the standard `http.ServerResponse`.
40
+ */
41
+ interface RouterResponse extends http.ServerResponse {
42
+ /**
43
+ * Write `data` (if provided) and end the response.
44
+ * Equivalent to `res.write(data); res.end()`.
45
+ */
46
+ send(data?: string): void;
47
+ /**
48
+ * Set the HTTP status code and optional response headers, then return
49
+ * `this` so calls can be chained (e.g. `res.status(404).end(...)`).
50
+ */
51
+ status(code: number, headers?: StringMap): this;
52
+ /** Redirect the client to `url` with a 302 Found response. */
53
+ redirect(url: string): void;
54
+ /**
55
+ * Append a `Set-Cookie` header for the given `name`/`value` pair.
56
+ * Returns `this` to allow chaining.
57
+ */
58
+ cookie(name: string, value: string | object, options?: CookieOptions): this;
59
+ }
60
+ /** Options accepted by `res.cookie()`. */
61
+ interface CookieOptions {
62
+ /** Sign the cookie value (requires a secret on the request). */
63
+ signed?: boolean;
64
+ /** Expiry date for the cookie. */
65
+ expires?: Date;
66
+ /** Max age in milliseconds; converted to seconds in the `Set-Cookie` header. */
67
+ maxAge?: number;
68
+ /** Cookie path (defaults to `'/'`). */
69
+ path?: string;
70
+ }
71
+ /** Options for HTTPS servers passed to `router.listen()`. */
72
+ interface TlsOptions {
73
+ key: string | Buffer;
74
+ cert: string | Buffer;
75
+ [key: string]: unknown;
76
+ }
77
+ /**
78
+ * A middleware function. Receives the current request and response objects
79
+ * and a `next` callback to hand control to the next registered middleware.
80
+ */
81
+ type Middleware = (req: RouterRequest, res: RouterResponse, next: NextFunction) => void;
82
+ /** Callback used to pass control to the next matching middleware. */
83
+ type NextFunction = () => void;
84
+ /**
85
+ * A value that can be registered as a route handler: a single `Middleware`
86
+ * function, a `Router` instance (whose `listener` will be used), or an array
87
+ * of either. Arrays may not be nested.
88
+ */
89
+ type MiddlewareArg = Middleware | Router | (Middleware | Router)[];
90
+ /**
91
+ * Internal representation of a single registered route.
92
+ * One layer is created per middleware function for every `router.get(...)` /
93
+ * `router.use(...)` etc. call, and stored in the route table.
94
+ */
95
+ interface Layer {
96
+ /** HTTP method this layer is restricted to, or `null` for any method. */
97
+ method: string | null;
98
+ /** The original path pattern supplied by the caller. */
99
+ path: string | RegExp;
100
+ /**
101
+ * The compiled `RegExp` used for matching, regardless of whether the
102
+ * original `path` was a plain string, a glob, or already a `RegExp`.
103
+ *
104
+ * - Plain strings (e.g. `'/users/:id'`) are compiled with named capture
105
+ * groups so that `:id` becomes `(?<id>[^/]+)`.
106
+ * - Glob strings (e.g. `'/**\/*.php'`) are compiled following `.gitignore`
107
+ * wildcard rules.
108
+ * - `RegExp` values are stored as-is; named groups are surfaced directly
109
+ * as route parameters.
110
+ */
111
+ regex: RegExp;
112
+ /**
113
+ * When `true`, the portion of `req.path` consumed by `layer.regex` is
114
+ * stripped before invoking the middleware, exposing only the remaining
115
+ * suffix to nested routers.
116
+ *
117
+ * Set to `true` for prefix-style registrations (`use`) and
118
+ * `false` for exact-method routes (`all`, `get`, `post`, etc.). Stripping on
119
+ * exact-match routes would break chained middlewares sharing the same path,
120
+ * because each subsequent layer would see a truncated path that no longer
121
+ * matches its own pattern.
122
+ */
123
+ stripPath: boolean;
124
+ /** The middleware function to invoke when the layer matches. */
125
+ middleware: Middleware;
126
+ }
127
+ /**
128
+ * The public interface of the object returned by `createRouter()`.
129
+ *
130
+ * All route-registration methods share the same uniform signature: a mandatory
131
+ * `path` argument followed by any number of `MiddlewareArg` values.
132
+ * A `MiddlewareArg` may be a `Middleware` function, a `Router` instance
133
+ * (whose `listener` is used automatically), or an array of either.
134
+ */
135
+ interface Router {
136
+ /**
137
+ * Register middleware for all HTTP methods, scoped to `path`.
138
+ * The matched path prefix is stripped from `req.path` before the middleware
139
+ * is invoked, so nested routers only see the remaining suffix.
140
+ * Equivalent to Express's `app.use()`.
141
+ */
142
+ use(path: string | RegExp, ...args: MiddlewareArg[]): void;
143
+ /**
144
+ * Register middleware for all HTTP methods.
145
+ * Unlike `use`, it doesn't strips the matched prefix from `req.path`.
146
+ */
147
+ all(path: string | RegExp, ...args: MiddlewareArg[]): void;
148
+ /** Register middleware for `GET` requests. */
149
+ get(path: string | RegExp, ...args: MiddlewareArg[]): void;
150
+ /** Register middleware for `PUT` requests. */
151
+ put(path: string | RegExp, ...args: MiddlewareArg[]): void;
152
+ /** Register middleware for `POST` requests. */
153
+ post(path: string | RegExp, ...args: MiddlewareArg[]): void;
154
+ /** Register middleware for `DELETE` requests. */
155
+ delete(path: string | RegExp, ...args: MiddlewareArg[]): void;
156
+ /** Register middleware for `PATCH` requests. */
157
+ patch(path: string | RegExp, ...args: MiddlewareArg[]): void;
158
+ /**
159
+ * Start listening on the given port.
160
+ * When `opts` contains both `key` and `cert`, an HTTPS server is created;
161
+ * otherwise a plain HTTP server is used.
162
+ */
163
+ listen(port: number, opts?: TlsOptions | (() => void), cb?: () => void): void;
164
+ /**
165
+ * The underlying `(req, res, next)` function, allowing this router to be
166
+ * mounted as middleware inside another router:
167
+ * `parent.use('/api', child)`.
168
+ */
169
+ readonly listener: Middleware;
170
+ }
171
+ /**
172
+ * Create and return a new `Router` instance.
173
+ *
174
+ * The returned object exposes an Express-compatible routing API and doubles
175
+ * as a middleware function itself (via `router.listener`), so it can be
176
+ * mounted inside another router:
177
+ * ```ts
178
+ * parent.use('/api', child);
179
+ * ```
180
+ *
181
+ * **Path patterns** accepted by all route-registration methods:
182
+ * - Plain strings with optional `:name` segments — e.g. `'/users/:id'`.
183
+ * Each `:name` is compiled to a named capture group and exposed in
184
+ * `req.params` on a match.
185
+ * - Glob strings following `.gitignore` rules — e.g. `'/**\/*.php'`.
186
+ * Supported wildcards: `?` (one non-slash char), `*` (any non-slash chars),
187
+ * `**` (any chars, including slashes).
188
+ * - `RegExp` objects — used directly; named groups become route parameters.
189
+ *
190
+ * **Middleware arguments** accept any number of variadic `MiddlewareArg`
191
+ * values, each of which may be:
192
+ * - A `Middleware` function.
193
+ * - A `Router` instance (its `listener` is registered automatically).
194
+ * - An array of either of the above.
195
+ *
196
+ * **Path stripping behaviour:**
197
+ * - `use` — strip the matched path prefix from `req.path` before
198
+ * invoking middleware. Nested routers therefore only see the remaining suffix.
199
+ * - `all` / `get` / `post` / `put` / `delete` / `patch` — leave `req.path` intact
200
+ * so that multiple middlewares registered for the same exact path can each
201
+ * match and be invoked in sequence via `next()`.
202
+ *
203
+ * @returns A fully initialised `Router` ready to register routes and
204
+ * optionally start an HTTP or HTTPS server.
205
+ *
206
+ * @example
207
+ * ```ts
208
+ * const auth = createRouter();
209
+ * auth.post('/login', handleLogin);
210
+ * auth.post('/logout', handleLogout);
211
+ *
212
+ * const app = createRouter();
213
+ * app.use('/auth', auth); // mount a sub-router
214
+ * app.get('/users/:id', requireAuth, getUser); // multiple middleware
215
+ * app.get('/**\/*.php', (req, res) => // glob pattern
216
+ * res.status(403).send('Forbidden'));
217
+ *
218
+ * app.listen(3000, () => console.log('Listening on :3000'));
219
+ * ```
220
+ */
221
+ declare function createRouter(): Router;
222
+ export default createRouter;
223
+ export type { Router, RouterRequest, RouterResponse, Middleware, MiddlewareArg, NextFunction, Layer, CookieOptions, TlsOptions, StringMap, };
224
+ //# sourceMappingURL=router.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAO7B,kDAAkD;AAClD,KAAK,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAExC;;;;GAIG;AACH,UAAU,aAAc,SAAQ,IAAI,CAAC,eAAe;IAClD,iEAAiE;IACjE,WAAW,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,MAAM,EAAE,SAAS,CAAC;IAClB;;;;OAIG;IACH,OAAO,EAAE;QACP,GAAG,CAAC,EAAE,SAAS,CAAC;QAChB,KAAK,CAAC,EAAE,SAAS,CAAC;KACnB,CAAC;IACF,4CAA4C;IAC5C,OAAO,EAAE,SAAS,CAAC;CACpB;AAED;;;GAGG;AACH,UAAU,cAAe,SAAQ,IAAI,CAAC,cAAc;IAClD;;;OAGG;IACH,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,GAAG,IAAI,CAAC;IAChD,8DAA8D;IAC9D,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KA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