Skip to content

Commit

Permalink
feat(api): fastify typebox integration (#314)
Browse files Browse the repository at this point in the history
* refactor(api): add bff types

* refactor(api): refactor typebox types

* feat(api): add findById integration

* refactor(api): remove /workspace

* refactor(api): add schema types

* feat(api): add name update api

* feat(api): add update project api
  • Loading branch information
riccardoperra committed Aug 3, 2022
1 parent c103dea commit 44cd83c
Show file tree
Hide file tree
Showing 38 changed files with 1,020 additions and 680 deletions.
9 changes: 9 additions & 0 deletions apps/api/api-types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export namespace ApiTypes {
export {
GetProjectByIdApi,
DeleteProjectApi,
CreateProjectApi,
UpdateProjectNameApi,
UpdateProjectApi,
} from '../src/schemas';
}
29 changes: 18 additions & 11 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"directories": {
"test": "test"
},
"exports": {
"./api-types": {
"types": "./index.d.ts"
}
},
"scripts": {
"test": "npm run build:ts && tsc -p test/tsconfig.json && tap --ts './test/**/*.test.ts'",
"start": "npm run build:ts && fastify start -l info dist/app.js",
Expand All @@ -21,19 +26,21 @@
"license": "ISC",
"dependencies": {
"@codeimage/prisma-models": "workspace:*",
"@fastify/autoload": "^5.0.0",
"@fastify/env": "4.0.0",
"@fastify/sensible": "^4.1.0",
"@fastify/swagger": "7.4.1",
"@fastify/autoload": "^5.1.0",
"@fastify/env": "^4.0.0",
"@fastify/sensible": "^5.1.0",
"@fastify/swagger": "^7.4.1",
"@fastify/type-provider-typebox": "^2.2.0",
"@prisma/client": "^4.1.1",
"dotenv-cli": "6.0.0",
"fastify": "^4.0.0",
"@sinclair/typebox": "^0.24.26",
"dotenv-cli": "^6.0.0",
"fastify": "^4.3.0",
"fastify-cli": "^4.4.0",
"fastify-plugin": "^3.0.0",
"fluent-json-schema": "3.1.0",
"graphql": "16.5.0",
"mercurius": "10.1.0",
"mercurius-codegen": "4.0.1",
"fastify-plugin": "^4.0.0",
"fluent-json-schema": "^3.1.0",
"graphql": "^16.5.0",
"mercurius": "^10.1.0",
"mercurius-codegen": "^4.0.1",
"prisma": "4.1.1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Warnings:
- You are about to drop the column `projectId` on the `SnippetEditorOptions` table. All the data in the column will be lost.
- You are about to drop the column `projectId` on the `SnippetFrame` table. All the data in the column will be lost.
- You are about to drop the column `projectId` on the `SnippetTerminal` table. All the data in the column will be lost.
- A unique constraint covering the columns `[frameId]` on the table `Project` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[terminalId]` on the table `Project` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[editorOptionsId]` on the table `Project` will be added. If there are existing duplicate values, this will fail.
- Added the required column `editorOptionsId` to the `Project` table without a default value. This is not possible if the table is not empty.
- Added the required column `frameId` to the `Project` table without a default value. This is not possible if the table is not empty.
- Added the required column `terminalId` to the `Project` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "SnippetEditorOptions" DROP CONSTRAINT "SnippetEditorOptions_projectId_fkey";

-- DropForeignKey
ALTER TABLE "SnippetFrame" DROP CONSTRAINT "SnippetFrame_projectId_fkey";

-- DropForeignKey
ALTER TABLE "SnippetTerminal" DROP CONSTRAINT "SnippetTerminal_projectId_fkey";

-- DropIndex
DROP INDEX "SnippetEditorOptions_projectId_key";

-- DropIndex
DROP INDEX "SnippetFrame_projectId_key";

-- DropIndex
DROP INDEX "SnippetTerminal_projectId_key";

-- AlterTable
ALTER TABLE "Project" ADD COLUMN "editorOptionsId" TEXT NOT NULL,
ADD COLUMN "frameId" TEXT NOT NULL,
ADD COLUMN "terminalId" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "SnippetEditorOptions" DROP COLUMN "projectId";

-- AlterTable
ALTER TABLE "SnippetFrame" DROP COLUMN "projectId";

-- AlterTable
ALTER TABLE "SnippetTerminal" DROP COLUMN "projectId";

-- CreateIndex
CREATE UNIQUE INDEX "Project_frameId_key" ON "Project"("frameId");

-- CreateIndex
CREATE UNIQUE INDEX "Project_terminalId_key" ON "Project"("terminalId");

-- CreateIndex
CREATE UNIQUE INDEX "Project_editorOptionsId_key" ON "Project"("editorOptionsId");

-- AddForeignKey
ALTER TABLE "Project" ADD CONSTRAINT "Project_frameId_fkey" FOREIGN KEY ("frameId") REFERENCES "SnippetFrame"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Project" ADD CONSTRAINT "Project_terminalId_fkey" FOREIGN KEY ("terminalId") REFERENCES "SnippetTerminal"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Project" ADD CONSTRAINT "Project_editorOptionsId_fkey" FOREIGN KEY ("editorOptionsId") REFERENCES "SnippetEditorOptions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- A unique constraint covering the columns `[id,projectId]` on the table `SnippetEditorTab` will be added. If there are existing duplicate values, this will fail.
*/
-- DropIndex
DROP INDEX "SnippetEditorTab_projectId_key";

-- CreateIndex
CREATE UNIQUE INDEX "SnippetEditorTab_id_projectId_key" ON "SnippetEditorTab"("id", "projectId");
36 changes: 19 additions & 17 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ model User {
}

model Project {
id String @id @default(uuid())
name String
frame SnippetFrame?
terminal SnippetTerminal?
editorOptions SnippetEditorOptions?
editorTabs SnippetEditorTab[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
id String @id @default(uuid())
name String
frame SnippetFrame @relation(fields: [frameId], references: [id])
terminal SnippetTerminal @relation(fields: [terminalId], references: [id])
editorOptions SnippetEditorOptions @relation(fields: [editorOptionsId], references: [id])
frameId String @unique
terminalId String @unique
editorOptionsId String @unique
editorTabs SnippetEditorTab[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
@@unique([id, userId])
@@index([name, userId])
}

model SnippetFrame {
id String @id @default(uuid())
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId String @unique
project Project?
background String?
padding Int?
radius Int?
Expand All @@ -47,8 +49,7 @@ model SnippetFrame {

model SnippetTerminal {
id String @id @default(uuid())
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId String @unique
project Project?
showHeader Boolean?
type String?
accentVisible Boolean?
Expand All @@ -63,8 +64,7 @@ model SnippetTerminal {

model SnippetEditorOptions {
id String @id @default(uuid())
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId String @unique
project Project?
fontId String?
fontWeight Int?
showLineNumbers Boolean?
Expand All @@ -74,8 +74,10 @@ model SnippetEditorOptions {
model SnippetEditorTab {
id String @id @default(uuid())
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId String @unique
projectId String
code String?
languageId String?
tabName String?
@@unique([id, projectId])
}
16 changes: 8 additions & 8 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,11 @@ const app: FastifyPluginAsync<AppOptions> = async (
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
void fastify.register(AutoLoad, {
await void fastify.register(AutoLoad, {
dir: join(__dirname, 'plugins'),
options: opts,
});

await fastify.register(AutoLoad, {
dir: join(__dirname, 'modules'),
options: opts,
encapsulate: false,
maxDepth: 1,
});

// This loads all plugins defined in routes
// define your routes in one of these
await fastify.register(AutoLoad, {
Expand All @@ -35,6 +28,13 @@ const app: FastifyPluginAsync<AppOptions> = async (
routeParams: true,
});

await fastify.register(AutoLoad, {
dir: join(__dirname, 'modules'),
options: opts,
encapsulate: false,
maxDepth: 1,
});

fastify.ready(() => fastify.log.info(`\n${fastify.printRoutes()}`));
};

Expand Down
24 changes: 24 additions & 0 deletions apps/api/src/common/types/extract-api-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {Static, TSchema} from '@sinclair/typebox';
import {FastifySchema} from 'fastify';

type StaticSchemaOrType<T> = T extends TSchema ? Static<T> : T;

type GetApiRequest<T extends FastifySchema> = {
body?: StaticSchemaOrType<T['body']>;
querystring?: StaticSchemaOrType<T['querystring']>;
params?: StaticSchemaOrType<T['params']>;
headers?: StaticSchemaOrType<T['headers']>;
};

type GetApiResponse<T extends FastifySchema> = T['response'] extends {
200?: infer U;
}
? U extends TSchema
? Static<U>
: U
: never;

export interface GetApiTypes<T extends FastifySchema> {
request: GetApiRequest<T>;
response: GetApiResponse<T>;
}
1 change: 1 addition & 0 deletions apps/api/src/modules/project/domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './projectCreateRequest';
export * from './projectDeleteRequest';
export * from './projectUpdateRequest';
23 changes: 23 additions & 0 deletions apps/api/src/modules/project/domain/projectUpdateRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
Project,
SnippetEditorOptions,
SnippetEditorTab,
SnippetFrame,
SnippetTerminal,
} from '@codeimage/prisma-models/*';

type IdAndProjectId = 'id' | 'projectId';

export interface ProjectUpdateRequest {
editorOptions: Omit<SnippetEditorOptions, IdAndProjectId>;
terminal: Omit<SnippetTerminal, IdAndProjectId>;
frame: Omit<SnippetFrame, IdAndProjectId>;
editors: SnippetEditorTab[];
}

export type ProjectUpdateResponse = Project & {
editorOptions: SnippetEditorOptions | null;
terminal: SnippetTerminal | null;
frame: SnippetFrame | null;
editorTabs: SnippetEditorTab[];
};
5 changes: 0 additions & 5 deletions apps/api/src/modules/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import {FastifyPluginAsync} from 'fastify';
import {makePrismaProjectRepository} from './infra/prisma/prisma-project.repository';
import {ProjectRepository} from './repository';
import * as projectSchemas from './schema';

export const project: FastifyPluginAsync = async fastify => {
fastify.decorate(
'projectRepository',
makePrismaProjectRepository(fastify.prisma),
);

for (const schema of Object.values(projectSchemas)) {
fastify.addSchema(schema);
}
};

declare module 'fastify' {
Expand Down
Loading

0 comments on commit 44cd83c

Please sign in to comment.