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
corsOptions(config) ⇒ 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 |
Object
CorsOptions : 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 |
RegExp |
|
PHONE_NUMBER | RegExp |
ALPHA_NUMERIC | RegExp |
ALPHA_NUMERIC_COMMA_DOT | RegExp |
ALPHA_NUMERIC_PLANE | RegExp |
PASSWORD | RegExp |
URL | RegExp |
System Privileges
- systemOverviewRelatedPrivileges :
SystemOverviewRelatedPrivileges
- subscriptionRelatedPrivileges :
SubscriptionRelatedPrivileges
- blogPostRelatedPrivileges :
BlogPostRelatedPrivileges
- trainingMaterialRelatedPrivileges :
Privileges
- appointmentRelatedPrivileges :
Privileges
- roleRelatedPrivileges :
Privileges
- userRelatedPrivileges :
Privileges
- courseRelatedPrivileges :
Privileges
- systemPrivileges
Typedefs
SystemOverviewRelatedPrivileges
systemOverviewRelatedPrivileges :
SubscriptionRelatedPrivileges
subscriptionRelatedPrivileges :
BlogPostRelatedPrivileges
blogPostRelatedPrivileges :
Privileges
trainingMaterialRelatedPrivileges :
Privileges
appointmentRelatedPrivileges :
Privileges
roleRelatedPrivileges :
Privileges
userRelatedPrivileges :
Privileges
courseRelatedPrivileges :
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
new CustomError(message, [config])
Param | Type |
---|---|
message | string |
[config] | Object |
[config.httpStatusCode] | number |
[config.stackTrace] | string |
[config.subCode] | number |
[config.reason] | * |
RequestValidationError
new RequestValidationError(message, [config])
Param | Type |
---|---|
message | string |
[config] | Object |
[config.subCode] | number |
[config.reason] | * |
AuthenticationError
new AuthenticationError(message, [config])
Param | Type |
---|---|
message | string |
[config] | Object |
[config.subCode] | number |
[config.reason] | * |
NotFoundError
new NotFoundError(message, [config])
Param | Type |
---|---|
message | string |
[config] | Object |
[config.subCode] | number |
[config.reason] | * |
ConflictError
new ConflictError(message, [config])
Param | Type |
---|---|
message | string |
[config] | Object |
[config.subCode] | number |
[config.reason] | * |
Utility Functions
Constants
- env :
Object
Functions
- isTypeOf(varToCheck, type) ⇒
*
|boolean
- getEnv() ⇒
Object
get all the system environment variables
- setEnv(newEnv) ⇒
null
set a new environment variable or update existing one
- paginate(model) ⇒
PaginationFunction
Paginates model
- removeFileExtension(fileName) ⇒
string
|*
To remove extension from a file
- isValidBase64Image(base64) ⇒
string
|*
Check if a string is a valid base64
- pickFromObject(obj, acceptedKeys) ⇒
Object
|*
To create a new object containing a specified key from an existing object
- discardFromObject(obj, forbiddenKeys) ⇒
Object
|*
To create a new object that does not contain a specified key from an existing object
- getBaseDomainFromUrl(url) ⇒
string
To get base domain from a giving url
- getCookieDomain(req) ⇒
string
To get cookie domain
- getTokensFromRequest(req) ⇒
Object
To get tokens from request
- setTokensToResponse(res, tokens, [cookieDomain]) ⇒
string
To set token to response
- normalizePort(port) ⇒
boolean
|number
|*
Typedefs
- PaginationFunction ⇒
Object
Object
env :
*
| boolean
isTypeOf(varToCheck, type) ⇒ 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 |
Object
getEnv() ⇒ get all the system environment variables
Kind: global function
Returns: Object
- Environment variables
null
setEnv(newEnv) ⇒ 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 |
PaginationFunction
paginate(model) ⇒ 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 |
string
| *
removeFileExtension(fileName) ⇒ 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 |
string
| *
isValidBase64Image(base64) ⇒ 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 |
Object
| *
pickFromObject(obj, acceptedKeys) ⇒ 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 |
Object
| *
discardFromObject(obj, forbiddenKeys) ⇒ 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 |
string
getBaseDomainFromUrl(url) ⇒ 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 |
string
getCookieDomain(req) ⇒ To get cookie domain
Kind: global function
Returns: string
- The base domain
Param | Type | Description |
---|---|---|
req | Object |
Express request object |
Object
getTokensFromRequest(req) ⇒ To get tokens from request
Kind: global function
Returns: Object
- Token Object
Param | Type | Description |
---|---|---|
req | Object |
Express request object |
string
setTokensToResponse(res, tokens, [cookieDomain]) ⇒ 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 |
boolean
| number
| *
normalizePort(port) ⇒ Kind: global function
Returns: boolean
| number
| *
- Normalized port
Param | Type | Description |
---|---|---|
port | string | number |
The port to normalize |
string
| *
convertToSlug(text) ⇒ Kind: global function
Returns: string
| *
- slug
Param | Type | Description |
---|---|---|
text | string |
the text to convert |
Object
PaginationFunction ⇒ 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 |
string
base64Encode(val, [urlEncode]) ⇒ Kind: global function
Returns: string
- The encoded string
Param | Type |
---|---|
val | * |
[urlEncode] | boolean |
string
base64Decode(val, [urlEncoded]) ⇒ Kind: global function
Returns: string
- The decoded string
Param | Type |
---|---|
val | * |
[urlEncoded] | boolean |
JWT Helper
Members
Functions
- makeToken(user, [lifeSpan]) ⇒
undefined
|string
To generate a jwt
- verifyToken(token, [includeSignature]) ⇒
Object
To verify a given jwt
- generateTokens(user) ⇒
Object
To generate both access and refresh token for a given user
- renewAccessToken(refreshToken) ⇒
Promise.<Array>
To get new access token from a refresh token
Typedefs
- JWTHelperGenerateToken ⇒
Object
To generate both access and refresh token for a given user
- JWTHelperRenewAccessToken ⇒
Promise.<Array>
To get new access token from a refresh token
- JWTHelperVerifyToken ⇒
Object
To verify a given jwt
- JWTHelperMakeToken ⇒
undefined
|string
To generate a jwt
- JWTHelper :
Object
JWT Helper
JWTHelper
jwtHelper :
undefined
| string
makeToken(user, [lifeSpan]) ⇒ 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 |
Object
verifyToken(token, [includeSignature]) ⇒ 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 |
Object
generateTokens(user) ⇒ 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 |
Promise.<Array>
renewAccessToken(refreshToken) ⇒ 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 |
Object
JWTHelperGenerateToken ⇒ 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 |
Promise.<Array>
JWTHelperRenewAccessToken ⇒ 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 |
Object
JWTHelperVerifyToken ⇒ 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 |
undefined
| string
JWTHelperMakeToken ⇒ 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 |
Object
JWTHelper : 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
- successResponse(res, statusCode, [data], [message]) ⇒
JSON
- okResponse(res, [data], [message]) ⇒
JSON
- createdResponse(res, data, [message]) ⇒
JSON
- redirectResponse(res, url, [code]) ⇒
Null
HTMLDocument
renderResponse(req, res, template, [data], [statusCode]) ⇒ 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 |
JSON
successResponse(res, statusCode, [data], [message]) ⇒ 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 |
JSON
okResponse(res, [data], [message]) ⇒ 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 |
JSON
createdResponse(res, data, [message]) ⇒ 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 |
Null
redirectResponse(res, url, [code]) ⇒ 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
- uploader
Uploader
Functions
- toDataUri(config) ⇒
*
This function converts the buffer to data url
- getBaseFolder() ⇒
string
- constructFolder(folder) ⇒
string
Typedefs
- UploaderInitialize ⇒
null
- UploadImageFromDataURI ⇒
Promise.<*>
- UploadImageFromFile ⇒
Promise.<*>
- UploaderRemoveImage ⇒
*
- UploaderGetFilesInFolder ⇒
Promise.<any>
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 |
string
getBaseFolder() ⇒ Kind: global function
Returns: string
- base folder
string
constructFolder(folder) ⇒ Kind: global function
Returns: string
- refined folder
Param | Type |
---|---|
folder | string |
null
UploaderInitialize ⇒ Kind: global typedef
Returns: null
- Null
Param | Type | Description |
---|---|---|
req | Object |
Express request object |
res | Object |
Express response object |
next | function |
Express next function |
Promise.<*>
UploadImageFromDataURI ⇒ 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 |
Promise.<*>
UploadImageFromFile ⇒ 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 |
Promise.<any>
UploaderGetFilesInFolder ⇒ Kind: global typedef
Returns: Promise.<any>
- Resources
Param | Type | Description |
---|---|---|
folder | string |
the folder to load the files from |
Validators
Constants
Typedefs
- CustomValidator ⇒
*
- CustomConfirmValidator ⇒
*
- CustomMatchPatterValidator ⇒
*
- CustomRequiredValidator ⇒
Array
bodyValidator
Kind: global constant
Properties
Name | Type |
---|---|
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 |
Array
CustomRequiredValidator ⇒ Kind: global typedef
Returns: Array
- Validation chain
Param | Type | Description |
---|---|---|
...fieldNames | string |
Name of the required field(s) |
Middlewares
function
decodeCurrentUser([accessTokenKey]) ⇒ Kind: global function
Returns: function
- Express middleware function
Param | Type | Default |
---|---|---|
[accessTokenKey] | string |
"accessToken" |
function
errorHandler([includeStackTrace]) ⇒ Kind: global function
Returns: function
- Express middleware function
Param | Type | Description |
---|---|---|
[includeStackTrace] | boolean |
Whether or not to include stack trace |
function
requireAuthentication([message]) ⇒ 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 |
function
preloadSwagger(documentation) ⇒ Kind: global function
Returns: function
- Middleware to update documentation properties
Param | Type | Description |
---|---|---|
documentation | Object |
Swagger json documentation |
function
refreshAccessToken([config]) ⇒ 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 |
function
validationResult() ⇒ Kind: global function
Returns: function
- Middleware to update documentation properties
function
requirePrivilege(requirements, [errorMessage]) ⇒ 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
- initializeKafka([callback], [config]) ⇒
Object
- registerKafkaConsumer(topics, onReceive) ⇒
null
- publishKafkaEvent(topicName, eventData, eventKey) ⇒
null
Typedefs
- OnKafkaEventReceive ⇒
null
subscribableEvents
Kind: global constant
Properties
Name | Type |
---|---|
USER_CREATED | string |
COURSE_CREATED | string |
SEND_EMAIL | string |
EMAIL_SENT | string |
Object
initializeKafka([callback], [config]) ⇒ Kind: global function
Returns: Object
- kafka
Param | Type |
---|---|
[callback] | function |
[config] | Object |
[config.showProducerLog] | boolean |
null
registerKafkaConsumer(topics, onReceive) ⇒ Kind: global function
Returns: null
- Null
Param | Type |
---|---|
topics | Array |
onReceive | OnKafkaEventReceive |
null
publishKafkaEvent(topicName, eventData, eventKey) ⇒ Kind: global function
Returns: null
- Null
Param | Type |
---|---|
topicName | string |
eventData | * |
eventKey | string |
null
OnKafkaEventReceive ⇒ Kind: global typedef
Returns: null
- Null
Param | Type |
---|---|
receivedEvent | Object |
eventData | any |