@tinyhttp/app 2.0.29 → 2.0.32

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
@@ -7,7 +7,7 @@ import { Middleware, Handler, NextFunction, Router, UseMethodParams } from '@tin
7
7
  /**
8
8
  * tinyhttp App has a few settings for toggling features
9
9
  */
10
- export declare type AppSettings = Partial<{
10
+ export type AppSettings = Partial<{
11
11
  networkExtensions: boolean;
12
12
  subdomainOffset: number;
13
13
  bindAppToReqRes: boolean;
@@ -18,8 +18,8 @@ export declare type AppSettings = Partial<{
18
18
  /**
19
19
  * Function that processes the template
20
20
  */
21
- export declare type TemplateFunc<O> = (path: string, locals: Record<string, any>, opts: TemplateEngineOptions<O>, cb: (err: Error, html: unknown) => void) => void;
22
- export declare type TemplateEngineOptions<O> = Partial<{
21
+ export type TemplateFunc<O> = (path: string, locals: Record<string, any>, opts: TemplateEngineOptions<O>, cb: (err: Error | null, html: unknown) => void) => void;
22
+ export type TemplateEngineOptions<O> = Partial<{
23
23
  cache: boolean;
24
24
  ext: string;
25
25
  renderOptions: Partial<O>;
@@ -50,7 +50,7 @@ export declare type TemplateEngineOptions<O> = Partial<{
50
50
  */
51
51
  export declare class App<RenderOptions = any, Req extends Request = Request, Res extends Response<RenderOptions> = Response<RenderOptions>> extends Router<App, Req, Res> {
52
52
  middleware: Middleware<Req, Res>[];
53
- locals: Record<string, string>;
53
+ locals: Record<string, unknown>;
54
54
  noMatchHandler: Handler;
55
55
  onError: ErrorHandler;
56
56
  settings: AppSettings;
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export { extendMiddleware } from './extend.js';
8
8
  export { onErrorHandler } from './onError.js';
9
9
  export type { ErrorHandler } from './onError.js';
10
10
  import type { NextFunction, Handler as RHandler, AsyncHandler as RAsyncHandler, SyncHandler as RSyncHandler, Middleware } from '@tinyhttp/router';
11
- export declare type Handler = RHandler<Request, Response>;
12
- export declare type AsyncHandler = RAsyncHandler<Request, Response>;
13
- export declare type SyncHandler = RSyncHandler<Request, Response>;
11
+ export type Handler = RHandler<Request, Response>;
12
+ export type AsyncHandler = RAsyncHandler<Request, Response>;
13
+ export type SyncHandler = RSyncHandler<Request, Response>;
14
14
  export type { NextFunction, Middleware, Request, Response };
package/dist/index.js CHANGED
@@ -132,34 +132,49 @@ const applyHandler = (h) => async (req, res, next) => {
132
132
  }
133
133
  };
134
134
  class App extends Router {
135
- middleware = [];
136
- locals = {};
137
- noMatchHandler;
138
- onError;
139
- settings;
140
- engines = {};
141
- applyExtensions;
142
- attach;
143
135
  constructor(options = {}) {
144
136
  super();
137
+ this.middleware = [];
138
+ this.locals = {};
139
+ this.engines = {};
145
140
  this.onError = (options == null ? void 0 : options.onError) || onErrorHandler;
146
141
  this.noMatchHandler = (options == null ? void 0 : options.noMatchHandler) || this.onError.bind(this, { code: 404 });
147
- this.settings = options.settings || { xPoweredBy: true, views: process.cwd() };
142
+ this.settings = options.settings || { xPoweredBy: true, views: `${process.cwd()}/views` };
148
143
  this.applyExtensions = options == null ? void 0 : options.applyExtensions;
149
144
  this.attach = (req, res) => setImmediate(this.handler.bind(this, req, res, void 0), req, res);
150
145
  }
146
+ /**
147
+ * Set app setting
148
+ * @param setting setting name
149
+ * @param value setting value
150
+ */
151
151
  set(setting, value) {
152
152
  this.settings[setting] = value;
153
153
  return this;
154
154
  }
155
+ /**
156
+ * Enable app setting
157
+ * @param setting Setting name
158
+ */
155
159
  enable(setting) {
156
160
  this.settings[setting] = true;
157
161
  return this;
158
162
  }
163
+ /**
164
+ * Disable app setting
165
+ * @param setting
166
+ */
159
167
  disable(setting) {
160
168
  this.settings[setting] = false;
161
169
  return this;
162
170
  }
171
+ /**
172
+ * Render a template
173
+ * @param file What to render
174
+ * @param data data that is passed to a template
175
+ * @param options Template engine options
176
+ * @param cb Callback that consumes error and html
177
+ */
163
178
  render(file, data = {}, cb, options = {}) {
164
179
  options.viewsFolder = options.viewsFolder || this.settings.views || `${process.cwd()}/views`;
165
180
  options.ext = options.ext || file.slice(file.lastIndexOf(".") + 1) || "ejs";
@@ -217,6 +232,9 @@ class App extends Router {
217
232
  });
218
233
  return this;
219
234
  }
235
+ /**
236
+ * Register a template engine with extension
237
+ */
220
238
  engine(ext, fn) {
221
239
  this.engines[ext] = fn;
222
240
  return this;
@@ -234,6 +252,11 @@ class App extends Router {
234
252
  return m.regex.pattern.test(url) && (m.type === "mw" && fullPathRegex ? fullPathRegex.pattern.test(url) : true);
235
253
  });
236
254
  }
255
+ /**
256
+ * Extends Req / Res objects, pushes 404 and 500 handlers, dispatches middleware
257
+ * @param req Req object
258
+ * @param res Res object
259
+ */
237
260
  handler(req, res, next) {
238
261
  const { xPoweredBy } = this.settings;
239
262
  if (xPoweredBy)
@@ -291,6 +314,12 @@ class App extends Router {
291
314
  next = next || ((err) => err ? this.onError(err, req, res) : loop());
292
315
  loop();
293
316
  }
317
+ /**
318
+ * Creates HTTP server and dispatches middleware
319
+ * @param port server listening port
320
+ * @param Server callback after server starts listening
321
+ * @param host server listening host
322
+ */
294
323
  listen(port, cb, host = "0.0.0.0") {
295
324
  return createServer().on("request", this.attach).listen(port, host, cb);
296
325
  }
package/dist/onError.d.ts CHANGED
@@ -2,5 +2,5 @@ import type { NextFunction } from '@tinyhttp/router';
2
2
  import type { Request } from './request.js';
3
3
  import type { Response } from './response.js';
4
4
  import type { App } from './app.js';
5
- export declare type ErrorHandler = (this: App, err: any, req: Request, res: Response, next?: NextFunction) => void;
5
+ export type ErrorHandler = (this: App, err: any, req: Request, res: Response, next?: NextFunction) => void;
6
6
  export declare const onErrorHandler: ErrorHandler;
package/dist/request.d.ts CHANGED
@@ -18,12 +18,12 @@ export declare const getHostname: (req: Request) => string | undefined;
18
18
  export declare const getIP: (req: Pick<Request, 'headers' | 'connection' | 'socket'>) => string | undefined;
19
19
  export declare const getIPs: (req: Pick<Request, 'headers' | 'connection' | 'socket'>) => string[] | undefined;
20
20
  export declare const getSubdomains: (req: Request, subdomainOffset?: number) => string[];
21
- export declare type Connection = IncomingMessage['socket'] & {
21
+ export type Connection = IncomingMessage['socket'] & {
22
22
  encrypted: boolean;
23
23
  };
24
- export declare type Protocol = 'http' | 'https' | string;
24
+ export type Protocol = 'http' | 'https' | string;
25
25
  export type { URLParams };
26
- declare type AcceptsReturns = string | boolean | string[];
26
+ type AcceptsReturns = string | boolean | string[];
27
27
  export interface Request extends IncomingMessage {
28
28
  originalUrl: string;
29
29
  path: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyhttp/app",
3
- "version": "2.0.29",
3
+ "version": "2.0.32",
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,13 +32,13 @@
32
32
  "author": "v1rtl",
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@tinyhttp/cookie": "2.0.6",
36
- "@tinyhttp/proxy-addr": "2.0.6",
37
- "@tinyhttp/req": "2.0.16",
38
- "@tinyhttp/res": "2.0.22",
39
- "@tinyhttp/router": "2.0.7",
40
35
  "header-range-parser": "1.1.3",
41
- "regexparam": "^2.0.1"
36
+ "regexparam": "^2.0.1",
37
+ "@tinyhttp/res": "2.0.23",
38
+ "@tinyhttp/req": "2.0.16",
39
+ "@tinyhttp/router": "2.1.0",
40
+ "@tinyhttp/proxy-addr": "2.0.7",
41
+ "@tinyhttp/cookie": "2.0.6"
42
42
  },
43
43
  "scripts": {
44
44
  "dev": "vite",