#1 JSON Schemas centralized

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
Benny Samir Hierl 2022-02-05 14:03:29 +01:00
parent 2b5f4a9fd6
commit 82ac242d96
5 changed files with 73 additions and 101 deletions

View file

@ -0,0 +1 @@
export * from './wishlist'

View file

@ -0,0 +1,53 @@
export const wishlistItemRequestSchema = {
type: 'object',
additionalProperties: false,
required: ['title', 'imageSrc'],
properties: {
title: { type: 'string' },
url: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
comment: { type: 'string' },
bought: { type: 'boolean' },
},
}
export const wishlistItemResponseSchema = {
type: 'object',
properties: {
id: { type: 'number' },
title: { type: 'string' },
url: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
comment: { type: 'string' },
bought: { type: 'boolean' },
wishlistId: { type: 'string' },
},
}
export const wishlistRequestSchema = {
type: 'object',
additionalProperties: false,
required: ['title', 'imageSrc', 'slugUrlText'],
properties: {
title: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
slugUrlText: { type: 'string' },
},
}
export const wishlistResponseSchema = {
type: 'object',
properties: {
id: { type: 'string' },
title: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
slugUrlText: { type: 'string' },
items: {
type: 'array',
items: wishlistItemResponseSchema,
},
},
}

View file

@ -3,33 +3,18 @@ import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
import { wishlist } from '../../models' import { wishlist } from '../../models'
import { prisma } from '../../services' import { prisma } from '../../services'
import { uniqueKeyError } from '../../config/errors' import { uniqueKeyError } from '../../config/errors'
import {
wishlistRequestSchema,
wishlistResponseSchema,
} from '../../config/schemas'
export const createList = <RouteOptions>{ export const createList = <RouteOptions>{
method: 'POST', method: 'POST',
url: '/', url: '/',
schema: { schema: {
body: { body: wishlistRequestSchema,
type: 'object',
additionalProperties: false,
required: ['title', 'imageSrc', 'slugUrlText'],
properties: {
title: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
slugUrlText: { type: 'string' },
},
},
response: { response: {
201: { 201: wishlistResponseSchema,
type: 'object',
properties: {
id: { type: 'string' },
title: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
slugUrlText: { type: 'string' },
},
},
}, },
}, },
errorHandler: (error, request, reply) => { errorHandler: (error, request, reply) => {

View file

@ -1,5 +1,6 @@
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify' import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
import { wishlist } from '../../models' import { wishlist } from '../../models'
import { wishlistResponseSchema } from '../../config/schemas'
export const getAll = <RouteOptions>{ export const getAll = <RouteOptions>{
method: 'GET', method: 'GET',
@ -8,15 +9,7 @@ export const getAll = <RouteOptions>{
response: { response: {
200: { 200: {
type: 'array', type: 'array',
items: { items: wishlistResponseSchema,
properties: {
id: { type: 'string' },
title: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
slugUrlText: { type: 'string' },
},
},
}, },
}, },
}, },
@ -36,31 +29,7 @@ export const getBySlugUrl = <RouteOptions>{
url: '/:slugText', url: '/:slugText',
schema: { schema: {
response: { response: {
200: { 200: wishlistResponseSchema,
type: 'object',
properties: {
id: { type: 'string' },
title: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
slugUrlText: { type: 'string' },
items: {
type: 'array',
items: {
properties: {
id: { type: 'number' },
title: { type: 'string' },
url: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
comment: { type: 'string' },
bought: { type: 'boolean' },
wishlistId: { type: 'string' },
},
},
},
},
},
}, },
}, },
handler: async (request: GetBySlugUrlTextRequest, reply: FastifyReply) => { handler: async (request: GetBySlugUrlTextRequest, reply: FastifyReply) => {

View file

@ -3,6 +3,12 @@ import { prisma } from '../../services'
import { Wishlist } from '@/types' import { Wishlist } from '@/types'
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify' import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
import { wishlist } from '../../models' import { wishlist } from '../../models'
import {
wishlistRequestSchema,
wishlistResponseSchema,
wishlistItemRequestSchema,
wishlistItemResponseSchema,
} from '../../config/schemas'
interface updateRequest extends FastifyRequest { interface updateRequest extends FastifyRequest {
params: { params: {
@ -20,28 +26,9 @@ export const updateList = <RouteOptions>{
method: 'PUT', method: 'PUT',
url: '/:wishlistId', url: '/:wishlistId',
schema: { schema: {
body: { body: wishlistRequestSchema,
type: 'object',
additionalProperties: false,
required: ['title', 'imageSrc', 'slugUrlText'],
properties: {
title: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
slugUrlText: { type: 'string' },
},
},
response: { response: {
200: { 200: wishlistResponseSchema,
type: 'object',
properties: {
id: { type: 'string' },
title: { type: 'string' },
imageSrc: { type: 'string' },
description: { type: 'string' },
slugUrlText: { type: 'string' },
},
},
}, },
}, },
errorHandler: (error, request, reply) => { errorHandler: (error, request, reply) => {
@ -68,32 +55,9 @@ export const updateItem = <RouteOptions>{
method: 'PUT', method: 'PUT',
url: '/:wishlistId/item/:itemId', url: '/:wishlistId/item/:itemId',
schema: { schema: {
body: { body: wishlistItemRequestSchema,
type: 'object',
additionalProperties: false,
properties: {
title: { type: 'string' },
url: { type: 'string' },
image: { type: 'string' },
description: { type: 'string' },
comment: { type: 'string' },
bought: { type: 'boolean' },
},
},
response: { response: {
204: { 200: wishlistItemResponseSchema,
type: 'object',
properties: {
id: { type: 'number' },
title: { type: 'string' },
url: { type: 'string' },
image: { type: 'string' },
description: { type: 'string' },
comment: { type: 'string' },
bought: { type: 'boolean' },
wishlistId: { type: 'string' },
},
},
}, },
}, },
handler: async (request: updateItemRequest, reply: FastifyReply) => { handler: async (request: updateItemRequest, reply: FastifyReply) => {