ilokesto

Fetcher

@ilokesto/fetcher is a small OpenAPI-aware wrapper around the real ky runtime. It keeps ky.create, extend, hooks, prefixUrl, custom fetch, lazy ResponsePromise parsing, and ky errors, then adds typed route templates, grouped shortcut requests, inferred .json() results, and a non-throwing safe surface.

Installation

Install fetcher together with its peer dependency ky.

npm install @ilokesto/fetcher ky

The package is published as a dist-based ESM package and currently targets Node.js 22 or newer for package tooling. Runtime behavior is delegated to ky, so browser, server, and test usage follow the same constraints as your chosen ky environment.

Quick example

import { createFetcher, type MergePaths } from '@ilokesto/fetcher/openapi';
import type { paths as GeneratedPaths } from './__generated__/openapi';

type ExtraPaths = {
  '/bff/checkout/quote': {
    post: {
      requestBody: {
        content: {
          'application/json': {
            cartId: string;
            couponCode?: string;
          };
        };
      };
      responses: {
        200: {
          content: {
            'application/json': {
              total: number;
              currency: 'KRW' | 'USD';
              discountApplied: boolean;
            };
          };
        };
      };
    };
  };
};

type ApiPaths = MergePaths<GeneratedPaths, ExtraPaths>;

const api = createFetcher<ApiPaths>({
  prefixUrl: '/api',
});

const user = await api
  .get('/users/{id}', {
    params: {
      path: { id: '42' },
      query: { include: 'profile' },
    },
  })
  .json();

const quote = await api
  .post('/bff/checkout/quote', {
    json: { cartId: 'cart_123', couponCode: 'WELCOME10' },
  })
  .json();

On this page