diesel-core 1.3.9 → 1.4.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/dist/ctx.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { Server } from "bun";
2
2
  import type { ContextType } from "./types";
3
- export default function createCtx(req: Request, server: Server, url: URL): ContextType;
3
+ export default function createCtx(req: Request, server: Server, pathname: string, routePattern: string | undefined): ContextType;
@@ -1,7 +1,8 @@
1
1
  import { BunRequest, Server } from "bun";
2
2
  import type { ContextType, DieselT } from "./types";
3
- export default function handleRequest(req: BunRequest, server: Server, diesel: DieselT): Promise<any>;
4
- export declare function executeMiddlewares(middlewares: Function[], ctx: ContextType, server: Server): Promise<Response | null>;
3
+ /** Fast parse that extracts pathname and raw query without constructing URL */
4
+ export declare function parseRequestUrl(rawUrl: string): string;
5
+ export default function handleRequest(req: BunRequest, server: Server, diesel: DieselT): Promise<Response>;
5
6
  export declare function executeBunMiddlewares(middlewares: Function[], req: BunRequest, server: Server): Promise<any>;
6
7
  export declare function handleFilterRequest(diesel: DieselT, path: string, ctx: ContextType, server: Server): Promise<Response | undefined>;
7
8
  export declare function handleBunFilterRequest(diesel: DieselT, path: string, req: BunRequest, server: Server): Promise<Response | undefined>;
package/dist/main.d.ts CHANGED
@@ -59,7 +59,7 @@ export default class Diesel {
59
59
  useAdvancedLogger(options: AdvancedLoggerOptions): this;
60
60
  BunRoute(method: string, path: string, ...handlers: any[]): void;
61
61
  listen(port: any, ...args: listenArgsT[]): Server | void;
62
- fetch(): (req: BunRequest, server: Server) => Promise<any>;
62
+ fetch(): (req: BunRequest, server: Server) => Promise<Response>;
63
63
  close(callback?: () => void): void;
64
64
  /**
65
65
  * Registers a router instance for subrouting.
@@ -5,17 +5,17 @@ export type AdvancedLoggerOptions = {
5
5
  app: Diesel;
6
6
  logger?: () => void;
7
7
  logLevel?: LogLevel;
8
- onRequest?: (req: Request, url: URL) => void;
8
+ onRequest?: (req: Request, pathname: string) => void;
9
9
  onSend?: (ctx: ContextType) => Response | void | Promise<Response | void>;
10
- onError?: (error: Error, req: Request, url: URL) => Response | void | Promise<Response | void>;
10
+ onError?: (error: Error, req: Request, pathname: string) => Response | void | Promise<Response | void>;
11
11
  };
12
12
  export declare const advancedLogger: (options?: AdvancedLoggerOptions) => void;
13
13
  export type LoggerOptions = {
14
14
  app: Diesel;
15
15
  log?: () => void;
16
- onRequest?: (req: Request, url: URL) => void;
16
+ onRequest?: (req: Request, pathname: string) => void;
17
17
  onSend?: (ctx: ContextType) => Response | Promise<Response> | void;
18
- onError?: (error: Error, req: Request, url: URL) => Response | Promise<Response> | void;
18
+ onError?: (error: Error, req: Request, pathname: string) => Response | Promise<Response> | void;
19
19
  };
20
20
  export declare const logger: (options: LoggerOptions) => void;
21
21
  export {};
package/dist/types.d.ts CHANGED
@@ -1,18 +1,18 @@
1
1
  import { Server } from "bun";
2
2
  export type listenCalllBackType = () => void;
3
- export type handlerFunction = (ctx: ContextType, server?: Server) => Response | Promise<Response | null | void>;
4
- export type middlewareFunc = (ctx: ContextType, server?: Server | undefined) => null | void | Response | Promise<Response | void | null>;
5
- export type HookFunction = (ctx: ContextType, result?: Response | null | void, server?: Server) => Response | Promise<Response | null | void> | void | null;
6
- export type RouteNotFoundHandler = (ctx: ContextType) => void | Response | Promise<void> | Promise<Response>;
3
+ export type handlerFunction = (ctx: ContextType, server?: Server) => Response | Promise<Response>;
4
+ export type middlewareFunc = (ctx: ContextType, server?: Server) => void | Response | Promise<void | Response>;
5
+ export type HookFunction = (ctx: ContextType, result?: Response | null, server?: Server) => void | null | Response | Promise<void | null | Response>;
6
+ export type RouteNotFoundHandler = (ctx: ContextType) => Promise<Response>;
7
7
  export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD" | "ANY" | "PROPFIND";
8
8
  export type HttpMethodOfApp = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head' | 'any' | 'propfind';
9
9
  export type HttpMethodLower = Lowercase<HttpMethod>;
10
- export type HookType = 'onRequest' | 'preHandler' | 'postHandler' | 'onSend' | 'onError' | 'onClose';
10
+ export type HookType = "onRequest" | "preHandler" | "postHandler" | "onSend" | "onError" | "onClose";
11
11
  export interface onError {
12
- (error: Error, req: Request, url: URL, server: Server): void | null | Response | Promise<Response | null | void>;
12
+ (error: Error, req: Request, pathname: string, server: Server): void | null | Response | Promise<void | null | Response>;
13
13
  }
14
14
  export interface onRequest {
15
- (req: Request, url: URL, server: Server): void | null | Response | Promise<Response | null | void>;
15
+ (req: Request, pathname: string, server: Server): void;
16
16
  }
17
17
  export interface Hooks {
18
18
  onRequest: onRequest[] | null;
@@ -25,25 +25,26 @@ export interface Hooks {
25
25
  export interface ContextType {
26
26
  req: Request;
27
27
  server: Server;
28
- url: URL;
28
+ pathname: string;
29
29
  headers: Headers;
30
30
  status: number;
31
- setHeader: (key: string, value: any) => this;
32
- json: (data: Object, status?: number) => Response;
31
+ setHeader: (key: string, value: string) => this;
32
+ json: (data: object, status?: number) => Response;
33
33
  text: (data: string, status?: number) => Response;
34
34
  send: <T>(data: T, status?: number) => Response;
35
35
  file: (filePath: string, mimeType?: string, status?: number) => Response;
36
36
  redirect: (path: string, status?: number) => Response;
37
37
  setCookie: (name: string, value: string, options?: CookieOptions) => this;
38
38
  ip: string | null;
39
+ url: URL;
39
40
  query: Record<string, string>;
40
41
  params: Record<string, string>;
41
42
  set<T>(key: string, value: T): this;
42
43
  get<T>(key: string): T | undefined;
43
44
  body: Promise<any>;
44
- cookies: any;
45
+ cookies: Record<string, string>;
45
46
  removeHeader: (key: string) => this;
46
- ejs: (viewPath: string, data: {}) => Response | Promise<Response>;
47
+ ejs: (viewPath: string, data: object) => Response | Promise<Response>;
47
48
  stream: (callback: () => void) => Response;
48
49
  yieldStream: (callback: () => AsyncIterable<any>) => Response;
49
50
  }
@@ -56,14 +57,9 @@ export interface CookieOptions {
56
57
  httpOnly?: boolean;
57
58
  sameSite?: "Strict" | "Lax" | "None";
58
59
  }
59
- export interface RouteNodeType {
60
- path: string;
61
- handler: Function[];
62
- method: string[];
63
- }
64
60
  export interface RouteHandlerT {
65
61
  method: string;
66
- handler: (ctx: ContextType) => Promise<Response | null | void>;
62
+ handler: (ctx: ContextType) => Promise<Response> | Response;
67
63
  isDynamic?: boolean;
68
64
  path?: string;
69
65
  }
@@ -77,45 +73,28 @@ export interface DieselT {
77
73
  hasPreHandlerHook: boolean;
78
74
  hasPostHandlerHook: boolean;
79
75
  hasOnSendHook: boolean;
76
+ hasFilterEnabled: boolean;
80
77
  hooks: {
81
- onRequest: ((req: Request, url: URL, serer: Server) => void) | null;
82
- preHandler: ((ctx: ContextType, serer?: Server) => Response | Promise<Response | void | null>) | null;
83
- postHandler: ((ctx: ContextType, serer?: Server) => Response | Promise<Response | void | null>) | null;
84
- onSend: ((ctx?: ContextType, result?: Response | null | void, serer?: Server) => Response | Promise<Response | void | null>) | null;
85
- onError: ((error: Error, req: Request, url: URL, server?: Server) => void | Response | Promise<Response | null | void>) | null;
86
- routeNotFound: ((ctx: ContextType) => Response | Promise<Response | null | void>) | null;
78
+ onRequest: ((req: Request, url: URL, server: Server) => void) | null;
79
+ preHandler: ((ctx: ContextType, server?: Server) => Response | Promise<Response>) | null;
80
+ postHandler: ((ctx: ContextType, server?: Server) => Response | Promise<Response>) | null;
81
+ onSend: ((ctx: ContextType, result?: Response | null, server?: Server) => Response | Promise<Response>) | null;
82
+ onError: ((error: Error, req: Request, url: URL, server?: Server) => void | Response | Promise<Response>) | null;
83
+ routeNotFound: ((ctx: ContextType) => Response | Promise<Response>) | null;
87
84
  };
88
85
  filters: Set<string>;
89
- hasFilterEnabled: boolean;
90
- filterFunction: Array<(ctx: ContextType, serer?: Server) => void | Response | Promise<Response | void | null>>;
86
+ filterFunction: Array<(ctx: ContextType, server?: Server) => void | Response | Promise<void | Response>>;
91
87
  corsConfig: corsT | null;
92
- globalMiddlewares: Array<(ctx: ContextType, serer?: Server) => void | Promise<Response | null | void>>;
93
- middlewares: Map<string, Array<(ctx: ContextType, serer?: Server) => void | Promise<Response | null | void>>>;
88
+ globalMiddlewares: Array<(ctx: ContextType, server?: Server) => void | Promise<void | Response>>;
89
+ middlewares: Map<string, Array<(ctx: ContextType, server?: Server) => void | Promise<void | Response>>>;
94
90
  trie: {
95
91
  search: (pathname: string, method: string) => RouteHandlerT | undefined;
96
92
  };
97
93
  staticPath: string | null;
98
- routeNotFoundFunc: (c: ContextType) => void | Promise<void> | Promise<Response> | Response;
94
+ routeNotFoundFunc: RouteNotFoundHandler;
99
95
  routerInstance: DieselT;
100
96
  tempRoutes: Map<string, TempRouteEntry>;
101
97
  }
102
- export interface RouteCache {
103
- [key: string]: RouteHandlerT | undefined;
104
- }
105
- declare global {
106
- interface Request {
107
- routePattern?: string;
108
- [key: string]: any;
109
- }
110
- }
111
- export interface ParseBodyResult {
112
- error?: string;
113
- data?: any;
114
- }
115
- export interface RouteT {
116
- method: string;
117
- handler: handlerFunction;
118
- }
119
98
  export type corsT = {
120
99
  origin?: string | string[] | null;
121
100
  methods?: string | string[] | null;
@@ -137,3 +116,13 @@ export type listenArgsT = string | (() => void) | {
137
116
  cert?: string;
138
117
  key?: string;
139
118
  };
119
+ export interface ParseBodyResult {
120
+ error?: string;
121
+ data?: any;
122
+ }
123
+ declare global {
124
+ interface Request {
125
+ routePattern?: string;
126
+ [key: string]: any;
127
+ }
128
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diesel-core",
3
- "version": "1.3.9",
3
+ "version": "1.4.0",
4
4
  "description": "Web framework built on Web Standards",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",