diff --git a/lib/src/functions/api/index.js b/lib/src/functions/api/index.js index 5062fcd..1ff46cb 100644 --- a/lib/src/functions/api/index.js +++ b/lib/src/functions/api/index.js @@ -7,6 +7,7 @@ const schemas = require('./schemas') const services = require('./services') const types = require('./types') const network = require('./network') +const utils = require('./utils') const writeFile = require('../../utils/writeFile') /* @@ -174,439 +175,8 @@ Server.start() file: `${projectName}/src/index.ts` } } - const expressData = { - utils: { - docs: { - content: `{ - "openapi": "3.0.0", - "info": { - "title": "${projectName}", - "description": "Documentation of the test", - "contact": { - "email": "${email}" - }, - "license": { - "name": "MIT", - "url": "https://opensource.org/licenses/MIT" - }, - "version": "${projectVersion}" - }, - "servers": [ - { - "url": "http://localhost:1996/api", - "description": "${projectName} local API" - } - ], - "tags": [ - { - "name": "user", - "description": "Operations related to the user" - } - ], - "paths": { - "/users": { - "post": { - "tags": [ - "user" - ], - "summary": "Save a user in the database", - "operationId": "store", - "requestBody": { - "$ref": "#/components/requestBodies/UserDTO" - }, - "responses": { - "201": { - "description": "User successfully stored", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/User" - } - } - } - }, - "422": { - "description": "Invalid request format", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - }, - "500": { - "description": "Internal server error", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - } - } - }, - "get": { - "tags": [ - "user" - ], - "summary": "Get all the users in the database", - "operationId": "getAll", - "responses": { - "200": { - "description": "All the users in the database", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "boolean", - "default": false - }, - "message": { - "type": "object", - "properties": { - "result": { - "type": "array", - "items": { - "$ref": "#/components/schemas/User" - } - } - } - } - } - } - } - } - }, - "500": { - "description": "Internal server error", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - } - } - }, - "delete": { - "tags": [ - "user" - ], - "summary": "Delete all the users in the database", - "operationId": "deleteAll", - "responses": { - "200": { - "description": "All the users in the database", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultSuccess" - } - } - } - }, - "500": { - "description": "Internal server error", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - } - } - } - }, - "/user/{id}": { - "get": { - "tags": [ - "user" - ], - "summary": "Get an specific user", - "operationId": "getOne", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "MongoDB user id", - "required": true, - "style": "simple", - "explode": false, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "User stored in the database", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/User" - } - } - } - }, - "404": { - "description": "User not found", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - }, - "422": { - "description": "Invalid request format", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - }, - "500": { - "description": "Internal server error", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - } - } - }, - "patch": { - "tags": [ - "user" - ], - "summary": "Update the user data", - "operationId": "update", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "MongoDB user id", - "required": true, - "style": "simple", - "explode": false, - "schema": { - "type": "string" - } - } - ], - "requestBody": { - "$ref": "#/components/requestBodies/UserDTO" - }, - "responses": { - "200": { - "description": "User successfully updated", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/User" - } - } - } - }, - "404": { - "description": "User not found", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - }, - "422": { - "description": "Invalid request format", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - }, - "500": { - "description": "Internal server error", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - } - } - }, - "delete": { - "tags": [ - "user" - ], - "summary": "Delete one user from the database", - "operationId": "delete", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "MongoDB user id", - "required": true, - "style": "simple", - "explode": false, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "User successfully deleted", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultSuccess" - } - } - } - }, - "404": { - "description": "User not found", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - }, - "422": { - "description": "Invalid request format", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - }, - "500": { - "description": "Internal server error", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DefaultError" - } - } - } - } - } - } - } - }, - "components": { - "schemas": { - "User": { - "type": "object", - "properties": { - "id": { - "type": "string" - }, - "lastName": { - "type": "string" - }, - "name": { - "type": "string" - } - } - }, - "DefaultSuccess": { - "type": "object", - "properties": { - "error": { - "type": "boolean", - "default": false - }, - "message": { - "type": "object", - "properties": { - "result": { - "type": "string" - } - } - } - } - }, - "DefaultError": { - "type": "object", - "properties": { - "error": { - "type": "boolean", - "default": true - }, - "message": { - "type": "object", - "properties": { - "result": { - "type": "string" - } - } - } - } - } - }, - "requestBodies": { - "UserDTO": { - "description": "User name and last name", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "args": { - "type": "object", - "properties": { - "name": { - "type": "string" - }, - "lastName": { - "type": "string" - } - } - } - } - } - } - }, - "required": true - } - } - } -}`, - file: `${projectName}/src/utils/docs.json` - }, - index: { - content: "export { default as docs } from './docs.json'\n", - file: `${projectName}/src/utils/index.ts` - } - } - } - const expressFolders = `${projectName}/src/utils` - - const createFoldersCommands = `mkdir ${projectName}/src \ -${fastify ? '' : `${expressFolders}`}` + const createFoldersCommands = `mkdir ${projectName}/src` if (os.platform() === 'win32') await exec(createFoldersCommands.replaceAll('/', '\\')) @@ -631,6 +201,13 @@ ${fastify ? '' : `${expressFolders}`}` schemas({ projectName }) // /services services({ projectName }) + // /utils + utils({ + express: !fastify, + projectName, + email, + projectVersion + }) // /test await writeFile(data.test.index.file, data.test.index.content) @@ -640,15 +217,4 @@ ${fastify ? '' : `${expressFolders}`}` // index await writeFile(data.index.file, data.index.content) - - if (fastify) { - console.log() - } else { - // /utils - await writeFile(expressData.utils.docs.file, expressData.utils.docs.content) - await writeFile( - expressData.utils.index.file, - expressData.utils.index.content - ) - } } diff --git a/lib/src/functions/api/utils.js b/lib/src/functions/api/utils.js new file mode 100644 index 0000000..d163f7d --- /dev/null +++ b/lib/src/functions/api/utils.js @@ -0,0 +1,453 @@ +const { platform } = require('os') +const { promisify } = require('util') +const exec = promisify(require('child_process').exec) +const writeFile = require('../../utils/writeFile') + +/** + * @param {Object} args + * @param {Boolean} args.express + * @param {String} args.projectName + * @param {String} args.email + * @param {String} args.projectVersion + */ +module.exports = async ({ express, projectName, email, projectVersion }) => { + if (express) { + const createFoldersCommand = `mkdir ${projectName}/src/utils` + + if (platform() === 'win32') + await exec(createFoldersCommand.replaceAll('/', '\\')) + else await exec(createFoldersCommand) + + const utils = { + docs: { + content: `{ + "openapi": "3.0.0", + "info": { + "title": "${projectName}", + "description": "Documentation of the test", + "contact": { + "email": "${email}" + }, + "license": { + "name": "MIT", + "url": "https://opensource.org/licenses/MIT" + }, + "version": "${projectVersion}" + }, + "servers": [ + { + "url": "http://localhost:1996/api", + "description": "${projectName} local API" + } + ], + "tags": [ + { + "name": "user", + "description": "Operations related to the user" + } + ], + "paths": { + "/users": { + "post": { + "tags": [ + "user" + ], + "summary": "Save a user in the database", + "operationId": "store", + "requestBody": { + "$ref": "#/components/requestBodies/UserDTO" + }, + "responses": { + "201": { + "description": "User successfully stored", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "422": { + "description": "Invalid request format", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + }, + "500": { + "description": "Internal server error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + } + } + }, + "get": { + "tags": [ + "user" + ], + "summary": "Get all the users in the database", + "operationId": "getAll", + "responses": { + "200": { + "description": "All the users in the database", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "boolean", + "default": false + }, + "message": { + "type": "object", + "properties": { + "result": { + "type": "array", + "items": { + "$ref": "#/components/schemas/User" + } + } + } + } + } + } + } + } + }, + "500": { + "description": "Internal server error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + } + } + }, + "delete": { + "tags": [ + "user" + ], + "summary": "Delete all the users in the database", + "operationId": "deleteAll", + "responses": { + "200": { + "description": "All the users in the database", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultSuccess" + } + } + } + }, + "500": { + "description": "Internal server error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + } + } + } + }, + "/user/{id}": { + "get": { + "tags": [ + "user" + ], + "summary": "Get an specific user", + "operationId": "getOne", + "parameters": [ + { + "name": "id", + "in": "path", + "description": "MongoDB user id", + "required": true, + "style": "simple", + "explode": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "User stored in the database", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "404": { + "description": "User not found", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + }, + "422": { + "description": "Invalid request format", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + }, + "500": { + "description": "Internal server error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + } + } + }, + "patch": { + "tags": [ + "user" + ], + "summary": "Update the user data", + "operationId": "update", + "parameters": [ + { + "name": "id", + "in": "path", + "description": "MongoDB user id", + "required": true, + "style": "simple", + "explode": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "$ref": "#/components/requestBodies/UserDTO" + }, + "responses": { + "200": { + "description": "User successfully updated", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "404": { + "description": "User not found", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + }, + "422": { + "description": "Invalid request format", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + }, + "500": { + "description": "Internal server error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + } + } + }, + "delete": { + "tags": [ + "user" + ], + "summary": "Delete one user from the database", + "operationId": "delete", + "parameters": [ + { + "name": "id", + "in": "path", + "description": "MongoDB user id", + "required": true, + "style": "simple", + "explode": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "User successfully deleted", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultSuccess" + } + } + } + }, + "404": { + "description": "User not found", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + }, + "422": { + "description": "Invalid request format", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + }, + "500": { + "description": "Internal server error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DefaultError" + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "User": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "lastName": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, + "DefaultSuccess": { + "type": "object", + "properties": { + "error": { + "type": "boolean", + "default": false + }, + "message": { + "type": "object", + "properties": { + "result": { + "type": "string" + } + } + } + } + }, + "DefaultError": { + "type": "object", + "properties": { + "error": { + "type": "boolean", + "default": true + }, + "message": { + "type": "object", + "properties": { + "result": { + "type": "string" + } + } + } + } + } + }, + "requestBodies": { + "UserDTO": { + "description": "User name and last name", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "args": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "lastName": { + "type": "string" + } + } + } + } + } + } + }, + "required": true + } + } + } +}`, + file: `${projectName}/src/utils/docs.json` + }, + index: { + content: "export { default as docs } from './docs.json'\n", + file: `${projectName}/src/utils/index.ts` + } + } + + if (express) { + await writeFile(utils.docs.file, utils.docs.content) + await writeFile(utils.index.file, utils.index.content) + } + } +}