@drunkcod/express-kit 0.0.9 → 0.0.11

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.
@@ -21,7 +21,12 @@ type ControllerErrorFns<C> = IsEmptyObject<HandlerErrorFns<C>> extends true ? ne
21
21
  type ControllerFns<C> = ControllerHandlerFns<C> | ControllerErrorFns<C>;
22
22
  type HandlerFn<Req, T> = Req extends [never] ? never : ((...args: [request: Req, response: express.Response]) => Promise<T>) | ((...args: [request: Req, response: express.Response, next: express.NextFunction]) => Promise<T>);
23
23
  type ErrorHandlerFn<Req, T> = Req extends [never] ? never : ((...args: [errpr: Error, request: Req, response: express.Response, next: express.NextFunction]) => Promise<T>);
24
+ type IsFn<T, P extends keyof T> = T[P] extends (...args: any) => any ? P : never;
25
+ type Fns<T> = {
26
+ [P in keyof T as IsFn<T, P>]: T[P];
27
+ };
24
28
  export declare const as: <T>(x: T) => T;
29
+ export declare function bind<T extends Fns<T>, K extends keyof T>(target: T, fn: K): T[K];
25
30
  export declare function asyncHandler<T, Req extends express.Request = express.Request<any>>(fn: HandlerFn<Req, T>): RequestHandler<T>;
26
31
  export declare function asyncHandler<T, Req extends express.Request = express.Request<any>>(fn: ErrorHandlerFn<Req, T>): ErrorHandler<T>;
27
32
  export declare function boundAsyncHandler<C extends ControllerHandlerFns<C>, T>(x: C, m: keyof ControllerHandlerFns<C>): RequestHandler<unknown>;
@@ -7,6 +7,9 @@ const safeResolve = (fn, ...args) => {
7
7
  }
8
8
  };
9
9
  export const as = (x) => x;
10
+ export function bind(target, fn) {
11
+ return as(target[fn]).bind(target);
12
+ }
10
13
  export function asyncHandler(fn) {
11
14
  switch (fn.length) {
12
15
  case 2:
package/lib/loggable.d.ts CHANGED
@@ -1,2 +1,5 @@
1
1
  export declare function at(message?: string): string;
2
2
  export declare function asLoggableError(error: unknown): object;
3
+ export declare function hasOwnJSON(x: object): x is {
4
+ toJSON(): unknown;
5
+ };
package/lib/loggable.js CHANGED
@@ -15,6 +15,8 @@ export function at(message) {
15
15
  function asLoggableCause(cause) {
16
16
  if (cause == null || typeof cause !== 'object')
17
17
  return cause;
18
+ if (hasOwnJSON(cause))
19
+ return asLoggableCause(cause.toJSON());
18
20
  if (cause instanceof Error) {
19
21
  const { message, stack, cause: innerCause, ...rest } = cause;
20
22
  return innerCause
@@ -34,3 +36,6 @@ export function asLoggableError(error) {
34
36
  Error.captureStackTrace(r, asLoggableError);
35
37
  return Object.defineProperty(r, 'stack', { enumerable: true });
36
38
  }
39
+ export function hasOwnJSON(x) {
40
+ return 'toJSON' in x && typeof x.toJSON === 'function';
41
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@drunkcod/express-kit",
3
3
  "type": "module",
4
- "version": "0.0.9",
4
+ "version": "0.0.11",
5
5
  "description": "Express4 utility things",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  ],
27
27
  "dependencies": {
28
28
  "@drunkcod/argis": "^0.0.5",
29
- "@drunkcod/express-async": "^0.0.9"
29
+ "@drunkcod/express-async": "^0.0.11"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@drunkcod/ts-jest-esm": "^0.0.1",