Some fixed to naming etc

This commit is contained in:
Mattias Hansson 2025-03-08 21:01:03 +01:00
parent 8d23b67592
commit 2dee4726d7
5 changed files with 62 additions and 9 deletions

45
.gitignore vendored Normal file
View file

@ -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

View file

@ -39,6 +39,14 @@ start();
/*
Try making a POST request with this "deranged" JSON:

View file

@ -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);

View file

@ -15,10 +15,10 @@ const UserSchema = Type.Object({
});
// Derive TypeScript type from the TypeBox schema
type User = Static<typeof UserSchema>;
type UserDTO = Static<typeof UserSchema>;
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

View file

@ -17,7 +17,7 @@ const UserSchema = Type.Object({
}, { $id: 'User' });
// Derive TypeScript type from the TypeBox schema
type User = Static<typeof UserSchema>;
type UserDTO = Static<typeof UserSchema>;
// 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",