diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41a4fcb --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +lib-cov +*.seed +*.log +*.csv +*.dat +*.out +*.pid +*.gz +*.swp + +pids +logs +results +tmp + +# Build +public/css/main.css + +# Coverage reports +coverage + +# API keys and secrets +.env + +# Dependency directory +node_modules +bower_components + +# TypeScript Target DIR +build + +# Editors +.idea +*.iml + +# OS metadata +.DS_Store +Thumbs.db + +# Ignore built ts files +dist/**/* +build/* + +# ignore yarn.lock +yarn.lock \ No newline at end of file diff --git a/src/example1.ts b/src/example1.ts index 91b481c..fe50ead 100644 --- a/src/example1.ts +++ b/src/example1.ts @@ -39,6 +39,14 @@ start(); + + + + + + + + /* Try making a POST request with this "deranged" JSON: diff --git a/src/example2b.ts b/src/example2b.ts index fb21c91..8aae04d 100644 --- a/src/example2b.ts +++ b/src/example2b.ts @@ -15,7 +15,7 @@ const TBUser = Type.Object({ //console.log(JSON.stringify(TBUser, null, 2)); // Convert to YAML -const openApiYaml = yaml.dump(TBUser); +const jsonSchemaYaml = yaml.dump(TBUser); console.log('JSON Schema (YAML):'); -console.log(openApiYaml); +console.log(jsonSchemaYaml); diff --git a/src/example3.ts b/src/example3.ts index 1cce6d4..07b9f07 100644 --- a/src/example3.ts +++ b/src/example3.ts @@ -15,10 +15,10 @@ const UserSchema = Type.Object({ }); // Derive TypeScript type from the TypeBox schema -type User = Static; +type UserDTO = Static; app.post<{ - Body: User; // Using the derived TypeScript type + Body: UserDTO; // Using the derived TypeScript type }>('/api/users', { schema: { body: UserSchema // Using the TypeBox schema for validation diff --git a/src/example4d.ts b/src/example4d.ts index 0fa537d..2f6cdcc 100644 --- a/src/example4d.ts +++ b/src/example4d.ts @@ -17,7 +17,7 @@ const UserSchema = Type.Object({ }, { $id: 'User' }); // Derive TypeScript type from the TypeBox schema -type User = Static; +type UserDTO = Static; // Response schema for better documentation const ResponseSchema = Type.Object({ @@ -48,10 +48,10 @@ async function startServer() { buildLocalReference: (json, baseUri, fragment, i) => { // Use $id property from schema if (json.$id) { - return `#/components/schemas/${json.$id}`; + return `components_schemas_${json.$id}`; } // Fall back to default behavior - return `#/components/schemas/${fragment[1]}${i}`; + return `components_schemas_${fragment[1]}${i}`; } } }); @@ -77,7 +77,7 @@ async function startServer() { } } }, async (request, reply) => { - const user = request.body as User; + const user = request.body as UserDTO; const canDrink = user.age >= 21; const greeting = `Hello ${user.name.toUpperCase()}, your email ${user.email.toLowerCase()} has been recorded`; @@ -95,7 +95,7 @@ async function startServer() { } }, async (request, reply) => { // Sample data that follows our User type - const users: User[] = [ + const users: UserDTO[] = [ { id: 1, name: "John Doe",