typesafe-api-call is a minimalistic JavaScript library designed to streamline API interactions by enforcing typesafety and a functional programming paradigm. It abstracts away traditional exception handling, instead ensuring that every API call resolves to an explicit `APISuccess` or `APIFailure` result, compelling developers to handle both successful and unsuccessful outcomes distinctly. The library is currently on version 5.1.2 and appears to have an active release cadence, with recent patches and new features like `callWithRetries` introduced in minor versions. A key differentiator is its emphasis on functional results over exceptions, promoting predictable state management. It also integrates seamlessly with complementary tools like `type-decoder` and `type-crafter` to facilitate YAML-driven type and decoder generation, further enhancing end-to-end typesafety for API responses.
npm install typesafe-api-callVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic GET API call using `APICaller`, showing how to define a request, provide decoding functions, and handle the explicit `APISuccess` or `APIFailure` result.
Upgrade to `typesafe-api-call@^5.1.1` or later to ensure proper module resolution across different environments. Prefer ESM imports where possible.
Always check the `APIResponse` object using `if (response instanceof APISuccess)` or `if (response instanceof APIFailure)` to handle outcomes, rather than relying on `try/catch` for HTTP-level errors.
Implement robust decoding logic within the success and error callbacks passed to `APICaller.call`. Consider using complementary libraries like `type-decoder` for structured decoding.
Ensure that the `url` property of your `APIRequest` object is always instantiated as `new URL('your-api-endpoint')`.Ensure you are using `import { APICaller } from 'typesafe-api-call';` (ESM) and have updated to `typesafe-api-call@^5.1.1` or later. Verify that `APICaller` is correctly imported and not `undefined`.Add `import { APISuccess } from 'typesafe-api-call';` to the top of your file. Remember it's a named export.Ensure your `APIResponse` is correctly typed (e.g., `APIResponse<MyDataType, ErrorType>`) and that the success/error decoding functions passed to `APICaller.call` return the expected types. The example's `(successResponse: unknown) => successResponse as Post[]` is a minimal cast; for production, implement actual type guards or decoders.
No dependency data recorded yet.