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 all 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
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 auth session generated',
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
69 changes: 62 additions & 7 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 @@ -101,6 +101,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
response: {
'2xx': {
type: 'object',
description: 'Fetch all information of the notePublicId passed',
properties: {
note: {
$ref: 'NoteSchema',
Expand Down Expand Up @@ -202,6 +203,17 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
$ref: 'NoteSchema#/properties/id',
},
},

response: {
'2xx': {
type: 'object',
properties: {
isDeleted: {
type: 'boolean',
},
},
},
},
},
config: {
policy: [
Expand Down Expand Up @@ -240,6 +252,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 response',
content: {
'application/json':{
schema: {
id: {
$ref: 'NoteSchema#/properties/id',
},
},
},
},
},
},
},
}, async (request, reply) => {
/**
* @todo Validate request query
Expand Down Expand Up @@ -291,6 +327,13 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
updatedAt: Note['updatedAt'],
}
}>('/:notePublicId', {
config: {
policy: [
'authRequired',
'userCanEdit',
],
},

schema: {
params: {
notePublicId: {
Expand All @@ -299,15 +342,20 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
},
body: {
content: {
$ref: 'NoteSchema#/properties/content',
type: 'object',
},
},

response: {
'2xx': {
description: 'Updated timestamp',
properties: {
updatedAt: {
type: 'string',
},
},
},
},
},
config: {
policy: [
'authRequired',
'userCanEdit',
],
},
preHandler: [
noteResolver,
Expand Down Expand Up @@ -352,6 +400,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
response: {
'2xx': {
type: 'object',
description: 'Updated note',
properties: {
isUpdated: {
type: 'boolean',
Expand Down Expand Up @@ -456,9 +505,15 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
}| ErrorResponse,
}>('/resolve-hostname/:hostname', {
schema: {
params: {
hostname: {
type: 'string',
},
},
response: {
'2xx': {
type: 'object',
description: 'Custom hostname response',
properties: {
note: {
$ref: 'NoteSchema',
Expand Down
15 changes: 15 additions & 0 deletions src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
maximum: 30,
},
},

response: {
'2xx':{
description: 'Query notelist',
properties: {
items: {
id: { type: 'string' },
content: { type: 'string' },
createdAt: { type: 'string' },
creatorId: { type: 'string' },
updatedAt: { type: 'string' },
},
},
},
},
},
}, async (request, reply) => {
const userId = request.userId as number;
Expand Down
48 changes: 47 additions & 1 deletion 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,27 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
$ref: 'NoteSchema#/properties/id',
},
},
body: {
properties: {
userId: {
$ref: 'UserSchema#/properties/id',
},
newRole:{
$ref: 'NoteSettingsSchema#/properties/team/items/properties/role',
},
},
},

response: {
'2xx': {
type: 'number',
properties: {
newRole: {
type: 'number',
},
},
},
},
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
},
preHandler: [
noteResolver,
Expand All @@ -134,6 +155,7 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
return reply.notFound('User does not belong to Note\'s team');
}


return reply.send(newRole);
});

Expand Down Expand Up @@ -196,7 +218,7 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
* Patch noteSettings by note id
*/
fastify.patch<{
Body: Partial<NoteSettings>,
Body: Pick<NoteSettings, 'customHostname' | 'isPublic'>,
Params: {
notePublicId: NotePublicId;
},
Expand All @@ -214,6 +236,17 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
$ref: 'NoteSchema#/properties/id',
},
},
body: {
properties: {
customHostname: {
type: 'string',
},

isPublic: {
type: 'boolean',
},
},
},
response: {
'2xx': {
$ref: 'NoteSettingsSchema',
Expand Down Expand Up @@ -266,6 +299,19 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
$ref: 'NoteSchema#/properties/id',
},
},

response: {
'2xx': {
type: 'array',
description: 'Fetch all teams associated with the notePublicId',
properties: {
id: { type: 'string' },
noteId: { type: 'string' },
role: { type: 'string' },
userId: { type: 'string' },
},
},
},
},
preHandler: [
noteResolver,
Expand Down
13 changes: 12 additions & 1 deletion src/presentation/http/router/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ 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',
},
},
},
}, async (request, reply) => {
/**
* Get referer from request headers
*/
Expand Down
9 changes: 9 additions & 0 deletions src/presentation/http/router/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ const UploadRouter: FastifyPluginCallback<UploadRouterOptions> = async (fastify,
$ref: 'UploadSchema#/properties/key',
},
},

response: {
'2xx': {
description: 'Generated buffer',
properties: {
fileData: { type: 'string' },
},
},
},
},
preHandler: [ noteResolver ],
}, async (request, reply) => {
Expand Down
Loading
Loading