1- import { HttpException , Injectable } from "@nestjs/common" ;
1+ import { BadRequestException , Injectable } from "@nestjs/common" ;
22import type { FastifyReply } from "fastify" ;
3- import type { OasInput , UntrustedClientInput , ValidateInput } from "@developer-overheid-nl/don-tools" ;
3+ import type { OasInput , UntrustedClientInput , ValidateInput } from "@developer-overheid-nl/don-tools-logic " ;
44import { ToolsApi } from "../api" ;
55import type { ModelsKeycloakClientResult , ModelsLintResult } from "../models" ;
66
@@ -10,31 +10,15 @@ const setHeaders = (reply: FastifyReply, headers: Record<string, string>) => {
1010
1111const importEsm = new Function ( "specifier" , "return import(specifier)" ) as < T > ( specifier : string ) => Promise < T > ;
1212const loadLogic = ( ) =>
13- import ( "@developer-overheid-nl/don-tools" ) . catch ( ( ) =>
14- importEsm < typeof import ( "@developer-overheid-nl/don-tools" ) > ( "@developer-overheid-nl/don-tools" ) ,
13+ import ( "@developer-overheid-nl/don-tools-logic " ) . catch ( ( ) =>
14+ importEsm < typeof import ( "@developer-overheid-nl/don-tools-logic " ) > ( "@developer-overheid-nl/don-tools-logic " ) ,
1515 ) ;
1616
17- const getLogicErrorStatus = ( error : unknown ) : number | undefined => {
18- if ( typeof error !== "object" || error === null || ! ( "status" in error ) ) return undefined ;
19- const status = ( error as { status : unknown } ) . status ;
20- return typeof status === "number" ? status : undefined ;
21- } ;
22-
23- const getLogicErrorMessage = ( error : unknown ) : string => {
24- if ( typeof error !== "object" || error === null ) {
25- return "Request validation failed" ;
26- }
27- if ( error instanceof Error && error . message . length > 0 ) return error . message ;
28- const detail = "detail" in error ? ( error as { detail : unknown } ) . detail : undefined ;
29- if ( typeof detail === "string" && detail . length > 0 ) return detail ;
30- return "Request validation failed" ;
31- } ;
32-
33- const asHttpException = async < T > ( operation : ( ) => Promise < T > ) : Promise < T > => {
17+ const asBadRequest = async < T > ( operation : ( ) => Promise < T > ) : Promise < T > => {
3418 try {
3519 return await operation ( ) ;
3620 } catch ( error ) {
37- throw new HttpException ( getLogicErrorMessage ( error ) , getLogicErrorStatus ( error ) ?? 400 ) ;
21+ throw new BadRequestException ( error instanceof Error ? error . message : "Request validation failed" ) ;
3822 }
3923} ;
4024
@@ -43,52 +27,52 @@ export class ToolsApiService extends ToolsApi {
4327 async arazzoMarkdown ( oasInput : OasInput | undefined , _request : Request , reply : FastifyReply ) : Promise < string > {
4428 const { arazzoMarkdown } = await loadLogic ( ) ;
4529 reply . type ( "text/markdown; charset=utf-8" ) ;
46- return asHttpException ( ( ) => arazzoMarkdown ( oasInput as OasInput ) ) ;
30+ return asBadRequest ( ( ) => arazzoMarkdown ( oasInput as OasInput ) ) ;
4731 }
4832
4933 async arazzoMermaid ( oasInput : OasInput | undefined , _request : Request , reply : FastifyReply ) : Promise < string > {
5034 const { arazzoMermaid } = await loadLogic ( ) ;
5135 reply . type ( "text/plain; charset=utf-8" ) ;
52- return asHttpException ( ( ) => arazzoMermaid ( oasInput as OasInput ) ) ;
36+ return asBadRequest ( ( ) => arazzoMermaid ( oasInput as OasInput ) ) ;
5337 }
5438
5539 async bundleOAS ( oasInput : OasInput | undefined , _request : Request , reply : FastifyReply ) : Promise < void > {
5640 const { bundleOAS } = await loadLogic ( ) ;
57- const result = await asHttpException ( ( ) => bundleOAS ( oasInput as OasInput ) ) ;
41+ const result = await asBadRequest ( ( ) => bundleOAS ( oasInput as OasInput ) ) ;
5842 setHeaders ( reply , result . headers ) ;
5943 return result . rawBody as never ;
6044 }
6145
6246 async convertOAS ( oasInput : OasInput | undefined , _request : Request , reply : FastifyReply ) : Promise < void > {
6347 const { convertOAS } = await loadLogic ( ) ;
64- const result = await asHttpException ( ( ) => convertOAS ( oasInput as OasInput ) ) ;
48+ const result = await asBadRequest ( ( ) => convertOAS ( oasInput as OasInput ) ) ;
6549 setHeaders ( reply , result . headers ) ;
6650 return result . rawBody as never ;
6751 }
6852
6953 async createPostmanCollection ( oasInput : OasInput | undefined , _request : Request , reply : FastifyReply ) : Promise < void > {
7054 const { createPostmanCollection } = await loadLogic ( ) ;
71- const result = await asHttpException ( ( ) => createPostmanCollection ( oasInput as OasInput ) ) ;
55+ const result = await asBadRequest ( ( ) => createPostmanCollection ( oasInput as OasInput ) ) ;
7256 setHeaders ( reply , result . headers ) ;
7357 return result . rawBody as never ;
7458 }
7559
7660 async generateOAS ( oasInput : OasInput | undefined , _request : Request , reply : FastifyReply ) : Promise < object > {
7761 const { generateOAS } = await loadLogic ( ) ;
78- const result = await asHttpException ( ( ) => generateOAS ( oasInput as OasInput ) ) ;
62+ const result = await asBadRequest ( ( ) => generateOAS ( oasInput as OasInput ) ) ;
7963 setHeaders ( reply , result . headers ) ;
8064 return JSON . parse ( Buffer . from ( result . rawBody ) . toString ( "utf8" ) ) as object ;
8165 }
8266
8367 async untrustClient ( untrustClientInput : UntrustedClientInput | undefined ) : Promise < ModelsKeycloakClientResult > {
8468 const { untrustedClient } = await loadLogic ( ) ;
85- return asHttpException ( ( ) =>
69+ return asBadRequest ( ( ) =>
8670 untrustedClient ( untrustClientInput as UntrustedClientInput ) ,
8771 ) as Promise < ModelsKeycloakClientResult > ;
8872 }
8973
9074 async validatorOpenAPIPost ( oasInput : OasInput | undefined ) : Promise < ModelsLintResult > {
9175 const { validatorOpenAPIPost } = await loadLogic ( ) ;
92- return asHttpException ( ( ) => validatorOpenAPIPost ( oasInput as ValidateInput ) ) as Promise < ModelsLintResult > ;
76+ return asBadRequest ( ( ) => validatorOpenAPIPost ( oasInput as ValidateInput ) ) as Promise < ModelsLintResult > ;
9377 }
9478}
0 commit comments