2.0.8 • Published 5 months ago

blob-common v2.0.8

Weekly downloads
3
License
MIT
Repository
github
Last release
5 months ago

NPM module for common functions in backend

useful in a setup with

  • serverless stack
  • AWS

Usage

All functions can be imported from their respective modules (results in lowest packages size), e.g.:

import { sleep } from 'blob-common/core/sleep';
import { btoa } from 'blob-common/core/base64';

All functions can be imported directly from the core, e.g.:

import { sleep, btoa } from 'blob-common';

NB: the sanitize function is not included in the core, due to the relatively large size of imported package.

Generic functions

ModuleFunctionDescription
/base64otob(object)takes an object and coverts to base64 string
/base64btoa(base64String)inverse: takes a base64 string and returns an object
/datenow()returns a string of the current date in format yyyy-mm-dd
/datediffDate(dateStr, days)takes a dateStr in standard format and difference, returns a dateStr in the standard format
/dateexpireDate(dateStr)takes a dateStr in standard format and returns a date 30 days later in the standard format
/handlerhandler(lamdba)wrapper function for event handler (to log errors)
/handlergetUserFromEvent(event)returns the userId (from AWS cognito user pool) of authenticated User
/handlerapiCall(promise)async wrapper for promise, returns the a tuple [err, result]
/idsnewPhotoId()returns a newly generated photoId (starts with P)
/idsnewGroupId()returns a newly generated groupId (starts with G)
/idsnewAlbumId()returns a newly generated albumId (starts with A)
/RNDRNDreturns a random number between 1 and 15, usable as an index key in dynamoDb to spread database load on very large queries
/sleepsleep(ms)await sleep(1000) sleeps for 1 second. Used in tests to wait for dynamoDB to have processed updates
/sanitizesanitize(dirty)sanitizes a string from any sequences that may cause security issues (e.g. when using name string in an email message)

DynamoDb functions

ModuleFunctionDescription
/dbdynamoDb.get(params)returns a promise for DynamoDb function using the params. Uses AWS-SDK, refer to official docs for more info. Also has functions .put, .update, .delete, query, transact (for TransactWrite)
/dbdbUpdate(PK, SK, key, newVal)returns a promise for a single update on the database, setting key to new value provided in newVal
/dbdbUpdateMulti(PK, SK, newKV)returns a promise for a single update on the database at multiple keys. newKV is an object containing keys to be updated with new values
/dbCleancleanRecord(object)removes keys RK, datePK, dateSK, cognitoId from the object - useful for storing data as derived items in other items (e.g. owner as part of photo object)
/dbCreatedbItem(object)returns an object with extended keys for database: createdAt, RK, datePK, dateSK
/dbCreatedbCreateItem(object)returns a promise to create an expanded item in database from object (object must have PK and SK keys)

Some caveats on the specific dynamoDB functions:

  • eu-central-1 is the standard region chosen
  • to work, there should be a process.env.photoTable variable for all functions (except dynamoDb)
  • database must have PK (string) as primary key, and SK (string) as sortkey

S3 and SES functions

ModuleFunctionDescription
/s3s3.getMetadata(params)returns a promise for an s3 function using the params.
/s3s3.delete(params)deletes stuff.
/s3s3.get(params)gets an object.
/s3s3.list(params)lists a bucket probably.
/s3s3.getSignedUrl(params)gets a signed url for ADDING a file to s3 (for uploads).
/s3s3.getSignedUrlGet(params)gets a signed url for READING a file from s3.
/sesses.send(params)sends an email from ses using params - returns promise
/sesses.sendEmail(params)sends an email from ses using {toEmail, fromEmail, subject, data, textData} - returns promise

NB:

  • toEmail can be either a string or an array of strings (multiple recipients)

Caveats:

  • s3 function uses s3 bucket specified in process.env.photoBucket or process.env.devBucket

email helper functions

ModuleFunctionDescription
/emailemailBody(rows)returns email body with rows
/emailrow(cells)generic row containing array of cells
/emailheaderRow(logoSrc, frontendUrl)header row with logo, links to frontend
/emailfooterRow
/emailphotoRow(photoSrc, linkUrl)row containing (header) photo
/emailtextCell(subtype)cell containing greeting, paragraph or buttonEscape (small text with link)
/emaildividerCell(dividerSrc)
/emailbuttonCell(text, link)
/emailcodeCell(code)big text block centered with code
/emailsignatureCell(src)left aligned signature under email
/emailgreeting(text)
/emailparagraph(text)
/emailbuttonEscape(url)
/emailmakeEmailSrc(url, width, height)returns publicly accessible url for image in S3 bucket

Caveats:

  • makeEmailSrc uses s3 bucketname from process.env.bucket or process.env.devBucket

Cognito functions

ModuleFunctionDescription
/cognitocognito.disableUser(userId)disables a user in the cognito user pool
/cognitocognito.deleteUser(userId)deletes a user in the cognito user pool

Caveats:

  • uses cognito userpool id from process.env.userPoolId or process.env.devUserPoolId
2.0.5

5 months ago

2.0.4

5 months ago

2.0.7

5 months ago

2.0.6

5 months ago

2.0.8

5 months ago

2.0.3

5 months ago

2.0.2

5 months ago

2.0.1

5 months ago

1.4.6

1 year ago

1.4.9

1 year ago

1.4.11

1 year ago

1.4.8

1 year ago

1.4.10

1 year ago

1.4.7

1 year ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.13

2 years ago

1.3.12

2 years ago

1.3.11

3 years ago

1.3.10

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.19

4 years ago

1.2.18

4 years ago

1.2.17

4 years ago

1.2.16

4 years ago

1.2.15

4 years ago

1.2.14

4 years ago

1.2.13

4 years ago

1.2.12

4 years ago

1.2.11

4 years ago

1.2.10

4 years ago

1.2.9

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.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago