diff --git a/example/1.ts b/example/1.ts index a05081b..40ae9ea 100644 --- a/example/1.ts +++ b/example/1.ts @@ -1,14 +1,11 @@ import { Type } from "@sinclair/typebox"; -import { TypeCompiler } from "@sinclair/typebox/compiler"; import { fetch } from "../src"; -const ResponseType = TypeCompiler.Compile( - Type.Object({ +const ResponseType = Type.Object({ id: Type.Number(), userId: Type.Number(), title: Type.String(), - }) -); +}) async function main() { const response = await fetch("https://jsonplaceholder.typicode.com/todos/1"); diff --git a/package-lock.json b/package-lock.json index 94f7a64..987162e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@erfanium/fetch-typebox", - "version": "0.1.2", + "version": "0.3.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@erfanium/fetch-typebox", - "version": "0.1.2", + "version": "0.3.1", "license": "ISC", "devDependencies": { "@sinclair/hammer": "^0.17.2", diff --git a/package.json b/package.json index cc1b065..c97ea0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@erfanium/fetch-typebox", - "version": "0.2.2", + "version": "0.3.1", "description": "", "main": "index.js", "types": "index.d.ts", diff --git a/screenshots/1.png b/screenshots/1.png index 0a0964a..9a0ae9b 100644 Binary files a/screenshots/1.png and b/screenshots/1.png differ diff --git a/src/index.ts b/src/index.ts index da23ada..f88c361 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { Static, TSchema, TUnknown } from "@sinclair/typebox"; -import { TypeCheck } from "@sinclair/typebox/compiler"; +import { Value } from '@sinclair/typebox/value' export class ResponseValidationError extends Error {} @@ -13,23 +13,20 @@ export class TypeBoxResponse extends Response { } json(): Promise; - json(typeChecker: TypeCheck): Promise>; + json(schema: T): Promise>; async json( - typeChecker?: TypeCheck + schema?: T ): Promise> { - if (!typeChecker) return super.json(); + if (!schema) return super.json(); const body = await super.json(); - const result = typeChecker.Check(body); + const result = Value.Check(schema, body); if (result) return body; - const errors = typeChecker.Errors(body); + const errors = Value.Errors(schema, body); const firstError = errors.First(); throw new ResponseValidationError( - `ResponseValidationError ${firstError?.message}. path: ${firstError?.path} value: ${firstError?.value}`, - { - cause: firstError, - } + `ResponseValidationError ${firstError?.message}. path: ${firstError?.path} value: ${firstError?.value}` ); } }