From 82ac242d9649168828a0c3e6740e47eba6ed5424 Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Sat, 5 Feb 2022 14:03:29 +0100 Subject: [PATCH] #1 JSON Schemas centralized Signed-off-by: Benny Samir Hierl --- src/api/config/schemas/index.ts | 1 + src/api/config/schemas/wishlist.ts | 53 ++++++++++++++++++++++++++++ src/api/routes/wishlist/create.ts | 27 ++++---------- src/api/routes/wishlist/read.ts | 37 ++------------------ src/api/routes/wishlist/update.ts | 56 ++++++------------------------ 5 files changed, 73 insertions(+), 101 deletions(-) create mode 100644 src/api/config/schemas/index.ts create mode 100644 src/api/config/schemas/wishlist.ts diff --git a/src/api/config/schemas/index.ts b/src/api/config/schemas/index.ts new file mode 100644 index 0000000..5284f07 --- /dev/null +++ b/src/api/config/schemas/index.ts @@ -0,0 +1 @@ +export * from './wishlist' diff --git a/src/api/config/schemas/wishlist.ts b/src/api/config/schemas/wishlist.ts new file mode 100644 index 0000000..897bf25 --- /dev/null +++ b/src/api/config/schemas/wishlist.ts @@ -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, + }, + }, +} diff --git a/src/api/routes/wishlist/create.ts b/src/api/routes/wishlist/create.ts index 198d400..9ee934a 100644 --- a/src/api/routes/wishlist/create.ts +++ b/src/api/routes/wishlist/create.ts @@ -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 = { 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) => { diff --git a/src/api/routes/wishlist/read.ts b/src/api/routes/wishlist/read.ts index 03e5928..3bc6e80 100644 --- a/src/api/routes/wishlist/read.ts +++ b/src/api/routes/wishlist/read.ts @@ -1,5 +1,6 @@ import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify' import { wishlist } from '../../models' +import { wishlistResponseSchema } from '../../config/schemas' export const getAll = { method: 'GET', @@ -8,15 +9,7 @@ export const getAll = { 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 = { 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) => { diff --git a/src/api/routes/wishlist/update.ts b/src/api/routes/wishlist/update.ts index bdc6952..b6f9a4d 100644 --- a/src/api/routes/wishlist/update.ts +++ b/src/api/routes/wishlist/update.ts @@ -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 = { 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 = { 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) => {