MSF TypeScript – Module Outline¶
@msflib/typescript is the shared, framework-agnostic TypeScript/JavaScript utility package for the MSF ecosystem. It ships a fetch-based HTTP client with consistent error handling plus a set of small object, function and string helpers. It has no runtime dependencies, works in any modern browser or Node 20+, and is the HTTP layer underneath @msflib/core in the msflib React modules.
Quick Start¶
1. Configure the registry (once)¶
The package lives on GitHub Packages. Create a GitHub personal access token (classic) with read:packages, export it as GITHUB_TOKEN, and add this .npmrc to your project:
@msflib:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
2. Install¶
pnpm add @msflib/typescript
3. Create a client and make a call¶
import { createApiClient, ApiResponseError } from '@msflib/typescript';
const { apiClient } = createApiClient({
baseURL: 'https://api.example.com',
getAccessToken: () => localStorage.getItem('access_token'),
});
try {
const notes = await apiClient<{ id: number; title: string }[]>('GET', '/notes');
console.log(notes);
} catch (error) {
if (error instanceof ApiResponseError) {
console.error(error.status, error.message, error.errors);
}
}
That's it. For login, file uploads, error display and common gotchas, see the full Quick Start.
Core module¶
- Errors (
error.ts,type.ts) — The foundation the HTTP client is built on.UserError(a user-safe message pluscause) and its subclassApiResponseError(status,statusText, field-levelerrors,rawbody);defaultErrorBodyParser, which understands FastAPI, Laravel-style and generic error bodies; theErrorBodyParserhook for custom shapes; and theClientErrorDispatchevent contract for global error reporting.
Feature modules¶
- HTTP Client (
http.ts) —createApiClientreturns threefetchwrappers sharing one config:apiClient(JSON, bearer token, query strings),apiFormDataClient(multipart uploads) andloginClient(x-www-form-urlencodedlogin). SupportspreRequest/postRequestdecorators and a pluggable error-body parser. Also exportsbuildQueryString. - Object Utilities (
object.ts) —omit,pick,omitUndefined(dropsundefinedandnull),nestedGet/nestedSetfor dot-paths,toRecordfor indexing lists by key, andasFormDatafor turning objects intoFormData. - Function Utilities (
function.ts) —debounce(trailing-edge). - String Utilities (
string.ts) —makeIdfor deterministic, readable element ids andslugifyfor URL slugs.
Overall architecture¶
- One package, one entry point: everything is re-exported from
@msflib/typescript(src/index.ts), and types are exported alongside values. It is built withtsupto CommonJS (dist/index.js), ESM (dist/index.mjs) and type declarations (dist/index.d.ts). - The error layer is the foundation:
createApiClientroutes every failure through it. Network failures go throughmakeNetworkErrorand become aUserError. Non-2xx responses go throughmakeResponseError, which runs the configuredErrorBodyParserand returns anApiResponseError. Both also notify the optionalemitErrorcallback, so an app gets a typed exception and a global event from the same place. - The HTTP client is stateless and framework-agnostic: auth is pulled per request from
getAccessToken, and cross-cutting behavior plugs in throughapiClientDecoratorrather than by forking. Framework integrations (e.g.@msflib/core'sconfiguredApiClientin the React modules) wrap it rather than reimplementing it. - The utility modules are independent of each other and of the HTTP layer, with no shared state, so you can use any one of them on its own.
- Published to GitHub Packages under the
@msflibscope (publish.yml, triggered byv*.*.*tags); CI runs the Vitest suite on pushes and PRs tomain/dev.
Module index¶
Working on this repo¶
pnpm install
pnpm build # tsup -> dist/
pnpm test # vitest
pnpm lint # tsc --noEmit
# docs (one-time setup, then serve/build)
python3 -m venv .venv-docs && .venv-docs/bin/pip install -r requirements-docs.txt
source .venv-docs/bin/activate
pnpm docs:serve # http://127.0.0.1:8000/docsite/typescript/
pnpm docs:build # mkdocs build --strict