diff --git a/src/sanitizer-factory.ts b/src/sanitizer-factory.ts index 8fea5ae..6a40ded 100644 --- a/src/sanitizer-factory.ts +++ b/src/sanitizer-factory.ts @@ -34,12 +34,16 @@ export class SanitizerFactory { ) {} build(): File[] { - return [ - { - path: buildFilePath(['sanitizers.ts'], this.service, this.options), - contents: format(from(this.buildFile()), this.options), - }, - ]; + if (!this.options?.typescriptValidators?.validatorsOnly) { + return [ + { + path: buildFilePath(['sanitizers.ts'], this.service, this.options), + contents: format(from(this.buildFile()), this.options), + }, + ]; + } + + return []; } private buildMethodName(member: Type | Union): string { diff --git a/src/types.ts b/src/types.ts index 049ee6e..f285980 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,6 +2,7 @@ import { NamespacedTypescriptOptions } from '@basketry/typescript/lib/types'; export declare type TypescriptValidatorsOptions = { typesImportPath?: string; + validatorsOnly?: boolean; }; export declare type NamespacedTypescriptValidatorsOptions = diff --git a/src/validator-factory.ts b/src/validator-factory.ts index 9378998..cc544b5 100644 --- a/src/validator-factory.ts +++ b/src/validator-factory.ts @@ -128,9 +128,11 @@ export class ValidatorFactory { this.options?.typescriptValidators?.typesImportPath ?? './types' }"`; - yield `import ${ - this.options?.typescript?.typeImports ? ' type ' : ' ' - } * as sanitizers from "./sanitizers"`; + if (!this.options?.typescriptValidators?.validatorsOnly) { + yield `import ${ + this.options?.typescript?.typeImports ? ' type ' : ' ' + } * as sanitizers from "./sanitizers"`; + } } private readonly codes = new Set(); @@ -428,118 +430,119 @@ export class ValidatorFactory { } private *buildValidatedServiceWrappers(): Iterable { - yield `export type ResponseBuilder = (validationErrors: ValidationError[], err: any) => T`; - for (const int of sort(this.service.interfaces)) { - const returnTypes = sort( - Array.from( - new Set( - int.methods - .map((m) => - getTypeByName(this.service, m.returnType?.typeName.value), - ) - .filter((t): t is Type => !!t), + if (!this.options?.typescriptValidators?.validatorsOnly) { + yield `export type ResponseBuilder = (validationErrors: ValidationError[], err: any) => T`; + for (const int of sort(this.service.interfaces)) { + const returnTypes = sort( + Array.from( + new Set( + int.methods + .map((m) => + getTypeByName(this.service, m.returnType?.typeName.value), + ) + .filter((t): t is Type => !!t), + ), ), - ), - ); - - const hasVoid = int.methods.some((m) => !m.returnType); - - const handlers = returnTypes.map( - (type) => - `${camel( - `build_${type.name.value}`, - )}: ResponseBuilder<${buildTypeName(type, 'types')}>`, - ); - if (hasVoid) { - handlers.push('buildVoid: ResponseBuilder'); - } - - const handlersType = `{${handlers.join(',')}}`; - - const intName = buildInterfaceName(int, 'types'); - yield `export class ${pascal( - `validated_${int.name.value}_service`, - )} implements ${buildInterfaceName(int, 'types')} {`; - yield `constructor(private readonly service: ${intName}, private readonly handlers: ${handlersType}){}`; - yield ''; - for (const method of sort(int.methods)) { - const methodName = buildMethodName(method); - const returnType = getTypeByName( - this.service, - method.returnType?.typeName.value, ); - const sanitize = (call: string, isAsync: boolean): string => { - if (returnType) { - return `sanitizers.${camel(`sanitize_${returnType.name.value}`)}(${ - isAsync ? 'await' : '' - } ${call})`; - } else { - return call; - } - }; + const hasVoid = int.methods.some((m) => !m.returnType); - const sanitizeAsync = (call: string): string => { - return sanitize(call, true); - }; + const handlers = returnTypes.map( + (type) => + `${camel( + `build_${type.name.value}`, + )}: ResponseBuilder<${buildTypeName(type, 'types')}>`, + ); + if (hasVoid) { + handlers.push('buildVoid: ResponseBuilder'); + } - const sanitizeSync = (call: string): string => { - return sanitize(call, false); - }; + const handlersType = `{${handlers.join(',')}}`; - const handlerName = returnType - ? `this.handlers.${camel(`build_${returnType.name.value}`)}` - : 'this.handlers.buildVoid'; + const intName = buildInterfaceName(int, 'types'); + yield `export class ${pascal( + `validated_${int.name.value}_service`, + )} implements ${buildInterfaceName(int, 'types')} {`; + yield `constructor(private readonly service: ${intName}, private readonly handlers: ${handlersType}){}`; + yield ''; + for (const method of sort(int.methods)) { + const methodName = buildMethodName(method); + const returnType = getTypeByName( + this.service, + method.returnType?.typeName.value, + ); - const hasParams = !!method.parameters.length; - const hasRequiredParams = method.parameters.some(isRequired); - const paramDef = method.parameters.length - ? `params${hasRequiredParams ? '' : '?'}: ${buildMethodParamsTypeName( + const sanitize = (call: string, isAsync: boolean): string => { + if (returnType) { + return `sanitizers.${camel( + `sanitize_${returnType.name.value}`, + )}(${isAsync ? 'await' : ''} ${call})`; + } else { + return call; + } + }; + + const sanitizeAsync = (call: string): string => { + return sanitize(call, true); + }; + + const sanitizeSync = (call: string): string => { + return sanitize(call, false); + }; + + const handlerName = returnType + ? `this.handlers.${camel(`build_${returnType.name.value}`)}` + : 'this.handlers.buildVoid'; + + const hasParams = !!method.parameters.length; + const hasRequiredParams = method.parameters.some(isRequired); + const paramDef = method.parameters.length + ? `params${ + hasRequiredParams ? '' : '?' + }: ${buildMethodParamsTypeName(method, 'types')}` + : ''; + yield `async ${methodName}(${paramDef}) {`; + yield `${ + hasParams ? 'let' : 'const' + } validationErrors: ValidationError[] = [];`; + yield 'try {'; + if (hasParams) { + yield `validationErrors = ${buildParamsValidatorName( method, - 'types', - )}` - : ''; - yield `async ${methodName}(${paramDef}) {`; - yield `${ - hasParams ? 'let' : 'const' - } validationErrors: ValidationError[] = [];`; - yield 'try {'; - if (hasParams) { - yield `validationErrors = ${buildParamsValidatorName( - method, - )}(params)`; - yield `if(validationErrors.length) {`; + )}(params)`; + yield `if(validationErrors.length) {`; + if (returnType) { + yield `return ${sanitizeSync( + `${handlerName}(validationErrors, undefined)`, + )}`; + } else { + yield `return ${handlerName}(validationErrors, undefined)`; + } + yield '}'; + } + if (hasParams) { + yield `const sanitizedParams = sanitizers.${camel( + `sanitize_${method.name.value}_params`, + )}(params)`; + } + yield `return ${sanitizeAsync( + `this.service.${methodName}(${hasParams ? 'sanitizedParams' : ''})`, + )}`; + yield '} catch (err) {'; if (returnType) { yield `return ${sanitizeSync( - `${handlerName}(validationErrors, undefined)`, + `${handlerName}(validationErrors, err)`, )}`; } else { - yield `return ${handlerName}(validationErrors, undefined)`; + yield `return ${handlerName}(validationErrors, err)`; } yield '}'; - } - if (hasParams) { - yield `const sanitizedParams = sanitizers.${camel( - `sanitize_${method.name.value}_params`, - )}(params)`; - } - yield `return ${sanitizeAsync( - `this.service.${methodName}(${hasParams ? 'sanitizedParams' : ''})`, - )}`; - yield '} catch (err) {'; - if (returnType) { - yield `return ${sanitizeSync( - `${handlerName}(validationErrors, err)`, - )}`; - } else { - yield `return ${handlerName}(validationErrors, err)`; + yield '}'; + yield ''; } yield '}'; - yield '}'; yield ''; } - yield '}'; - yield ''; } } }