@saplings/utils
v0.18.0
Published
Use typed helpers for collection transforms, dates, identifiers, i18n, and permissions.
Readme
@saplings/utils
Use typed helpers for collection transforms, dates, identifiers, i18n, and permissions.
Install
npm install @saplings/utilsQuick start
import { chunk, groupBy, nanoid, unique } from '@saplings/utils';
const ids = unique(['draft', 'draft', 'published']);
console.log(chunk(ids, 1));
console.log(groupBy([{ status: 'draft' }, { status: 'published' }], (item) => item.status));
console.log(nanoid());API and integration
chunk(items, size)splits a list.unique(items)deduplicates using JavaScript set semantics.groupBy(items, keyOf)groups records by their derived key.- The root exports named functions and namespaces such as
fp,date, andpermissions. Check the manifest for supported subpaths before deep-importing. - Keep server-only helpers in Node.js entry points. Use
@saplings/hooksfor the React hooks library.
Read the integration guide for configuration and operational details.
JSON HTTP client
createJsonClient provides a small browser-compatible client with structured HTTP errors, a bounded timeout, and caller cancellation. Applications still own authentication, CSRF policy, validation and retry decisions.
import { createJsonClient, HttpRequestError } from '@saplings/utils/http';
const request = createJsonClient({ baseUrl: 'https://api.example.com', timeoutMs: 10000 });
try {
const projects = await request<{ id: string }[]>('/projects');
console.log(projects);
} catch (error) {
if (error instanceof HttpRequestError) console.error(error.status);
}Only root-relative paths are accepted. A caller's AbortSignal and the timeout both cancel the request. Responses with status 204 or 205 return undefined.
