Skip to content

Commit

Permalink
Add Swagger documentation and UI, update API schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
n4ze3m committed Dec 24, 2023
1 parent 6f297c4 commit 3ceb20c
Show file tree
Hide file tree
Showing 18 changed files with 554 additions and 71 deletions.
2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"@fastify/sensible": "^5.0.0",
"@fastify/session": "^10.5.0",
"@fastify/static": "^6.10.2",
"@fastify/swagger": "^8.12.1",
"@fastify/swagger-ui": "^2.0.1",
"@ffmpeg.wasm/core-mt": "^0.12.0",
"@ffmpeg.wasm/main": "^0.12.0",
"@google-ai/generativelanguage": "^2.0.0",
Expand Down
20 changes: 18 additions & 2 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { FastifySSEPlugin } from "@waylaidwanderer/fastify-sse-v2";
import fastifyCookie from "@fastify/cookie";
import fastifySession from "@fastify/session";
import { getSessionSecret, isCookieSecure } from "./utils/session";

import swagger from "@fastify/swagger";
import swaggerUi from "@fastify/swagger-ui";
declare module "fastify" {
interface Session {
is_bot_allowed: boolean;
Expand All @@ -21,7 +22,7 @@ const options: AppOptions = {};

const app: FastifyPluginAsync<AppOptions> = async (
fastify,
opts,
opts
): Promise<void> => {
void fastify.register(cors);

Expand All @@ -34,6 +35,21 @@ const app: FastifyPluginAsync<AppOptions> = async (
},
});

void fastify.register(swagger);

void fastify.register(swaggerUi, {
routePrefix: "/docs",
staticCSP: true,
transformStaticCSP: (header: string) => header,
theme: {
title: "Dialoqbots API Docs",
},
uiConfig: {
docExpansion: "none",
withCredentials: true,
},
});

void fastify.register(AutoLoad, {
dir: join(__dirname, "plugins"),
options: opts,
Expand Down
47 changes: 46 additions & 1 deletion server/src/routes/api/v1/admin/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { FastifySchema } from "fastify";

export const dialoqbaseSettingsSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to get dialoqbase settings",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
response: {
200: {
noOfBotsPerUser: { type: "number" },
Expand All @@ -11,6 +20,15 @@ export const dialoqbaseSettingsSchema: FastifySchema = {
};

export const updateDialoqbaseSettingsSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to update dialoqbase settings",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
body: {
type: "object",
properties: {
Expand All @@ -35,6 +53,15 @@ export const updateDialoqbaseSettingsSchema: FastifySchema = {
};

export const getAllUsersSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to get all users",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
response: {
200: {
type: "array",
Expand All @@ -54,6 +81,15 @@ export const getAllUsersSchema: FastifySchema = {
};

export const resetUserPasswordByAdminSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to reset user password by admin",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
body: {
type: "object",
properties: {
Expand All @@ -73,6 +109,15 @@ export const resetUserPasswordByAdminSchema: FastifySchema = {
};

export const registerUserByAdminSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to register user by admin",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
body: {
type: "object",
properties: {
Expand All @@ -90,4 +135,4 @@ export const registerUserByAdminSchema: FastifySchema = {
},
},
},
};
};
39 changes: 38 additions & 1 deletion server/src/routes/api/v1/admin/schema/model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { FastifySchema } from "fastify";

export const getAllModelsSchema: FastifySchema = {};
export const getAllModelsSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to get all models",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
};

export const fetchModelFromInputedUrlSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to fetch avialable model from inputed url",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
body: {
type: "object",
properties: {
Expand All @@ -13,6 +32,15 @@ export const fetchModelFromInputedUrlSchema: FastifySchema = {
};

export const saveModelFromInputedUrlSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to save model from inputed url",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
body: {
type: "object",
properties: {
Expand All @@ -26,6 +54,15 @@ export const saveModelFromInputedUrlSchema: FastifySchema = {
};

export const toogleModelSchema: FastifySchema = {
tags: ["Admin"],
summary: "API to toogle model",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
body: {
type: "object",
properties: {
Expand Down
18 changes: 18 additions & 0 deletions server/src/routes/api/v1/bot/appearance/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { FastifySchema } from "fastify";

export const getBotAppearanceByIdSchema: FastifySchema = {
tags: ["Bot", "Appearance"],
summary: "API to get bot appearance by id",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
params: {
type: "object",
properties: {
Expand All @@ -12,6 +21,15 @@ export const getBotAppearanceByIdSchema: FastifySchema = {
};

export const saveBotAppearanceSchema: FastifySchema = {
tags: ["Bot", "Appearance"],
summary: "API to save bot appearance by id",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
params: {
type: "object",
properties: {
Expand Down
50 changes: 33 additions & 17 deletions server/src/routes/api/v1/bot/conversations/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { FastifySchema } from "fastify";
import { CHANNELS } from "../../../../../../utils/intergation";

export const getChatHistoryByTypeSchema: FastifySchema = {
tags: ["Bot", "Conversation"],
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
summary: "Get chat history by intergation type",
params: {
type: "object",
required: ["id", "type"],
Expand All @@ -17,23 +26,30 @@ export const getChatHistoryByTypeSchema: FastifySchema = {
},
};



export const getChatHistoryByChatIdSchema: FastifySchema = {
params: {
type: "object",
required: ["id", "type", "chat_id"],
properties: {
id: {
type: "string",
},
type: {
type: "string",
enum: CHANNELS,
},
chat_id: {
type: "string",
}
},
tags: ["Bot", "Conversation"],
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
summary: "Get chat history by chat id",
params: {
type: "object",
required: ["id", "type", "chat_id"],
properties: {
id: {
type: "string",
},
type: {
type: "string",
enum: CHANNELS,
},
chat_id: {
type: "string",
},
},
},
};
46 changes: 44 additions & 2 deletions server/src/routes/api/v1/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
refreshSourceByIdHandler,
updateBotByIdHandler,
getCreateBotConfigHandler,
getBotByIdSettingsHandler
getBotByIdSettingsHandler,
} from "./handlers";
import {
addNewSourceByIdSchema,
Expand Down Expand Up @@ -118,6 +118,17 @@ const root: FastifyPluginAsync = async (fastify, _): Promise<void> => {
"/",
{
onRequest: [fastify.authenticate],
schema: {
tags: ["Bot"],
summary: "API to get all bots",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
},
},
getAllBotsHandler
);
Expand All @@ -127,6 +138,17 @@ const root: FastifyPluginAsync = async (fastify, _): Promise<void> => {
"/upload",
{
onRequest: [fastify.authenticate],
schema: {
tags: ["Bot"],
summary: "API to upload pdf",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
},
},
createBotFileHandler
);
Expand All @@ -136,12 +158,32 @@ const root: FastifyPluginAsync = async (fastify, _): Promise<void> => {
"/:id/source/upload",
{
onRequest: [fastify.authenticate],
schema: {
tags: ["Bot"],
summary: "API to upload pdf",
headers: {
type: "object",
properties: {
Authorization: { type: "string" },
},
required: ["Authorization"],
},
},
},
addNewSourceFileByIdHandler
);

// get bot config
fastify.get("/config", getCreateBotConfigHandler);
fastify.get(
"/config",
{
schema: {
tags: ["Bot"],
summary: "API to get bot configuraion",
},
},
getCreateBotConfigHandler
);

// get bot settings by id
fastify.get(
Expand Down
Loading

0 comments on commit 3ceb20c

Please sign in to comment.