@catbee/utils 0.0.8-next.0 → 0.0.8-rc.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/README.md +1 -26
- package/build/esm/config.d.ts +122 -0
- package/build/esm/config.js +133 -0
- package/build/esm/config.js.map +1 -0
- package/build/esm/index.d.ts +27 -0
- package/build/esm/index.js +50 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/servers/server.builder.d.ts +508 -0
- package/build/esm/servers/server.builder.js +702 -0
- package/build/esm/servers/server.builder.js.map +1 -0
- package/build/esm/servers/server.d.ts +256 -0
- package/build/esm/servers/server.js +1248 -0
- package/build/esm/servers/server.js.map +1 -0
- package/build/esm/types/api-response.d.ts +152 -0
- package/build/esm/types/api-response.js +34 -0
- package/build/esm/types/api-response.js.map +1 -0
- package/build/esm/types/index.d.ts +125 -0
- package/build/esm/types/index.js +25 -0
- package/build/esm/types/index.js.map +1 -0
- package/build/esm/types/server.d.ts +268 -0
- package/build/esm/types/server.js +25 -0
- package/build/esm/types/server.js.map +1 -0
- package/build/esm/utils/array.utils.d.ts +168 -0
- package/build/esm/utils/array.utils.js +382 -0
- package/build/esm/utils/array.utils.js.map +1 -0
- package/build/esm/utils/async.utils.d.ts +265 -0
- package/build/esm/utils/async.utils.js +896 -0
- package/build/esm/utils/async.utils.js.map +1 -0
- package/build/esm/utils/cache.utils.d.ts +153 -0
- package/build/esm/utils/cache.utils.js +499 -0
- package/build/esm/utils/cache.utils.js.map +1 -0
- package/build/esm/utils/context-store.utils.d.ts +189 -0
- package/build/esm/utils/context-store.utils.js +307 -0
- package/build/esm/utils/context-store.utils.js.map +1 -0
- package/build/esm/utils/crypto.utils.d.ts +160 -0
- package/build/esm/utils/crypto.utils.js +375 -0
- package/build/esm/utils/crypto.utils.js.map +1 -0
- package/build/esm/utils/date.utils.d.ts +159 -0
- package/build/esm/utils/date.utils.js +389 -0
- package/build/esm/utils/date.utils.js.map +1 -0
- package/build/esm/utils/decorators.utils.d.ts +315 -0
- package/build/esm/utils/decorators.utils.js +666 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +196 -0
- package/build/esm/utils/dir.utils.js +998 -0
- package/build/esm/utils/dir.utils.js.map +1 -0
- package/build/esm/utils/env.utils.d.ts +377 -0
- package/build/esm/utils/env.utils.js +873 -0
- package/build/esm/utils/env.utils.js.map +1 -0
- package/build/esm/utils/exception.utils.d.ts +230 -0
- package/build/esm/utils/exception.utils.js +559 -0
- package/build/esm/utils/exception.utils.js.map +1 -0
- package/build/esm/utils/fs.utils.d.ts +164 -0
- package/build/esm/utils/fs.utils.js +580 -0
- package/build/esm/utils/fs.utils.js.map +1 -0
- package/build/esm/utils/http-status-codes.d.ts +266 -0
- package/build/esm/utils/http-status-codes.js +295 -0
- package/build/esm/utils/http-status-codes.js.map +1 -0
- package/build/esm/utils/id.utils.d.ts +36 -0
- package/build/esm/utils/id.utils.js +87 -0
- package/build/esm/utils/id.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +160 -0
- package/build/esm/utils/logger.utils.js +341 -0
- package/build/esm/utils/logger.utils.js.map +1 -0
- package/build/esm/utils/middleware.utils.d.ts +100 -0
- package/build/esm/utils/middleware.utils.js +325 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +124 -0
- package/build/esm/utils/obj.utils.js +519 -0
- package/build/esm/utils/obj.utils.js.map +1 -0
- package/build/esm/utils/performance.utils.d.ts +136 -0
- package/build/esm/utils/performance.utils.js +376 -0
- package/build/esm/utils/performance.utils.js.map +1 -0
- package/build/esm/utils/request.utils.d.ts +86 -0
- package/build/esm/utils/request.utils.js +223 -0
- package/build/esm/utils/request.utils.js.map +1 -0
- package/build/esm/utils/response.utils.d.ts +163 -0
- package/build/esm/utils/response.utils.js +284 -0
- package/build/esm/utils/response.utils.js.map +1 -0
- package/build/esm/utils/stream.utils.d.ts +88 -0
- package/build/esm/utils/stream.utils.js +284 -0
- package/build/esm/utils/stream.utils.js.map +1 -0
- package/build/esm/utils/string.utils.d.ts +93 -0
- package/build/esm/utils/string.utils.js +169 -0
- package/build/esm/utils/string.utils.js.map +1 -0
- package/build/esm/utils/type.utils.d.ts +90 -0
- package/build/esm/utils/type.utils.js +190 -0
- package/build/esm/utils/type.utils.js.map +1 -0
- package/build/esm/utils/url.utils.d.ts +141 -0
- package/build/esm/utils/url.utils.js +381 -0
- package/build/esm/utils/url.utils.js.map +1 -0
- package/build/esm/utils/validate.utils.d.ts +177 -0
- package/build/esm/utils/validate.utils.js +336 -0
- package/build/esm/utils/validate.utils.js.map +1 -0
- package/build/esnext/config.d.ts +122 -0
- package/build/esnext/config.js +133 -0
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/index.d.ts +27 -0
- package/build/esnext/index.js +50 -0
- package/build/esnext/index.js.map +1 -0
- package/build/esnext/servers/server.builder.d.ts +508 -0
- package/build/esnext/servers/server.builder.js +657 -0
- package/build/esnext/servers/server.builder.js.map +1 -0
- package/build/esnext/servers/server.d.ts +256 -0
- package/build/esnext/servers/server.js +955 -0
- package/build/esnext/servers/server.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +152 -0
- package/build/esnext/types/api-response.js +34 -0
- package/build/esnext/types/api-response.js.map +1 -0
- package/build/esnext/types/index.d.ts +125 -0
- package/build/esnext/types/index.js +25 -0
- package/build/esnext/types/index.js.map +1 -0
- package/build/esnext/types/server.d.ts +268 -0
- package/build/esnext/types/server.js +25 -0
- package/build/esnext/types/server.js.map +1 -0
- package/build/esnext/utils/array.utils.d.ts +168 -0
- package/build/esnext/utils/array.utils.js +345 -0
- package/build/esnext/utils/array.utils.js.map +1 -0
- package/build/esnext/utils/async.utils.d.ts +265 -0
- package/build/esnext/utils/async.utils.js +620 -0
- package/build/esnext/utils/async.utils.js.map +1 -0
- package/build/esnext/utils/cache.utils.d.ts +153 -0
- package/build/esnext/utils/cache.utils.js +293 -0
- package/build/esnext/utils/cache.utils.js.map +1 -0
- package/build/esnext/utils/context-store.utils.d.ts +189 -0
- package/build/esnext/utils/context-store.utils.js +289 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -0
- package/build/esnext/utils/crypto.utils.d.ts +160 -0
- package/build/esnext/utils/crypto.utils.js +276 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -0
- package/build/esnext/utils/date.utils.d.ts +159 -0
- package/build/esnext/utils/date.utils.js +384 -0
- package/build/esnext/utils/date.utils.js.map +1 -0
- package/build/esnext/utils/decorators.utils.d.ts +315 -0
- package/build/esnext/utils/decorators.utils.js +504 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +196 -0
- package/build/esnext/utils/dir.utils.js +477 -0
- package/build/esnext/utils/dir.utils.js.map +1 -0
- package/build/esnext/utils/env.utils.d.ts +377 -0
- package/build/esnext/utils/env.utils.js +785 -0
- package/build/esnext/utils/env.utils.js.map +1 -0
- package/build/esnext/utils/exception.utils.d.ts +230 -0
- package/build/esnext/utils/exception.utils.js +381 -0
- package/build/esnext/utils/exception.utils.js.map +1 -0
- package/build/esnext/utils/fs.utils.d.ts +164 -0
- package/build/esnext/utils/fs.utils.js +349 -0
- package/build/esnext/utils/fs.utils.js.map +1 -0
- package/build/esnext/utils/http-status-codes.d.ts +266 -0
- package/build/esnext/utils/http-status-codes.js +295 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -0
- package/build/esnext/utils/id.utils.d.ts +36 -0
- package/build/esnext/utils/id.utils.js +84 -0
- package/build/esnext/utils/id.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +160 -0
- package/build/esnext/utils/logger.utils.js +300 -0
- package/build/esnext/utils/logger.utils.js.map +1 -0
- package/build/esnext/utils/middleware.utils.d.ts +100 -0
- package/build/esnext/utils/middleware.utils.js +236 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +124 -0
- package/build/esnext/utils/obj.utils.js +413 -0
- package/build/esnext/utils/obj.utils.js.map +1 -0
- package/build/esnext/utils/performance.utils.d.ts +136 -0
- package/build/esnext/utils/performance.utils.js +271 -0
- package/build/esnext/utils/performance.utils.js.map +1 -0
- package/build/esnext/utils/request.utils.d.ts +86 -0
- package/build/esnext/utils/request.utils.js +186 -0
- package/build/esnext/utils/request.utils.js.map +1 -0
- package/build/esnext/utils/response.utils.d.ts +163 -0
- package/build/esnext/utils/response.utils.js +247 -0
- package/build/esnext/utils/response.utils.js.map +1 -0
- package/build/esnext/utils/stream.utils.d.ts +88 -0
- package/build/esnext/utils/stream.utils.js +210 -0
- package/build/esnext/utils/stream.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.d.ts +93 -0
- package/build/esnext/utils/string.utils.js +165 -0
- package/build/esnext/utils/string.utils.js.map +1 -0
- package/build/esnext/utils/type.utils.d.ts +90 -0
- package/build/esnext/utils/type.utils.js +187 -0
- package/build/esnext/utils/type.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.d.ts +141 -0
- package/build/esnext/utils/url.utils.js +304 -0
- package/build/esnext/utils/url.utils.js.map +1 -0
- package/build/esnext/utils/validate.utils.d.ts +177 -0
- package/build/esnext/utils/validate.utils.js +320 -0
- package/build/esnext/utils/validate.utils.js.map +1 -0
- package/build/src/config.d.ts +122 -0
- package/build/src/config.js +138 -0
- package/build/src/config.js.map +1 -0
- package/build/src/index.d.ts +27 -0
- package/build/src/index.js +69 -0
- package/build/src/index.js.map +1 -0
- package/build/src/servers/server.builder.d.ts +508 -0
- package/build/src/servers/server.builder.js +661 -0
- package/build/src/servers/server.builder.js.map +1 -0
- package/build/src/servers/server.d.ts +256 -0
- package/build/src/servers/server.js +962 -0
- package/build/src/servers/server.js.map +1 -0
- package/build/src/types/api-response.d.ts +152 -0
- package/build/src/types/api-response.js +37 -0
- package/build/src/types/api-response.js.map +1 -0
- package/build/src/types/index.d.ts +125 -0
- package/build/src/types/index.js +26 -0
- package/build/src/types/index.js.map +1 -0
- package/build/src/types/server.d.ts +268 -0
- package/build/src/types/server.js +26 -0
- package/build/src/types/server.js.map +1 -0
- package/build/src/utils/array.utils.d.ts +168 -0
- package/build/src/utils/array.utils.js +364 -0
- package/build/src/utils/array.utils.js.map +1 -0
- package/build/src/utils/async.utils.d.ts +265 -0
- package/build/src/utils/async.utils.js +641 -0
- package/build/src/utils/async.utils.js.map +1 -0
- package/build/src/utils/cache.utils.d.ts +153 -0
- package/build/src/utils/cache.utils.js +297 -0
- package/build/src/utils/cache.utils.js.map +1 -0
- package/build/src/utils/context-store.utils.d.ts +189 -0
- package/build/src/utils/context-store.utils.js +296 -0
- package/build/src/utils/context-store.utils.js.map +1 -0
- package/build/src/utils/crypto.utils.d.ts +160 -0
- package/build/src/utils/crypto.utils.js +293 -0
- package/build/src/utils/crypto.utils.js.map +1 -0
- package/build/src/utils/date.utils.d.ts +159 -0
- package/build/src/utils/date.utils.js +396 -0
- package/build/src/utils/date.utils.js.map +1 -0
- package/build/src/utils/decorators.utils.d.ts +315 -0
- package/build/src/utils/decorators.utils.js +516 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +196 -0
- package/build/src/utils/dir.utils.js +501 -0
- package/build/src/utils/dir.utils.js.map +1 -0
- package/build/src/utils/env.utils.d.ts +377 -0
- package/build/src/utils/env.utils.js +789 -0
- package/build/src/utils/env.utils.js.map +1 -0
- package/build/src/utils/exception.utils.d.ts +230 -0
- package/build/src/utils/exception.utils.js +407 -0
- package/build/src/utils/exception.utils.js.map +1 -0
- package/build/src/utils/fs.utils.d.ts +164 -0
- package/build/src/utils/fs.utils.js +372 -0
- package/build/src/utils/fs.utils.js.map +1 -0
- package/build/src/utils/http-status-codes.d.ts +266 -0
- package/build/src/utils/http-status-codes.js +298 -0
- package/build/src/utils/http-status-codes.js.map +1 -0
- package/build/src/utils/id.utils.d.ts +36 -0
- package/build/src/utils/id.utils.js +91 -0
- package/build/src/utils/id.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +160 -0
- package/build/src/utils/logger.utils.js +346 -0
- package/build/src/utils/logger.utils.js.map +1 -0
- package/build/src/utils/middleware.utils.d.ts +100 -0
- package/build/src/utils/middleware.utils.js +244 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +124 -0
- package/build/src/utils/obj.utils.js +429 -0
- package/build/src/utils/obj.utils.js.map +1 -0
- package/build/src/utils/performance.utils.d.ts +136 -0
- package/build/src/utils/performance.utils.js +278 -0
- package/build/src/utils/performance.utils.js.map +1 -0
- package/build/src/utils/request.utils.d.ts +86 -0
- package/build/src/utils/request.utils.js +195 -0
- package/build/src/utils/request.utils.js.map +1 -0
- package/build/src/utils/response.utils.d.ts +163 -0
- package/build/src/utils/response.utils.js +260 -0
- package/build/src/utils/response.utils.js.map +1 -0
- package/build/src/utils/stream.utils.d.ts +88 -0
- package/build/src/utils/stream.utils.js +218 -0
- package/build/src/utils/stream.utils.js.map +1 -0
- package/build/src/utils/string.utils.d.ts +93 -0
- package/build/src/utils/string.utils.js +179 -0
- package/build/src/utils/string.utils.js.map +1 -0
- package/build/src/utils/type.utils.d.ts +90 -0
- package/build/src/utils/type.utils.js +196 -0
- package/build/src/utils/type.utils.js.map +1 -0
- package/build/src/utils/url.utils.d.ts +141 -0
- package/build/src/utils/url.utils.js +317 -0
- package/build/src/utils/url.utils.js.map +1 -0
- package/build/src/utils/validate.utils.d.ts +177 -0
- package/build/src/utils/validate.utils.js +345 -0
- package/build/src/utils/validate.utils.js.map +1 -0
- package/package.json +66 -12
- package/build/index.cjs +0 -6888
- package/build/index.d.ts +0 -5242
- package/build/index.mjs +0 -6615
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { Express, json, NextFunction, urlencoded, Request, Response } from 'express';
|
|
2
|
+
import { ExpressServer } from '../servers/server';
|
|
3
|
+
import { HelmetOptions } from 'helmet';
|
|
4
|
+
import { CompressionOptions } from 'compression';
|
|
5
|
+
import { CookieParseOptions } from 'cookie-parser';
|
|
6
|
+
import http from 'http';
|
|
7
|
+
import { CorsOptions } from 'cors';
|
|
8
|
+
import { ToggleConfig } from '.';
|
|
9
|
+
/**
|
|
10
|
+
* Server configuration interface with smart defaults and full customization.
|
|
11
|
+
* All options are designed with security and performance best practices.
|
|
12
|
+
* Most options can be overridden via environment variables.
|
|
13
|
+
*/
|
|
14
|
+
export interface ServerConfig {
|
|
15
|
+
/** Port the server should listen on (default: 3000, env: PORT)
|
|
16
|
+
* - default: 3000
|
|
17
|
+
*/
|
|
18
|
+
port: number;
|
|
19
|
+
/** Optional host address for binding (default: '0.0.0.0', env: HOST)
|
|
20
|
+
* - default: '0.0.0.0'
|
|
21
|
+
*/
|
|
22
|
+
host?: string;
|
|
23
|
+
/** CORS configuration (default: false disables CORS, or provide options object)
|
|
24
|
+
* - default: false
|
|
25
|
+
*/
|
|
26
|
+
cors?: ToggleConfig<CorsOptions>;
|
|
27
|
+
/** Enable Helmet security headers (default: false disables Helmet, or provide options object)
|
|
28
|
+
* - default: false
|
|
29
|
+
*/
|
|
30
|
+
helmet?: ToggleConfig<HelmetOptions>;
|
|
31
|
+
/** Enable gzip/deflate compression (default: false disables compression, or provide options object)
|
|
32
|
+
* - default: false
|
|
33
|
+
*/
|
|
34
|
+
compression?: ToggleConfig<CompressionOptions>;
|
|
35
|
+
/** Request body parsing with size limits
|
|
36
|
+
* - json: { limit: '1mb' }
|
|
37
|
+
* - urlencoded: { extended: true, limit: '1mb' }
|
|
38
|
+
*/
|
|
39
|
+
bodyParser?: {
|
|
40
|
+
/** JSON parser (default: { limit: '1mb' }) */
|
|
41
|
+
json?: Parameters<typeof json>[0];
|
|
42
|
+
/** URL-encoded parser (default: { extended: true, limit: '1mb' }) */
|
|
43
|
+
urlencoded?: Parameters<typeof urlencoded>[0];
|
|
44
|
+
};
|
|
45
|
+
/** Enable cookie parsing with options (default: false)
|
|
46
|
+
* - default: false
|
|
47
|
+
*/
|
|
48
|
+
cookieParser?: ToggleConfig<CookieParseOptions>;
|
|
49
|
+
/** Trust proxy headers (default: false)
|
|
50
|
+
* - default: false
|
|
51
|
+
*/
|
|
52
|
+
trustProxy?: boolean;
|
|
53
|
+
/** Static file serving configuration for assets, uploads, etc.
|
|
54
|
+
* - path: file system path to serve
|
|
55
|
+
* - route: URL route prefix (default: "/")
|
|
56
|
+
* - maxAge: cache max age (default: 0)
|
|
57
|
+
* - etag: enable ETag headers (default: true)
|
|
58
|
+
* - immutable: enable immutable caching (default: false)
|
|
59
|
+
* - lastModified: enable last-modified caching (default: true)
|
|
60
|
+
* - cacheControl: enable Cache-Control headers (default: true)
|
|
61
|
+
*/
|
|
62
|
+
staticFolders?: Array<{
|
|
63
|
+
/** URL route prefix (default: "/") */
|
|
64
|
+
path?: string;
|
|
65
|
+
/** File system path to serve */
|
|
66
|
+
directory: string;
|
|
67
|
+
/** Cache-Control: max-age=<duration> (default: 0) */
|
|
68
|
+
maxAge?: string;
|
|
69
|
+
/** Enable ETag headers (default: true) */
|
|
70
|
+
etag?: boolean;
|
|
71
|
+
/** Enable immutable caching (default: false) */
|
|
72
|
+
immutable?: boolean;
|
|
73
|
+
/** Enable last-modified caching (default: true) */
|
|
74
|
+
lastModified?: boolean;
|
|
75
|
+
/** Enable Cache-Control headers (default: true) */
|
|
76
|
+
cacheControl?: boolean;
|
|
77
|
+
}>;
|
|
78
|
+
/** Enable microservice mode (default: false)
|
|
79
|
+
* - default: false
|
|
80
|
+
*/
|
|
81
|
+
isMicroservice?: boolean;
|
|
82
|
+
/** Service name for headers/metrics (default: 'express_app', env: APP_NAME)
|
|
83
|
+
* - default: 'express_app'
|
|
84
|
+
*/
|
|
85
|
+
appName?: string;
|
|
86
|
+
/** Global response headers (default: {})
|
|
87
|
+
* - default: {}
|
|
88
|
+
*/
|
|
89
|
+
globalHeaders?: Record<string, string | (() => string)>;
|
|
90
|
+
/** Rate limiting settings
|
|
91
|
+
* - enable: false
|
|
92
|
+
* - windowMs: 15 * 60 * 1000
|
|
93
|
+
* - max: 100
|
|
94
|
+
* - message: 'Too many requests'
|
|
95
|
+
* - standardHeaders: true
|
|
96
|
+
* - legacyHeaders: false
|
|
97
|
+
*/
|
|
98
|
+
rateLimit?: {
|
|
99
|
+
/** Enable rate limiting (default: false) */
|
|
100
|
+
enable: boolean;
|
|
101
|
+
/** Time window in ms (default: 900000 - 15 minutes) */
|
|
102
|
+
windowMs?: number;
|
|
103
|
+
/** Max requests per window (default: 100) */
|
|
104
|
+
max?: number;
|
|
105
|
+
/** Rate limit message (default: 'Too many requests') */
|
|
106
|
+
message?: string;
|
|
107
|
+
/** Add standard headers (default: true) */
|
|
108
|
+
standardHeaders?: boolean;
|
|
109
|
+
/** Add legacy headers (default: false) */
|
|
110
|
+
legacyHeaders?: boolean;
|
|
111
|
+
};
|
|
112
|
+
/** Request logging configuration
|
|
113
|
+
* - enable: true in dev, false in prod
|
|
114
|
+
* - ignorePaths: skips /healthz, /favicon.ico, /metrics, /docs, /.well-known
|
|
115
|
+
* - skipNotFoundRoutes: false
|
|
116
|
+
*/
|
|
117
|
+
requestLogging?: {
|
|
118
|
+
/** Enable request logging (default: true in dev, false in prod) */
|
|
119
|
+
enable: boolean;
|
|
120
|
+
/** Ignore paths function or string[] (default: skips /healthz, /favicon.ico, /metrics, /docs, /.well-known) */
|
|
121
|
+
ignorePaths?: string[] | ((req: Request, res: Response) => boolean);
|
|
122
|
+
/** Skip logging for not found routes (default: false) */
|
|
123
|
+
skipNotFoundRoutes?: boolean;
|
|
124
|
+
};
|
|
125
|
+
/** Health check settings
|
|
126
|
+
* - path: '/healthz'
|
|
127
|
+
* - detailed: true
|
|
128
|
+
* - withGlobalPrefix: false
|
|
129
|
+
*/
|
|
130
|
+
healthCheck?: {
|
|
131
|
+
/** Health check path (default: '/healthz') */
|
|
132
|
+
path?: string;
|
|
133
|
+
/** Custom checks (default: []) */
|
|
134
|
+
checks?: Array<{
|
|
135
|
+
name: string;
|
|
136
|
+
check: () => Promise<boolean> | boolean;
|
|
137
|
+
}>;
|
|
138
|
+
/** Show detailed checks status in response (default: true) */
|
|
139
|
+
detailed?: boolean;
|
|
140
|
+
/** Include in global prefix (default: false) */
|
|
141
|
+
withGlobalPrefix?: boolean;
|
|
142
|
+
};
|
|
143
|
+
/** Request timeout in ms (default: 30000 - 30 seconds)
|
|
144
|
+
* - default: 30000
|
|
145
|
+
*/
|
|
146
|
+
requestTimeout?: number;
|
|
147
|
+
/** Response timing configuration
|
|
148
|
+
* - enable: false
|
|
149
|
+
* - addHeader: true
|
|
150
|
+
* - logOnComplete: false
|
|
151
|
+
*/
|
|
152
|
+
responseTime?: {
|
|
153
|
+
/** Enable timing (default: false) */
|
|
154
|
+
enable: boolean;
|
|
155
|
+
/** Add X-Response-Time header (default: true) */
|
|
156
|
+
addHeader?: boolean;
|
|
157
|
+
/** Log completion time (default: false) */
|
|
158
|
+
logOnComplete?: boolean;
|
|
159
|
+
};
|
|
160
|
+
/** Request ID configuration
|
|
161
|
+
* - headerName: 'x-request-id'
|
|
162
|
+
* - exposeHeader: true
|
|
163
|
+
* - generator: uuid()
|
|
164
|
+
*/
|
|
165
|
+
requestId?: {
|
|
166
|
+
/** Header name (default: 'x-request-id') */
|
|
167
|
+
headerName?: string;
|
|
168
|
+
/** Add to response headers (default: true) */
|
|
169
|
+
exposeHeader?: boolean;
|
|
170
|
+
/** ID generator (default: uuid()) */
|
|
171
|
+
generator?: () => string;
|
|
172
|
+
};
|
|
173
|
+
/** Global route prefix (default: '/')
|
|
174
|
+
* - default: '/'
|
|
175
|
+
*/
|
|
176
|
+
globalPrefix?: string;
|
|
177
|
+
/** OpenAPI documentation
|
|
178
|
+
* - enable: false
|
|
179
|
+
* - mountPath: '/docs'
|
|
180
|
+
* - verbose: false
|
|
181
|
+
* - withGlobalPrefix: false
|
|
182
|
+
*/
|
|
183
|
+
openApi?: {
|
|
184
|
+
/** Enable docs (default: false) */
|
|
185
|
+
enable: boolean;
|
|
186
|
+
/** UI path (default: '/docs') */
|
|
187
|
+
mountPath?: string;
|
|
188
|
+
/** Spec file path (required if enabled) */
|
|
189
|
+
filePath?: string;
|
|
190
|
+
/** Enable verbose logs (default: false) */
|
|
191
|
+
verbose?: boolean;
|
|
192
|
+
/** Include in global prefix (default: false) */
|
|
193
|
+
withGlobalPrefix?: boolean;
|
|
194
|
+
};
|
|
195
|
+
/** Prometheus metrics
|
|
196
|
+
* - enable: false
|
|
197
|
+
* - path: '/metrics'
|
|
198
|
+
* - withGlobalPrefix: false
|
|
199
|
+
*/
|
|
200
|
+
metrics?: {
|
|
201
|
+
/** Enable metrics (default: false) */
|
|
202
|
+
enable: boolean;
|
|
203
|
+
/** Metrics path (default: '/metrics') */
|
|
204
|
+
path?: string;
|
|
205
|
+
/** Include in global prefix (default: false) */
|
|
206
|
+
withGlobalPrefix?: boolean;
|
|
207
|
+
};
|
|
208
|
+
/** Service version header
|
|
209
|
+
* - enable: false
|
|
210
|
+
* - headerName: 'x-service-version'
|
|
211
|
+
* - version: '0.0.0'
|
|
212
|
+
*/
|
|
213
|
+
serviceVersion?: {
|
|
214
|
+
/** Enable version header (default: false) */
|
|
215
|
+
enable: boolean;
|
|
216
|
+
/** Header name (default: 'x-service-version') */
|
|
217
|
+
headerName?: string;
|
|
218
|
+
/** Version value (default: '0.0.0') */
|
|
219
|
+
version?: string | (() => string);
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* HTTPS configuration (if provided, server will use HTTPS)
|
|
223
|
+
* Requires 'key' and 'cert' at minimum.
|
|
224
|
+
* @command - to generate self-signed certificates
|
|
225
|
+
* ```bash
|
|
226
|
+
* choco install mkcert
|
|
227
|
+
* mkcert -key-file localhost-key.pem -cert-file localhost-cert.pem localhost 127.0.0.1 ::1
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
https?: {
|
|
231
|
+
/** Path to SSL private key file (PEM) */
|
|
232
|
+
key: string;
|
|
233
|
+
/** Path to SSL certificate file (PEM) */
|
|
234
|
+
cert: string;
|
|
235
|
+
/** Optional path to CA bundle file (PEM) */
|
|
236
|
+
ca?: string;
|
|
237
|
+
/** Optional passphrase for the private key */
|
|
238
|
+
passphrase?: string;
|
|
239
|
+
/** Any other https.ServerOptions */
|
|
240
|
+
[key: string]: any;
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Server lifecycle hooks for custom behavior injection.
|
|
245
|
+
* Allows extending server functionality without modifying core code.
|
|
246
|
+
* All hooks can be async and support error handling.
|
|
247
|
+
*/
|
|
248
|
+
export interface ServerHooks {
|
|
249
|
+
/** Called before any middleware or routes are initialized - good for early setup */
|
|
250
|
+
beforeInit?: (server: ExpressServer) => Promise<void> | void;
|
|
251
|
+
/** Called after middleware and routes are set up - good for final configuration */
|
|
252
|
+
afterInit?: (server: ExpressServer) => Promise<void> | void;
|
|
253
|
+
/** Called just before the server starts listening - good for last-minute checks */
|
|
254
|
+
beforeStart?: (app: Express) => Promise<void> | void;
|
|
255
|
+
/** Called after the server starts successfully - good for announcing readiness */
|
|
256
|
+
afterStart?: (server: http.Server) => Promise<void> | void;
|
|
257
|
+
/** Called before graceful shutdown begins - good for cleanup preparation */
|
|
258
|
+
beforeStop?: (server: http.Server) => Promise<void> | void;
|
|
259
|
+
/** Called after server has stopped - good for final cleanup */
|
|
260
|
+
afterStop?: () => Promise<void> | void;
|
|
261
|
+
/** Custom global error handler - overrides the default error handling */
|
|
262
|
+
onError?: (error: Error, req: Request, res: Response, next: NextFunction) => void;
|
|
263
|
+
/** Called at the start of each request - good for request preprocessing */
|
|
264
|
+
onRequest?: (req: Request, res: Response, next: NextFunction) => void;
|
|
265
|
+
/** Called before response is sent - good for response modification */
|
|
266
|
+
onResponse?: (req: Request, res: Response, next: NextFunction) => void;
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/types/server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Express, json, NextFunction, urlencoded, Request, Response } from 'express';\nimport { ExpressServer } from '../servers/server';\nimport { HelmetOptions } from 'helmet';\nimport { CompressionOptions } from 'compression';\nimport { CookieParseOptions } from 'cookie-parser';\nimport http from 'http';\nimport { CorsOptions } from 'cors';\nimport { ToggleConfig } from '.';\n\n/**\n * Server configuration interface with smart defaults and full customization.\n * All options are designed with security and performance best practices.\n * Most options can be overridden via environment variables.\n */\nexport interface ServerConfig {\n /** Port the server should listen on (default: 3000, env: PORT)\n * - default: 3000\n */\n port: number;\n /** Optional host address for binding (default: '0.0.0.0', env: HOST)\n * - default: '0.0.0.0'\n */\n host?: string;\n\n /** CORS configuration (default: false disables CORS, or provide options object)\n * - default: false\n */\n cors?: ToggleConfig<CorsOptions>;\n\n /** Enable Helmet security headers (default: false disables Helmet, or provide options object)\n * - default: false\n */\n helmet?: ToggleConfig<HelmetOptions>;\n\n /** Enable gzip/deflate compression (default: false disables compression, or provide options object)\n * - default: false\n */\n compression?: ToggleConfig<CompressionOptions>;\n\n /** Request body parsing with size limits\n * - json: { limit: '1mb' }\n * - urlencoded: { extended: true, limit: '1mb' }\n */\n bodyParser?: {\n /** JSON parser (default: { limit: '1mb' }) */\n json?: Parameters<typeof json>[0];\n /** URL-encoded parser (default: { extended: true, limit: '1mb' }) */\n urlencoded?: Parameters<typeof urlencoded>[0];\n };\n\n /** Enable cookie parsing with options (default: false)\n * - default: false\n */\n cookieParser?: ToggleConfig<CookieParseOptions>;\n\n /** Trust proxy headers (default: false)\n * - default: false\n */\n trustProxy?: boolean;\n\n /** Static file serving configuration for assets, uploads, etc.\n * - path: file system path to serve\n * - route: URL route prefix (default: \"/\")\n * - maxAge: cache max age (default: 0)\n * - etag: enable ETag headers (default: true)\n * - immutable: enable immutable caching (default: false)\n * - lastModified: enable last-modified caching (default: true)\n * - cacheControl: enable Cache-Control headers (default: true)\n */\n staticFolders?: Array<{\n /** URL route prefix (default: \"/\") */\n path?: string;\n /** File system path to serve */\n directory: string;\n /** Cache-Control: max-age=<duration> (default: 0) */\n maxAge?: string;\n /** Enable ETag headers (default: true) */\n etag?: boolean;\n /** Enable immutable caching (default: false) */\n immutable?: boolean;\n /** Enable last-modified caching (default: true) */\n lastModified?: boolean;\n /** Enable Cache-Control headers (default: true) */\n cacheControl?: boolean;\n }>;\n\n /** Enable microservice mode (default: false)\n * - default: false\n */\n isMicroservice?: boolean;\n\n /** Service name for headers/metrics (default: 'express_app', env: APP_NAME)\n * - default: 'express_app'\n */\n appName?: string;\n\n /** Global response headers (default: {})\n * - default: {}\n */\n globalHeaders?: Record<string, string | (() => string)>;\n\n /** Rate limiting settings\n * - enable: false\n * - windowMs: 15 * 60 * 1000\n * - max: 100\n * - message: 'Too many requests'\n * - standardHeaders: true\n * - legacyHeaders: false\n */\n rateLimit?: {\n /** Enable rate limiting (default: false) */\n enable: boolean;\n /** Time window in ms (default: 900000 - 15 minutes) */\n windowMs?: number;\n /** Max requests per window (default: 100) */\n max?: number;\n /** Rate limit message (default: 'Too many requests') */\n message?: string;\n /** Add standard headers (default: true) */\n standardHeaders?: boolean;\n /** Add legacy headers (default: false) */\n legacyHeaders?: boolean;\n };\n\n /** Request logging configuration\n * - enable: true in dev, false in prod\n * - ignorePaths: skips /healthz, /favicon.ico, /metrics, /docs, /.well-known\n * - skipNotFoundRoutes: false\n */\n requestLogging?: {\n /** Enable request logging (default: true in dev, false in prod) */\n enable: boolean;\n /** Ignore paths function or string[] (default: skips /healthz, /favicon.ico, /metrics, /docs, /.well-known) */\n ignorePaths?: string[] | ((req: Request, res: Response) => boolean);\n /** Skip logging for not found routes (default: false) */\n skipNotFoundRoutes?: boolean;\n };\n\n /** Health check settings\n * - path: '/healthz'\n * - detailed: true\n * - withGlobalPrefix: false\n */\n healthCheck?: {\n /** Health check path (default: '/healthz') */\n path?: string;\n /** Custom checks (default: []) */\n checks?: Array<{\n name: string;\n check: () => Promise<boolean> | boolean;\n }>;\n /** Show detailed checks status in response (default: true) */\n detailed?: boolean;\n /** Include in global prefix (default: false) */\n withGlobalPrefix?: boolean;\n };\n\n /** Request timeout in ms (default: 30000 - 30 seconds)\n * - default: 30000\n */\n requestTimeout?: number;\n\n /** Response timing configuration\n * - enable: false\n * - addHeader: true\n * - logOnComplete: false\n */\n responseTime?: {\n /** Enable timing (default: false) */\n enable: boolean;\n /** Add X-Response-Time header (default: true) */\n addHeader?: boolean;\n /** Log completion time (default: false) */\n logOnComplete?: boolean;\n };\n\n /** Request ID configuration\n * - headerName: 'x-request-id'\n * - exposeHeader: true\n * - generator: uuid()\n */\n requestId?: {\n /** Header name (default: 'x-request-id') */\n headerName?: string;\n /** Add to response headers (default: true) */\n exposeHeader?: boolean;\n /** ID generator (default: uuid()) */\n generator?: () => string;\n };\n\n /** Global route prefix (default: '/')\n * - default: '/'\n */\n globalPrefix?: string;\n\n /** OpenAPI documentation\n * - enable: false\n * - mountPath: '/docs'\n * - verbose: false\n * - withGlobalPrefix: false\n */\n openApi?: {\n /** Enable docs (default: false) */\n enable: boolean;\n /** UI path (default: '/docs') */\n mountPath?: string;\n /** Spec file path (required if enabled) */\n filePath?: string;\n /** Enable verbose logs (default: false) */\n verbose?: boolean;\n /** Include in global prefix (default: false) */\n withGlobalPrefix?: boolean;\n };\n\n /** Prometheus metrics\n * - enable: false\n * - path: '/metrics'\n * - withGlobalPrefix: false\n */\n metrics?: {\n /** Enable metrics (default: false) */\n enable: boolean;\n /** Metrics path (default: '/metrics') */\n path?: string;\n /** Include in global prefix (default: false) */\n withGlobalPrefix?: boolean;\n };\n\n /** Service version header\n * - enable: false\n * - headerName: 'x-service-version'\n * - version: '0.0.0'\n */\n serviceVersion?: {\n /** Enable version header (default: false) */\n enable: boolean;\n /** Header name (default: 'x-service-version') */\n headerName?: string;\n /** Version value (default: '0.0.0') */\n version?: string | (() => string);\n };\n\n /**\n * HTTPS configuration (if provided, server will use HTTPS)\n * Requires 'key' and 'cert' at minimum.\n * @command - to generate self-signed certificates\n * ```bash\n * choco install mkcert\n * mkcert -key-file localhost-key.pem -cert-file localhost-cert.pem localhost 127.0.0.1 ::1\n * ```\n */\n https?: {\n /** Path to SSL private key file (PEM) */\n key: string;\n /** Path to SSL certificate file (PEM) */\n cert: string;\n /** Optional path to CA bundle file (PEM) */\n ca?: string;\n /** Optional passphrase for the private key */\n passphrase?: string;\n /** Any other https.ServerOptions */\n [key: string]: any;\n };\n}\n\n/**\n * Server lifecycle hooks for custom behavior injection.\n * Allows extending server functionality without modifying core code.\n * All hooks can be async and support error handling.\n */\nexport interface ServerHooks {\n /** Called before any middleware or routes are initialized - good for early setup */\n beforeInit?: (server: ExpressServer) => Promise<void> | void;\n /** Called after middleware and routes are set up - good for final configuration */\n afterInit?: (server: ExpressServer) => Promise<void> | void;\n /** Called just before the server starts listening - good for last-minute checks */\n beforeStart?: (app: Express) => Promise<void> | void;\n /** Called after the server starts successfully - good for announcing readiness */\n afterStart?: (server: http.Server) => Promise<void> | void;\n /** Called before graceful shutdown begins - good for cleanup preparation */\n beforeStop?: (server: http.Server) => Promise<void> | void;\n /** Called after server has stopped - good for final cleanup */\n afterStop?: () => Promise<void> | void;\n /** Custom global error handler - overrides the default error handling */\n onError?: (error: Error, req: Request, res: Response, next: NextFunction) => void;\n /** Called at the start of each request - good for request preprocessing */\n onRequest?: (req: Request, res: Response, next: NextFunction) => void;\n /** Called before response is sent - good for response modification */\n onResponse?: (req: Request, res: Response, next: NextFunction) => void;\n}\n"]}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits an array into chunks of the specified size.
|
|
3
|
+
*
|
|
4
|
+
* @template T The type of array elements.
|
|
5
|
+
* @param {T[]} array - The array to split into chunks.
|
|
6
|
+
* @param {number} size - The number of elements per chunk.
|
|
7
|
+
* @returns {T[][]} A new array containing chunked arrays.
|
|
8
|
+
* @throws {TypeError} If array is not an array.
|
|
9
|
+
* @throws {Error} If chunk size is not a positive integer.
|
|
10
|
+
*/
|
|
11
|
+
export declare function chunk<T>(array: T[], size: number): T[][];
|
|
12
|
+
/**
|
|
13
|
+
* Removes duplicate values from an array.
|
|
14
|
+
* Optionally enforces uniqueness by a key function.
|
|
15
|
+
*
|
|
16
|
+
* @template T The type of array elements.
|
|
17
|
+
* @param {T[]} array - The input array.
|
|
18
|
+
* @param {(item: T) => unknown} [keyFn] - Optional function to determine uniqueness by key.
|
|
19
|
+
* @returns {T[]} A new array with unique values.
|
|
20
|
+
*/
|
|
21
|
+
export declare function unique<T>(array: T[], keyFn?: (item: T) => unknown): T[];
|
|
22
|
+
/**
|
|
23
|
+
* Deeply flattens a nested array to a single-level array.
|
|
24
|
+
*
|
|
25
|
+
* @template T The leaf type of array elements.
|
|
26
|
+
* @param {any[]} array - The (possibly deeply nested) input array.
|
|
27
|
+
* @returns {T[]} A deeply flattened array.
|
|
28
|
+
*/
|
|
29
|
+
export declare function flattenDeep<T>(array: any[]): T[];
|
|
30
|
+
/**
|
|
31
|
+
* Returns a random element from an array, or undefined if empty.
|
|
32
|
+
*
|
|
33
|
+
* @template T The type of array elements.
|
|
34
|
+
* @param {T[]} array - The input array.
|
|
35
|
+
* @returns {T | undefined} A randomly selected item, or undefined if array is empty or not an array.
|
|
36
|
+
*/
|
|
37
|
+
export declare function random<T>(array: T[]): T | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Groups items in an array by a key or key function.
|
|
40
|
+
*
|
|
41
|
+
* @template T The type of array elements.
|
|
42
|
+
* @overload
|
|
43
|
+
* @param {T[]} array - The array to group.
|
|
44
|
+
* @param {keyof T} key - Property key to group by.
|
|
45
|
+
* @returns {Record<string, T[]>}
|
|
46
|
+
* @overload
|
|
47
|
+
* @param {T[]} array - The array to group.
|
|
48
|
+
* @param {(item: T) => string | number | symbol} keyFn - Function to generate group key from item.
|
|
49
|
+
* @returns {Record<K, T[]>}
|
|
50
|
+
* @param {T[]} array - The array to group.
|
|
51
|
+
* @param {keyof T | ((item: T) => string | number | symbol)} keyOrFn - Property key or key-generating function.
|
|
52
|
+
* @returns {Record<string | number | symbol, T[]>} An object mapping group keys to item arrays.
|
|
53
|
+
*/
|
|
54
|
+
export declare function groupBy<T>(array: T[], key: keyof T): Record<string, T[]>;
|
|
55
|
+
export declare function groupBy<T, K extends string | number | symbol>(array: T[], keyFn: (item: T) => K): Record<K, T[]>;
|
|
56
|
+
/**
|
|
57
|
+
* Shuffles an array using the Fisher-Yates algorithm.
|
|
58
|
+
*
|
|
59
|
+
* @template T The type of array elements.
|
|
60
|
+
* @param {T[]} array - The input array.
|
|
61
|
+
* @returns {T[]} A new shuffled array.
|
|
62
|
+
* @throws {TypeError} If array is not an array.
|
|
63
|
+
*/
|
|
64
|
+
export declare function shuffle<T>(array: T[]): T[];
|
|
65
|
+
/**
|
|
66
|
+
* Returns an array of property values from an array of objects.
|
|
67
|
+
*
|
|
68
|
+
* @template T The type of array elements.
|
|
69
|
+
* @template K The object property to pluck.
|
|
70
|
+
* @param {T[]} array - The input array.
|
|
71
|
+
* @param {K} key - The property name to pluck.
|
|
72
|
+
* @returns {T[K][]} Array of property values.
|
|
73
|
+
*/
|
|
74
|
+
export declare function pluck<T, K extends keyof T>(array: T[], key: K): T[K][];
|
|
75
|
+
/**
|
|
76
|
+
* Returns values in array A that are not in array B.
|
|
77
|
+
*
|
|
78
|
+
* @template T The type of array elements.
|
|
79
|
+
* @param {T[]} a - First array.
|
|
80
|
+
* @param {T[]} b - Second array.
|
|
81
|
+
* @returns {T[]} Elements in A that are not in B.
|
|
82
|
+
*/
|
|
83
|
+
export declare function difference<T>(a: T[], b: T[]): T[];
|
|
84
|
+
/**
|
|
85
|
+
* Returns common values between arrays A and B.
|
|
86
|
+
*
|
|
87
|
+
* @template T The type of array elements.
|
|
88
|
+
* @param {T[]} a - First array.
|
|
89
|
+
* @param {T[]} b - Second array.
|
|
90
|
+
* @returns {T[]} Elements that exist in both arrays.
|
|
91
|
+
*/
|
|
92
|
+
export declare function intersect<T>(a: T[], b: T[]): T[];
|
|
93
|
+
/**
|
|
94
|
+
* Sorts an array of objects by a nested key using Merge Sort (O(n log n)).
|
|
95
|
+
* Missing/undefined keys are sorted to the "end" (asc) or "start" (desc).
|
|
96
|
+
*
|
|
97
|
+
* @template T The type of array elements (objects).
|
|
98
|
+
* @param {T[]} array - Array of objects to sort.
|
|
99
|
+
* @param {string | ((item: T) => any)} key - Dot-notated key (e.g., "profile.age") or function.
|
|
100
|
+
* @param {"asc" | "desc"} [direction="asc"] - Sort direction: 'asc' or 'desc'.
|
|
101
|
+
* @returns {T[]} A new sorted array.
|
|
102
|
+
* @throws {TypeError} If array is not an array.
|
|
103
|
+
*/
|
|
104
|
+
export declare function mergeSort<T>(array: T[], key: string | ((item: T) => any), direction?: 'asc' | 'desc'): T[];
|
|
105
|
+
/**
|
|
106
|
+
* Combines multiple arrays into a single array of grouped elements.
|
|
107
|
+
* Output array length equals the length of the shortest input array.
|
|
108
|
+
*
|
|
109
|
+
* @example zip([1, 2], ['a', 'b']) => [[1, 'a'], [2, 'b']]
|
|
110
|
+
* @param {...Array<T>[]} arrays - Two or more arrays to zip together.
|
|
111
|
+
* @returns {Array<T[]>} Array of grouped elements.
|
|
112
|
+
*/
|
|
113
|
+
export declare function zip<T>(...arrays: T[][]): T[][];
|
|
114
|
+
/**
|
|
115
|
+
* Splits an array into two arrays based on a predicate function.
|
|
116
|
+
*
|
|
117
|
+
* @template T The type of array elements.
|
|
118
|
+
* @param {T[]} array - The input array.
|
|
119
|
+
* @param {(item: T, index: number, array: T[]) => boolean} predicate - Function to test each element.
|
|
120
|
+
* @returns {[T[], T[]]} A tuple of two arrays: [matched, unmatched].
|
|
121
|
+
*/
|
|
122
|
+
export declare function partition<T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): [T[], T[]];
|
|
123
|
+
/**
|
|
124
|
+
* Generates an array of numbers within a specified range.
|
|
125
|
+
*
|
|
126
|
+
* @param {number} start - Start of range (inclusive).
|
|
127
|
+
* @param {number} end - End of range (exclusive).
|
|
128
|
+
* @param {number} [step=1] - Step between numbers.
|
|
129
|
+
* @returns {number[]} Array of numbers in range.
|
|
130
|
+
*/
|
|
131
|
+
export declare function range(start: number, end: number, step?: number): number[];
|
|
132
|
+
/**
|
|
133
|
+
* Returns the first n elements of an array.
|
|
134
|
+
*
|
|
135
|
+
* @template T The type of array elements.
|
|
136
|
+
* @param {T[]} array - The input array.
|
|
137
|
+
* @param {number} [n=1] - Number of elements to take.
|
|
138
|
+
* @returns {T[]} New array with first n elements.
|
|
139
|
+
*/
|
|
140
|
+
export declare function take<T>(array: T[], n?: number): T[];
|
|
141
|
+
/**
|
|
142
|
+
* Takes elements from the array while predicate returns true.
|
|
143
|
+
*
|
|
144
|
+
* @template T The type of array elements.
|
|
145
|
+
* @param {T[]} array - The input array.
|
|
146
|
+
* @param {(item: T, index: number) => boolean} predicate - Function to test each element.
|
|
147
|
+
* @returns {T[]} New array with taken elements.
|
|
148
|
+
*/
|
|
149
|
+
export declare function takeWhile<T>(array: T[], predicate: (item: T, index: number) => boolean): T[];
|
|
150
|
+
/**
|
|
151
|
+
* Removes all falsy values from an array.
|
|
152
|
+
* False, null, 0, "", undefined, and NaN are falsy.
|
|
153
|
+
*
|
|
154
|
+
* @template T The type of array elements.
|
|
155
|
+
* @param {T[]} array - The input array.
|
|
156
|
+
* @returns {NonNullable<T>[]} New array with falsy values removed.
|
|
157
|
+
*/
|
|
158
|
+
export declare function compact<T>(array: T[]): NonNullable<T>[];
|
|
159
|
+
/**
|
|
160
|
+
* Counts array elements by a key function.
|
|
161
|
+
*
|
|
162
|
+
* @template T The type of array elements.
|
|
163
|
+
* @param {T[]} array - The input array.
|
|
164
|
+
* @param {(item: T) => string | number | symbol} keyFn - Function to generate count key.
|
|
165
|
+
* @returns {Record<string, number>} Object with counts by key.
|
|
166
|
+
*/
|
|
167
|
+
export declare function countBy<T>(array: T[], keyFn: (item: T) => string | number | symbol): Record<string, number>;
|
|
168
|
+
//# sourceMappingURL=array.utils.d.ts.map
|