From f4da6f1618ac32986054fc0dc38c31ee6f4971e7 Mon Sep 17 00:00:00 2001 From: Paolo Brasolin <paolo.brasolin@eurac.edu> Date: Wed, 23 Feb 2022 15:19:43 +0100 Subject: [PATCH] feat: proper handling of API schemas --- backend/package-lock.json | 35 +++++++++++++++++++++++++++++++---- backend/package.json | 1 + backend/src/index.ts | 19 +++++++++++++------ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/backend/package-lock.json b/backend/package-lock.json index e98f988..11f5a72 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "fastify": "^3.27.1", "fastify-cors": "^6.0.2", + "json-schema-to-ts": "^1.6.5", "knex": "^1.0.3" }, "devDependencies": { @@ -1219,8 +1220,7 @@ "node_modules/@types/json-schema": { "version": "7.0.9", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", - "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", - "dev": true + "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==" }, "node_modules/@types/node": { "version": "16.11.25", @@ -4889,6 +4889,15 @@ "dev": true, "optional": true }, + "node_modules/json-schema-to-ts": { + "version": "1.6.5", + "resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-1.6.5.tgz", + "integrity": "sha512-BLUdzgz50XV+NMrSg8+KrUvV7Oh1eb/kLsyPbkWXFTXTloWkAsq7MbAppibE+DyMy4PasS/7bFQPwTIHx35r+A==", + "dependencies": { + "@types/json-schema": "^7.0.6", + "ts-toolbelt": "^6.15.5" + } + }, "node_modules/json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", @@ -7134,6 +7143,11 @@ "node": ">=0.4.0" } }, + "node_modules/ts-toolbelt": { + "version": "6.15.5", + "resolved": "https://registry.npmjs.org/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz", + "integrity": "sha512-FZIXf1ksVyLcfr7M317jbB67XFJhOO1YqdTcuGaq9q5jLUoTikukZ+98TPjKiP2jC5CgmYdWWYs0s2nLSU0/1A==" + }, "node_modules/tslib": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", @@ -8576,8 +8590,7 @@ "@types/json-schema": { "version": "7.0.9", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", - "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", - "dev": true + "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==" }, "@types/node": { "version": "16.11.25", @@ -11376,6 +11389,15 @@ "dev": true, "optional": true }, + "json-schema-to-ts": { + "version": "1.6.5", + "resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-1.6.5.tgz", + "integrity": "sha512-BLUdzgz50XV+NMrSg8+KrUvV7Oh1eb/kLsyPbkWXFTXTloWkAsq7MbAppibE+DyMy4PasS/7bFQPwTIHx35r+A==", + "requires": { + "@types/json-schema": "^7.0.6", + "ts-toolbelt": "^6.15.5" + } + }, "json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", @@ -13068,6 +13090,11 @@ } } }, + "ts-toolbelt": { + "version": "6.15.5", + "resolved": "https://registry.npmjs.org/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz", + "integrity": "sha512-FZIXf1ksVyLcfr7M317jbB67XFJhOO1YqdTcuGaq9q5jLUoTikukZ+98TPjKiP2jC5CgmYdWWYs0s2nLSU0/1A==" + }, "tslib": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", diff --git a/backend/package.json b/backend/package.json index a5037e3..b36f087 100644 --- a/backend/package.json +++ b/backend/package.json @@ -20,6 +20,7 @@ "dependencies": { "fastify": "^3.27.1", "fastify-cors": "^6.0.2", + "json-schema-to-ts": "^1.6.5", "knex": "^1.0.3" }, "devDependencies": { diff --git a/backend/src/index.ts b/backend/src/index.ts index fcd5244..5d00344 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -1,6 +1,7 @@ import fastify from "fastify"; import fastifyCors from "fastify-cors"; import { connection } from "./db"; +import { FromSchema } from "json-schema-to-ts"; const server = fastify({ logger: false, @@ -51,11 +52,9 @@ server.route({ }, }); -server.route({ - method: "POST", - url: "/CheckTranscription", - schema: { - body: { +//=[ CheckTranscription ]======================================================= + +const CheckTranscriptionBodySchema = { type: "object", properties: { deltaTime: { type: "number" }, @@ -65,10 +64,18 @@ server.route({ id: { type: "number" }, image: { type: "string" }, }, + required: ["id", "image"], }, transcription: { type: "string" }, }, - }, + required: ["deltaTime", "refData", "transcription"], +} as const; + +server.route<{ Body: FromSchema<typeof CheckTranscriptionBodySchema> }>({ + method: "POST", + url: "/CheckTranscription", + schema: { + body: CheckTranscriptionBodySchema, response: { 200: { type: "object", -- GitLab