{"version":3,"file":"NestiaSimulator.mjs","names":[],"sources":["../src/NestiaSimulator.ts"],"sourcesContent":["import { HttpError } from \"./HttpError\";\nimport { join_host_and_path } from \"./internal/join_host_and_path\";\n\nexport namespace NestiaSimulator {\n  export interface IProps {\n    host: string;\n    path: string;\n    method: \"GET\" | \"POST\" | \"PATCH\" | \"PUT\" | \"DELETE\";\n    contentType: string;\n  }\n\n  export const assert = (props: IProps) => {\n    return {\n      param: param(props),\n      query: query(props),\n      body: body(props),\n    };\n  };\n  const param =\n    (props: IProps) =>\n    (name: string) =>\n    <T>(task: () => T): void => {\n      validate((exp) => `URL parameter \"${name}\" is not ${exp.expected} type.`)(\n        props,\n      )(task);\n    };\n\n  const query =\n    (props: IProps) =>\n    <T>(task: () => T): void =>\n      validate(\n        () => \"Request query parameters are not following the promised type.\",\n      )(props)(task);\n\n  const body =\n    (props: IProps) =>\n    <T>(task: () => T): void =>\n      validate(() => \"Request body is not following the promised type.\")(props)(\n        task,\n      );\n\n  const validate =\n    (message: (exp: TypeGuardError) => string, path?: string) =>\n    (props: IProps) =>\n    <T>(task: () => T): void => {\n      try {\n        task();\n      } catch (exp) {\n        if (isTypeGuardError(exp))\n          throw new HttpError(\n            props.method,\n            join_host_and_path(props.host, props.path),\n            400,\n            {\n              \"Content-Type\": props.contentType,\n            },\n            JSON.stringify({\n              method: exp.method,\n              path: path ?? exp.path,\n              expected: exp.expected,\n              value: exp.value,\n              message: message(exp),\n            }),\n          );\n        throw exp;\n      }\n    };\n}\n\nconst isTypeGuardError = (input: any): input is TypeGuardError =>\n  \"string\" === typeof input.method &&\n  (undefined === input.path || \"string\" === typeof input.path) &&\n  \"string\" === typeof input.expected &&\n  \"string\" === typeof input.name &&\n  \"string\" === typeof input.message &&\n  (undefined === input.stack || \"string\" === typeof input.stack);\n\ninterface TypeGuardError extends Error {\n  method: string;\n  path: string | undefined;\n  expected: string;\n  value: any;\n}\n"],"mappings":";;;AAGO,IAAA;;4BAQkB,UAAkB;EACvC,OAAO;GACL,OAAO,MAAM,KAAK;GAClB,OAAO,MAAM,KAAK;GAClB,MAAM,KAAK,KAAK;EAClB;CACF;CACA,MAAM,SACH,WACA,UACG,SAAwB;EAC1B,UAAU,QAAQ,kBAAkB,KAAK,WAAW,IAAI,SAAS,OAAO,CAAC,CACvE,KACF,CAAC,CAAC,IAAI;CACR;CAEF,MAAM,SACH,WACG,SACF,eACQ,+DACR,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI;CAEjB,MAAM,QACH,WACG,SACF,eAAe,kDAAkD,CAAC,CAAC,KAAK,CAAC,CACvE,IACF;CAEJ,MAAM,YACH,SAA0C,UAC1C,WACG,SAAwB;EAC1B,IAAI;GACF,KAAK;EACP,SAAS,KAAK;GACZ,IAAI,iBAAiB,GAAG,GACtB,MAAM,IAAI,UACR,MAAM,QACN,mBAAmB,MAAM,MAAM,MAAM,IAAI,GACzC,KACA,EACE,gBAAgB,MAAM,YACxB,GACA,KAAK,UAAU;IACb,QAAQ,IAAI;IACZ,MAAM,QAAQ,IAAI;IAClB,UAAU,IAAI;IACd,OAAO,IAAI;IACX,SAAS,QAAQ,GAAG;GACtB,CAAC,CACH;GACF,MAAM;EACR;CACF;GACH,oBAAA,kBAAA,CAAA,EAAD;AAEA,MAAM,oBAAoB,UACxB,aAAa,OAAO,MAAM,WACzB,KAAA,MAAc,MAAM,QAAQ,aAAa,OAAO,MAAM,SACvD,aAAa,OAAO,MAAM,YAC1B,aAAa,OAAO,MAAM,QAC1B,aAAa,OAAO,MAAM,YACzB,KAAA,MAAc,MAAM,SAAS,aAAa,OAAO,MAAM"}