@wbc-sprout/shared

v1.4.2

Published

Shared library for sprout application api

Downloads

30

Readme

Sprout Shared Library

This is a repository for sprout api shared library

Installation

Run npm i @wbc-sprout/shared to install with npm or yarn add @wbc-sprout/shared to install with yarn

Required Environment Variables

  • CLOUDINARY_CLOUD_NAME
  • CLOUDINARY_API_KEY
  • CLOUDINARY_API_SECRET
  • ACCESS_TOKEN_LIFESPAN
  • REFRESH_TOKEN_LIFESPAN
  • APP_KEY
  • APP_URL
  • APP_ENV

Configurations

corsOptions(config) ⇒ CorsOptions

Kind: global function
Returns: CorsOptions - Cors Option

| Param | Type | Description | | --- | --- | --- | | config | Object | the cors configuration | | [config.whitelist] | Array.<string> | Array of url to whitelist | | [config.whitelistRegEx] | Array.<RegExp> | Array of url to whitelist using matching | | config.appURL | string | The application url |

CorsOptions : Object

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | credentials | boolean | The cors credentials. | | exposedHeaders | Array.<string> | The headers to expose | | origin | function | The origin function |

jwtConfig

Kind: global constant
Properties

| Name | Type | | --- | --- | | ACTIVATION_TOKEN_LIFESPAN | string | | SECRET_KEY | string | | ACCESS_TOKEN_LIFESPAN | string | | REFRESH_TOKEN_LIFESPAN | string |

regExp

Kind: global constant
Properties

| Name | Type | | --- | --- | | NUMBER | RegExp | | POSITIVE_NUMBER | RegExp | | POSITIVE_NUMBER_DECIMAL_POINT | RegExp | | EMAIL | RegExp | | PHONE_NUMBER | RegExp | | ALPHA_NUMERIC | RegExp | | ALPHA_NUMERIC_COMMA_DOT | RegExp | | ALPHA_NUMERIC_PLANE | RegExp | | PASSWORD | RegExp | | URL | RegExp |

System Privileges

Typedefs

systemOverviewRelatedPrivileges : SystemOverviewRelatedPrivileges

Kind: global constant

subscriptionRelatedPrivileges : SubscriptionRelatedPrivileges

Kind: global constant

blogPostRelatedPrivileges : BlogPostRelatedPrivileges

Kind: global constant

trainingMaterialRelatedPrivileges : Privileges

Kind: global constant

appointmentRelatedPrivileges : Privileges

Kind: global constant

roleRelatedPrivileges : Privileges

Kind: global constant

userRelatedPrivileges : Privileges

Kind: global constant

courseRelatedPrivileges : Privileges

Kind: global constant

systemPrivileges

Kind: global constant
Properties

| Name | Type | | --- | --- | | systemOverview | SystemOverviewRelatedPrivileges | | user | Privileges | | course | Privileges | | role | Privileges | | blogPost | BlogPostRelatedPrivileges | | subscription | SubscriptionRelatedPrivileges | | trainingMaterial | Privileges | | appointment | Privileges |

SystemOverviewRelatedPrivileges

Kind: global typedef
Properties

| Name | Type | | --- | --- | | READ | string |

SubscriptionRelatedPrivileges

Kind: global typedef
Properties

| Name | Type | | --- | --- | | CREATE | string | | REVOKE | string |

BlogPostRelatedPrivileges

Kind: global typedef
Properties

| Name | Type | | --- | --- | | CREATE | string | | UPDATE | string | | DELETE | string |

Privileges

Kind: global typedef
Properties

| Name | Type | | --- | --- | | CREATE | string | | READ | string | | UPDATE | string | | DELETE | string |

Error Classes

Classes

CustomError

Kind: global class

new CustomError(message, [config])

| Param | Type | | --- | --- | | message | string | | [config] | Object | | [config.httpStatusCode] | number | | [config.stackTrace] | string | | [config.subCode] | number | | [config.reason] | * |

RequestValidationError

Kind: global class

new RequestValidationError(message, [config])

| Param | Type | | --- | --- | | message | string | | [config] | Object | | [config.subCode] | number | | [config.reason] | * |

AuthenticationError

Kind: global class

new AuthenticationError(message, [config])

| Param | Type | | --- | --- | | message | string | | [config] | Object | | [config.subCode] | number | | [config.reason] | * |

NotFoundError

Kind: global class

new NotFoundError(message, [config])

| Param | Type | | --- | --- | | message | string | | [config] | Object | | [config.subCode] | number | | [config.reason] | * |

ConflictError

Kind: global class

new ConflictError(message, [config])

| Param | Type | | --- | --- | | message | string | | [config] | Object | | [config.subCode] | number | | [config.reason] | * |

Utility Functions

Constants

Functions

Typedefs

env : Object

Kind: global constant

isTypeOf(varToCheck, type) ⇒ * | boolean

Kind: global function
Returns: * | boolean - check - the strict type of a variable

| Param | Type | Description | | --- | --- | --- | | varToCheck | * | variable to check its type | | type | string | The type to check against |

getEnv() ⇒ Object

get all the system environment variables

Kind: global function
Returns: Object - Environment variables

setEnv(newEnv) ⇒ null

set a new environment variable or update existing one

Kind: global function
Returns: null - Null

| Param | Type | Description | | --- | --- | --- | | newEnv | Object | The new environment variable(s) as object |

paginate(model) ⇒ PaginationFunction

Paginates model

Kind: global function
Returns: PaginationFunction - Function to paginate the specified model

| Param | Type | Description | | --- | --- | --- | | model | Object | model to add pagination to | | [model.findAndCountAll] | function | inbuilt model function |

removeFileExtension(fileName) ⇒ string | *

To remove extension from a file

Kind: global function
Returns: string | * - Name without extension

| Param | Type | Description | | --- | --- | --- | | fileName | string | The name of the file to remove its extension |

isValidBase64Image(base64) ⇒ string | *

Check if a string is a valid base64

Kind: global function
Returns: string | * - If the string is base64 formatted

| Param | Type | Description | | --- | --- | --- | | base64 | string | the base 64 string |

pickFromObject(obj, acceptedKeys) ⇒ Object | *

To create a new object containing a specified key from an existing object

Kind: global function
Returns: Object | * - Newly created object

| Param | Type | Description | | --- | --- | --- | | obj | Object | object to pick key value pair from | | acceptedKeys | Array.<string> | the keys to pick from the object |

discardFromObject(obj, forbiddenKeys) ⇒ Object | *

To create a new object that does not contain a specified key from an existing object

Kind: global function
Returns: Object | * - Newly created object

| Param | Type | Description | | --- | --- | --- | | obj | Object | Object to create from | | forbiddenKeys | Array.<string> | the keys that should not exist in the new object |

getBaseDomainFromUrl(url) ⇒ string

To get base domain from a giving url

Kind: global function
Returns: string - The base domain

| Param | Type | Description | | --- | --- | --- | | url | string | The url to extract base domain from |

getCookieDomain(req) ⇒ string

To get cookie domain

Kind: global function
Returns: string - The base domain

| Param | Type | Description | | --- | --- | --- | | req | Object | Express request object |

getTokensFromRequest(req) ⇒ Object

To get tokens from request

Kind: global function
Returns: Object - Token Object

| Param | Type | Description | | --- | --- | --- | | req | Object | Express request object |

setTokensToResponse(res, tokens, [cookieDomain]) ⇒ string

To set token to response

Kind: global function
Returns: string - Referer Url

| Param | Type | Description | | --- | --- | --- | | res | Object | Express response object | | tokens | Object | The tokens to set | | [tokens.access] | string | The access token to set | | [tokens.refresh] | string | The access token to set | | [cookieDomain] | string | The domain to set the token cookies on |

normalizePort(port) ⇒ boolean | number | *

Kind: global function
Returns: boolean | number | * - Normalized port

| Param | Type | Description | | --- | --- | --- | | port | string | number | The port to normalize |

convertToSlug(text) ⇒ string | *

Kind: global function
Returns: string | * - slug

| Param | Type | Description | | --- | --- | --- | | text | string | the text to convert |

PaginationFunction ⇒ Object

Kind: global typedef
Returns: Object - Paginated data

| Param | Type | Default | Description | | --- | --- | --- | --- | | [page] | string | number | 1 | page to query | | [limit] | string | number | 10 | limit for the query | | [options] | Object | | Options for the model |

base64Encode(val, [urlEncode]) ⇒ string

Kind: global function
Returns: string - The encoded string

| Param | Type | | --- | --- | | val | * | | [urlEncode] | boolean |

base64Decode(val, [urlEncoded]) ⇒ string

Kind: global function
Returns: string - The decoded string

| Param | Type | | --- | --- | | val | * | | [urlEncoded] | boolean |

JWT Helper

Members

Functions

Typedefs

jwtHelper : JWTHelper

Kind: global variable

makeToken(user, [lifeSpan]) ⇒ undefined | string

To generate a jwt

Kind: global function
Returns: undefined | string - The generated token string

| Param | Type | Description | | --- | --- | --- | | user | Object | The user to create a token for | | [lifeSpan] | string | The lifespan of the token to be created |

verifyToken(token, [includeSignature]) ⇒ Object

To verify a given jwt

Kind: global function
Returns: Object - the user object

| Param | Type | Default | Description | | --- | --- | --- | --- | | token | String | | the access token string | | [includeSignature] | Boolean | true | Whether or not to include signature |

generateTokens(user) ⇒ Object

To generate both access and refresh token for a given user

Kind: global function
Returns: Object - The generated token object containing access token and refresh token

| Param | Type | Description | | --- | --- | --- | | user | Object | The user to generate tokens for |

renewAccessToken(refreshToken) ⇒ Promise.<Array>

To get new access token from a refresh token

Kind: global function
Returns: Promise.<Array> - The new access token and the user data

| Param | Type | Description | | --- | --- | --- | | refreshToken | string | the refresh token |

JWTHelperGenerateToken ⇒ Object

To generate both access and refresh token for a given user

Kind: global typedef
Returns: Object - The generated token object containing access token and refresh token

| Param | Type | Description | | --- | --- | --- | | user | Object | The user to generate tokens for |

JWTHelperRenewAccessToken ⇒ Promise.<Array>

To get new access token from a refresh token

Kind: global typedef
Returns: Promise.<Array> - The new access token and the user data

| Param | Type | Description | | --- | --- | --- | | refreshToken | string | the refresh token |

JWTHelperVerifyToken ⇒ Object

To verify a given jwt

Kind: global typedef
Returns: Object - the user object

| Param | Type | Default | Description | | --- | --- | --- | --- | | token | String | | the access token string | | [includeSignature] | Boolean | true | Whether or not to include signature |

JWTHelperMakeToken ⇒ undefined | string

To generate a jwt

Kind: global typedef
Returns: undefined | string - The generated token string

| Param | Type | Description | | --- | --- | --- | | user | Object | The user to create a token for | | [lifeSpan] | string | The lifespan of the token to be created |

JWTHelper : Object

JWT Helper

Kind: global typedef
Properties

| Name | Type | | --- | --- | | generateTokens | JWTHelperGenerateToken | | renewAccessToken | JWTHelperRenewAccessToken | | verifyToken | JWTHelperVerifyToken | | makeToken | JWTHelperMakeToken |

App Logger

logger

Application logger

Kind: global constant
Properties

| Name | Type | | --- | --- | | stream | * | | add | function | | info | function |

Response Helper

Functions

renderResponse(req, res, template, [data], [statusCode]) ⇒ HTMLDocument

Kind: global function
Returns: HTMLDocument - Full html content

| Param | Type | Default | Description | | --- | --- | --- | --- | | req | Object | | Express request object | | res | Object | | Express response object | | template | String | | Http status code for the response | | [data] | Object | | Data to be return as part of response body | | [statusCode] | Number | 200 | Http status code for the response |

successResponse(res, statusCode, [data], [message]) ⇒ JSON

Kind: global function
Returns: JSON - Formatted JSON server response

| Param | Type | Description | | --- | --- | --- | | res | object | Express response object | | statusCode | Number | Http status code for the response | | [data] | object | Data to be return as part of response body | | [message] | String | Message accompanying the response data |

okResponse(res, [data], [message]) ⇒ JSON

Kind: global function
Returns: JSON - Formatted JSON server response

| Param | Type | Description | | --- | --- | --- | | res | object | Express response object | | [data] | object | Data to be return as part of response body | | [message] | String | Message accompanying the response data |

createdResponse(res, data, [message]) ⇒ JSON

Kind: global function
Returns: JSON - Formatted JSON server response

| Param | Type | Description | | --- | --- | --- | | res | object | Express response object | | data | object | Data to be return as part of response body | | [message] | String | Message accompanying the response data |

redirectResponse(res, url, [code]) ⇒ Null

Kind: global function
Returns: Null - Null

| Param | Type | Description | | --- | --- | --- | | res | object | Express response object | | url | String | url to redirect to | | [code] | Number | 301 or 302 based on permanent or temporary |

Uploader

Constants

Functions

Typedefs

uploader

Uploader

Kind: global constant
Properties

| Name | Type | | --- | --- | | initialize | UploaderInitialize | | uploadImageFromDataURI | UploadImageFromDataURI | | uploadImageFromFile | UploadImageFromDataURI | | removeImage | UploaderRemoveImage | | getFilesInFolder | UploaderGetFilesInFolder |

toDataUri(config) ⇒ *

This function converts the buffer to data url

Kind: global function
Returns: * - The data url from the string buffer

| Param | Type | Description | | --- | --- | --- | | config | Object | File | Express request object |

getBaseFolder() ⇒ string

Kind: global function
Returns: string - base folder

constructFolder(folder) ⇒ string

Kind: global function
Returns: string - refined folder

| Param | Type | | --- | --- | | folder | string |

UploaderInitialize ⇒ null

Kind: global typedef
Returns: null - Null

| Param | Type | Description | | --- | --- | --- | | req | Object | Express request object | | res | Object | Express response object | | next | function | Express next function |

UploadImageFromDataURI ⇒ Promise.<*>

Kind: global typedef
Returns: Promise.<*> - Uploader

| Param | Type | Description | | --- | --- | --- | | dataURI | string | a base64 representation of the file | | fileName | string | Name of the file to upload | | [folder] | string | the folder to upload the file excluding the main folder |

UploadImageFromFile ⇒ Promise.<*>

Kind: global typedef
Returns: Promise.<*> - Uploader

| Param | Type | Description | | --- | --- | --- | | dataURI | string | a base64 representation of the file | | fileName | string | Name of the file to upload | | [folder] | string | the folder to upload the file excluding the main folder |

UploaderRemoveImage ⇒ *

Kind: global typedef
Returns: * - Null

| Param | Type | Description | | --- | --- | --- | | link | string | The cloudinary link to the image |

UploaderGetFilesInFolder ⇒ Promise.<any>

Kind: global typedef
Returns: Promise.<any> - Resources

| Param | Type | Description | | --- | --- | --- | | folder | string | the folder to load the files from |

Validators

Constants

Typedefs

bodyValidator

Kind: global constant
Properties

| Name | Type | | --- | --- | | email | CustomValidator | | password | CustomValidator | | confirm | CustomConfirmValidator | | matchPattern | CustomMatchPatterValidator | | required | CustomRequiredValidator |

CustomValidator ⇒ *

Kind: global typedef
Returns: * - Validation chain

| Param | Type | Description | | --- | --- | --- | | [fieldName] | string | The field to validate | | [message] | string | Error message |

CustomConfirmValidator ⇒ *

Kind: global typedef
Returns: * - Validation chain

| Param | Type | Description | | --- | --- | --- | | [fieldName] | string | The field to validate | | [benchmark] | string | What to benchmark against | | [message] | string | Error message |

CustomMatchPatterValidator ⇒ *

Kind: global typedef
Returns: * - Validation chain

| Param | Type | Description | | --- | --- | --- | | fieldName | string | The field to validate | | pattern | RegExp | pattern to test for | | [message] | string | Error message |

CustomRequiredValidator ⇒ Array

Kind: global typedef
Returns: Array - Validation chain

| Param | Type | Description | | --- | --- | --- | | ...fieldNames | string | Name of the required field(s) |

Middlewares

decodeCurrentUser([accessTokenKey]) ⇒ function

Kind: global function
Returns: function - Express middleware function

| Param | Type | Default | | --- | --- | --- | | [accessTokenKey] | string | "accessToken" |

errorHandler([includeStackTrace]) ⇒ function

Kind: global function
Returns: function - Express middleware function

| Param | Type | Description | | --- | --- | --- | | [includeStackTrace] | boolean | Whether or not to include stack trace |

requireAuthentication([message]) ⇒ function

Kind: global function
Returns: function - Express middleware function

| Param | Type | | --- | --- | | [message] | string |

multerUploads

Kind: global constant
Properties

| Name | Type | | --- | --- | | single | function | | any | function | | array | function | | fields | function | | none | function |

preloadSwagger(documentation) ⇒ function

Kind: global function
Returns: function - Middleware to update documentation properties

| Param | Type | Description | | --- | --- | --- | | documentation | Object | Swagger json documentation |

refreshAccessToken([config]) ⇒ function

Kind: global function
Returns: function - Middleware to refresh accessToken

| Param | Type | Description | | --- | --- | --- | | [config] | Object | Configuration object | | [config.forceRefreshEvenIfNotExpire] | boolean | Whether or not to refresh even if the token has not yet expired |

validationResult() ⇒ function

Kind: global function
Returns: function - Middleware to update documentation properties

requirePrivilege(requirements, [errorMessage]) ⇒ function

Kind: global function
Returns: function - Privilege checker middleware

| Param | Type | Description | | --- | --- | --- | | requirements | Object | | | [requirements.has] | string | Checks for this privilege | | [requirements.hasAll] | Array.<string> | Checks for all privilege in this array | | [requirements.hasAny] | Array.<string> | Checks for any privilege in this array | | [errorMessage] | string | Error message to show user in case requirement not met |

Services

Constants

Functions

Typedefs

subscribableEvents

Kind: global constant
Properties

| Name | Type | | --- | --- | | USER_CREATED | string | | COURSE_CREATED | string | | SEND_EMAIL | string | | EMAIL_SENT | string |

initializeKafka([callback], [config]) ⇒ Object

Kind: global function
Returns: Object - kafka

| Param | Type | | --- | --- | | [callback] | function | | [config] | Object | | [config.showProducerLog] | boolean |

registerKafkaConsumer(topics, onReceive) ⇒ null

Kind: global function
Returns: null - Null

| Param | Type | | --- | --- | | topics | Array | | onReceive | OnKafkaEventReceive |

publishKafkaEvent(topicName, eventData, eventKey) ⇒ null

Kind: global function
Returns: null - Null

| Param | Type | | --- | --- | | topicName | string | | eventData | * | | eventKey | string |

OnKafkaEventReceive ⇒ null

Kind: global typedef
Returns: null - Null

| Param | Type | | --- | --- | | receivedEvent | Object | | eventData | any |