1.4.2 • Published 4 years ago

@wbc-sprout/shared v1.4.2

Weekly downloads
216
License
ISC
Repository
gitlab
Last release
4 years ago

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

ParamTypeDescription
configObjectthe cors configuration
config.whitelistArray.<string>Array of url to whitelist
config.whitelistRegExArray.<RegExp>Array of url to whitelist using matching
config.appURLstringThe application url

CorsOptions : Object

Kind: global typedef
Properties

NameTypeDescription
credentialsbooleanThe cors credentials.
exposedHeadersArray.<string>The headers to expose
originfunctionThe origin function

jwtConfig

Kind: global constant
Properties

NameType
ACTIVATION_TOKEN_LIFESPANstring
SECRET_KEYstring
ACCESS_TOKEN_LIFESPANstring
REFRESH_TOKEN_LIFESPANstring

regExp

Kind: global constant
Properties

NameType
NUMBERRegExp
POSITIVE_NUMBERRegExp
POSITIVE_NUMBER_DECIMAL_POINTRegExp
EMAILRegExp
PHONE_NUMBERRegExp
ALPHA_NUMERICRegExp
ALPHA_NUMERIC_COMMA_DOTRegExp
ALPHA_NUMERIC_PLANERegExp
PASSWORDRegExp
URLRegExp

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

NameType
systemOverviewSystemOverviewRelatedPrivileges
userPrivileges
coursePrivileges
rolePrivileges
blogPostBlogPostRelatedPrivileges
subscriptionSubscriptionRelatedPrivileges
trainingMaterialPrivileges
appointmentPrivileges

SystemOverviewRelatedPrivileges

Kind: global typedef
Properties

NameType
READstring

SubscriptionRelatedPrivileges

Kind: global typedef
Properties

NameType
CREATEstring
REVOKEstring

BlogPostRelatedPrivileges

Kind: global typedef
Properties

NameType
CREATEstring
UPDATEstring
DELETEstring

Privileges

Kind: global typedef
Properties

NameType
CREATEstring
READstring
UPDATEstring
DELETEstring

Error Classes

Classes

CustomError

Kind: global class

new CustomError(message, config)

ParamType
messagestring
configObject
config.httpStatusCodenumber
config.stackTracestring
config.subCodenumber
config.reason*

RequestValidationError

Kind: global class

new RequestValidationError(message, config)

ParamType
messagestring
configObject
config.subCodenumber
config.reason*

AuthenticationError

Kind: global class

new AuthenticationError(message, config)

ParamType
messagestring
configObject
config.subCodenumber
config.reason*

NotFoundError

Kind: global class

new NotFoundError(message, config)

ParamType
messagestring
configObject
config.subCodenumber
config.reason*

ConflictError

Kind: global class

new ConflictError(message, config)

ParamType
messagestring
configObject
config.subCodenumber
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

ParamTypeDescription
varToCheck*variable to check its type
typestringThe 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

ParamTypeDescription
newEnvObjectThe new environment variable(s) as object

paginate(model) ⇒ PaginationFunction

Paginates model

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

ParamTypeDescription
modelObjectmodel to add pagination to
model.findAndCountAllfunctioninbuilt model function

removeFileExtension(fileName) ⇒ string | *

To remove extension from a file

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

ParamTypeDescription
fileNamestringThe 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

ParamTypeDescription
base64stringthe 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

ParamTypeDescription
objObjectobject to pick key value pair from
acceptedKeysArray.<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

ParamTypeDescription
objObjectObject to create from
forbiddenKeysArray.<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

ParamTypeDescription
urlstringThe url to extract base domain from

getCookieDomain(req) ⇒ string

To get cookie domain

Kind: global function
Returns: string - The base domain

ParamTypeDescription
reqObjectExpress request object

getTokensFromRequest(req) ⇒ Object

To get tokens from request

Kind: global function
Returns: Object - Token Object

ParamTypeDescription
reqObjectExpress request object

setTokensToResponse(res, tokens, cookieDomain) ⇒ string

To set token to response

Kind: global function
Returns: string - Referer Url

ParamTypeDescription
resObjectExpress response object
tokensObjectThe tokens to set
tokens.accessstringThe access token to set
tokens.refreshstringThe access token to set
cookieDomainstringThe domain to set the token cookies on

normalizePort(port) ⇒ boolean | number | *

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

ParamTypeDescription
portstring | numberThe port to normalize

convertToSlug(text) ⇒ string | *

Kind: global function
Returns: string | * - slug

ParamTypeDescription
textstringthe text to convert

PaginationFunction ⇒ Object

Kind: global typedef
Returns: Object - Paginated data

ParamTypeDefaultDescription
pagestring | number1page to query
limitstring | number10limit for the query
optionsObjectOptions for the model

base64Encode(val, urlEncode) ⇒ string

Kind: global function
Returns: string - The encoded string

ParamType
val*
urlEncodeboolean

base64Decode(val, urlEncoded) ⇒ string

Kind: global function
Returns: string - The decoded string

ParamType
val*
urlEncodedboolean

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

ParamTypeDescription
userObjectThe user to create a token for
lifeSpanstringThe lifespan of the token to be created

verifyToken(token, includeSignature) ⇒ Object

To verify a given jwt

Kind: global function
Returns: Object - the user object

ParamTypeDefaultDescription
tokenStringthe access token string
includeSignatureBooleantrueWhether 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

ParamTypeDescription
userObjectThe 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

ParamTypeDescription
refreshTokenstringthe 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

ParamTypeDescription
userObjectThe 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

ParamTypeDescription
refreshTokenstringthe refresh token

JWTHelperVerifyToken ⇒ Object

To verify a given jwt

Kind: global typedef
Returns: Object - the user object

ParamTypeDefaultDescription
tokenStringthe access token string
includeSignatureBooleantrueWhether or not to include signature

JWTHelperMakeToken ⇒ undefined | string

To generate a jwt

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

ParamTypeDescription
userObjectThe user to create a token for
lifeSpanstringThe lifespan of the token to be created

JWTHelper : Object

JWT Helper

Kind: global typedef
Properties

NameType
generateTokensJWTHelperGenerateToken
renewAccessTokenJWTHelperRenewAccessToken
verifyTokenJWTHelperVerifyToken
makeTokenJWTHelperMakeToken

App Logger

logger

Application logger

Kind: global constant
Properties

NameType
stream*
addfunction
infofunction

Response Helper

Functions

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

Kind: global function
Returns: HTMLDocument - Full html content

ParamTypeDefaultDescription
reqObjectExpress request object
resObjectExpress response object
templateStringHttp status code for the response
dataObjectData to be return as part of response body
statusCodeNumber200Http status code for the response

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

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

ParamTypeDescription
resobjectExpress response object
statusCodeNumberHttp status code for the response
dataobjectData to be return as part of response body
messageStringMessage accompanying the response data

okResponse(res, data, message) ⇒ JSON

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

ParamTypeDescription
resobjectExpress response object
dataobjectData to be return as part of response body
messageStringMessage accompanying the response data

createdResponse(res, data, message) ⇒ JSON

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

ParamTypeDescription
resobjectExpress response object
dataobjectData to be return as part of response body
messageStringMessage accompanying the response data

redirectResponse(res, url, code) ⇒ Null

Kind: global function
Returns: Null - Null

ParamTypeDescription
resobjectExpress response object
urlStringurl to redirect to
codeNumber301 or 302 based on permanent or temporary

Uploader

Constants

Functions

Typedefs

uploader

Uploader

Kind: global constant
Properties

NameType
initializeUploaderInitialize
uploadImageFromDataURIUploadImageFromDataURI
uploadImageFromFileUploadImageFromDataURI
removeImageUploaderRemoveImage
getFilesInFolderUploaderGetFilesInFolder

toDataUri(config) ⇒ *

This function converts the buffer to data url

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

ParamTypeDescription
configObject | FileExpress request object

getBaseFolder() ⇒ string

Kind: global function
Returns: string - base folder

constructFolder(folder) ⇒ string

Kind: global function
Returns: string - refined folder

ParamType
folderstring

UploaderInitialize ⇒ null

Kind: global typedef
Returns: null - Null

ParamTypeDescription
reqObjectExpress request object
resObjectExpress response object
nextfunctionExpress next function

UploadImageFromDataURI ⇒ Promise.<*>

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

ParamTypeDescription
dataURIstringa base64 representation of the file
fileNamestringName of the file to upload
folderstringthe folder to upload the file excluding the main folder

UploadImageFromFile ⇒ Promise.<*>

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

ParamTypeDescription
dataURIstringa base64 representation of the file
fileNamestringName of the file to upload
folderstringthe folder to upload the file excluding the main folder

UploaderRemoveImage ⇒ *

Kind: global typedef
Returns: * - Null

ParamTypeDescription
linkstringThe cloudinary link to the image

UploaderGetFilesInFolder ⇒ Promise.<any>

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

ParamTypeDescription
folderstringthe folder to load the files from

Validators

Constants

Typedefs

bodyValidator

Kind: global constant
Properties

NameType
emailCustomValidator
passwordCustomValidator
confirmCustomConfirmValidator
matchPatternCustomMatchPatterValidator
requiredCustomRequiredValidator

CustomValidator ⇒ *

Kind: global typedef
Returns: * - Validation chain

ParamTypeDescription
fieldNamestringThe field to validate
messagestringError message

CustomConfirmValidator ⇒ *

Kind: global typedef
Returns: * - Validation chain

ParamTypeDescription
fieldNamestringThe field to validate
benchmarkstringWhat to benchmark against
messagestringError message

CustomMatchPatterValidator ⇒ *

Kind: global typedef
Returns: * - Validation chain

ParamTypeDescription
fieldNamestringThe field to validate
patternRegExppattern to test for
messagestringError message

CustomRequiredValidator ⇒ Array

Kind: global typedef
Returns: Array - Validation chain

ParamTypeDescription
...fieldNamesstringName of the required field(s)

Middlewares

decodeCurrentUser(accessTokenKey) ⇒ function

Kind: global function
Returns: function - Express middleware function

ParamTypeDefault
accessTokenKeystring"accessToken"

errorHandler(includeStackTrace) ⇒ function

Kind: global function
Returns: function - Express middleware function

ParamTypeDescription
includeStackTracebooleanWhether or not to include stack trace

requireAuthentication(message) ⇒ function

Kind: global function
Returns: function - Express middleware function

ParamType
messagestring

multerUploads

Kind: global constant
Properties

NameType
singlefunction
anyfunction
arrayfunction
fieldsfunction
nonefunction

preloadSwagger(documentation) ⇒ function

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

ParamTypeDescription
documentationObjectSwagger json documentation

refreshAccessToken(config) ⇒ function

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

ParamTypeDescription
configObjectConfiguration object
config.forceRefreshEvenIfNotExpirebooleanWhether 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

ParamTypeDescription
requirementsObject
requirements.hasstringChecks for this privilege
requirements.hasAllArray.<string>Checks for all privilege in this array
requirements.hasAnyArray.<string>Checks for any privilege in this array
errorMessagestringError message to show user in case requirement not met

Services

Constants

Functions

Typedefs

subscribableEvents

Kind: global constant
Properties

NameType
USER_CREATEDstring
COURSE_CREATEDstring
SEND_EMAILstring
EMAIL_SENTstring

initializeKafka(callback, config) ⇒ Object

Kind: global function
Returns: Object - kafka

ParamType
callbackfunction
configObject
config.showProducerLogboolean

registerKafkaConsumer(topics, onReceive) ⇒ null

Kind: global function
Returns: null - Null

ParamType
topicsArray
onReceiveOnKafkaEventReceive

publishKafkaEvent(topicName, eventData, eventKey) ⇒ null

Kind: global function
Returns: null - Null

ParamType
topicNamestring
eventData*
eventKeystring

OnKafkaEventReceive ⇒ null

Kind: global typedef
Returns: null - Null

ParamType
receivedEventObject
eventDataany
1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.9

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago