From 4dc399cd9fe0a5d92b5a0a512dc4ac38c6f13ae5 Mon Sep 17 00:00:00 2001 From: Mauricio Oruezabal Date: Mon, 29 Nov 2021 09:25:26 -0300 Subject: [PATCH] add type-definition --- .github/workflows/npm-publish.yml | 2 ++ .gitignore | 3 ++- lib/api-get-error.js | 15 +++++++++++++++ lib/api-get.js | 23 +++++++++++++++++++++++ lib/helpers/endpoint-parser.js | 12 ++++++++++++ lib/utils/camelize.js | 5 +++++ package.json | 7 +++++-- 7 files changed, 64 insertions(+), 3 deletions(-) diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 7f4c617..b81fdbe 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -25,6 +25,8 @@ jobs: node-version: 12 registry-url: https://registry.npmjs.org/ - run: npm i + - name: Build types + run: npm run build-types - run: npm publish --access public env: NODE_AUTH_TOKEN: ${{secrets.npm_token}} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 504d29a..1e39b93 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules/ package-lock.json coverage/ -.nyc_output/ \ No newline at end of file +.nyc_output/ +types/ \ No newline at end of file diff --git a/lib/api-get-error.js b/lib/api-get-error.js index 8c70db1..8d35261 100644 --- a/lib/api-get-error.js +++ b/lib/api-get-error.js @@ -1,7 +1,18 @@ 'use strict'; +/** + * @typedef CodesError + * @property {Number} INVALID_REQUEST_DATA + * @property {Number} INVALID_ENTITY + * @property {Number} INTERNAL_ERROR + */ + class ApiGetError extends Error { + /** + * Get the error codes + * @returns {CodesError} + */ static get codes() { return { @@ -12,6 +23,10 @@ class ApiGetError extends Error { } + /** + * @param {Error} err The details of the error + * @param {Number} code The error code + */ constructor(err, code) { const message = err.message || err; diff --git a/lib/api-get.js b/lib/api-get.js index bb00518..68a2699 100644 --- a/lib/api-get.js +++ b/lib/api-get.js @@ -6,8 +6,15 @@ const path = require('path'); const ApiGetError = require('./api-get-error'); const EndpointParser = require('./helpers/endpoint-parser'); +/** + * @typedef {Object} ApiGetError A instance of APIGetError class + */ + class ApiGet extends API { + /** + * Validate the model, extracted from the parsed endpoint + */ async validate() { this._parseEndpoint(); @@ -15,6 +22,10 @@ class ApiGet extends API { this._validateModel(); } + /** + * It makes the query to the DB with the filters and params obtained from the endpoint + * @returns {void} + */ async process() { const filters = { @@ -51,6 +62,9 @@ class ApiGet extends API { this.setBody(response); } + /** + * Set the modelName, recordId and parents of API after parsing the endpoint + */ _parseEndpoint() { const { modelName, recordId, parents } = EndpointParser.parse(this.endpoint); @@ -60,6 +74,10 @@ class ApiGet extends API { this.parents = parents; } + /** + * Set the model of the API getting the model instance from its name + * @throws {ApiGetError} if the model not exists + */ _validateModel() { try { this.model = this._getModelInstance(path.join(process.cwd(), process.env.MS_PATH || '', 'models', this.modelName)); @@ -68,6 +86,11 @@ class ApiGet extends API { } } + /** + * Get the instance of the Model indicated by parameter + * @param {String} modelPath The model path + * @returns {Object} An instance injected with the session + */ _getModelInstance(modelPath) { // eslint-disable-next-line global-require, import/no-dynamic-require diff --git a/lib/helpers/endpoint-parser.js b/lib/helpers/endpoint-parser.js index c0dd7b8..674d559 100644 --- a/lib/helpers/endpoint-parser.js +++ b/lib/helpers/endpoint-parser.js @@ -3,6 +3,18 @@ const ApiGetError = require('../api-get-error'); const camelize = require('../utils/camelize'); +/** + * @typedef {Object} ParseEndpoint + * @property {String} [modelName] The model name + * @property {String} [recordId] The ID of the record + * @property {String} [parents] The rest of the text string that does not have a specific function +*/ + +/** + * Parse the endpoint passed by parameter in different parts for use + * @param {String} endpoint The endpoint to parse + * @returns {ParseEndpoint} The parsed endpoint + */ class EndpointParser { static parse(endpoint) { diff --git a/lib/utils/camelize.js b/lib/utils/camelize.js index e62d571..cfd4e67 100644 --- a/lib/utils/camelize.js +++ b/lib/utils/camelize.js @@ -1,5 +1,10 @@ 'use strict'; +/** + * Transform the string entered in camelCase format + * @param {String} string + * @returns {String} + */ const camelize = string => string.replace(/-+([^-])/g, (_, firstLetter) => firstLetter.toUpperCase()); module.exports = camelize; diff --git a/package.json b/package.json index 261a456..9f971b9 100644 --- a/package.json +++ b/package.json @@ -25,11 +25,14 @@ "mocha": "^8.1.2", "mock-require": "^3.0.3", "nyc": "^15.1.0", - "sinon": "^9.0.3" + "sinon": "^9.0.3", + "typescript": "^4.5.2" }, "files": [ - "lib/" + "lib/", + "types/" ], + "types": "types/index.d.ts", "directories": { "test": "tests" },