Skip to content

Commit 1ed327c

Browse files
committed
fixes
Signed-off-by: Kirill Mokevnin <[email protected]>
1 parent 49253eb commit 1ed327c

File tree

21 files changed

+255
-418
lines changed

21 files changed

+255
-418
lines changed

app.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from 'node:path';
22
import type { AutoloadPluginOptions } from '@fastify/autoload';
33
import AutoLoad from '@fastify/autoload';
4-
import { ValiError } from 'valibot'
54
import type { FastifyPluginAsync, FastifyServerOptions } from 'fastify';
65
import glue from 'fastify-openapi-glue';
6+
import { ValiError } from 'valibot';
77
import serviceHandlers from './routes/index.ts';
88

99
export interface AppOptions
@@ -20,25 +20,31 @@ const app: FastifyPluginAsync<AppOptions> = async (
2020
fastify.setErrorHandler((error, _request, reply) => {
2121
if (error instanceof ValiError) {
2222
const errors = error.issues.map((issue) => {
23-
const lastPathItem = issue.path && issue.path.length > 0 ? issue.path[issue.path.length - 1] : undefined
24-
const field = lastPathItem && 'key' in lastPathItem && lastPathItem.key != null ? String(lastPathItem.key) : ''
23+
const lastPathItem =
24+
issue.path && issue.path.length > 0
25+
? issue.path[issue.path.length - 1]
26+
: undefined;
27+
const field =
28+
lastPathItem && 'key' in lastPathItem && lastPathItem.key != null
29+
? String(lastPathItem.key)
30+
: '';
2531
return {
2632
message: issue.message ?? 'Validation error',
2733
rule: String(issue.type ?? 'validation'),
2834
field,
29-
}
30-
})
35+
};
36+
});
3137
const errorDetail = {
3238
status: 422,
3339
title: 'Validation Error',
3440
detail: 'Errors related to business logic such as uniqueness',
3541
errors,
36-
}
37-
reply.type('application/problem+json').code(422).send(errorDetail)
42+
};
43+
reply.type('application/problem+json').code(422).send(errorDetail);
3844
} else {
39-
reply.send(error)
45+
reply.send(error);
4046
}
41-
})
47+
});
4248

4349
fastify.addContentTypeParser(
4450
'application/problem+json',

biome.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
},
88
"files": {
99
"ignoreUnknown": false,
10-
"includes": [
11-
"**",
12-
"!test/**",
13-
"!public/**",
14-
"!types/handlers/**"
15-
]
10+
"includes": ["**", "!test/**", "!public/**", "!types/handlers/**"]
1611
},
1712
"formatter": {
1813
"enabled": true,

db/seeds.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
import { buildCourse, buildCourseLesson, buildUser } from '../lib/data.ts'
2-
import type { DrizzleDB } from '../types/index.ts'
3-
import * as schemas from './schema.ts'
1+
import { buildCourse, buildCourseLesson, buildUser } from '../lib/data.ts';
2+
import type { DrizzleDB } from '../types/index.ts';
3+
import * as schemas from './schema.ts';
44
/**
55
* @param {import("drizzle-orm/better-sqlite3").BetterSQLite3Database<typeof schemas>} db
66
*/
77
export default async (db: DrizzleDB) => {
8-
const [_user1] = await db.insert(schemas.users).values(buildUser()).returning()
9-
const [user2] = await db.insert(schemas.users).values(buildUser()).returning()
10-
const [_user3] = await db.insert(schemas.users).values(buildUser({
11-
email: '[email protected]', fullName: 'Тото Поддерживающий',
12-
})).returning()
13-
const [_course1] = await db.insert(schemas.courses).values(
14-
buildCourse({ creatorId: user2.id }),
15-
).returning()
16-
const [course2] = await db.insert(schemas.courses).values(
17-
buildCourse({ creatorId: user2.id }),
18-
).returning()
19-
await db.insert(schemas.courseLessons).values(
20-
buildCourseLesson({ courseId: course2.id }),
21-
)
22-
}
8+
const [_user1] = await db
9+
.insert(schemas.users)
10+
.values(buildUser())
11+
.returning();
12+
const [user2] = await db
13+
.insert(schemas.users)
14+
.values(buildUser())
15+
.returning();
16+
const [_user3] = await db
17+
.insert(schemas.users)
18+
.values(
19+
buildUser({
20+
21+
fullName: 'Тото Поддерживающий',
22+
}),
23+
)
24+
.returning();
25+
const [_course1] = await db
26+
.insert(schemas.courses)
27+
.values(buildCourse({ creatorId: user2.id }))
28+
.returning();
29+
const [course2] = await db
30+
.insert(schemas.courses)
31+
.values(buildCourse({ creatorId: user2.id }))
32+
.returning();
33+
await db
34+
.insert(schemas.courseLessons)
35+
.values(buildCourseLesson({ courseId: course2.id }));
36+
};

drizzle.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineConfig } from "drizzle-kit";
1+
import { defineConfig } from 'drizzle-kit';
22

33
export default defineConfig({
4-
dialect: "sqlite",
5-
schema: "./db/schema.ts",
4+
dialect: 'sqlite',
5+
schema: './db/schema.ts',
66
// out: "./drizzle",
77
});

drizzle/meta/0000_snapshot.json

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@
5050
"name": "course_lessons_courseId_courses_id_fk",
5151
"tableFrom": "course_lessons",
5252
"tableTo": "courses",
53-
"columnsFrom": [
54-
"courseId"
55-
],
56-
"columnsTo": [
57-
"id"
58-
],
53+
"columnsFrom": ["courseId"],
54+
"columnsTo": ["id"],
5955
"onDelete": "no action",
6056
"onUpdate": "no action"
6157
}
@@ -109,12 +105,8 @@
109105
"name": "courses_creator_id_users_id_fk",
110106
"tableFrom": "courses",
111107
"tableTo": "users",
112-
"columnsFrom": [
113-
"creator_id"
114-
],
115-
"columnsTo": [
116-
"id"
117-
],
108+
"columnsFrom": ["creator_id"],
109+
"columnsTo": ["id"],
118110
"onDelete": "no action",
119111
"onUpdate": "no action"
120112
}
@@ -165,9 +157,7 @@
165157
"indexes": {
166158
"users_email_unique": {
167159
"name": "users_email_unique",
168-
"columns": [
169-
"email"
170-
],
160+
"columns": ["email"],
171161
"isUnique": true
172162
}
173163
},
@@ -185,4 +175,4 @@
185175
"internal": {
186176
"indexes": {}
187177
}
188-
}
178+
}

drizzle/meta/_journal.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
"breakpoints": true
1111
}
1212
]
13-
}
13+
}

lib/utils.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
import openapiConst from '../tsp-output/@typespec/openapi3/openapi.v2.json' with { type: 'json' }
1+
import type { FastifyReply } from 'fastify';
2+
import openapiConst from '../tsp-output/@typespec/openapi3/openapi.v2.json' with {
3+
type: 'json',
4+
};
25

36
export function getPagingOptions(page: number, perPage = 10) {
47
return {
58
limit: perPage,
69
offset: (page - 1) * perPage,
7-
}
10+
};
811
}
912

1013
export function openapi() {
11-
return openapiConst
14+
return openapiConst;
15+
}
16+
17+
export function ensure<T>(
18+
reply: FastifyReply,
19+
value: T | null | undefined,
20+
status: number = 404,
21+
msg?: string,
22+
): asserts value is NonNullable<T> {
23+
const m = msg || 'Not Found';
24+
if (value == null) reply.server.httpErrors.createError(status, m);
1225
}

plugins/jwt.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
import jwtPlugin from '@fastify/jwt'
2-
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
3-
import fp from 'fastify-plugin'
1+
import jwtPlugin from '@fastify/jwt';
2+
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
3+
import fp from 'fastify-plugin';
44

55
export default fp(async (fastify: FastifyInstance) => {
66
fastify.register(jwtPlugin, {
77
secret: 'supersecret',
8-
})
9-
fastify.decorate('authenticate', async function (this: FastifyInstance, request: FastifyRequest, reply: FastifyReply) {
10-
try {
11-
await request.jwtVerify()
12-
}
13-
catch (err) {
14-
reply.send(err)
15-
}
16-
})
17-
})
8+
});
9+
fastify.decorate(
10+
'authenticate',
11+
async function (
12+
this: FastifyInstance,
13+
request: FastifyRequest,
14+
reply: FastifyReply,
15+
) {
16+
try {
17+
await request.jwtVerify();
18+
} catch (err) {
19+
reply.send(err);
20+
}
21+
},
22+
);
23+
});

plugins/response-validation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import responseValidation from '@fastify/response-validation'
2-
import fp from 'fastify-plugin'
1+
import responseValidation from '@fastify/response-validation';
2+
import fp from 'fastify-plugin';
33

44
export default fp(async (fastify) => {
5-
await fastify.register(responseValidation)
6-
})
5+
await fastify.register(responseValidation);
6+
});

plugins/routes.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)