Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response schema to all API methods #247

Merged
merged 17 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ module.exports = {
'selector': 'property',
'format': ['camelCase', 'PascalCase'],
'filter': {
// Allow "2xx" as a property name, used in the API response schema
'regex': '^(2xx|2\d{2}|application\/json)$',
// Allow "2xx", "3xx", "5xx" as a property name, used in the API response schema
elizachi marked this conversation as resolved.
Show resolved Hide resolved
'regex': '^(2xx|2\d{2}|5xx|3xx|application\/json)$',
'match': false,
},
},
Expand Down
6 changes: 4 additions & 2 deletions src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import EditorToolsRouter from './router/editorTools.js';
import { UserSchema } from './schema/User.js';
import { NoteSchema } from './schema/Note.js';
import { NoteSettingsSchema } from './schema/NoteSettings.js';
import { OauthSchema } from './schema/OauthSchema.js';
import Policies from './policies/index.js';
import type { RequestParams, Response } from '@presentation/api.interface.js';
import NoteSettingsRouter from './router/noteSettings.js';
Expand Down Expand Up @@ -60,9 +61,9 @@ export default class HttpApi implements Api {
this.server = fastify({
logger: appServerLogger as FastifyBaseLogger,
ajv: {
plugins: [
plugins: [
/** Allows to validate files in schema */
ajvFilePlugin
ajvFilePlugin,
],
},
});
Expand Down Expand Up @@ -300,6 +301,7 @@ export default class HttpApi implements Api {
this.server?.addSchema(NoteSettingsSchema);
this.server?.addSchema(JoinSchemaParams);
this.server?.addSchema(JoinSchemaResponse);
this.server?.addSchema(OauthSchema);
this.server?.addSchema(UploadSchema);
}

Expand Down
84 changes: 67 additions & 17 deletions src/presentation/http/router/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,85 @@ const AuthRouter: FastifyPluginCallback<AuthRouterOptions> = (fastify, opts, don
fastify.post<{
Body: AuthOptions;
Reply: AuthSession | ErrorResponse;
}>('/', async (request, reply) => {
const { token } = request.body;
const userSession = await opts.authService.verifyRefreshToken(token);
}>('/',
{
schema: {
body: {
required: [ 'token' ],
properties: {
token: {
type: 'string',
},
},
},

/**
* Check if session is valid
*/
if (!userSession) {
return await reply.unauthorized('Session is not valid');
}
response: {
elizachi marked this conversation as resolved.
Show resolved Hide resolved
'2xx': {
description: 'New token generated',
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
content: {
'application/json': {
schema: {
accessToken: { type: 'string' },
refreshToken: { type: 'string' },
},
},
},
},
},
},
},
async (request, reply) => {
const { token } = request.body;
const userSession = await opts.authService.verifyRefreshToken(token);

const accessToken = opts.authService.signAccessToken({ id: userSession.userId });
/**
* Check if session is valid
*/
if (!userSession) {
return await reply.unauthorized('Session is not valid');
}

await opts.authService.removeSessionByRefreshToken(token);
const refreshToken = await opts.authService.signRefreshToken(userSession.userId);
const accessToken = opts.authService.signAccessToken({ id: userSession.userId });

return reply.send({
accessToken,
refreshToken,
await opts.authService.removeSessionByRefreshToken(token);
const refreshToken = await opts.authService.signRefreshToken(userSession.userId);

return reply.send({
accessToken,
refreshToken,
});
});
});

/**
* Route for logout, removes session from database by refresh token
*/
fastify.delete<{
Body: AuthOptions;
Reply: { ok: boolean }
}>('/', async (request, reply) => {
}>('/', {
schema: {
body: {
token: {
type: 'string',
},
},

response: {
elizachi marked this conversation as resolved.
Show resolved Hide resolved
'2xx': {
description: 'Check for successful deletion of the token',
content: {
'application/json': {
schema:{
ok: {
type: 'boolean',
},
},
},
},
},
},
},
}, async (request, reply) => {
await opts.authService.removeSessionByRefreshToken(request.body.token);

return reply.status(StatusCodes.OK).send({
Expand Down
24 changes: 24 additions & 0 deletions src/presentation/http/router/note.ts
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,30 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
'authRequired',
],
},
schema: {
body: {
content: {
$ref: 'NoteSchema#/properties/content',
},
parentId: {
$ref: 'NoteSchema#/properties/id',
},
},
response: {
'2xx': {
description: 'Note fields',
content: {
'application/json':{
schema: {
id: {
$ref: 'NoteSchema#/properties/id',
},
},
},
},
},
},
},
}, async (request, reply) => {
/**
* @todo Validate request query
Expand Down
8 changes: 8 additions & 0 deletions src/presentation/http/router/noteSettings.ts
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
$ref: 'NoteSchema#/properties/id',
},
},
body: {
userId: {
$ref: 'NoteSettingsSchema#/properties/team/items/properties/id',
},
newRole:{
$ref: 'NoteSettingsSchema#/properties/team/items/properties/role',
},
},
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
},
preHandler: [
noteResolver,
Expand Down
43 changes: 42 additions & 1 deletion src/presentation/http/router/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,48 @@ const OauthRouter: FastifyPluginCallback<OauthRouterOptions> = (fastify, opts, d
/**
* Callback for Google oauth2. Google redirects to this endpoint after user authentication.
*/
fastify.get('/google/callback', async (request, reply) => {
fastify.get('/google/callback', {
schema: {
params: {
clientId: {
$ref: 'OauthSchema#/properties/clientId',
},
clientSecret: {
$ref: 'OauthSchema#/properties/clientSecret',
},
},

response:{
'2xx': {
elizachi marked this conversation as resolved.
Show resolved Hide resolved
accessToken: {
$ref: 'OauthSchema#/properties/accessToken',
},
refreshToken: {
$ref: 'OauthSchema#/properties/refreshToken',
},
},

'3xx': {
code: {
type: 'string',
},

message: {
type: 'string',
},
},

// '5xx': {
// code: {
// type: 'string',
// },
// message: {
// type: 'string',
// },
// },
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
},
},
}, async (request, reply) => {
/**
* Get referer from request headers
*/
Expand Down
8 changes: 3 additions & 5 deletions src/presentation/http/router/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ const UserRouter: FastifyPluginCallback<UserRouterOptions> = (fastify, opts, don
schema: {
body: {
toolId: {
type: 'string',
description: 'Unique editor tool id',
$ref: 'EditorToolSchema#/properties/id',
},
},
response: {
Expand Down Expand Up @@ -162,8 +161,7 @@ const UserRouter: FastifyPluginCallback<UserRouterOptions> = (fastify, opts, don
schema: {
body: {
toolId: {
type: 'string',
description: 'Unique editor tool id',
$ref: 'EditorToolSchema#/properties/id',
},
},
response: {
Expand All @@ -173,7 +171,7 @@ const UserRouter: FastifyPluginCallback<UserRouterOptions> = (fastify, opts, don
'application/json': {
schema: {
removedId: {
type: 'string',
$ref: 'EditorToolSchema#/properties/id',
},
},
},
Expand Down
1 change: 1 addition & 0 deletions src/presentation/http/schema/Join.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const JoinSchemaParams = {
$id: 'JoinSchemaParams',
type: 'object',
required: [ 'hash' ],
properties: {
hash: {
type: 'string',
Expand Down
30 changes: 30 additions & 0 deletions src/presentation/http/schema/OauthSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

export const OauthSchema = {
$id: 'OauthSchema',
type: 'object',
required: [
'clientId',
'clientSecret',
],
properties: {
clientId: {
type: 'string',
description: 'Google client id token',
},

clientSecret: {
type: 'string',
description: 'Google client secret key',
},

accessToken: {
type: 'string',
description: 'The returned access token from Google',
},

refreshToken: {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
type: 'string',
description: 'The returned refreshtoken from Google',
},
},
};
1 change: 1 addition & 0 deletions src/presentation/http/schema/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export const UserSchema = {
$id: 'UserSchema',
type: 'object',
required: [ 'email' ],
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
properties: {
id: { type: 'number' },
email: { type: 'string' },
Expand Down
Loading