kuwan-expresspack-core 0.1.12 → 0.1.14

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.
@@ -0,0 +1,11 @@
1
+ import { z } from 'zod';
2
+ import _ from 'consola';
3
+ import { colorize } from 'consola/utils';
4
+
5
+ var p=process.env.EXPRESSPACK_LOG_LEVEL,h=_.create({level:Number.isNaN(Number(p))?3:Number(p)});function v(r){switch(typeof r){case "number":case "bigint":return colorize("cyan",r.toString());case "boolean":return colorize(r?"green":"red",String(r));case "symbol":return colorize("magenta",r.toString());case "string":return isNaN(Number(r))?colorize("whiteBright",`${r}`):colorize("yellow",`'${r}'`);case "undefined":return colorize("gray","undefined")}return r===null?colorize("dim","null"):String(r)}var o=h;var s=null,f=null,m={};function x(r,n){let t=r.safeParse(n);if(t.success)return t.data;console.error("\u274C Invalid environment variables:"),console.error(z.prettifyError(t.error));}function w(r,n,t={fatal:false,immediate_log_error:true,log_error_after_graceful_start:true}){if(m=t,t.fatal)try{return r.parse(n)}catch(e){throw e instanceof z.ZodError?(s=z.prettifyError(e),f=e,o.error("\u274C Invalid environment variables:"),o.error("\u274C",s),new Error("Invalid environment variables. The environment variable did not pass the schema validation. Check if you have set all required environment variables correctly.")):e}let i=r.safeParse(n);if(!i.success){let e=r.partial().safeParse(n);if(!e.success)return s=z.prettifyError(i.error),f=i.error,t.immediate_log_error&&(o.error("\u274C Invalid environment variables:"),o.error("\u274C",s)),{};s=z.prettifyError(i.error),f=i.error,t.immediate_log_error&&(o.error("\u274C Invalid environment variables:"),o.error("\u274C",s));let c=r.keyof();for(let d of c.options)d in e.data||(e.data[d]=void 0);return e.data}return i.data}function N(){return f}function E(){return s}function O(r){if(!r||Object.keys(r).length===0){o.warn("No environment variables to print. Make sure the env is complete and valid.");return}let n=Object.entries(r).sort(([l],[g])=>l.localeCompare(g)),t=Math.max(...n.map(([l])=>l.length))+2,i=n.map(([l,g])=>`${l.padEnd(t," ")} ${v(g)}`),e=`KEY${" ".repeat(t-3)} VALUE`,c=" ".repeat(e.length);return [c,e,c,...i].join(`
6
+ `)}function S(){if(m){let{log_error_after_graceful_start:r}=m;if(r){let n=E();n&&o.error("Invalid Environment Variable Value",`
7
+ -----
8
+ ${n}
9
+ -----`);}}}var j={create:w,parseEnv:x,prettifyEnv:O,getEnvErrorRaw:N,getEnvErrorString:E,logEnvErrors:S};
10
+
11
+ export { o as a, w as b, N as c, E as d, O as e, S as f, j as g };
package/dist/env.d.ts CHANGED
@@ -1,26 +1,66 @@
1
1
  import { z } from 'zod';
2
2
 
3
- declare function parseEnv<T extends z.ZodType>(schema: T, env: Record<string, string | undefined>): z.infer<T> | undefined;
3
+ interface EnvOptions {
4
+ /**
5
+ * If true, throw an error on validation failure.
6
+ * This will stop the application from starting
7
+ * if the environment variables are invalid.
8
+ */
9
+ fatal?: boolean;
10
+ /**
11
+ * If true, log errors immediately when validation fails.
12
+ * This will usually show at the start of the application logs.
13
+ * If false, no logs will be made. Errors can then be logged manually using `getEnvErrorString()` or `getEnvErrorRaw()`.
14
+ *
15
+ * This only works when `fatal` is false.
16
+ */
17
+ immediate_log_error?: boolean;
18
+ /**
19
+ * If true, log errors after the application has started gracefully.
20
+ * Only works when using the provided graceful start handler included in this package.
21
+ *
22
+ * Set to `false` to disable logging after graceful start.
23
+ */
24
+ log_error_after_graceful_start?: boolean;
25
+ }
4
26
  /**
5
- * Load env with types and validation from a Zod schema
27
+ * This function is no longer maintained and was used
28
+ * only for `Env.create()` before to parse and exposed for convenience.
29
+ *
30
+ * `Env.create()` has it's own internal parsing logic now.
31
+ *
32
+ * This caused type inference issues with zod schema leading
33
+ * to "Typescript infinite instantiation" error.
6
34
  *
7
- * @param appRoot
8
- * @param zodSchema
9
- * @param env
35
+ * @deprecated Write your own parsing logic using zod directly or use `Env.create()` to validate env variables against a zod schema.
10
36
  */
11
- declare function create<T extends z.ZodType>(appRoot: string | URL, zodSchema: T, env?: Record<string, string | undefined>): z.infer<T>;
37
+ declare function parseEnv<T>(schema: z.ZodObject, env: Record<string, string | undefined>): Record<string, any> | undefined;
12
38
  /**
13
- * Helper type to infer the environment variable types from a Zod schema
14
- *
15
- * @example
16
- * ```
17
- * export type Env = InferEnv<typeof envSchema>;
18
- * ```
39
+ * Load env with types and validation from a Zod schema
40
+ */
41
+ declare function create<T extends z.ZodObject>(schema: T, data: unknown, options?: Partial<EnvOptions>): z.infer<T> | undefined | {};
42
+ declare function getEnvErrorRaw(): z.ZodError | null;
43
+ /**
44
+ * @returns Formatted string of environment variable validation error.
45
+ * The format is the exact same as zod's `prettifyError` output.
46
+ */
47
+ declare function getEnvErrorString(): string | null;
48
+ /**
49
+ * Does not log to console, just returns the formatted string.
50
+ */
51
+ declare function prettifyEnv(env: Record<string, any>): string | undefined;
52
+ /**
53
+ * This is a convenience function to log env errors manually.
54
+ * Errors are already logged after by graceful start.
19
55
  */
20
- type InferEnv<T extends z.ZodType> = z.infer<T>;
56
+ declare function logEnvErrors(): void;
21
57
  declare const _default: {
22
58
  create: typeof create;
23
59
  parseEnv: typeof parseEnv;
60
+ prettifyEnv: typeof prettifyEnv;
61
+ getEnvErrorRaw: typeof getEnvErrorRaw;
62
+ getEnvErrorString: typeof getEnvErrorString;
63
+ logEnvErrors: typeof logEnvErrors;
24
64
  };
25
65
 
26
- export { type InferEnv, create, _default as default, parseEnv };
66
+ export { type EnvOptions, create, _default as default, getEnvErrorRaw, getEnvErrorString, logEnvErrors, prettifyEnv };
package/dist/env.mjs CHANGED
@@ -1,5 +1 @@
1
- import { z } from 'zod';
2
-
3
- function t(n,r){let e=n.safeParse(r);return e.success||(console.error("\u274C Invalid environment variables:"),console.error(z.prettifyError(e.error))),e?.data}function s(n,r,e){return t(r,e||process.env)||{}}var a={create:s,parseEnv:t};
4
-
5
- export { s as create, a as default, t as parseEnv };
1
+ export { b as create, g as default, c as getEnvErrorRaw, d as getEnvErrorString, f as logEnvErrors, e as prettifyEnv } from './chunk-VOOKMRDS.mjs';
package/dist/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  import { c } from './chunk-CRIPGIHI.mjs';
2
- import { a } from './chunk-VSPZT4K6.mjs';
3
- import N from 'consola';
2
+ import { a as a$1 } from './chunk-VSPZT4K6.mjs';
3
+ import { a, f } from './chunk-VOOKMRDS.mjs';
4
4
  import { Router } from 'express';
5
- import _, { relative, join } from 'path';
5
+ import N, { relative, join } from 'path';
6
6
  import { existsSync } from 'fs';
7
7
  import { glob } from 'tinyglobby';
8
8
  import { pathToFileURL } from 'url';
9
9
 
10
- var v=process.env.EXPRESSPACK_LOG_LEVEL,j=N.create({level:Number.isNaN(Number(v))?3:Number(v)}),t=j;function T(r,n,e){return r.listen(n,async()=>{e&&(t.debug("Executing onStart hook..."),await e().catch(o=>{t.error("Error during onStart hook:",o);})),t.box({message:`Server is running at http://localhost:${n}`,style:{borderColor:"cyan"},level:"info"}),c.emit("app:mounted");})}function k(r,n){process.on("SIGINT",async()=>{t.info("Received SIGINT. Shutting down gracefully..."),await new Promise(e=>{r.close(async i=>i instanceof Error?(t.error("Error shutting down server:",i),e()):(n&&await n().catch(o=>{t.error("Error during shutdown hook:",o);}),t.info("Server has been shut down."),e()));}),process.exit(0);});}function A(r){return r}async function K({app:r},{kernel:n,listener:e,appBootstrap:i}){t.debug("Loading kernel...");try{let o=await n();await(o?.default||o)({app:r});}catch(o){throw t.error("Error loading kernel:",o),o}if(e&&typeof e=="function")try{let o=await e(),a=o?.default||o;a===void 0||typeof a!="function"?t.warn("No listener setup function found"):await a();}catch(o){throw t.error("Error loading listener:",o),o}if(i&&typeof i=="function")try{await i({app:r});}catch(o){throw t.error("Error during boot process:",o),o}t.debug("Kernel loaded.");}function g(r){return Router(r)}function O(r,n){let e=g(n);return r(e),e}function b(r){let n=join(r,"expresspack.config"),e=join(r,"app"),i=join(r,"config"),o=join(i,"app"),a=join(i,"body-parser"),f=join(e,"controllers"),u=join(e,"services"),d=join(e,"errors"),m=join(e,"middlewares"),l=join(e,"routes");return {rc:n,root:r,app:e,middlewareFilePath:m,routesFilePath:l,config:{base:i,bodyParserFile:a,appConfigFile:o},controllers:{base:f},services:{base:u},errors:{base:d}}}var P=r=>{let n=["ts","mts","js","mjs","cjs","cts"];for(let e of n){let i=`${r}.${e}`;if(existsSync(i))return i}return null},R=r=>existsSync(r)?r:null;function S(r){if(!r)return "";let n=_.sep;return r.endsWith(n)?r:r+n}var I="*.{js,ts,mjs,cjs,mts,cts}",D=["!*.d.{ts,mts,mjs,cts}","!*.map"];async function C(r){let n=await glob([I,...D],{cwd:r}),e=new Set;return n.forEach(i=>{let o=i.replace(/\.(js|ts|mjs|cjs|mts|cts)$/,"");e.add(o);}),Array.from(e)}async function M(r){let{config:n}=b(r),e=n.base;if(t.debug("Loading all configurations from: `"+S(relative(r,n.base)+"`")),!R(e)){t.debug("No config directory found. Looked for:",e);return}let o=await C(e);t.debug(`Found ${o.length} config files: %s`,`
10
+ function T(r,n,e){return r.listen(n,async()=>{e&&(a.debug("Executing onStart hook..."),await e().catch(o=>{a.error("Error during onStart hook:",o);})),a.box({message:`Server is running at http://localhost:${n}`,style:{borderColor:"cyan"},level:"info"});try{await c.emit("app:mounted");}catch(o){a.error("Error during app:mounted event:",o);}finally{f();}})}function k(r,n){process.on("SIGINT",async()=>{a.info("Received SIGINT. Shutting down gracefully..."),await new Promise(e=>{r.close(async i=>i instanceof Error?(a.error("Error shutting down server:",i),e()):(n&&await n().catch(o=>{a.error("Error during shutdown hook:",o);}),a.info("Server has been shut down."),e()));}),process.exit(0);});}function L(r){return r}async function $({app:r},{kernel:n,listener:e,appBootstrap:i}){a.debug("Loading kernel...");try{let o=await n();await(o?.default||o)({app:r});}catch(o){throw a.error("Error loading kernel:",o),o}if(e&&typeof e=="function")try{let o=await e(),a$1=o?.default||o;a$1===void 0||typeof a$1!="function"?a.warn("No listener setup function found"):await a$1();}catch(o){throw a.error("Error loading listener:",o),o}if(i&&typeof i=="function")try{await i({app:r});}catch(o){throw a.error("Error during boot process:",o),o}a.debug("Kernel loaded.");}function g(r){return Router(r)}function K(r,n){let e=g(n);return r(e),e}function P(r){let n=join(r,"expresspack.config"),e=join(r,"app"),i=join(r,"config"),o=join(i,"app"),a=join(i,"body-parser"),f=join(e,"controllers"),l=join(e,"services"),d=join(e,"errors"),m=join(e,"middlewares"),u=join(e,"routes");return {rc:n,root:r,app:e,middlewareFilePath:m,routesFilePath:u,config:{base:i,bodyParserFile:a,appConfigFile:o},controllers:{base:f},services:{base:l},errors:{base:d}}}var b=r=>{let n=["ts","mts","js","mjs","cjs","cts"];for(let e of n){let i=`${r}.${e}`;if(existsSync(i))return i}return null},R=r=>existsSync(r)?r:null;function S(r){if(!r)return "";let n=N.sep;return r.endsWith(n)?r:r+n}var I="*.{js,ts,mjs,cjs,mts,cts}",_=["!*.d.{ts,mts,mjs,cts}","!*.map"];async function C(r){let n=await glob([I,..._],{cwd:r}),e=new Set;return n.forEach(i=>{let o=i.replace(/\.(js|ts|mjs|cjs|mts|cts)$/,"");e.add(o);}),Array.from(e)}async function U(r){let{config:n}=P(r),e=n.base;if(a.debug("Loading all configurations from: `"+S(relative(r,n.base)+"`")),!R(e)){a.debug("No config directory found. Looked for:",e);return}let o=await C(e);a.debug(`Found ${o.length} config files: %s`,`
11
11
  ${o.join(`
12
- `)}`);for(let f of o){let u=P(join(e,f));if(!u){t.warn(`No config file found for topic: ${f}`);continue}try{let m=await import(pathToFileURL(u).href),l=m.default||m;typeof l=="function"&&(l=await l());let x=f.replace(/[-_](\w)/g,(H,h)=>h?h.toUpperCase():"");a[x]=l;let F=relative(r,u);t.debug(`Loaded config: \`${x}\` from \`${F}\``);}catch(d){t.error(`Failed to load config ${f}:`,d);}}}
12
+ `)}`);for(let f of o){let l=b(join(e,f));if(!l){a.warn(`No config file found for topic: ${f}`);continue}try{let m=await import(pathToFileURL(l).href),u=m.default||m;typeof u=="function"&&(u=await u());let x=f.replace(/[-_](\w)/g,(B,h)=>h?h.toUpperCase():"");a$1[x]=u;let j=relative(r,l);a.debug(`Loaded config: \`${x}\` from \`${j}\``);}catch(d){a.error(`Failed to load config ${f}:`,d);}}}
13
13
 
14
- export { M as configLoader, g as createRouter, A as defineKernel, O as defineRouterMiddleware, T as gracefulHTTPStart, k as gracefulShutdown, K as loadKernel };
14
+ export { U as configLoader, g as createRouter, L as defineKernel, K as defineRouterMiddleware, T as gracefulHTTPStart, k as gracefulShutdown, $ as loadKernel };
@@ -1,6 +1,14 @@
1
- import { ConsolaInstance } from 'consola';
1
+ import { ConsolaInstance, ConsolaOptions } from 'consola';
2
2
 
3
3
  declare const println: ConsolaInstance;
4
+ /**
5
+ * Create a console logger with a specific name. Uses the log-level specified in `APP_LOG_LEVEL` env variable.
6
+ */
4
7
  declare function useConsoleLogger(name: string): ConsolaInstance;
8
+ /**
9
+ * Create a console logger allowing to specify log level in options.
10
+ */
11
+ declare function createConsoleLogger(name: string, options: Partial<Pick<ConsolaOptions, "level">>): ConsolaInstance;
12
+ declare const Logger: typeof useConsoleLogger;
5
13
 
6
- export { println, useConsoleLogger };
14
+ export { Logger, createConsoleLogger, println, useConsoleLogger };
@@ -1,6 +1,6 @@
1
1
  import n from 'consola';
2
2
  import { env } from 'process';
3
3
 
4
- var e=env.APP_LOG_LEVEL,t=n.create({level:Number.isNaN(Number(e))?3:Number(e)});function i(o){return t.withTag(o)}
4
+ var e=env.APP_LOG_LEVEL,l=n.create({level:Number.isNaN(Number(e))?3:Number(e)});function s(o){return l.withTag(o)}function u(o,r){return n.create({level:r.level??(Number.isNaN(Number(e))?3:Number(e))}).withTag(o)}var m=s;
5
5
 
6
- export { t as println, i as useConsoleLogger };
6
+ export { m as Logger, u as createConsoleLogger, l as println, s as useConsoleLogger };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kuwan-expresspack-core",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -91,7 +91,7 @@
91
91
  "express": "^5.1.0",
92
92
  "kysely": "^0.28.8",
93
93
  "tinyglobby": "^0.2.15",
94
- "zod": "^4.0.14"
94
+ "zod": "4.1.12"
95
95
  },
96
96
  "keywords": [],
97
97
  "author": {
@@ -122,7 +122,8 @@
122
122
  "eslint": "^9.37.0",
123
123
  "express": "^5.1.0",
124
124
  "kysely": "^0.28.8",
125
- "typescript": "^5.9.3"
125
+ "typescript": "^5.9.3",
126
+ "zod": "4.1.12"
126
127
  },
127
128
  "sideEffects": false,
128
129
  "scripts": {