@hyperse-hub/hps-common
Common utilities and shared types for Hub Base plugins.
Install
yarn add @hyperse-hub/hps-common
What is included
- Crypto helpers (
encrypt, decrypt) for AES-256-CBC scenarios
- Vendure entity helper (
safeAddCustomFields) to avoid duplicate custom fields
- Merge helpers (
mergeOption, mergeOptions, simpleDeepClone)
- Utility families for array/date/file/get/omit/path/pdf/pick/price/string/tree/typeorm/unique
- Shared utility types (
DeepPartial, Nullable, SetOptional, SetRequired, UnionTuple, MutationUploadFile)
Exported modules
crypto: encrypt(), decrypt()
entities: safeAddCustomFields()
merge-options: mergeOption(), mergeOptions(), simpleDeepClone()
types: DeepPartial, Nullable, SetOptional, SetRequired, UnionTuple, MutationUploadFile
utils-array: arrayChunk(), arrayFlatten(), arrayFlattenDeep(), arraysIntersect(), arrayRemove(), arrayUnique()
utils-date: dateUtcToZonedTime()
utils-file: stream/buffer helpers, temp/read stream helpers, directory merge helpers, image helpers
utils-get: get()
utils-omit: omit()
utils-path: URL/path parsing and mutation helpers (getQueryString, getURLRoot, modifyQueryString, urlJoin, etc.)
utils-pdf: createPDF()
utils-pick: pick()
utils-price: price()
utils-str: string helpers (capitalize, ensureSlash, normalizeSlug, parseJson, truncateSentences, etc.)
utils-tree: tree conversion/traversal/update helpers
utils-typeorm: transformBitToBoolean()
utils-unique: unique()
Quick usage
import {
encrypt,
safeAddCustomFields,
mergeOptions,
arrayUnique,
arraysIntersect,
getURLRoot,
modifyQueryString,
parseJson,
price,
} from '@hyperse-hub/hps-common';
const ids = arrayUnique([1, 2, 2, 3]);
const hasAdminRole = arraysIntersect(['admin', 'editor'], ['guest', 'admin']);
const root = getURLRoot('https://example.com/path?a=1#hash');
const next = modifyQueryString('/login?redirect=%2Fhome', 'lang', 'en');
const payload = parseJson('{"enabled":true}', { enabled: false });
const amount = price('10.00').add('2.50').format();
const cipherKey = Buffer.alloc(32, 1);
const encrypted = encrypt('hello', cipherKey);
const merged = mergeOptions(
{ feature: { enabled: false } },
{ feature: { enabled: true } }
);
Vendure example
import { safeAddCustomFields } from '@hyperse-hub/hps-common';
safeAddCustomFields(config, 'Product', [
{
name: 'badgeText',
type: 'string',
nullable: true,
},
]);
Development
yarn lint
yarn test-unit
yarn typecheck
Notes
arraysIntersect() compares object values by reference identity.
- URL helpers support both absolute URLs and relative paths.
await file/image/pdf helpers in server-side environments (Node.js).