A small library for sending serialized data and receiving deserialized data with strict data type checking. This library is built on top of the Fetch API and provides additional features like caching, error handling, and support for serializable classes.
You can use the following command to install this package:
npm install ts-fetch
import { tfetch } from "ts-fetch";
// Example with primitive types
const fetchNumber = async () => {
const result: number = await tfetch({
url: "https://example.com/number",
returnType: 0
});
console.log(result); // Logs the number fetched from the API
};
fetchNumber();
import { tfetch } from "ts-fetch";
import { TestClass } from "./fixtures/TestClass";
const fetchClass = async () => {
const result: TestClass = await tfetch({
url: "https://example.com/class",
returnType: TestClass
});
console.log(result instanceof TestClass); // true
};
fetchClass();
import { CrudHttpRepository } from "ts-fetch";
import { TestClass } from "./fixtures/TestClass";
class TestRepository extends CrudHttpRepository<TestClass> {
protected apiRoot = "https://example.com/api";
protected modelConstructor = TestClass;
}
const repository = new TestRepository();
const fetchData = async () => {
const item = await repository.getById(1);
console.log(item);
};
fetchData();
The library provides custom error classes for handling network and backend errors:
import { tfetch } from "ts-fetch";
const fetchWithErrorHandling = async () => {
try {
await tfetch({
url: "https://example.com/error"
});
} catch (error) {
console.error(error);
}
};
fetchWithErrorHandling();
GET and HEAD requests are cached automatically to improve performance. The cache is cleared when an error occurs or when the request completes successfully.