From e21da3ddcaad2b0f757a8f156f313eb01327b08c Mon Sep 17 00:00:00 2001 From: OfrenDialsa Date: Sat, 13 Dec 2025 02:13:28 +0700 Subject: [PATCH] minor fix: swagger authorization --- src/Infrastructures/http/createServer.js | 14 +++++- .../http/api/comments/swagger/comments.js | 10 ++++ .../http/api/likes/swagger/likes.js | 24 +++++---- .../http/api/replies/swagger/replies.js | 50 ++++++++++++------- .../http/api/threads/swagger/threads.js | 5 ++ 5 files changed, 73 insertions(+), 30 deletions(-) diff --git a/src/Infrastructures/http/createServer.js b/src/Infrastructures/http/createServer.js index 1396a5d..a9489c9 100644 --- a/src/Infrastructures/http/createServer.js +++ b/src/Infrastructures/http/createServer.js @@ -21,12 +21,23 @@ const comments = require("../../Interfaces/http/api/comments"); const replies = require("../../Interfaces/http/api/replies"); const likes = require("../../Interfaces/http/api/likes"); +let schemes = ["https"]; +if (process.env.NODE_ENV !== "production") { + schemes = ["http", "https"]; +} const swaggerOptions = { info: { title: "Forum API Documentation by Ofren dialsa", version: "1.0.0", }, - schemes: ['https'], + schemes: schemes, + securityDefinitions: { + jwt: { + type: "apiKey", + name: "Authorization", + in: "header", + }, + }, }; const createServer = async (container) => { @@ -87,7 +98,6 @@ const createServer = async (container) => { const { response } = request; if (response instanceof Error) { - const translatedError = DomainErrorTranslator.translate(response); if (translatedError instanceof ClientError) { diff --git a/src/Interfaces/http/api/comments/swagger/comments.js b/src/Interfaces/http/api/comments/swagger/comments.js index 507bb1a..6c99481 100644 --- a/src/Interfaces/http/api/comments/swagger/comments.js +++ b/src/Interfaces/http/api/comments/swagger/comments.js @@ -6,6 +6,11 @@ const postAddComment = { description: "Menambah komentar pada sebuah thread", notes: "User harus login (JWT). Komentar akan ditambahkan ke thread tertentu.", + plugins: { + "hapi-swagger": { + security: [{ jwt: [] }], + }, + }, validate: { params: Joi.object({ @@ -46,6 +51,11 @@ const deleteComment = { tags: ["api", "Comments"], description: "Menghapus komentar dari thread", notes: "User harus login. Hanya pemilik komentar yang dapat menghapus.", + plugins: { + "hapi-swagger": { + security: [{ jwt: [] }], + }, + }, validate: { params: Joi.object({ diff --git a/src/Interfaces/http/api/likes/swagger/likes.js b/src/Interfaces/http/api/likes/swagger/likes.js index 2236ee9..d965339 100644 --- a/src/Interfaces/http/api/likes/swagger/likes.js +++ b/src/Interfaces/http/api/likes/swagger/likes.js @@ -1,15 +1,21 @@ -const Joi = require('joi'); +const Joi = require("joi"); const putCommentLike = { - auth: 'forumapi_jwt', - tags: ['api', 'CommentLikes'], - description: 'Memberikan atau menghapus like pada komentar', - notes: 'User harus login. Like bersifat toggle: jika sudah di-like, maka unlike.', + auth: "forumapi_jwt", + tags: ["api", "CommentLikes"], + description: "Memberikan atau menghapus like pada komentar", + notes: + "User harus login. Like bersifat toggle: jika sudah di-like, maka unlike.", + plugins: { + "hapi-swagger": { + security: [{ jwt: [] }], + }, + }, validate: { params: Joi.object({ - thread_id: Joi.string().required().description('ID thread'), - comment_id: Joi.string().required().description('ID komentar'), + thread_id: Joi.string().required().description("ID thread"), + comment_id: Joi.string().required().description("ID komentar"), }), failAction: (request, h, err) => { @@ -25,8 +31,8 @@ const putCommentLike = { response: { schema: Joi.object({ - status: Joi.string().valid('success').required(), - }).label('PutCommentLikeResponse'), + status: Joi.string().valid("success").required(), + }).label("PutCommentLikeResponse"), }, }; diff --git a/src/Interfaces/http/api/replies/swagger/replies.js b/src/Interfaces/http/api/replies/swagger/replies.js index b99b660..5492a2b 100644 --- a/src/Interfaces/http/api/replies/swagger/replies.js +++ b/src/Interfaces/http/api/replies/swagger/replies.js @@ -1,18 +1,23 @@ -const Joi = require('joi'); +const Joi = require("joi"); const postAddReply = { - auth: 'forumapi_jwt', - tags: ['api', 'Replies'], - description: 'Menambah reply pada sebuah komentar', - notes: 'User harus login (JWT). Reply akan ditambahkan ke komentar tertentu.', + auth: "forumapi_jwt", + tags: ["api", "Replies"], + description: "Menambah reply pada sebuah komentar", + notes: "User harus login (JWT). Reply akan ditambahkan ke komentar tertentu.", + plugins: { + "hapi-swagger": { + security: [{ jwt: [] }], + }, + }, validate: { params: Joi.object({ - thread_id: Joi.string().required().description('ID thread'), - comment_id: Joi.string().required().description('ID komentar'), + thread_id: Joi.string().required().description("ID thread"), + comment_id: Joi.string().required().description("ID komentar"), }), payload: Joi.object({ - content: Joi.string().required().description('Isi reply'), + content: Joi.string().required().description("Isi reply"), }), failAction: (request, h, err) => { @@ -28,7 +33,7 @@ const postAddReply = { response: { schema: Joi.object({ - status: Joi.string().valid('success').required(), + status: Joi.string().valid("success").required(), message: Joi.string().required(), data: Joi.object({ addedReply: Joi.object({ @@ -37,28 +42,35 @@ const postAddReply = { owner: Joi.string().required(), }), }), - }).label('AddReplyResponse'), + }).label("AddReplyResponse"), }, }; const deleteReply = { - auth: 'forumapi_jwt', - tags: ['api', 'Replies'], - description: 'Menghapus reply dari komentar', - notes: 'User harus login. Hanya pemilik reply yang dapat menghapus.', + auth: "forumapi_jwt", + tags: ["api", "Replies"], + description: "Menghapus reply dari komentar", + notes: "User harus login. Hanya pemilik reply yang dapat menghapus.", + plugins: { + "hapi-swagger": { + security: [{ jwt: [] }], + }, + }, validate: { params: Joi.object({ - thread_id: Joi.string().required().description('ID thread'), - comment_id: Joi.string().required().description('ID komentar'), - reply_id: Joi.string().required().description('ID reply yang akan dihapus'), + thread_id: Joi.string().required().description("ID thread"), + comment_id: Joi.string().required().description("ID komentar"), + reply_id: Joi.string() + .required() + .description("ID reply yang akan dihapus"), }), }, response: { schema: Joi.object({ - status: Joi.string().valid('success').required(), - }).label('DeleteReplyResponse'), + status: Joi.string().valid("success").required(), + }).label("DeleteReplyResponse"), }, }; diff --git a/src/Interfaces/http/api/threads/swagger/threads.js b/src/Interfaces/http/api/threads/swagger/threads.js index 9662fa2..27a249e 100644 --- a/src/Interfaces/http/api/threads/swagger/threads.js +++ b/src/Interfaces/http/api/threads/swagger/threads.js @@ -5,6 +5,11 @@ const postAddThread = { tags: ["api", "Threads"], description: "Menambah thread baru", notes: "User harus login (JWT). Thread akan ditambahkan ke forum.", + plugins: { + "hapi-swagger": { + security: [{ jwt: [] }], + }, + }, validate: { payload: Joi.object({