@drawbridge/drawbridge-utils
v0.0.26
Published
Shared helpers and data used across the drawbridge-* repositories.
Readme
@drawbridge/drawbridge-utils
Shared helpers and data used across the drawbridge-* repositories.
Main entry
const utils = require( '@drawbridge/drawbridge-utils' );
// or: import { capitalize } from '@drawbridge/drawbridge-utils';Exports:
bytesToGB( bytes )/bytesToMB( bytes )— byte conversion.capitalize( string )— uppercases the first character.constants— default record shape per Mongo collection name (action, brand, campaign, draw, member, organization, page, prize, range, referrer, template, usage, user).currencies/currency( code )— list of ISO currencies (key/value/code/digits) and lookup by code.expiredPaymentMethod( card )— checks if a card record's year/month is in the past.formatCurrency({ code, digits, display, locale, value })— Intl-based formatter.display: 'standard'(default) returns Intl's standard string;'parts'splits symbol/number for custom layouts.formatDateString({ date, locale, time, options })— Intl-based date formatter.time: trueadds hour/minute.formatNumber( number, digits )— Intl-based number formatter with fixed-digit min/max.getPlanFeature( plan, key )—{ granted, message }lookup.infinite,isInfinite( value ),megabyte,gigabyte— size constants.nanoid()— 8-char base36 random ID.percentage( value1, value2, decimals )—(value1 / value2) * 100, fixed decimals (default 1).reducers.fields— reducer for grouping fields byadditional/leadtype.regex.url/regex.protocols/regex.domain— shared regex constants.shareUrls({ formUri, organization, page })— builds share URLs from the configured form base URI. Falls back toprocess.env.APP_CLIENT_FORM_URIifformUriis not passed (server-only).urlRoot( string )—'https://'if input contains'https', else'http://'.
Encrypt entry (server-only)
const { encrypt, decrypt } = require( '@drawbridge/drawbridge-utils/encrypt' );Server-only because it uses Node crypto and reads process.env.ENCRYPT_CONNECTION_SECRET. Exposed as a sub-export so client bundles don't pull Node built-ins.
Axios entry (server-only)
const { safe, axios } = require( '@drawbridge/drawbridge-utils/axios' );safe.get( url, axiosOptions )— SSRF-protected GET request.axios— raw axios passthrough for advanced usage.
Circuit entry (server-only)
const { circuit } = require( '@drawbridge/drawbridge-utils/circuit' );circuit({ name, threshold, timeout })— circuit breaker for async operations.
Upload entry
const { allowedUploadTypes, allowedMimes, resolveUploadType, isAllowedMime } = require( '@drawbridge/drawbridge-utils/upload' );allowedUploadTypes/allowedMimes— shared whitelist constants.resolveUploadType( type, extension )— resolves an upload type from MIME + extension.isAllowedMime( mime )— predicate for the MIME whitelist.
Nanoid entry (server-only)
const { nanoid, retry } = require( '@drawbridge/drawbridge-utils/nanoid' );nanoid()— 8-char base36 random ID (same as the main export).retry({ field, operation, maxRetries })— runs an async operation that generates a nanoid, retrying on Mongo duplicate-key errors.
Build & publish
nvm use
npm install
npm run buildbuild runs tsup (multi-entry, dual CJS + ESM output) and then npm publish. Bump version in package.json first.
