Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Kirill Mokevnin <mokevnin@gmail.com>
  • Loading branch information
mokevnin committed Sep 22, 2024
1 parent 67455c7 commit f4a84e0
Show file tree
Hide file tree
Showing 9 changed files with 998 additions and 27 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ types-to-typebox:

types: types-to-openapi types-to-typebox

mock:
npx prism mock ./tsp-output/@typespec/openapi3/openapi.v1.json

tsp-build:

.PHONY: test routes
18 changes: 9 additions & 9 deletions main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@ import "@typespec/http";
import "@typespec/rest";
import "@typespec/versioning";
import "@typespec/openapi3";
import "@typespec/json-schema";

using TypeSpec.Versioning;
using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.JsonSchema;

@service({
title: "Hexlet Fastify Rest Api Example",
})
@server("https://localhost", "Single server endpoint")
@jsonSchema
@versioned(Versions)
namespace FastifyRestApiExample;

enum Versions {
v1,
v1, v2
}

/**
Expand Down Expand Up @@ -73,6 +69,7 @@ model User {
@minLength(2)
@maxLength(100)
fullName: string | null;
@added(Versions.v2) phone: string;

@format("email")
email: string;
Expand Down Expand Up @@ -129,32 +126,35 @@ model CourseLessonCreateDTO {
}

@route("/users")
@useAuth(BearerAuth)
namespace users {
@get
@useAuth(BearerAuth)
op index(@query page?: numeric = 1): {
@body users: {
data: User[];
};
};

@get
@useAuth(BearerAuth)
op show(@path id: numeric): {
@body user: User;
@body _: User;
} | NotFoundError;

@post
op create(@body user: UserCreateDTO): {
@body user: User;
op create(@body _: UserCreateDTO): {
@body _: User;
@statusCode statusCode: 201;
} | UnprocessableEntityError;

@patch
@useAuth(BearerAuth)
op update(@path id: numeric, @body user: UserEditDTO): {
@body user: User;
} | NotFoundError | UnprocessableEntityError;

@delete
@useAuth(BearerAuth)
op destroy(@path id: numeric): {
@statusCode statusCode: 204;
} | NotFoundError;
Expand Down
Empty file removed policies/Course.js
Empty file.
5 changes: 5 additions & 0 deletions policies/CoursePolicy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class CoursePolicy {
canShowIndex(creatorId, user) {
// Проверка
}
}
4 changes: 2 additions & 2 deletions routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default async function (fastify) {
...getPagingOptions(page, 1),
})

return users
// return users
return { data: users, meta: {} }
})

fastify.get(
Expand All @@ -49,7 +50,6 @@ export default async function (fastify) {
fastify.post(
'/users',
{
onRequest: [fastify.authenticate],
schema: {
body: schema['/users'].POST.args.properties.body,
response: {
Expand Down
2 changes: 1 addition & 1 deletion schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ const _components = {
User: CloneType(ComponentsSchemasUser),
UserCreateDTO: CloneType(ComponentsSchemasUserCreateDto),
UserEditDTO: CloneType(ComponentsSchemasUserEditDto),
Versions: T.Literal('v1')
Versions: T.Union([T.Literal('v1'), T.Literal('v2')])
}
}

Expand Down
5 changes: 5 additions & 0 deletions serializers/UserSerializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class UserSerializer {
index(users) {
return { data: users, meta: {} }
}
}
19 changes: 4 additions & 15 deletions tsp-output/@typespec/openapi3/openapi.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,7 @@
}
}
}
},
"security": [
{
"BearerAuth": []
}
]
}
}
},
"/users/{id}": {
Expand Down Expand Up @@ -962,7 +957,8 @@
"Versions": {
"type": "string",
"enum": [
"v1"
"v1",
"v2"
]
}
},
Expand All @@ -972,12 +968,5 @@
"scheme": "bearer"
}
}
},
"servers": [
{
"url": "https://localhost",
"description": "Single server endpoint",
"variables": {}
}
]
}
}
Loading

0 comments on commit f4a84e0

Please sign in to comment.