@tinyhttp/app 2.0.6 → 2.0.12

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/dist/app.d.ts CHANGED
@@ -18,7 +18,7 @@ export declare type AppSettings = Partial<{
18
18
  * Function that processes the template
19
19
  */
20
20
  export declare type TemplateFunc<O> = (path: string, locals: Record<string, any>, opts: TemplateEngineOptions<O>, cb: (err: Error, html: unknown) => void) => void;
21
- export declare type TemplateEngineOptions<O extends any> = Partial<{
21
+ export declare type TemplateEngineOptions<O> = Partial<{
22
22
  cache: boolean;
23
23
  ext: string;
24
24
  renderOptions: Partial<O>;
@@ -67,7 +67,7 @@ export declare class App<RenderOptions = any, Req extends Request = Request, Res
67
67
  * @param setting setting name
68
68
  * @param value setting value
69
69
  */
70
- set(setting: string, value: any): this;
70
+ set<T = unknown>(setting: string, value: T): this;
71
71
  /**
72
72
  * Enable app setting
73
73
  * @param setting Setting name
@@ -85,7 +85,7 @@ export declare class App<RenderOptions = any, Req extends Request = Request, Res
85
85
  * @param options Template engine options
86
86
  * @param cb Callback that consumes error and html
87
87
  */
88
- render(file: string, data: Record<string, any>, cb: (err: unknown, html: unknown) => void, options?: TemplateEngineOptions<RenderOptions>): this;
88
+ render(file: string, data: Record<string, unknown>, cb: (err: unknown, html: unknown) => void, options?: TemplateEngineOptions<RenderOptions>): this;
89
89
  use(...args: UseMethodParams<Req, Res, App>): this;
90
90
  /**
91
91
  * Register a template engine with extension
package/dist/index.js CHANGED
@@ -223,12 +223,11 @@ class App extends Router {
223
223
  var _a;
224
224
  const base = args[0];
225
225
  const fns = args.slice(1).flat();
226
- if (base instanceof App) {
227
- // Set App parent to current App
228
- base.parent = this;
229
- // Mount on root
230
- base.mountpath = '/';
231
- this.apps['/'] = base;
226
+ if (typeof base === 'function' || base instanceof App) {
227
+ fns.unshift(base);
228
+ }
229
+ else if (Array.isArray(base)) {
230
+ fns.unshift(...base);
232
231
  }
233
232
  const path = typeof base === 'string' ? base : '/';
234
233
  let regex;
@@ -240,40 +239,29 @@ class App extends Router {
240
239
  fn.parent = this;
241
240
  }
242
241
  }
243
- if (base === '/') {
244
- for (const fn of fns)
245
- super.use(base, mount(fn));
246
- }
247
- else if (typeof base === 'function' || base instanceof App) {
248
- super.use('/', [base, ...fns].map(mount));
249
- }
250
- else if (Array.isArray(base)) {
251
- super.use('/', [...base, ...fns].map(mount));
252
- }
253
- else {
254
- const handlerPaths = [];
255
- const handlerFunctions = [];
256
- for (const fn of fns) {
257
- if (fn instanceof App && ((_a = fn.middleware) === null || _a === void 0 ? void 0 : _a.length)) {
258
- for (const mw of fn.middleware) {
259
- handlerPaths.push(lead(base) + lead(mw.path));
260
- handlerFunctions.push(fn);
261
- }
262
- }
263
- else {
264
- handlerPaths.push('');
242
+ const handlerPaths = [];
243
+ const handlerFunctions = [];
244
+ const handlerPathBase = path === '/' ? '' : lead(path);
245
+ for (const fn of fns) {
246
+ if (fn instanceof App && ((_a = fn.middleware) === null || _a === void 0 ? void 0 : _a.length)) {
247
+ for (const mw of fn.middleware) {
248
+ handlerPaths.push(handlerPathBase + lead(mw.path));
265
249
  handlerFunctions.push(fn);
266
250
  }
267
251
  }
268
- pushMiddleware(this.middleware)({
269
- path: base,
270
- regex,
271
- type: 'mw',
272
- handler: mount(handlerFunctions[0]),
273
- handlers: handlerFunctions.slice(1).map(mount),
274
- fullPaths: handlerPaths
275
- });
252
+ else {
253
+ handlerPaths.push('');
254
+ handlerFunctions.push(fn);
255
+ }
276
256
  }
257
+ pushMiddleware(this.middleware)({
258
+ path,
259
+ regex,
260
+ type: 'mw',
261
+ handler: mount(handlerFunctions[0]),
262
+ handlers: handlerFunctions.slice(1).map(mount),
263
+ fullPaths: handlerPaths
264
+ });
277
265
  return this;
278
266
  }
279
267
  /**
package/dist/request.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage } from 'http';
3
3
  import { ParsedUrlQuery } from 'querystring';
4
- import { Ranges } from 'header-range-parser';
4
+ import { Options, Ranges } from 'header-range-parser';
5
5
  import { App } from './app';
6
6
  import type { Middleware, Handler } from '@tinyhttp/router';
7
7
  import type { Response } from './response';
8
8
  import type { URLParams } from '@tinyhttp/req';
9
9
  export { getURLParams } from '@tinyhttp/req';
10
- export declare const getRouteFromApp: ({ middleware }: App, h: Handler<Request, Response>) => Middleware<Request, Response<any>>;
10
+ export declare const getRouteFromApp: ({ middleware }: App, h: Handler<Request, Response>) => Middleware<Request, Response>;
11
11
  export declare const getProtocol: (req: Request) => Protocol;
12
12
  export declare const getHostname: (req: Request) => string | undefined;
13
13
  export declare const getIP: (req: Pick<IncomingMessage, 'headers' | 'connection'>) => string | undefined;
@@ -18,7 +18,7 @@ export declare type Connection = IncomingMessage['socket'] & {
18
18
  };
19
19
  export declare type Protocol = 'http' | 'https' | string;
20
20
  export type { URLParams };
21
- declare type AcceptsReturns = string | false | string[];
21
+ declare type AcceptsReturns = string | boolean | string[];
22
22
  export interface Request extends IncomingMessage {
23
23
  originalUrl: string;
24
24
  path: string;
@@ -35,7 +35,7 @@ export interface Request extends IncomingMessage {
35
35
  ips?: string[];
36
36
  subdomains?: string[];
37
37
  get: (header: string) => string | string[] | undefined;
38
- range: (size: number, options?: any) => -1 | -2 | -3 | Ranges | undefined;
38
+ range: (size: number, options?: Options) => -1 | -2 | -3 | Ranges | undefined;
39
39
  accepts: (...types: string[]) => AcceptsReturns;
40
40
  acceptsEncodings: (...encodings: string[]) => AcceptsReturns;
41
41
  acceptsCharsets: (...charsets: string[]) => AcceptsReturns;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyhttp/app",
3
- "version": "2.0.6",
3
+ "version": "2.0.12",
4
4
  "description": "0-legacy, tiny & fast web framework as a replacement of Express",
5
5
  "type": "module",
6
6
  "homepage": "https://tinyhttp.v1rtl.site",
@@ -32,11 +32,12 @@
32
32
  "author": "v1rtl",
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@tinyhttp/cookie": "2.0.2",
35
+ "@tinyhttp/cookie": "2.0.3",
36
36
  "@tinyhttp/proxy-addr": "2.0.2",
37
- "@tinyhttp/req": "2.0.4",
38
- "@tinyhttp/res": "2.0.5",
39
- "@tinyhttp/router": "2.0.2",
37
+ "@tinyhttp/req": "2.0.8",
38
+ "@tinyhttp/res": "2.0.9",
39
+ "@tinyhttp/router": "2.0.4",
40
+ "header-range-parser": "^1.1.1",
40
41
  "regexparam": "^2.0.0"
41
42
  },
42
43
  "scripts": {