@autofleet/node-common 4.1.4 → 4.1.5

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/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var express=require('express'),node_stream=require('node:stream');var u=Object.defineProperty;var a=(e,n)=>u(e,"name",{value:n,configurable:true});var i=["all","get","post","put","delete","patch","options","head"],R=a((e,n)=>async(t,o,r)=>{try{await n(t,o,r);}catch(p){let s=p;if(e.error(s.message),s.statusCode&&s.statusCode<500){o.status(400).json({error:s.message,status:"ERROR"});return}r(s);}},"AfEntryPoint"),f=a(({logger:e,...n})=>{let t=express.Router({mergeParams:true,...n});return i.forEach(r=>{let p=t[r].bind(t),s=a((...m)=>p(...m.map(o)),"newMethodHandler");t[r]=s;}),t;function o(r){return Array.isArray(r)?r.map(o):typeof r=="function"?R(e,r):r}},"AfRouter");async function y(e,n,{concurrency:t,signal:o}={}){return t?node_stream.Readable.from(e).map(n,{concurrency:t,signal:o}).toArray():Promise.all(Array.from(e,r=>n(r,o?{signal:o}:void 0)))}a(y,"promiseMap");var A=Object.freeze({ok:"OK",error:"ERROR",fail:"FAIL"});var w=f;exports.AfRouter=f;exports.Router=w;exports.consts=A;exports.promiseMap=y;//# sourceMappingURL=index.cjs.map
1
+ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=s(require(`express`)),l=s(require(`node:stream`)),u=[`all`,`get`,`post`,`put`,`delete`,`patch`,`options`,`head`],d=(e,t)=>async(n,r,i)=>{try{await t(n,r,i)}catch(t){let n=t;if(e.error(n.message),n.statusCode&&n.statusCode<500){r.status(400).json({error:n.message,status:`ERROR`});return}i(n)}},f=({logger:e,...t})=>{let n=(0,c.Router)({mergeParams:!0,...t});return u.forEach(e=>{let t=n[e].bind(n),i=(...e)=>t(...e.map(r));n[e]=i}),n;function r(t){return Array.isArray(t)?t.map(r):typeof t==`function`?d(e,t):t}};async function p(e,t,{concurrency:n,signal:r}={}){return n?l.Readable.from(e).map(t,{concurrency:n,signal:r}).toArray():Promise.all(Array.from(e,e=>t(e,r?{signal:r}:void 0)))}const m=Object.freeze({ok:`OK`,error:`ERROR`,fail:`FAIL`}),h=f;exports.AfRouter=f,exports.Router=h,exports.consts=m,exports.promiseMap=p;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/router/index.ts","../src/promise-utils/index.ts","../src/consts/index.ts","../src/index.ts"],"names":["METHODS","AfEntryPoint","__name","logger","func","req","res","next","err","e","error","message","statusCode","status","json","AfRouter","options","myRouter","Router","mergeParams","forEach","method","internalMethod","bind","newMethodHandler","args","map","argMapper","Array","isArray","promiseMap","iterable","handler","concurrency","signal","Readable","from","toArray","Promise","all","item","undefined","consts","Object","freeze","ok","fail"],"mappings":"gKAKO,IAAMA,EAAU,CACrB,KAAA,CACA,KACA,CAAA,MAAA,CACA,KACA,CAAA,QAAA,CACA,QACA,SACA,CAAA,MAAA,CAAA,CAGIC,CAAeC,CAAAA,CAAAA,CAAA,CAACC,CAAAA,CAA+BC,IAAmG,MAAOC,CAAAA,CAAKC,CAAKC,CAAAA,CAAAA,GAAAA,CACvK,GAAI,CACF,MAAMH,CAAAA,CAAKC,CAAKC,CAAAA,CAAAA,CAAKC,CAAAA,EACvB,OAASC,CAAK,CAAA,CACZ,IAAMC,CAAAA,CAAID,CAEV,CAAA,GADAL,EAAOO,KAAMD,CAAAA,CAAAA,CAAEE,OA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aC,MAAAA,CAAAA,CAAM,CAAqC,CAAA,GAAE,CAE5D,OAAKD,CAGEE,CAAAA,oBAAAA,CAASC,IAAKL,CAAAA,CAAAA,EAAUL,GAAIM,CAAAA,CAAAA,CAAS,CAAEC,WAAAA,CAAAA,CAAaC,CAAAA,MAAAA,CAAAA,CAAO,CAAA,CAAA,CAAGG,OAAO,EAAA,CAFnEC,OAAQC,CAAAA,GAAAA,CAAIX,MAAMQ,IAAKL,CAAAA,CAAAA,CAAUS,CAAQR,EAAAA,CAAAA,CAAQQ,CAAMN,CAAAA,CAAAA,CAAS,CAAEA,MAAAA,CAAAA,CAAO,CAAIO,CAAAA,MAAAA,CAAAA,CAAAA,CAGxF,CATsBX,CAAAA,CAAAA,CAAAA,CAAAA,YAAAA,CAAAA,CCFTY,IAAAA,CAAAA,CAASC,OAAOC,MAAO,CAAA,CAClCC,EAAI,CAAA,IAAA,CACJnC,KAAO,CAAA,OAAA,CACPoC,KAAM,MACR,CAAA,ECGO,IAAM5B,CAASH,CAAAA","file":"index.cjs","sourcesContent":["import {\n Router, type Request, type Response, type NextFunction, type Handler, type RouterOptions, type IRouterMatcher,\n} from 'express';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\n\nexport const METHODS = [\n 'all',\n 'get',\n 'post',\n 'put',\n 'delete',\n 'patch',\n 'options',\n 'head',\n] as const;\n\nconst AfEntryPoint = (logger: LoggerInstanceManager, func: (req: Request, res: Response, nextFn: NextFunction) => void | PromiseLike<void>): Handler => async (req, res, next) => {\n try {\n await func(req, res, next);\n } catch (err) {\n const e = err as Error & { statusCode?: number; };\n logger.error(e.message);\n if (e.statusCode && e.statusCode < 500) {\n res.status(400).json({ error: e.message, status: 'ERROR' });\n return;\n }\n next(e);\n }\n};\n\n/** @returns a monkey-patched express router that will handle async routes (and force a 400 status for codes <500) */\nexport const AfRouter = ({ logger, ...options }: RouterOptions & { logger: LoggerInstanceManager; }): Router => {\n const myRouter = Router({ mergeParams: true, ...options });\n METHODS.forEach((method) => {\n const internalMethod = myRouter[method].bind(myRouter);\n const newMethodHandler: IRouterMatcher<Router> = (...args: [...unknown[]]) => internalMethod(...args.map(argMapper) as [string]);\n myRouter[method] = newMethodHandler;\n });\n return myRouter;\n\n function argMapper(args: unknown): unknown {\n if (Array.isArray(args)) return args.map(argMapper);\n return typeof args === 'function' ? AfEntryPoint(logger, args as Handler) : args;\n }\n};\n","import { Readable } from 'node:stream';\n\nexport async function promiseMap<T, U>(\n iterable: T[] | Iterable<T>,\n handler: (item: T, options?: { signal?: AbortSignal; }) => PromiseLike<U>,\n { concurrency, signal }: Parameters<Readable['map']>[1] = {},\n): Promise<U[]> {\n if (!concurrency) {\n return Promise.all(Array.from(iterable, item => handler(item, signal ? { signal } : undefined)));\n }\n return Readable.from(iterable).map(handler, { concurrency, signal }).toArray();\n}\n","export const consts = Object.freeze({\n ok: 'OK',\n error: 'ERROR',\n fail: 'FAIL',\n});\n","import { AfRouter } from './router';\n\nexport { AfRouter } from './router';\nexport { promiseMap } from './promise-utils';\n\nexport { consts } from './consts';\n\nexport const Router = AfRouter;\n"]}
1
+ {"version":3,"file":"index.cjs","names":["logger: LoggerInstanceManager","func: (req: Request, res: Response, nextFn: NextFunction) => void | PromiseLike<void>","newMethodHandler: IRouterMatcher<Router>","args: unknown","iterable: T[] | Iterable<T>","handler: (item: T, options?: { signal?: AbortSignal; }) => PromiseLike<U>","Readable","consts: Readonly<{ ok: 'OK'; error: 'ERROR'; fail: 'FAIL'; }>","Router: typeof AfRouter"],"sources":["../src/router/index.ts","../src/promise-utils/index.ts","../src/consts/index.ts","../src/index.ts"],"sourcesContent":["import {\n Router, type Request, type Response, type NextFunction, type Handler, type RouterOptions, type IRouterMatcher,\n} from 'express';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\n\nexport const METHODS = [\n 'all',\n 'get',\n 'post',\n 'put',\n 'delete',\n 'patch',\n 'options',\n 'head',\n] as const;\n\nconst AfEntryPoint = (logger: LoggerInstanceManager, func: (req: Request, res: Response, nextFn: NextFunction) => void | PromiseLike<void>): Handler => async (req, res, next) => {\n try {\n await func(req, res, next);\n } catch (err) {\n const e = err as Error & { statusCode?: number; };\n logger.error(e.message);\n if (e.statusCode && e.statusCode < 500) {\n res.status(400).json({ error: e.message, status: 'ERROR' });\n return;\n }\n next(e);\n }\n};\n\n/** @returns a monkey-patched express router that will handle async routes (and force a 400 status for codes <500) */\nexport const AfRouter = ({ logger, ...options }: RouterOptions & { logger: LoggerInstanceManager; }): Router => {\n const myRouter = Router({ mergeParams: true, ...options });\n METHODS.forEach((method) => {\n const internalMethod = myRouter[method].bind(myRouter);\n const newMethodHandler: IRouterMatcher<Router> = (...args: [...unknown[]]) => internalMethod(...args.map(argMapper) as [string]);\n myRouter[method] = newMethodHandler;\n });\n return myRouter;\n\n function argMapper(args: unknown): unknown {\n if (Array.isArray(args)) return args.map(argMapper);\n return typeof args === 'function' ? AfEntryPoint(logger, args as Handler) : args;\n }\n};\n","import { Readable } from 'node:stream';\n\nexport async function promiseMap<T, U>(\n iterable: T[] | Iterable<T>,\n handler: (item: T, options?: { signal?: AbortSignal; }) => PromiseLike<U>,\n { concurrency, signal }: Parameters<Readable['map']>[1] = {},\n): Promise<U[]> {\n if (!concurrency) {\n return Promise.all(Array.from(iterable, item => handler(item, signal ? { signal } : undefined)));\n }\n return Readable.from(iterable).map(handler, { concurrency, signal }).toArray();\n}\n","export const consts: Readonly<{ ok: 'OK'; error: 'ERROR'; fail: 'FAIL'; }> = Object.freeze({\n ok: 'OK',\n error: 'ERROR',\n fail: 'FAIL',\n});\n","import { AfRouter } from './router';\n\nexport { AfRouter } from './router';\nexport { promiseMap } from './promise-utils';\n\nexport { consts } from './consts';\n\nexport const Router: typeof AfRouter = AfRouter;\n"],"mappings":"whBAKa,EAAU,CACrB,MACA,MACA,OACA,MACA,SACA,QACA,UACA,MACD,EAEK,EAAe,CAACA,EAA+BC,IAAmG,MAAO,EAAK,EAAK,IAAS,CAChL,GAAI,CACF,MAAM,EAAK,EAAK,EAAK,EAAK,AAC3B,OAAQ,EAAK,CACZ,IAAM,EAAI,EAEV,GADA,EAAO,MAAM,EAAE,QAAQ,CACnB,EAAE,YAAc,EAAE,WAAa,IAAK,CACtC,EAAI,OAAO,IAAI,CAAC,KAAK,CAAE,MAAO,EAAE,QAAS,OAAQ,OAAS,EAAC,CAC3D,MACD,CACD,EAAK,EAAE,AACR,CACF,EAGY,EAAW,CAAC,CAAE,SAAQ,GAAG,EAA6D,GAAa,CAC9G,IAAM,GAAA,EAAA,EAAA,QAAkB,CAAE,YAAa,GAAM,GAAG,CAAS,EAAC,CAM1D,OALA,EAAQ,QAAQ,AAAC,GAAW,CAC1B,IAAM,EAAiB,EAAS,GAAQ,KAAK,EAAS,CAChDC,EAA2C,CAAC,GAAG,IAAyB,EAAe,GAAG,EAAK,IAAI,EAAU,CAAa,CAChI,EAAS,GAAU,CACpB,EAAC,CACK,EAEP,SAAS,EAAUC,EAAwB,CAEzC,OADI,MAAM,QAAQ,EAAK,CAAS,EAAK,IAAI,EAAU,CAC5C,OAAO,GAAS,WAAa,EAAa,EAAQ,EAAgB,CAAG,CAC7E,CACF,EC1CD,eAAsB,EACpBC,EACAC,EACA,CAAE,cAAa,SAAwC,CAAG,CAAE,EAC9C,CAId,OAHK,EAGEC,EAAAA,SAAS,KAAK,EAAS,CAAC,IAAI,EAAS,CAAE,cAAa,QAAQ,EAAC,CAAC,SAAS,CAFrE,QAAQ,IAAI,MAAM,KAAK,EAAU,GAAQ,EAAQ,EAAM,EAAS,CAAE,QAAQ,EAAG,IAAA,GAAU,CAAC,CAAC,AAGnG,CCXD,MAAaC,EAAgE,OAAO,OAAO,CACzF,GAAI,KACJ,MAAO,QACP,KAAM,MACP,EAAC,CCGWC,EAA0B"}
package/dist/index.d.cts CHANGED
@@ -1,26 +1,59 @@
1
- import * as _autofleet_logger from '@autofleet/logger';
2
- import { LoggerInstanceManager } from '@autofleet/logger';
3
- import * as express from 'express';
4
- import { RouterOptions, Router as Router$1 } from 'express';
5
- import { Readable } from 'node:stream';
1
+ import { Router as Router$1, RouterOptions } from "express";
2
+ import Pino from "pino";
3
+ import { Readable } from "node:stream";
6
4
 
5
+ //#region ../logger/src/index.d.ts
6
+ declare enum LogLevel {
7
+ trace = "trace",
8
+ debug = "debug",
9
+ info = "info",
10
+ warn = "warn",
11
+ error = "error",
12
+ fatal = "fatal",
13
+ }
14
+ type MiddlewareFunction = () => Record<string, unknown>;
15
+ declare class LoggerInstanceManager {
16
+ #private;
17
+ constructor(logLevel?: LogLevel);
18
+ private winstonLikeLoggerCall;
19
+ private addMetadata;
20
+ addContextMiddleware: (middleware: MiddlewareFunction) => number;
21
+ trace: (message: string, meta?: unknown) => void;
22
+ debug: (message: string, meta?: unknown) => void;
23
+ info: (message: string, meta?: unknown) => void;
24
+ warn: (message: string, meta?: unknown) => void;
25
+ error: (message: string, meta?: unknown) => void;
26
+ fatal: (message: string, meta?: unknown) => void;
27
+ child: (metadata: Record<string, unknown>) => Pino.Logger;
28
+ get level(): Pino.LevelWithSilentOrString;
29
+ }
30
+ //#endregion
31
+ //#region src/router/index.d.ts
7
32
  /** @returns a monkey-patched express router that will handle async routes (and force a 400 status for codes <500) */
8
- declare const AfRouter: ({ logger, ...options }: RouterOptions & {
9
- logger: LoggerInstanceManager;
33
+ declare const AfRouter: ({
34
+ logger,
35
+ ...options
36
+ }: RouterOptions & {
37
+ logger: LoggerInstanceManager;
10
38
  }) => Router$1;
11
-
39
+ //#endregion
40
+ //#region src/promise-utils/index.d.ts
12
41
  declare function promiseMap<T, U>(iterable: T[] | Iterable<T>, handler: (item: T, options?: {
13
- signal?: AbortSignal;
14
- }) => PromiseLike<U>, { concurrency, signal }?: Parameters<Readable['map']>[1]): Promise<U[]>;
15
-
42
+ signal?: AbortSignal;
43
+ }) => PromiseLike<U>, {
44
+ concurrency,
45
+ signal
46
+ }?: Parameters<Readable["map"]>[1]): Promise<U[]>;
47
+ //#endregion
48
+ //#region src/consts/index.d.ts
16
49
  declare const consts: Readonly<{
17
- ok: "OK";
18
- error: "ERROR";
19
- fail: "FAIL";
50
+ ok: "OK";
51
+ error: "ERROR";
52
+ fail: "FAIL";
20
53
  }>;
21
-
22
- declare const Router: ({ logger, ...options }: express.RouterOptions & {
23
- logger: _autofleet_logger.LoggerInstanceManager;
24
- }) => express.Router;
25
-
54
+ //#endregion
55
+ //#region src/index.d.ts
56
+ declare const Router: typeof AfRouter;
57
+ //#endregion
26
58
  export { AfRouter, Router, consts, promiseMap };
59
+ //# sourceMappingURL=index.d.cts.map
package/dist/index.d.ts CHANGED
@@ -1,26 +1,59 @@
1
- import * as _autofleet_logger from '@autofleet/logger';
2
- import { LoggerInstanceManager } from '@autofleet/logger';
3
- import * as express from 'express';
4
- import { RouterOptions, Router as Router$1 } from 'express';
5
- import { Readable } from 'node:stream';
1
+ import { Router as Router$1, RouterOptions } from "express";
2
+ import { Readable } from "node:stream";
3
+ import Pino from "pino";
6
4
 
5
+ //#region ../logger/src/index.d.ts
6
+ declare enum LogLevel {
7
+ trace = "trace",
8
+ debug = "debug",
9
+ info = "info",
10
+ warn = "warn",
11
+ error = "error",
12
+ fatal = "fatal",
13
+ }
14
+ type MiddlewareFunction = () => Record<string, unknown>;
15
+ declare class LoggerInstanceManager {
16
+ #private;
17
+ constructor(logLevel?: LogLevel);
18
+ private winstonLikeLoggerCall;
19
+ private addMetadata;
20
+ addContextMiddleware: (middleware: MiddlewareFunction) => number;
21
+ trace: (message: string, meta?: unknown) => void;
22
+ debug: (message: string, meta?: unknown) => void;
23
+ info: (message: string, meta?: unknown) => void;
24
+ warn: (message: string, meta?: unknown) => void;
25
+ error: (message: string, meta?: unknown) => void;
26
+ fatal: (message: string, meta?: unknown) => void;
27
+ child: (metadata: Record<string, unknown>) => Pino.Logger;
28
+ get level(): Pino.LevelWithSilentOrString;
29
+ }
30
+ //#endregion
31
+ //#region src/router/index.d.ts
7
32
  /** @returns a monkey-patched express router that will handle async routes (and force a 400 status for codes <500) */
8
- declare const AfRouter: ({ logger, ...options }: RouterOptions & {
9
- logger: LoggerInstanceManager;
33
+ declare const AfRouter: ({
34
+ logger,
35
+ ...options
36
+ }: RouterOptions & {
37
+ logger: LoggerInstanceManager;
10
38
  }) => Router$1;
11
-
39
+ //#endregion
40
+ //#region src/promise-utils/index.d.ts
12
41
  declare function promiseMap<T, U>(iterable: T[] | Iterable<T>, handler: (item: T, options?: {
13
- signal?: AbortSignal;
14
- }) => PromiseLike<U>, { concurrency, signal }?: Parameters<Readable['map']>[1]): Promise<U[]>;
15
-
42
+ signal?: AbortSignal;
43
+ }) => PromiseLike<U>, {
44
+ concurrency,
45
+ signal
46
+ }?: Parameters<Readable["map"]>[1]): Promise<U[]>;
47
+ //#endregion
48
+ //#region src/consts/index.d.ts
16
49
  declare const consts: Readonly<{
17
- ok: "OK";
18
- error: "ERROR";
19
- fail: "FAIL";
50
+ ok: "OK";
51
+ error: "ERROR";
52
+ fail: "FAIL";
20
53
  }>;
21
-
22
- declare const Router: ({ logger, ...options }: express.RouterOptions & {
23
- logger: _autofleet_logger.LoggerInstanceManager;
24
- }) => express.Router;
25
-
54
+ //#endregion
55
+ //#region src/index.d.ts
56
+ declare const Router: typeof AfRouter;
57
+ //#endregion
26
58
  export { AfRouter, Router, consts, promiseMap };
59
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import {Router}from'express';import {Readable}from'node:stream';var u=Object.defineProperty;var a=(e,n)=>u(e,"name",{value:n,configurable:true});var i=["all","get","post","put","delete","patch","options","head"],R=a((e,n)=>async(t,o,r)=>{try{await n(t,o,r);}catch(p){let s=p;if(e.error(s.message),s.statusCode&&s.statusCode<500){o.status(400).json({error:s.message,status:"ERROR"});return}r(s);}},"AfEntryPoint"),f=a(({logger:e,...n})=>{let t=Router({mergeParams:true,...n});return i.forEach(r=>{let p=t[r].bind(t),s=a((...m)=>p(...m.map(o)),"newMethodHandler");t[r]=s;}),t;function o(r){return Array.isArray(r)?r.map(o):typeof r=="function"?R(e,r):r}},"AfRouter");async function y(e,n,{concurrency:t,signal:o}={}){return t?Readable.from(e).map(n,{concurrency:t,signal:o}).toArray():Promise.all(Array.from(e,r=>n(r,o?{signal:o}:void 0)))}a(y,"promiseMap");var A=Object.freeze({ok:"OK",error:"ERROR",fail:"FAIL"});var w=f;export{f as AfRouter,w as Router,A as consts,y as promiseMap};//# sourceMappingURL=index.js.map
1
+ import{Router as e}from"express";import{Readable as t}from"node:stream";const n=[`all`,`get`,`post`,`put`,`delete`,`patch`,`options`,`head`],r=(e,t)=>async(n,r,i)=>{try{await t(n,r,i)}catch(t){let n=t;if(e.error(n.message),n.statusCode&&n.statusCode<500){r.status(400).json({error:n.message,status:`ERROR`});return}i(n)}},i=({logger:t,...i})=>{let a=e({mergeParams:!0,...i});return n.forEach(e=>{let t=a[e].bind(a),n=(...e)=>t(...e.map(o));a[e]=n}),a;function o(e){return Array.isArray(e)?e.map(o):typeof e==`function`?r(t,e):e}};async function a(e,n,{concurrency:r,signal:i}={}){return r?t.from(e).map(n,{concurrency:r,signal:i}).toArray():Promise.all(Array.from(e,e=>n(e,i?{signal:i}:void 0)))}const o=Object.freeze({ok:`OK`,error:`ERROR`,fail:`FAIL`}),s=i;export{i as AfRouter,s as Router,o as consts,a as promiseMap};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/router/index.ts","../src/promise-utils/index.ts","../src/consts/index.ts","../src/index.ts"],"names":["METHODS","AfEntryPoint","__name","logger","func","req","res","next","err","e","error","message","statusCode","status","json","AfRouter","options","myRouter","Router","mergeParams","forEach","method","internalMethod","bind","newMethodHandler","args","map","argMapper","Array","isArray","promiseMap","iterable","handler","concurrency","signal","Readable","from","toArray","Promise","all","item","undefined","consts","Object","freeze","ok","fail"],"mappings":"iJAKO,IAAMA,EAAU,CACrB,KAAA,CACA,KACA,CAAA,MAAA,CACA,KACA,CAAA,QAAA,CACA,QACA,SACA,CAAA,M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AIG,KAAMC,CAAAA,OAAAA,CAAQJ,CAAAA,CAAcA,CAAAA,CAAAA,CAAKC,GAAIC,CAAAA,CAAAA,CAClC,CAAA,OAAOF,GAAS,UAAaxB,CAAAA,CAAAA,CAAaE,CAAQsB,CAAAA,CAAAA,CAAmBA,CAAAA,CAC9E,CACF,CAAA,CAbwB,UC/BxB,EAEA,eAAsBK,CAAAA,CACpBC,CACAC,CAAAA,CAAAA,CACA,CAAEC,WAAAA,CAAAA,CAAAA,CAAaC,MAAAA,CAAAA,CAAM,CAAqC,CAAA,GAAE,CAE5D,OAAKD,CAGEE,CAAAA,QAAAA,CAASC,IAAKL,CAAAA,CAAAA,EAAUL,GAAIM,CAAAA,CAAAA,CAAS,CAAEC,WAAAA,CAAAA,CAAaC,CAAAA,MAAAA,CAAAA,CAAO,CAAA,CAAA,CAAGG,OAAO,EAAA,CAFnEC,OAAQC,CAAAA,GAAAA,CAAIX,MAAMQ,IAAKL,CAAAA,CAAAA,CAAUS,CAAQR,EAAAA,CAAAA,CAAQQ,CAAMN,CAAAA,CAAAA,CAAS,CAAEA,MAAAA,CAAAA,CAAO,CAAIO,CAAAA,MAAAA,CAAAA,CAAAA,CAGxF,CATsBX,CAAAA,CAAAA,CAAAA,CAAAA,YAAAA,CAAAA,CCFTY,IAAAA,CAAAA,CAASC,OAAOC,MAAO,CAAA,CAClCC,EAAI,CAAA,IAAA,CACJnC,KAAO,CAAA,OAAA,CACPoC,KAAM,MACR,CAAA,ECGO,IAAM5B,CAASH,CAAAA","file":"index.js","sourcesContent":["import {\n Router, type Request, type Response, type NextFunction, type Handler, type RouterOptions, type IRouterMatcher,\n} from 'express';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\n\nexport const METHODS = [\n 'all',\n 'get',\n 'post',\n 'put',\n 'delete',\n 'patch',\n 'options',\n 'head',\n] as const;\n\nconst AfEntryPoint = (logger: LoggerInstanceManager, func: (req: Request, res: Response, nextFn: NextFunction) => void | PromiseLike<void>): Handler => async (req, res, next) => {\n try {\n await func(req, res, next);\n } catch (err) {\n const e = err as Error & { statusCode?: number; };\n logger.error(e.message);\n if (e.statusCode && e.statusCode < 500) {\n res.status(400).json({ error: e.message, status: 'ERROR' });\n return;\n }\n next(e);\n }\n};\n\n/** @returns a monkey-patched express router that will handle async routes (and force a 400 status for codes <500) */\nexport const AfRouter = ({ logger, ...options }: RouterOptions & { logger: LoggerInstanceManager; }): Router => {\n const myRouter = Router({ mergeParams: true, ...options });\n METHODS.forEach((method) => {\n const internalMethod = myRouter[method].bind(myRouter);\n const newMethodHandler: IRouterMatcher<Router> = (...args: [...unknown[]]) => internalMethod(...args.map(argMapper) as [string]);\n myRouter[method] = newMethodHandler;\n });\n return myRouter;\n\n function argMapper(args: unknown): unknown {\n if (Array.isArray(args)) return args.map(argMapper);\n return typeof args === 'function' ? AfEntryPoint(logger, args as Handler) : args;\n }\n};\n","import { Readable } from 'node:stream';\n\nexport async function promiseMap<T, U>(\n iterable: T[] | Iterable<T>,\n handler: (item: T, options?: { signal?: AbortSignal; }) => PromiseLike<U>,\n { concurrency, signal }: Parameters<Readable['map']>[1] = {},\n): Promise<U[]> {\n if (!concurrency) {\n return Promise.all(Array.from(iterable, item => handler(item, signal ? { signal } : undefined)));\n }\n return Readable.from(iterable).map(handler, { concurrency, signal }).toArray();\n}\n","export const consts = Object.freeze({\n ok: 'OK',\n error: 'ERROR',\n fail: 'FAIL',\n});\n","import { AfRouter } from './router';\n\nexport { AfRouter } from './router';\nexport { promiseMap } from './promise-utils';\n\nexport { consts } from './consts';\n\nexport const Router = AfRouter;\n"]}
1
+ {"version":3,"file":"index.js","names":["logger: LoggerInstanceManager","func: (req: Request, res: Response, nextFn: NextFunction) => void | PromiseLike<void>","Router","newMethodHandler: IRouterMatcher<Router>","args: unknown","iterable: T[] | Iterable<T>","handler: (item: T, options?: { signal?: AbortSignal; }) => PromiseLike<U>","consts: Readonly<{ ok: 'OK'; error: 'ERROR'; fail: 'FAIL'; }>","Router: typeof AfRouter"],"sources":["../src/router/index.ts","../src/promise-utils/index.ts","../src/consts/index.ts","../src/index.ts"],"sourcesContent":["import {\n Router, type Request, type Response, type NextFunction, type Handler, type RouterOptions, type IRouterMatcher,\n} from 'express';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\n\nexport const METHODS = [\n 'all',\n 'get',\n 'post',\n 'put',\n 'delete',\n 'patch',\n 'options',\n 'head',\n] as const;\n\nconst AfEntryPoint = (logger: LoggerInstanceManager, func: (req: Request, res: Response, nextFn: NextFunction) => void | PromiseLike<void>): Handler => async (req, res, next) => {\n try {\n await func(req, res, next);\n } catch (err) {\n const e = err as Error & { statusCode?: number; };\n logger.error(e.message);\n if (e.statusCode && e.statusCode < 500) {\n res.status(400).json({ error: e.message, status: 'ERROR' });\n return;\n }\n next(e);\n }\n};\n\n/** @returns a monkey-patched express router that will handle async routes (and force a 400 status for codes <500) */\nexport const AfRouter = ({ logger, ...options }: RouterOptions & { logger: LoggerInstanceManager; }): Router => {\n const myRouter = Router({ mergeParams: true, ...options });\n METHODS.forEach((method) => {\n const internalMethod = myRouter[method].bind(myRouter);\n const newMethodHandler: IRouterMatcher<Router> = (...args: [...unknown[]]) => internalMethod(...args.map(argMapper) as [string]);\n myRouter[method] = newMethodHandler;\n });\n return myRouter;\n\n function argMapper(args: unknown): unknown {\n if (Array.isArray(args)) return args.map(argMapper);\n return typeof args === 'function' ? AfEntryPoint(logger, args as Handler) : args;\n }\n};\n","import { Readable } from 'node:stream';\n\nexport async function promiseMap<T, U>(\n iterable: T[] | Iterable<T>,\n handler: (item: T, options?: { signal?: AbortSignal; }) => PromiseLike<U>,\n { concurrency, signal }: Parameters<Readable['map']>[1] = {},\n): Promise<U[]> {\n if (!concurrency) {\n return Promise.all(Array.from(iterable, item => handler(item, signal ? { signal } : undefined)));\n }\n return Readable.from(iterable).map(handler, { concurrency, signal }).toArray();\n}\n","export const consts: Readonly<{ ok: 'OK'; error: 'ERROR'; fail: 'FAIL'; }> = Object.freeze({\n ok: 'OK',\n error: 'ERROR',\n fail: 'FAIL',\n});\n","import { AfRouter } from './router';\n\nexport { AfRouter } from './router';\nexport { promiseMap } from './promise-utils';\n\nexport { consts } from './consts';\n\nexport const Router: typeof AfRouter = AfRouter;\n"],"mappings":"wEAKA,MAAa,EAAU,CACrB,MACA,MACA,OACA,MACA,SACA,QACA,UACA,MACD,EAEK,EAAe,CAACA,EAA+BC,IAAmG,MAAO,EAAK,EAAK,IAAS,CAChL,GAAI,CACF,MAAM,EAAK,EAAK,EAAK,EAAK,AAC3B,OAAQ,EAAK,CACZ,IAAM,EAAI,EAEV,GADA,EAAO,MAAM,EAAE,QAAQ,CACnB,EAAE,YAAc,EAAE,WAAa,IAAK,CACtC,EAAI,OAAO,IAAI,CAAC,KAAK,CAAE,MAAO,EAAE,QAAS,OAAQ,OAAS,EAAC,CAC3D,MACD,CACD,EAAK,EAAE,AACR,CACF,EAGY,EAAW,CAAC,CAAE,SAAQ,GAAG,EAA6D,GAAa,CAC9G,IAAM,EAAWC,EAAO,CAAE,YAAa,GAAM,GAAG,CAAS,EAAC,CAM1D,OALA,EAAQ,QAAQ,AAAC,GAAW,CAC1B,IAAM,EAAiB,EAAS,GAAQ,KAAK,EAAS,CAChDC,EAA2C,CAAC,GAAG,IAAyB,EAAe,GAAG,EAAK,IAAI,EAAU,CAAa,CAChI,EAAS,GAAU,CACpB,EAAC,CACK,EAEP,SAAS,EAAUC,EAAwB,CAEzC,OADI,MAAM,QAAQ,EAAK,CAAS,EAAK,IAAI,EAAU,CAC5C,OAAO,GAAS,WAAa,EAAa,EAAQ,EAAgB,CAAG,CAC7E,CACF,EC1CD,eAAsB,EACpBC,EACAC,EACA,CAAE,cAAa,SAAwC,CAAG,CAAE,EAC9C,CAId,OAHK,EAGE,EAAS,KAAK,EAAS,CAAC,IAAI,EAAS,CAAE,cAAa,QAAQ,EAAC,CAAC,SAAS,CAFrE,QAAQ,IAAI,MAAM,KAAK,EAAU,GAAQ,EAAQ,EAAM,EAAS,CAAE,QAAQ,EAAG,IAAA,GAAU,CAAC,CAAC,AAGnG,CCXD,MAAaC,EAAgE,OAAO,OAAO,CACzF,GAAI,KACJ,MAAO,QACP,KAAM,MACP,EAAC,CCGWC,EAA0B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/node-common",
3
- "version": "4.1.4",
3
+ "version": "4.1.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -22,17 +22,9 @@
22
22
  "scripts": {
23
23
  "coverage": "vitest --coverage",
24
24
  "test": "vitest",
25
- "build": "tsup",
25
+ "build": "tsdown",
26
26
  "linter": "eslint ."
27
27
  },
28
- "jest": {
29
- "setupFilesAfterEnv": [
30
- "jest-extended"
31
- ],
32
- "testEnvironmentOptions": {
33
- "url": "http://localhost:8085/"
34
- }
35
- },
36
28
  "repository": {
37
29
  "type": "git",
38
30
  "url": "git+ssh://git@github.com/Autofleet/autorepo.git"
@@ -45,8 +37,7 @@
45
37
  "homepage": "https://github.com/Autofleet/autorepo/tree/master/packages/node-common#readme",
46
38
  "devDependencies": {
47
39
  "@autofleet/logger": "^4.2.1",
48
- "@types/express": "^4.17.21",
49
- "tsup": "^8.3.6"
40
+ "@types/express": "^4.17.21"
50
41
  },
51
42
  "peerDependencies": {
52
43
  "@autofleet/logger": ">=4.2.1",