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 🙏

© 2026 – Pkg Stats / Ryan Hefner

apiplatform-fetch-builder

v0.0.5

Published

A TypeScript library for making typed fetch requests to ApiPlatform and Hydra APIs, supporting pagination, sorting, filtering, and more.

Readme

apiplatform-fetch-builder

GitHub stars GitHub issues npm downloads License: MIT

apiplatform-fetch-builder is a TypeScript library designed to simplify and streamline interactions with ApiPlatform / Hydra APIs. It provides a builder-pattern interface for making typed fetch requests, handling pagination, sorting, filtering, property selection, and includes an optional entityServiceBuilder for more advanced resource CRUD operations with type safety.

Features

  • Typed fetch requests
  • Builder-pattern interface (pagination, sorting, filtering, property selection)
  • Hydra & ApiPlatform compatibility
  • Optional entityServiceBuilder for CRUD (Create, Read, Update, Delete) operations with typed IRIs
  • No bundler required

Installation

npm install apiplatform-fetch-builder
# or
yarn add apiplatform-fetch-builder
# or
pnpm add apiplatform-fetch-builder

Quick Usage Example

import builder from "apiplatform-fetch-builder";

const api = builder("https://api.example.com", {
  getToken: async () => "your_jwt_token_here",
  onUnauthorized: async () => {
    console.log("Unauthorized! Redirecting to login...");
  },
});

// Simple GET
const result = await api.get<{ items: { id: number; name: string }[] }>("/items").fetch();

if (result.success) {
  console.log("Fetched items:", result.data.items);
} else {
  console.error("Failed to fetch items:", result.error);
}

// GET with pagination, sorting, filtering, and property selection
const getOptions = {
  pagination: true,
  pageIndex: 0,
  pageSize: 20,
  sortBy: [{ id: "name", desc: false }],
  filters: [{ id: "category", value: "books" }],
  properties: ["id", "name"] as const,
};

const paginatedResult = await api
  .get<{ items: { id: number; name: string }[] }>("/items")
  .withOptions(getOptions)
  .fetch();

if (paginatedResult.success) {
  console.log("Paginated items:", paginatedResult.data.items);
}

Using entityServiceBuilder

import fetchBuilder from "apiplatform-fetch-builder";
import entityServiceBuilder from "apiplatform-fetch-builder/entity-service-builder"; 
import type { Company, CompanyBody, CompanyIri } from "./types/company";

// Create a typed service builder
const companyService = entityServiceBuilder<CompanyIri, CompanyBody, Company>(
  fetchBuilder("https://api.example.com"), 
  "/companies"
);

// GET collection with options
const companiesResult = await companyService.getAll({
  pagination: true,
  pageIndex: 1,
  pageSize: 10,
  sortBy: [{ id: "name", desc: false }],
  properties: ["name", "description", "ceo", "employees.id"] as const,
});

if (companiesResult.success) {
  console.log("Companies:", companiesResult.data["hydra:member"]);
}

// GET single item
const companyResult = await companyService.get(1);
if (companyResult.success) {
  console.log("Company:", companyResult.data);
}

// CREATE new item
const createResult = await companyService.create({
  name: "New Company",
  description: "We build new things",
});
if (createResult.success) {
  console.log("Created company:", createResult.data);
}

API

builder(entrypoint: string, config?: BuilderConfig)

Parameters:

  • entrypoint: string: Base URL of your API (e.g., "https://api.example.com").
  • config?: BuilderConfig: Optional config object.
    • getToken?: () => string | null | Promise<string | null>
    • onUnauthorized?: () => void | Promise<void>

Returns: An object with methods get, post, patch, put, and delete.


get(url: string)

Returns an object with:

  • fetch(options?: FetchOptions): Performs a GET request.
  • withOptions(getOptions: GetOptions): Applies pagination, sorting, filtering, property selection.

post(url: string), patch(url: string), put(url: string), delete(url: string)

Similar to get but for respective HTTP methods. post, patch, and put accept a request body. delete returns null data on success.


entityServiceBuilder(...)

Parameters:

  • Generic type parameters: <IriType, BodyType, EntityType>
  • Accepts a fetcher (from builder(...)) or a string/entrypoint object.
  • entityPath: string for the resource (e.g. "/companies").

Returns:
An object with methods: create, get, getAll, update, replace, delete.


Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

Licensed under the MIT License.