Skip to main content
Home

A type-safe fetch wrapper with Standard Schema validation.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
94%
Published
4 hours ago (0.4.0)
function $fetch
$fetch<TSchema extends StandardSchemaV1>(
resource: string,
schema: TSchema,
options: ExtendedRequestInit<false>,
): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>

Type-safe fetch wrapper with Standard Schema validation.

  • When throwOnValidationError: true: validated data of type TSchema
  • When throwOnValidationError: false: Standard Schema Result object { value?, issues? }
  • When throwOnFetchError: true: throws FetchError on non-ok responses

If no schema is provided, returns the raw Response object.

Examples

import { z } from "zod"; import { $fetch } from "@zap-studio/fetch";

const UserSchema = z.object({ id: z.number(), name: z.string() });

// Basic usage (schema validation) const user = await $fetch("/api/users/1", UserSchema, { headers: { "Authorization": "Bearer token" } }); console.log("Validated user:", user);

// Raw usage (no schema validation and typed Response object) const result = await $fetch("/api/data", { method: "POST", body: JSON.stringify({ key: "value" }) }); const json = await result.json() as ResultType; console.log("Raw response data:", json);

// Usage with validation errors returned instead of thrown const result = await $fetch("/api/users/1", UserSchema, { throwOnValidationError: false });

if (result.issues) { console.error("Validation errors:", result.issues); } else { console.log("Validated user:", result.value); }

Type Parameters

TSchema extends StandardSchemaV1

Parameters

resource: string
schema: TSchema
options: ExtendedRequestInit<false>

Return Type

Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>

Throws

FetchError

When throwOnFetchError: true and response is not ok

ValidationError

When throwOnValidationError: true and validation fails

$fetch<TSchema extends StandardSchemaV1>(
resource: string,
schema: TSchema,
options?: ExtendedRequestInit<true | undefined>,
): Promise<StandardSchemaV1.InferOutput<TSchema>>

Type Parameters

TSchema extends StandardSchemaV1

Parameters

resource: string
schema: TSchema
optional
options: ExtendedRequestInit<true | undefined>

Return Type

Promise<StandardSchemaV1.InferOutput<TSchema>>
$fetch(
resource: string,
): Promise<Response>

Parameters

resource: string
optional
options: ExtendedRequestInit

Return Type

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@zap-studio/fetch

Import symbol

import { $fetch } from "@zap-studio/fetch";
or

Import directly with a jsr specifier

import { $fetch } from "jsr:@zap-studio/fetch";

Add Package

pnpm i jsr:@zap-studio/fetch
or (using pnpm 10.8 or older)
pnpm dlx jsr add @zap-studio/fetch

Import symbol

import { $fetch } from "@zap-studio/fetch";

Add Package

yarn add jsr:@zap-studio/fetch
or (using Yarn 4.8 or older)
yarn dlx jsr add @zap-studio/fetch

Import symbol

import { $fetch } from "@zap-studio/fetch";

Add Package

vlt install jsr:@zap-studio/fetch

Import symbol

import { $fetch } from "@zap-studio/fetch";

Add Package

npx jsr add @zap-studio/fetch

Import symbol

import { $fetch } from "@zap-studio/fetch";

Add Package

bunx jsr add @zap-studio/fetch

Import symbol

import { $fetch } from "@zap-studio/fetch";