npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@labeg/tfetch

v0.8.3

Published

A small library for sending serialized data and receiving deserialized data with strict data type checking.

Readme

Typescript Serializable Fetch

npm version npm downloads GitHub build status CodeQL

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.

Installation

You can use the following command to install this package:

npm install @labeg/tfetch

Usage

Basic Usage

import { tfetch } from "@labeg/tfetch";

// 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();

Working with Serializable Classes

To use automatic deserialization with classes, you need to use the ts-serializable library to define your models:

import { tfetch } from "@labeg/tfetch";
import { Serializable, jsonProperty } from "ts-serializable";

class User extends Serializable {
    @jsonProperty(String)
    public name: string = "";

    @jsonProperty(String)
    public email: string = "";

    @jsonProperty(Number)
    public age: number = 0;
}

const fetchUser = async () => {
    const result: User = await tfetch({
        url: "https://example.com/api/user/1",
        returnType: User
    });
    console.log(result instanceof User); // true
    console.log(result.name); // Properly deserialized
};

fetchUser();

POST Request with Body

import { tfetch } from "@labeg/tfetch";

const createUser = async () => {
    const result = await tfetch({
        method: "POST",
        url: "https://example.com/api/users",
        body: {
            name: "John Doe",
            email: "[email protected]"
        },
        returnType: Object
    });
    console.log(result);
};

createUser();

Custom Headers

import { tfetch } from "@labeg/tfetch";

const fetchWithHeaders = async () => {
    const result = await tfetch({
        method: "GET",
        url: "https://example.com/api/data",
        headers: {
            "Authorization": "Bearer your-token-here",
            "X-Custom-Header": "custom-value"
        },
        returnType: Object
    });
    console.log(result);
};

fetchWithHeaders();

All HTTP Methods

import { tfetch } from "@labeg/tfetch";

// GET request
const getData = async () => {
    return await tfetch({
        method: "GET",
        url: "https://example.com/api/resource",
        returnType: Object
    });
};

// POST request
const postData = async () => {
    return await tfetch({
        method: "POST",
        url: "https://example.com/api/resource",
        body: { data: "value" },
        returnType: Object
    });
};

// PUT request
const updateData = async () => {
    return await tfetch({
        method: "PUT",
        url: "https://example.com/api/resource/1",
        body: { data: "updated value" }
    });
};

// DELETE request
const deleteData = async () => {
    return await tfetch({
        method: "DELETE",
        url: "https://example.com/api/resource/1"
    });
};

Advanced Fetch Options

You can pass any standard Fetch API options:

import { tfetch } from "@labeg/tfetch";

const advancedRequest = async () => {
    const result = await tfetch({
        method: "POST",
        url: "https://example.com/api/data",
        body: { key: "value" },
        returnType: Object,
        // Standard Fetch API options
        cache: "no-cache",
        credentials: "include",
        mode: "cors",
        redirect: "follow",
        referrerPolicy: "no-referrer",
        signal: AbortSignal.timeout(5000), // 5 second timeout
    });
    console.log(result);
};

advancedRequest();

Working with FormData

import { tfetch } from "@labeg/tfetch";

const uploadFile = async (file: File) => {
    const formData = new FormData();
    formData.append("file", file);
    formData.append("description", "My file");

    const result = await tfetch({
        method: "POST",
        url: "https://example.com/api/upload",
        body: formData,
        returnType: Object
    });
    console.log(result);
};

CRUD Operations with CrudHttpRepository

import { CrudHttpRepository } from "@labeg/tfetch";
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();

Error Handling

The library provides custom error classes for handling network and backend errors:

import { tfetch } from "@labeg/tfetch";

const fetchWithErrorHandling = async () => {
    try {
        await tfetch({
            url: "https://example.com/error"
        });
    } catch (error) {
        console.error(error);
    }
};

fetchWithErrorHandling();

Caching

GET and HEAD requests are cached automatically to improve performance. The cache is cleared when an error occurs or when the request completes successfully.