@carno.js/core 0.2.7 → 0.2.9
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/Carno.d.ts +9 -7
- package/dist/Carno.js +14 -1
- package/dist/commons/decorators/index.d.ts +1 -0
- package/dist/commons/decorators/index.js +1 -0
- package/dist/commons/decorators/validation.decorator.d.ts +32 -0
- package/dist/commons/decorators/validation.decorator.js +40 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +2 -1
- package/dist/container/InjectorService.d.ts +3 -1
- package/dist/container/InjectorService.js +4 -3
- package/dist/container/MethodInvoker.d.ts +3 -2
- package/dist/container/MethodInvoker.js +4 -17
- package/dist/exceptions/HttpException.js +5 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/route/ParamResolverFactory.d.ts +0 -5
- package/dist/route/ParamResolverFactory.js +0 -40
- package/dist/route/RouteCompiler.d.ts +3 -3
- package/dist/route/RouteCompiler.js +2 -11
- package/dist/route/memoirist.js +4 -0
- package/dist/utils/ValidationCache.d.ts +2 -0
- package/dist/utils/ValidationCache.js +10 -2
- package/dist/utils/formatValidationErrors.d.ts +5 -0
- package/dist/utils/formatValidationErrors.js +80 -0
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/validation/ValidatorAdapter.d.ts +66 -0
- package/dist/validation/ValidatorAdapter.js +20 -0
- package/dist/validation/adapters/ClassValidatorAdapter.d.ts +23 -0
- package/dist/validation/adapters/ClassValidatorAdapter.js +47 -0
- package/dist/validation/adapters/ZodAdapter.d.ts +14 -0
- package/dist/validation/adapters/ZodAdapter.js +56 -0
- package/dist/validation/adapters/index.d.ts +4 -0
- package/dist/validation/adapters/index.js +7 -0
- package/dist/validation/index.d.ts +3 -0
- package/dist/validation/index.js +20 -0
- package/package.json +17 -6
- package/dist/Cheetah.d.ts +0 -65
- package/dist/Cheetah.js +0 -307
- package/dist/default-routes-cheetah.d.ts +0 -3
- package/dist/default-routes-cheetah.js +0 -29
- package/dist/domain/CheetahClosure.d.ts +0 -1
- package/dist/domain/CheetahClosure.js +0 -2
- package/dist/domain/CheetahMiddleware.d.ts +0 -5
- package/dist/domain/CheetahMiddleware.js +0 -2
- package/dist/services/request-logger.service.d.ts +0 -15
- package/dist/services/request-logger.service.js +0 -50
- package/dist/utils/isClassValidator.d.ts +0 -6
- package/dist/utils/isClassValidator.js +0 -13
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base interface for validation adapters
|
|
3
|
+
* Adapters provide validation capabilities for different libraries (Zod, class-validator, etc.)
|
|
4
|
+
*/
|
|
5
|
+
export interface ValidatorAdapter<TOptions = any> {
|
|
6
|
+
/**
|
|
7
|
+
* Checks if a class/function has validation metadata
|
|
8
|
+
* Used by ValidationCache for detection and optimization
|
|
9
|
+
*
|
|
10
|
+
* @param target - The class/function to check
|
|
11
|
+
* @returns true if validation metadata exists
|
|
12
|
+
*/
|
|
13
|
+
hasValidation(target: Function): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Validates and transforms a value to the target type
|
|
16
|
+
*
|
|
17
|
+
* @param target - The DTO class to validate against
|
|
18
|
+
* @param value - The raw value to validate (usually from request body/query/params)
|
|
19
|
+
* @returns Transformed and validated instance
|
|
20
|
+
* @throws HttpException with formatted errors on validation failure
|
|
21
|
+
*/
|
|
22
|
+
validateAndTransform(target: Function, value: any): any;
|
|
23
|
+
/**
|
|
24
|
+
* Get the name of the validator (for debugging/logging)
|
|
25
|
+
*
|
|
26
|
+
* @returns The validator name (e.g., "ZodAdapter", "ClassValidatorAdapter")
|
|
27
|
+
*/
|
|
28
|
+
getName(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Get the adapter options (for internal use)
|
|
31
|
+
*/
|
|
32
|
+
getOptions(): TOptions;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Constructor type for validator adapters
|
|
36
|
+
*/
|
|
37
|
+
export type ValidatorAdapterConstructor<TOptions = any> = new (options?: TOptions) => ValidatorAdapter<TOptions>;
|
|
38
|
+
/**
|
|
39
|
+
* Type to extract options type from a ValidatorAdapter constructor
|
|
40
|
+
*/
|
|
41
|
+
export type ValidatorAdapterOptions<TAdapter> = TAdapter extends new (options?: infer TOptions) => any ? TOptions : never;
|
|
42
|
+
/**
|
|
43
|
+
* Validation configuration with adapter and options
|
|
44
|
+
*/
|
|
45
|
+
export interface ValidationConfig<TAdapter extends ValidatorAdapterConstructor = ValidatorAdapterConstructor> {
|
|
46
|
+
adapter?: TAdapter;
|
|
47
|
+
options?: ValidatorAdapterOptions<TAdapter>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Helper function to create a strongly-typed validation config.
|
|
51
|
+
* Use this to get proper autocomplete for adapter options.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const app = new Carno({
|
|
56
|
+
* validation: defineValidation({
|
|
57
|
+
* adapter: ClassValidatorAdapter,
|
|
58
|
+
* options: { whitelist: true } // ✓ Autocomplete works!
|
|
59
|
+
* })
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function defineValidation<TAdapter extends ValidatorAdapterConstructor>(config: {
|
|
64
|
+
adapter: TAdapter;
|
|
65
|
+
options?: ValidatorAdapterOptions<TAdapter>;
|
|
66
|
+
}): ValidationConfig<TAdapter>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineValidation = defineValidation;
|
|
4
|
+
/**
|
|
5
|
+
* Helper function to create a strongly-typed validation config.
|
|
6
|
+
* Use this to get proper autocomplete for adapter options.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const app = new Carno({
|
|
11
|
+
* validation: defineValidation({
|
|
12
|
+
* adapter: ClassValidatorAdapter,
|
|
13
|
+
* options: { whitelist: true } // ✓ Autocomplete works!
|
|
14
|
+
* })
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function defineValidation(config) {
|
|
19
|
+
return config;
|
|
20
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ValidatorAdapter } from "../ValidatorAdapter";
|
|
2
|
+
export interface ClassValidatorAdapterOptions {
|
|
3
|
+
skipMissingProperties?: boolean;
|
|
4
|
+
whitelist?: boolean;
|
|
5
|
+
forbidNonWhitelisted?: boolean;
|
|
6
|
+
forbidUnknownValues?: boolean;
|
|
7
|
+
disableErrorMessages?: boolean;
|
|
8
|
+
errorHttpStatusCode?: number;
|
|
9
|
+
dismissDefaultMessages?: boolean;
|
|
10
|
+
validationError?: {
|
|
11
|
+
target?: boolean;
|
|
12
|
+
value?: boolean;
|
|
13
|
+
};
|
|
14
|
+
stopAtFirstError?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare class ClassValidatorAdapter implements ValidatorAdapter<ClassValidatorAdapterOptions> {
|
|
17
|
+
private options;
|
|
18
|
+
constructor(options?: ClassValidatorAdapterOptions);
|
|
19
|
+
getName(): string;
|
|
20
|
+
getOptions(): ClassValidatorAdapterOptions;
|
|
21
|
+
hasValidation(target: Function): boolean;
|
|
22
|
+
validateAndTransform(target: Function, value: any): any;
|
|
23
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ClassValidatorAdapter = void 0;
|
|
4
|
+
const HttpException_1 = require("../../exceptions/HttpException");
|
|
5
|
+
const formatValidationErrors_1 = require("../../utils/formatValidationErrors");
|
|
6
|
+
let classValidator;
|
|
7
|
+
let classTransformer;
|
|
8
|
+
try {
|
|
9
|
+
classValidator = require("class-validator");
|
|
10
|
+
classTransformer = require("class-transformer");
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
// Will be checked in constructor
|
|
14
|
+
}
|
|
15
|
+
class ClassValidatorAdapter {
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.options = options;
|
|
18
|
+
if (!classValidator || !classTransformer) {
|
|
19
|
+
throw new Error("ClassValidatorAdapter requires class-validator and class-transformer. " +
|
|
20
|
+
"Install with: npm install class-validator class-transformer");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
getName() {
|
|
24
|
+
return "ClassValidatorAdapter";
|
|
25
|
+
}
|
|
26
|
+
getOptions() {
|
|
27
|
+
return this.options;
|
|
28
|
+
}
|
|
29
|
+
hasValidation(target) {
|
|
30
|
+
const { getMetadataStorage } = classValidator;
|
|
31
|
+
const metadataStorage = getMetadataStorage();
|
|
32
|
+
const validationMetadata = metadataStorage.getTargetValidationMetadatas(target, "", false, false, []);
|
|
33
|
+
return validationMetadata.length > 0;
|
|
34
|
+
}
|
|
35
|
+
validateAndTransform(target, value) {
|
|
36
|
+
const { validateSync } = classValidator;
|
|
37
|
+
const { plainToInstance } = classTransformer;
|
|
38
|
+
const instance = plainToInstance(target, value);
|
|
39
|
+
const errors = validateSync(instance, this.options);
|
|
40
|
+
if (errors.length > 0) {
|
|
41
|
+
const formatted = (0, formatValidationErrors_1.formatValidationErrors)(errors);
|
|
42
|
+
throw new HttpException_1.HttpException(formatted, 400);
|
|
43
|
+
}
|
|
44
|
+
return instance;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ClassValidatorAdapter = ClassValidatorAdapter;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ValidatorAdapter } from "../ValidatorAdapter";
|
|
2
|
+
export interface ZodAdapterOptions {
|
|
3
|
+
}
|
|
4
|
+
export declare class ZodAdapter implements ValidatorAdapter<ZodAdapterOptions> {
|
|
5
|
+
private options;
|
|
6
|
+
constructor(options?: ZodAdapterOptions);
|
|
7
|
+
getName(): string;
|
|
8
|
+
getOptions(): ZodAdapterOptions;
|
|
9
|
+
hasValidation(target: Function): boolean;
|
|
10
|
+
validateAndTransform(target: Function, value: any): any;
|
|
11
|
+
private getSchema;
|
|
12
|
+
private formatZodErrors;
|
|
13
|
+
private buildFieldPath;
|
|
14
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ZodAdapter = void 0;
|
|
4
|
+
const Metadata_1 = require("../../domain/Metadata");
|
|
5
|
+
const HttpException_1 = require("../../exceptions/HttpException");
|
|
6
|
+
const constants_1 = require("../../constants");
|
|
7
|
+
class ZodAdapter {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
getName() {
|
|
12
|
+
return "ZodAdapter";
|
|
13
|
+
}
|
|
14
|
+
getOptions() {
|
|
15
|
+
return this.options;
|
|
16
|
+
}
|
|
17
|
+
hasValidation(target) {
|
|
18
|
+
return Metadata_1.Metadata.has(constants_1.VALIDATION_ZOD_SCHEMA, target);
|
|
19
|
+
}
|
|
20
|
+
validateAndTransform(target, value) {
|
|
21
|
+
const schema = this.getSchema(target);
|
|
22
|
+
if (!schema) {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
const result = schema.safeParse(value);
|
|
26
|
+
if (!result.success) {
|
|
27
|
+
const formattedErrors = this.formatZodErrors(result.error);
|
|
28
|
+
throw new HttpException_1.HttpException(formattedErrors, 400);
|
|
29
|
+
}
|
|
30
|
+
return result.data;
|
|
31
|
+
}
|
|
32
|
+
getSchema(target) {
|
|
33
|
+
return Metadata_1.Metadata.get(constants_1.VALIDATION_ZOD_SCHEMA, target);
|
|
34
|
+
}
|
|
35
|
+
formatZodErrors(error) {
|
|
36
|
+
const issuesMap = new Map();
|
|
37
|
+
for (const issue of error.issues) {
|
|
38
|
+
const field = this.buildFieldPath(issue.path);
|
|
39
|
+
if (!issuesMap.has(field)) {
|
|
40
|
+
issuesMap.set(field, []);
|
|
41
|
+
}
|
|
42
|
+
issuesMap.get(field).push(issue.message);
|
|
43
|
+
}
|
|
44
|
+
return Array.from(issuesMap.entries()).map(([field, messages]) => ({
|
|
45
|
+
field,
|
|
46
|
+
messages,
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
buildFieldPath(path) {
|
|
50
|
+
if (path.length === 0) {
|
|
51
|
+
return "body";
|
|
52
|
+
}
|
|
53
|
+
return path.map(p => String(p)).join(".");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.ZodAdapter = ZodAdapter;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ClassValidatorAdapter = exports.ZodAdapter = void 0;
|
|
4
|
+
var ZodAdapter_1 = require("./ZodAdapter");
|
|
5
|
+
Object.defineProperty(exports, "ZodAdapter", { enumerable: true, get: function () { return ZodAdapter_1.ZodAdapter; } });
|
|
6
|
+
var ClassValidatorAdapter_1 = require("./ClassValidatorAdapter");
|
|
7
|
+
Object.defineProperty(exports, "ClassValidatorAdapter", { enumerable: true, get: function () { return ClassValidatorAdapter_1.ClassValidatorAdapter; } });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.defineValidation = void 0;
|
|
18
|
+
var ValidatorAdapter_1 = require("./ValidatorAdapter");
|
|
19
|
+
Object.defineProperty(exports, "defineValidation", { enumerable: true, get: function () { return ValidatorAdapter_1.defineValidation; } });
|
|
20
|
+
__exportStar(require("./adapters"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carno.js/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "Carno.js is a framework for building web applications object oriented with TypeScript and Bun.sh",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bun",
|
|
@@ -33,23 +33,34 @@
|
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"bentocache": "^1.0.0-beta.5",
|
|
36
|
-
"class-transformer": "^0.5.1",
|
|
37
|
-
"class-validator": "^0.14.0",
|
|
38
36
|
"globby": "^10.0.2",
|
|
39
37
|
"parseurl-fast": "^1.2.0",
|
|
40
38
|
"pino": "^8.16.1",
|
|
41
39
|
"pino-pretty": "^10.2.3",
|
|
42
|
-
"reflect-metadata": "^0.1.13"
|
|
40
|
+
"reflect-metadata": "^0.1.13",
|
|
41
|
+
"zod": "^4.3.4"
|
|
43
42
|
},
|
|
44
43
|
"devDependencies": {
|
|
45
44
|
"@types/globby": "^9.1.0",
|
|
46
|
-
"bun-types": "latest"
|
|
45
|
+
"bun-types": "latest",
|
|
46
|
+
"class-transformer": "^0.5.1",
|
|
47
|
+
"class-validator": "^0.14.3"
|
|
47
48
|
},
|
|
48
49
|
"peerDependencies": {
|
|
50
|
+
"class-transformer": "^0.5.1",
|
|
51
|
+
"class-validator": "^0.14.0",
|
|
49
52
|
"typescript": "^5.0.0"
|
|
50
53
|
},
|
|
54
|
+
"peerDependenciesMeta": {
|
|
55
|
+
"class-validator": {
|
|
56
|
+
"optional": true
|
|
57
|
+
},
|
|
58
|
+
"class-transformer": {
|
|
59
|
+
"optional": true
|
|
60
|
+
}
|
|
61
|
+
},
|
|
51
62
|
"publishConfig": {
|
|
52
63
|
"access": "public"
|
|
53
64
|
},
|
|
54
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "cc116bcd2f96e3cf6e87bbb38d5e4dfe1245150a"
|
|
55
66
|
}
|
package/dist/Cheetah.d.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { Server } from "bun";
|
|
2
|
-
import { ValidatorOptions } from "class-validator";
|
|
3
|
-
import * as pino from "pino";
|
|
4
|
-
import { TokenRouteWithProvider } from "./container/ContainerConfiguration";
|
|
5
|
-
import { CorsConfig } from "./domain/cors-config";
|
|
6
|
-
import Memoirist from "./route/memoirist";
|
|
7
|
-
export interface ApplicationConfig {
|
|
8
|
-
validation?: ValidatorOptions;
|
|
9
|
-
logger?: pino.LoggerOptions;
|
|
10
|
-
exports?: any[];
|
|
11
|
-
providers?: any[];
|
|
12
|
-
cors?: CorsConfig;
|
|
13
|
-
globalMiddlewares?: any[];
|
|
14
|
-
}
|
|
15
|
-
export declare class Cheetah {
|
|
16
|
-
config: ApplicationConfig;
|
|
17
|
-
router: Memoirist<TokenRouteWithProvider>;
|
|
18
|
-
private injector;
|
|
19
|
-
private fetch;
|
|
20
|
-
private server;
|
|
21
|
-
constructor(config?: ApplicationConfig);
|
|
22
|
-
/**
|
|
23
|
-
* Use the Cheetah plugin.
|
|
24
|
-
*
|
|
25
|
-
* @param plugin
|
|
26
|
-
*/
|
|
27
|
-
use(plugin: Cheetah): this;
|
|
28
|
-
private findProviderInConfig;
|
|
29
|
-
private getProviderToken;
|
|
30
|
-
private shouldCloneProvider;
|
|
31
|
-
private cloneProvider;
|
|
32
|
-
/**
|
|
33
|
-
* Set the custom logger provider.
|
|
34
|
-
* The provider must be a class with the @Service() decorator.
|
|
35
|
-
* The provider must extend the LoggerService class.
|
|
36
|
-
*
|
|
37
|
-
* @param provider
|
|
38
|
-
*/
|
|
39
|
-
useLogger(provider: any): this;
|
|
40
|
-
private loadProvidersAndControllers;
|
|
41
|
-
private registerControllers;
|
|
42
|
-
private registerMetadataProviders;
|
|
43
|
-
private registerConfigProviders;
|
|
44
|
-
private normalizeProvider;
|
|
45
|
-
init(): Promise<void>;
|
|
46
|
-
listen(port?: number): Promise<void>;
|
|
47
|
-
private registerShutdownHandlers;
|
|
48
|
-
getHttpServer(): Server<any>;
|
|
49
|
-
getInjector(): import(".").InjectorService;
|
|
50
|
-
private createHttpServer;
|
|
51
|
-
private fetcher;
|
|
52
|
-
private catcher;
|
|
53
|
-
private bootstrapApplication;
|
|
54
|
-
private handleShutdownHook;
|
|
55
|
-
private closeHttpServer;
|
|
56
|
-
private exitProcess;
|
|
57
|
-
private reportHookFailure;
|
|
58
|
-
private isCorsEnabled;
|
|
59
|
-
private isOriginAllowed;
|
|
60
|
-
private buildCorsHeaders;
|
|
61
|
-
private handlePreflightRequest;
|
|
62
|
-
private applyCorsHeaders;
|
|
63
|
-
close(closeActiveConnections?: boolean): void;
|
|
64
|
-
private discoverRoutePath;
|
|
65
|
-
}
|