mirror of
https://github.com/ThisIsBenny/wishlist-app.git
synced 2025-06-07 05:57:41 +00:00
#1 JSON Schemas centralized
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
parent
2b5f4a9fd6
commit
82ac242d96
5 changed files with 73 additions and 101 deletions
1
src/api/config/schemas/index.ts
Normal file
1
src/api/config/schemas/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from './wishlist'
|
53
src/api/config/schemas/wishlist.ts
Normal file
53
src/api/config/schemas/wishlist.ts
Normal 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,
|
||||
},
|
||||
},
|
||||
}
|
|
@ -3,33 +3,18 @@ import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
|
|||
import { wishlist } from '../../models'
|
||||
import { prisma } from '../../services'
|
||||
import { uniqueKeyError } from '../../config/errors'
|
||||
import {
|
||||
wishlistRequestSchema,
|
||||
wishlistResponseSchema,
|
||||
} from '../../config/schemas'
|
||||
|
||||
export const createList = <RouteOptions>{
|
||||
method: 'POST',
|
||||
url: '/',
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
required: ['title', 'imageSrc', 'slugUrlText'],
|
||||
properties: {
|
||||
title: { type: 'string' },
|
||||
imageSrc: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
slugUrlText: { type: 'string' },
|
||||
},
|
||||
},
|
||||
body: wishlistRequestSchema,
|
||||
response: {
|
||||
201: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' },
|
||||
title: { type: 'string' },
|
||||
imageSrc: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
slugUrlText: { type: 'string' },
|
||||
},
|
||||
},
|
||||
201: wishlistResponseSchema,
|
||||
},
|
||||
},
|
||||
errorHandler: (error, request, reply) => {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
|
||||
import { wishlist } from '../../models'
|
||||
import { wishlistResponseSchema } from '../../config/schemas'
|
||||
|
||||
export const getAll = <RouteOptions>{
|
||||
method: 'GET',
|
||||
|
@ -8,15 +9,7 @@ export const getAll = <RouteOptions>{
|
|||
response: {
|
||||
200: {
|
||||
type: 'array',
|
||||
items: {
|
||||
properties: {
|
||||
id: { type: 'string' },
|
||||
title: { type: 'string' },
|
||||
imageSrc: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
slugUrlText: { type: 'string' },
|
||||
},
|
||||
},
|
||||
items: wishlistResponseSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -36,31 +29,7 @@ export const getBySlugUrl = <RouteOptions>{
|
|||
url: '/:slugText',
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
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' },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
200: wishlistResponseSchema,
|
||||
},
|
||||
},
|
||||
handler: async (request: GetBySlugUrlTextRequest, reply: FastifyReply) => {
|
||||
|
|
|
@ -3,6 +3,12 @@ import { prisma } from '../../services'
|
|||
import { Wishlist } from '@/types'
|
||||
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
|
||||
import { wishlist } from '../../models'
|
||||
import {
|
||||
wishlistRequestSchema,
|
||||
wishlistResponseSchema,
|
||||
wishlistItemRequestSchema,
|
||||
wishlistItemResponseSchema,
|
||||
} from '../../config/schemas'
|
||||
|
||||
interface updateRequest extends FastifyRequest {
|
||||
params: {
|
||||
|
@ -20,28 +26,9 @@ export const updateList = <RouteOptions>{
|
|||
method: 'PUT',
|
||||
url: '/:wishlistId',
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
required: ['title', 'imageSrc', 'slugUrlText'],
|
||||
properties: {
|
||||
title: { type: 'string' },
|
||||
imageSrc: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
slugUrlText: { type: 'string' },
|
||||
},
|
||||
},
|
||||
body: wishlistRequestSchema,
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' },
|
||||
title: { type: 'string' },
|
||||
imageSrc: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
slugUrlText: { type: 'string' },
|
||||
},
|
||||
},
|
||||
200: wishlistResponseSchema,
|
||||
},
|
||||
},
|
||||
errorHandler: (error, request, reply) => {
|
||||
|
@ -68,32 +55,9 @@ export const updateItem = <RouteOptions>{
|
|||
method: 'PUT',
|
||||
url: '/:wishlistId/item/:itemId',
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
title: { type: 'string' },
|
||||
url: { type: 'string' },
|
||||
image: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
comment: { type: 'string' },
|
||||
bought: { type: 'boolean' },
|
||||
},
|
||||
},
|
||||
body: wishlistItemRequestSchema,
|
||||
response: {
|
||||
204: {
|
||||
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' },
|
||||
},
|
||||
},
|
||||
200: wishlistItemResponseSchema,
|
||||
},
|
||||
},
|
||||
handler: async (request: updateItemRequest, reply: FastifyReply) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue