From 0de523eae07592cff5ad775d73fa89200b481d31 Mon Sep 17 00:00:00 2001 From: Jiwon <87058411+raipen@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:32:48 +0900 Subject: [PATCH] Feat: use fastify-schema-to-ts (#86) * feat: install local fastify schema to ts * feat: use fastify schema to ts * feat: install fastify schema to ts from npm --- package-lock.json | 16 ++++++++++++++++ package.json | 1 + src/DTO/index.dto.ts | 26 -------------------------- src/DTO/menu.dto.ts | 2 +- src/DTO/mileage.dto.ts | 2 +- src/DTO/order.dto.ts | 2 +- src/DTO/preOrder.dto.ts | 2 +- src/DTO/stock.dto.ts | 2 +- src/DTO/store.dto.ts | 2 +- src/DTO/user.dto.ts | 2 +- 10 files changed, 24 insertions(+), 33 deletions(-) diff --git a/package-lock.json b/package-lock.json index db15b6b..3966794 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "dotenv": "^16.3.1", "fastify": "^4.19.1", "fastify-plugin": "^4.5.0", + "fastify-schema-to-ts": "^1.0.1", "json-schema-to-ts": "^2.9.2", "jsonwebtoken": "^9.0.1" }, @@ -3181,6 +3182,15 @@ "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-4.5.0.tgz", "integrity": "sha512-79ak0JxddO0utAXAQ5ccKhvs6vX2MGyHHMMsmZkBANrq3hXc1CHzvNPHOcvTsVMEPl5I+NT+RO4YKMGehOfSIg==" }, + "node_modules/fastify-schema-to-ts": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/fastify-schema-to-ts/-/fastify-schema-to-ts-1.0.1.tgz", + "integrity": "sha512-I8YOThM/EPu8JbM5/8qJzGU7e0CSdjphT9haee1LQ/RhrHZl+pfEjDJRyTi7nBAKpTCor+ZRGlvPh/KhQprimg==", + "peerDependencies": { + "fastify": "^4.19.0", + "json-schema-to-ts": "^2.9.0" + } + }, "node_modules/fastq": { "version": "1.15.0", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.15.0.tgz", @@ -8849,6 +8859,12 @@ "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-4.5.0.tgz", "integrity": "sha512-79ak0JxddO0utAXAQ5ccKhvs6vX2MGyHHMMsmZkBANrq3hXc1CHzvNPHOcvTsVMEPl5I+NT+RO4YKMGehOfSIg==" }, + "fastify-schema-to-ts": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/fastify-schema-to-ts/-/fastify-schema-to-ts-1.0.1.tgz", + "integrity": "sha512-I8YOThM/EPu8JbM5/8qJzGU7e0CSdjphT9haee1LQ/RhrHZl+pfEjDJRyTi7nBAKpTCor+ZRGlvPh/KhQprimg==", + "requires": {} + }, "fastq": { "version": "1.15.0", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.15.0.tgz", diff --git a/package.json b/package.json index 80b989f..6441f69 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "dotenv": "^16.3.1", "fastify": "^4.19.1", "fastify-plugin": "^4.5.0", + "fastify-schema-to-ts": "^1.0.1", "json-schema-to-ts": "^2.9.2", "jsonwebtoken": "^9.0.1" }, diff --git a/src/DTO/index.dto.ts b/src/DTO/index.dto.ts index 69cdc72..4a016d9 100644 --- a/src/DTO/index.dto.ts +++ b/src/DTO/index.dto.ts @@ -63,29 +63,3 @@ export type ErrorInterface = FromSchema<{ toast: { type: 'string', enum: string[] } } }>; - -export type SchemaToInterface< - T extends { - body?: JSONSchema; - querystring?: JSONSchema; - params?: JSONSchema; - headers?: JSONSchema; - response: { [key: string]: JSONSchema }; - }, - Option extends [{ pattern: unknown; output: unknown }] | false = false -> = { - Body: T['body'] extends JSONSchema ? FromSchema : unknown; - Querystring: T['querystring'] extends JSONSchema - ? FromSchema - : unknown; - Params: T['params'] extends JSONSchema ? FromSchema : unknown; - Headers: T['headers'] extends JSONSchema - ? FromSchema - : unknown; - Reply: { - [key in keyof T['response']]: FromSchema< - T['response'][key], - { deserialize: Option } - >; - }; -}; diff --git a/src/DTO/menu.dto.ts b/src/DTO/menu.dto.ts index 6fc7053..88aa5cf 100644 --- a/src/DTO/menu.dto.ts +++ b/src/DTO/menu.dto.ts @@ -1,9 +1,9 @@ import { StoreAuthorizationHeader, errorSchema, - SchemaToInterface, } from '@DTO/index.dto'; import * as E from '@errors'; +import { SchemaToInterface } from 'fastify-schema-to-ts'; export const getMenuListSchema = { tags: ['menu'], diff --git a/src/DTO/mileage.dto.ts b/src/DTO/mileage.dto.ts index c7406c6..d5d4abe 100644 --- a/src/DTO/mileage.dto.ts +++ b/src/DTO/mileage.dto.ts @@ -2,9 +2,9 @@ import { Mileage as prismaMileage } from '@prisma/client'; import { StoreAuthorizationHeader, errorSchema, - SchemaToInterface, } from '@DTO/index.dto'; import * as E from '@errors'; +import { SchemaToInterface } from 'fastify-schema-to-ts'; export type Mileage = prismaMileage; export const getMileageSchema = { diff --git a/src/DTO/order.dto.ts b/src/DTO/order.dto.ts index b6acb02..db24d87 100644 --- a/src/DTO/order.dto.ts +++ b/src/DTO/order.dto.ts @@ -2,9 +2,9 @@ import { Order as prismaOrder } from '@prisma/client'; import { StoreAuthorizationHeader, errorSchema, - SchemaToInterface, } from '@DTO/index.dto'; import * as E from '@errors'; +import { SchemaToInterface } from 'fastify-schema-to-ts'; export type Order = prismaOrder; export const newOrderSchema = { diff --git a/src/DTO/preOrder.dto.ts b/src/DTO/preOrder.dto.ts index d759fc5..5b6dacf 100644 --- a/src/DTO/preOrder.dto.ts +++ b/src/DTO/preOrder.dto.ts @@ -2,9 +2,9 @@ import { PreOrder as prismaPreOrder } from '@prisma/client'; import { StoreAuthorizationHeader, errorSchema, - SchemaToInterface, } from '@DTO/index.dto'; import * as E from '@errors'; +import { SchemaToInterface } from 'fastify-schema-to-ts'; export type PreOrder = prismaPreOrder; export const newPreOrderSchema = { diff --git a/src/DTO/stock.dto.ts b/src/DTO/stock.dto.ts index ab3317a..86973fd 100644 --- a/src/DTO/stock.dto.ts +++ b/src/DTO/stock.dto.ts @@ -1,9 +1,9 @@ import { StoreAuthorizationHeader, errorSchema, - SchemaToInterface, } from '@DTO/index.dto'; import * as E from '@errors'; +import { SchemaToInterface } from 'fastify-schema-to-ts'; export const createStockSchema = { tags: ['stock'], diff --git a/src/DTO/store.dto.ts b/src/DTO/store.dto.ts index 5b78ebb..12a2d63 100644 --- a/src/DTO/store.dto.ts +++ b/src/DTO/store.dto.ts @@ -2,10 +2,10 @@ import { Store as prismaStore } from '@prisma/client'; import { AuthorizationHeader, errorSchema, - SchemaToInterface, StoreAuthorizationHeader, } from '@DTO/index.dto'; import * as E from '@errors'; +import { SchemaToInterface } from 'fastify-schema-to-ts'; export type Store = prismaStore; export const newStoreSchema = { diff --git a/src/DTO/user.dto.ts b/src/DTO/user.dto.ts index e9b38ac..6d8d1a5 100644 --- a/src/DTO/user.dto.ts +++ b/src/DTO/user.dto.ts @@ -2,9 +2,9 @@ import { User as prismaUser } from '@prisma/client'; import { AuthorizationHeader, errorSchema, - SchemaToInterface, } from '@DTO/index.dto'; import * as E from '@errors'; +import { SchemaToInterface } from 'fastify-schema-to-ts'; export type User = prismaUser; export const phoneSchema = {