Skip to content

Commit

Permalink
add type-definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Oruezabal committed Nov 29, 2021
1 parent 0b2b2ce commit 4dc399c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
package-lock.json
coverage/
.nyc_output/
.nyc_output/
types/
15 changes: 15 additions & 0 deletions lib/api-get-error.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions lib/api-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ 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();

this._validateModel();
}

/**
* It makes the query to the DB with the filters and params obtained from the endpoint
* @returns {void}
*/
async process() {

const filters = {
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/helpers/endpoint-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/camelize.js
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 4dc399c

Please sign in to comment.