@tsed/platform-params
Advanced tools
Comparing version 8.0.0-rc.5 to 8.0.0-rc.6
@@ -1,3 +0,2 @@ | ||
import { __decorate, __metadata } from "tslib"; | ||
import { Inject, Injectable, InjectorService, ProviderScope } from "@tsed/di"; | ||
import { injectable, injector, ProviderScope } from "@tsed/di"; | ||
import { JsonMethodStore, JsonParameterStore } from "@tsed/schema"; | ||
@@ -10,9 +9,9 @@ import { ParamValidationError } from "../errors/ParamValidationError.js"; | ||
*/ | ||
let PlatformParams = class PlatformParams { | ||
export class PlatformParams { | ||
getPipes(param) { | ||
const get = (pipe) => { | ||
return this.injector.getProvider(pipe).priority || 0; | ||
return injector().getProvider(pipe).priority || 0; | ||
}; | ||
const sort = (p1, p2) => (get(p1) < get(p2) ? -1 : get(p1) > get(p2) ? 1 : 0); | ||
const map = (token) => this.injector.get(token); | ||
const map = (token) => injector().get(token); | ||
return [ParseExpressionPipe, ...param.pipes.sort(sort)].map(map).filter(Boolean); | ||
@@ -28,8 +27,9 @@ } | ||
} | ||
const inj = injector(); | ||
const store = JsonMethodStore.fromMethod(token, propertyKey); | ||
const getArguments = this.compile(store); | ||
const provider = this.injector.getProvider(token); | ||
const provider = inj.getProvider(token); | ||
return async (scope) => { | ||
const container = provider.scope === ProviderScope.REQUEST ? scope.$ctx.container : undefined; | ||
const [instance, args] = await Promise.all([this.injector.invoke(token, container), getArguments(scope)]); | ||
const [instance, args] = await Promise.all([inj.invoke(token, { locals: container }), getArguments(scope)]); | ||
return instance[propertyKey].call(instance, ...args, scope.$ctx); | ||
@@ -62,14 +62,4 @@ }; | ||
} | ||
}; | ||
__decorate([ | ||
Inject(), | ||
__metadata("design:type", InjectorService) | ||
], PlatformParams.prototype, "injector", void 0); | ||
PlatformParams = __decorate([ | ||
Injectable({ | ||
scope: ProviderScope.SINGLETON, | ||
imports: [ParseExpressionPipe] | ||
}) | ||
], PlatformParams); | ||
export { PlatformParams }; | ||
} | ||
injectable(PlatformParams).imports([ParseExpressionPipe]); | ||
//# sourceMappingURL=PlatformParams.js.map |
@@ -1,6 +0,4 @@ | ||
var DeserializerPipe_1; | ||
import { __decorate } from "tslib"; | ||
import { Injectable } from "@tsed/di"; | ||
import { injectable } from "@tsed/di"; | ||
import { deserialize } from "@tsed/json-mapper"; | ||
let DeserializerPipe = DeserializerPipe_1 = class DeserializerPipe { | ||
export class DeserializerPipe { | ||
transform(value, param) { | ||
@@ -10,10 +8,7 @@ return deserialize(value, { | ||
store: param, | ||
...(param.store.get(DeserializerPipe_1) || {}) | ||
...(param.store.get(DeserializerPipe) || {}) | ||
}); | ||
} | ||
}; | ||
DeserializerPipe = DeserializerPipe_1 = __decorate([ | ||
Injectable() | ||
], DeserializerPipe); | ||
export { DeserializerPipe }; | ||
} | ||
injectable(DeserializerPipe); | ||
//# sourceMappingURL=DeserializerPipe.js.map |
@@ -1,6 +0,5 @@ | ||
import { __decorate } from "tslib"; | ||
import { getValue } from "@tsed/core"; | ||
import { Injectable } from "@tsed/di"; | ||
import { injectable } from "@tsed/di"; | ||
import { ParamTypes } from "../domain/ParamTypes.js"; | ||
let ParseExpressionPipe = class ParseExpressionPipe { | ||
export class ParseExpressionPipe { | ||
transform(scope, param) { | ||
@@ -22,9 +21,4 @@ const { paramType, type } = param; | ||
} | ||
}; | ||
ParseExpressionPipe = __decorate([ | ||
Injectable({ | ||
priority: -1000 | ||
}) | ||
], ParseExpressionPipe); | ||
export { ParseExpressionPipe }; | ||
} | ||
injectable(ParseExpressionPipe).priority(-1000); | ||
//# sourceMappingURL=ParseExpressionPipe.js.map |
@@ -1,3 +0,2 @@ | ||
import { __decorate, __metadata, __param } from "tslib"; | ||
import { Inject, Injectable } from "@tsed/di"; | ||
import { constant, injectable, injectMany } from "@tsed/di"; | ||
import { deserialize } from "@tsed/json-mapper"; | ||
@@ -17,5 +16,14 @@ import { getJsonSchema } from "@tsed/schema"; | ||
} | ||
let ValidationPipe = class ValidationPipe { | ||
constructor(validators) { | ||
this.validator = validators[0]; | ||
export class ValidationPipe { | ||
constructor() { | ||
this.validators = new Map(); | ||
const validators = injectMany("validator:service"); | ||
const defaultValidator = constant("validators.default"); | ||
validators.length && this.validators.set("default", validators[0]); | ||
validators.map((service) => { | ||
this.validators.set(service.name, service); | ||
if (service.name === defaultValidator) { | ||
this.validators.set("default", service); | ||
} | ||
}); | ||
} | ||
@@ -41,3 +49,3 @@ coerceTypes(value, metadata) { | ||
transform(value, metadata) { | ||
if (!this.validator) { | ||
if (!this.validators.size) { | ||
this.checkIsRequired(value, metadata); | ||
@@ -58,7 +66,12 @@ return value; | ||
}); | ||
return this.validator.validate(value, { | ||
schema, | ||
type: metadata.isClass ? metadata.type : undefined, | ||
collectionType: metadata.collectionType | ||
}); | ||
// TODO retrieve the right validator from metadata | ||
const validator = this.validators.get("default"); | ||
if (validator) { | ||
return validator.validate(value, { | ||
schema, | ||
type: metadata.isClass ? metadata.type : undefined, | ||
collectionType: metadata.collectionType | ||
}); | ||
} | ||
return value; | ||
} | ||
@@ -71,11 +84,4 @@ checkIsRequired(value, metadata) { | ||
} | ||
}; | ||
ValidationPipe = __decorate([ | ||
Injectable({ | ||
type: "validator" | ||
}), | ||
__param(0, Inject("validator:service")), | ||
__metadata("design:paramtypes", [Array]) | ||
], ValidationPipe); | ||
export { ValidationPipe }; | ||
} | ||
injectable(ValidationPipe).type("validator"); | ||
//# sourceMappingURL=ValidationPipe.js.map |
@@ -1,2 +0,2 @@ | ||
import { DIContext, InjectorService } from "@tsed/di"; | ||
import { DIContext } from "@tsed/di"; | ||
import { JsonMethodStore, JsonParameterStore, PipeMethods } from "@tsed/schema"; | ||
@@ -12,3 +12,2 @@ export type PlatformParamsScope<Context extends DIContext = DIContext> = { | ||
export declare class PlatformParams { | ||
protected injector: InjectorService; | ||
getPipes(param: JsonParameterStore): PipeMethods<any, any>[]; | ||
@@ -15,0 +14,0 @@ /** |
import { JsonParameterStore, PipeMethods } from "@tsed/schema"; | ||
export type ValidatorServiceMethods = { | ||
export interface ValidatorServiceMethods { | ||
readonly name: string; | ||
validate(value: any, options: any): Promise<any>; | ||
}; | ||
} | ||
export declare class ValidationPipe implements PipeMethods { | ||
private validator; | ||
constructor(validators: ValidatorServiceMethods[]); | ||
private validators; | ||
constructor(); | ||
coerceTypes(value: any, metadata: JsonParameterStore): any; | ||
@@ -9,0 +10,0 @@ skip(value: any, metadata: JsonParameterStore): boolean; |
@@ -5,3 +5,3 @@ { | ||
"type": "module", | ||
"version": "8.0.0-rc.5", | ||
"version": "8.0.0-rc.6", | ||
"source": "./src/index.ts", | ||
@@ -29,9 +29,9 @@ "main": "./lib/esm/index.js", | ||
"devDependencies": { | ||
"@tsed/barrels": "8.0.0-rc.5", | ||
"@tsed/core": "8.0.0-rc.5", | ||
"@tsed/di": "8.0.0-rc.5", | ||
"@tsed/exceptions": "8.0.0-rc.5", | ||
"@tsed/json-mapper": "8.0.0-rc.5", | ||
"@tsed/schema": "8.0.0-rc.5", | ||
"@tsed/typescript": "8.0.0-rc.5", | ||
"@tsed/barrels": "8.0.0-rc.6", | ||
"@tsed/core": "8.0.0-rc.6", | ||
"@tsed/di": "8.0.0-rc.6", | ||
"@tsed/exceptions": "8.0.0-rc.6", | ||
"@tsed/json-mapper": "8.0.0-rc.6", | ||
"@tsed/schema": "8.0.0-rc.6", | ||
"@tsed/typescript": "8.0.0-rc.6", | ||
"eslint": "9.12.0", | ||
@@ -42,7 +42,7 @@ "typescript": "5.4.5", | ||
"peerDependencies": { | ||
"@tsed/core": "8.0.0-rc.5", | ||
"@tsed/di": "8.0.0-rc.5", | ||
"@tsed/exceptions": "8.0.0-rc.5", | ||
"@tsed/json-mapper": "8.0.0-rc.5", | ||
"@tsed/schema": "8.0.0-rc.5" | ||
"@tsed/core": "8.0.0-rc.6", | ||
"@tsed/di": "8.0.0-rc.6", | ||
"@tsed/exceptions": "8.0.0-rc.6", | ||
"@tsed/json-mapper": "8.0.0-rc.6", | ||
"@tsed/schema": "8.0.0-rc.6" | ||
}, | ||
@@ -49,0 +49,0 @@ "peerDependenciesMeta": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
72626
1326