@zap-studio/fetch 0.4.5 → 0.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,145 +1,134 @@
1
1
  # @zap-studio/fetch
2
2
 
3
- ## 0.4.5
3
+ ## 0.4.7
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - Updated dependencies [f75b984]
8
- - @zap-studio/validation@0.3.0
7
+ - e26293e: Updated dependencies.
8
+ - @zap-studio/validation@0.3.2
9
9
 
10
- ## 0.4.4
10
+ ## 0.4.6
11
11
 
12
12
  ### Patch Changes
13
13
 
14
- - 59a8d71: Fix request body handling for JSON payloads.
14
+ - 5ea3d3b: Updated dependencies.
15
+ - @zap-studio/validation@0.3.1
15
16
 
16
- - Accept JSON values (including arrays) in `ExtendedRequestInit.body`
17
- - Auto-stringify plain JSON body values regardless of whether a response schema is provided
18
- - Set `Content-Type: application/json` only when auto-stringifying and no explicit content type is set
17
+ ## 0.4.5
19
18
 
20
- ## 0.4.3
19
+ ### Dependencies
21
20
 
22
- ### Patch Changes
21
+ - f75b984: Updated dependency `@zap-studio/validation` to `0.3.0`.
23
22
 
24
- - e4542bb: Refine `standardValidate` typings so the return type depends on the `throwOnError` flag, and update `@zap-studio/fetch` to integrate with the new overloads while preserving its boolean configuration API.
25
- - Updated dependencies [e4542bb]
26
- - @zap-studio/validation@0.2.1
23
+ ## 0.4.4
27
24
 
28
- ## 0.4.2
25
+ ### Fixed
29
26
 
30
- ### Patch Changes
27
+ - 59a8d71: Fixed JSON request body handling.
28
+ - Accept JSON values (including arrays) in `ExtendedRequestInit.body`.
29
+ - Auto-stringify plain JSON body values even when no response schema is provided.
30
+ - Set `Content-Type: application/json` only when auto-stringifying and no explicit content type is set.
31
31
 
32
- - Updated dependencies [2de8183]
33
- - @zap-studio/validation@0.2.0
32
+ ## 0.4.3
34
33
 
35
- ## 0.4.1
34
+ ### Changed
36
35
 
37
- ### Patch Changes
36
+ - e4542bb: Refined `standardValidate` typings so return types depend on `throwOnError`, and updated `@zap-studio/fetch` integration while preserving the existing boolean configuration API.
38
37
 
39
- - 447dbda: Extract shared Standard Schema validation utilities into `@zap-studio/validation` and update `@zap-studio/fetch` to depend on them.
40
- - Updated dependencies [447dbda]
41
- - @zap-studio/validation@0.1.0
38
+ ### Dependencies
42
39
 
43
- ## 0.4.0
40
+ - e4542bb: Updated dependency `@zap-studio/validation` to `0.2.1`.
44
41
 
45
- ### Minor Changes
42
+ ## 0.4.2
46
43
 
47
- - 69057cd: Expose fetch defaults constants and utility helpers as public exports.
44
+ ### Dependencies
48
45
 
49
- ## 0.3.1
46
+ - 2de8183: Updated dependency `@zap-studio/validation` to `0.2.0`.
50
47
 
51
- ### Patch Changes
48
+ ## 0.4.1
52
49
 
53
- - 9919f63: Add discriminated return types based on `throwOnValidationError` option
50
+ ### Changed
54
51
 
55
- The return type of `$fetch` and `api.*` methods now correctly narrows based on the `throwOnValidationError` option:
52
+ - 447dbda: Switched shared Standard Schema validation utilities to `@zap-studio/validation`.
56
53
 
57
- - When `throwOnValidationError: true` (default) or unspecified: returns `Promise<TSchema>` (the validated data directly)
58
- - When `throwOnValidationError: false`: returns `Promise<StandardSchemaV1.Result<TSchema>>` (the result object with `value` or `issues`)
54
+ ### Dependencies
59
55
 
60
- This improves type safety by eliminating the need for manual type narrowing when using the default behavior.
56
+ - 447dbda: Updated dependency `@zap-studio/validation` to `0.1.0`.
61
57
 
62
- ## 0.3.0
58
+ ## 0.4.0
63
59
 
64
- ### Minor Changes
60
+ ### Added
65
61
 
66
- - 659621c: Add `searchParams` option to `createFetch` to allow factory-level default query/search parameters. Per-request `searchParams` continue to override factory defaults.
62
+ - 69057cd: Exposed fetch defaults constants and utility helpers as public exports.
67
63
 
68
- ## 0.2.2
64
+ ## 0.3.1
69
65
 
70
- ### Patch Changes
66
+ ### Changed
71
67
 
72
- - 5c3abbf: Prepare JSR publish and isolatedDeclarations support with new explicit types for `$Fetch` and `ApiMethods`
68
+ - 9919f63: Added discriminated return types based on `throwOnValidationError`.
69
+ - `throwOnValidationError: true` (default) returns `Promise<TSchema>`.
70
+ - `throwOnValidationError: false` returns `Promise<StandardSchemaV1.Result<TSchema>>`.
71
+ - This improves type safety and removes manual narrowing in default usage.
73
72
 
74
- ## 0.2.1
73
+ ## 0.3.0
75
74
 
76
- ### Patch Changes
75
+ ### Added
77
76
 
78
- - 82bac5c: Replace regex-based slash trimming with more efficient string manipulation functions for URL normalization
77
+ - 659621c: Added `searchParams` support in `createFetch` for factory-level default query parameters.
78
+ - Per-request `searchParams` still override factory defaults.
79
79
 
80
- ## 0.2.0
80
+ ## 0.2.2
81
81
 
82
- ### Minor Changes
82
+ ### Changed
83
83
 
84
- - 78afb76: ### Standard Schema Support
84
+ - 5c3abbf: Prepared JSR publish and `isolatedDeclarations` support with explicit `$Fetch` and `ApiMethods` types.
85
85
 
86
- Migrated from Zod-only validation to **Standard Schema v1** specification, enabling support for multiple validation libraries:
86
+ ## 0.2.1
87
87
 
88
- - Zod
89
- - Valibot
90
- - ArkType
91
- - Any Standard Schema compliant library
88
+ ### Changed
92
89
 
93
- ### New Features
90
+ - 82bac5c: Replaced regex-based slash trimming with more efficient string manipulation for URL normalization.
94
91
 
95
- - **Factory Pattern**: `createFetch()` for creating pre-configured fetch instances with `baseURL`, default `headers`, and error handling options
96
- - **Smart URL Handling**: Absolute URLs bypass `baseURL` configuration
97
- - **Auto JSON Body**: Automatic `JSON.stringify()` and `Content-Type` header when using schemas with request bodies
92
+ ## 0.2.0
98
93
 
99
- ### Breaking Changes
94
+ ### Changed
100
95
 
101
- - Schema validation now requires Standard Schema compliant libraries (Zod 3.23+, Valibot 1.0+, ArkType 2.0+)
102
- - Internal file structure reorganized (affects deep imports if any were used)
103
- - `FetchError` constructor signature changed: now requires `(message, response)`
96
+ - 78afb76: Migrated from Zod-only validation to Standard Schema v1 for broader validator compatibility.
97
+ - Supported libraries include Zod, Valibot, ArkType, and other Standard Schema-compliant validators.
104
98
 
105
- ### Migration Guide
99
+ ### Added
106
100
 
107
- ```typescript
108
- // Before (Zod-only)
109
- import { $fetch } from "@zap-studio/fetch";
110
- import { z } from "zod";
101
+ - 78afb76: Added `createFetch()` factory pattern for pre-configured instances.
102
+ - 78afb76: Added smart URL behavior so absolute URLs bypass `baseURL`.
103
+ - 78afb76: Added automatic JSON body serialization and `Content-Type` handling for schema-based requests.
111
104
 
112
- // After (Standard Schema - works the same with Zod!)
113
- import { $fetch } from "@zap-studio/fetch";
114
- import { z } from "zod"; // Zod 3.23+ is Standard Schema compliant
105
+ ### Breaking
115
106
 
116
- // Or use other libraries
117
- import * as v from "valibot";
118
- import { type } from "arktype";
119
- ```
107
+ - 78afb76: Standard Schema-compliant validator libraries are now required (for example Zod 3.23+, Valibot 1.0+, ArkType 2.0+).
108
+ - 78afb76: Internal file structure was reorganized (affects deep imports).
109
+ - 78afb76: `FetchError` constructor now requires `(message, response)`.
120
110
 
121
111
  ## 0.1.2
122
112
 
123
- ### Patch Changes
113
+ ### Changed
124
114
 
125
- - 69c2b21: Change `safeFetch` to `$fetch` syntax and make sure `safeFetch` can also be used for legacy
115
+ - 69c2b21: Renamed `safeFetch` to `$fetch` while preserving `safeFetch` compatibility for legacy usage.
126
116
 
127
117
  ## 0.1.1
128
118
 
129
- ### Patch Changes
119
+ ### Changed
130
120
 
131
- - 5f1812b: Change files field in package.json to distribute only necessary artifacts
121
+ - 5f1812b: Updated `files` in `package.json` to publish only required artifacts.
132
122
 
133
123
  ## 0.1.0
134
124
 
135
- ### Minor Changes
136
-
137
- - 1644006: Comprehensive description of the initial release features including:
125
+ ### Added
138
126
 
139
- - Type-safe HTTP requests with Zod validation
140
- - Automatic content-type handling
141
- - Multiple response type support
142
- - Convenient API methods (GET, POST, PUT, PATCH, DELETE)
143
- - Flexible error handling
144
- - Custom FetchError class
145
- - Full TypeScript support
127
+ - 1644006: Initial release of `@zap-studio/fetch`.
128
+ - Type-safe HTTP requests with Zod validation.
129
+ - Automatic content-type handling.
130
+ - Multiple response type support.
131
+ - API methods for GET, POST, PUT, PATCH, and DELETE.
132
+ - Flexible error handling.
133
+ - Custom `FetchError` class.
134
+ - Full TypeScript support.
package/bin/intent.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ // Auto-generated by @tanstack/intent setup
3
+ // Exposes the intent end-user CLI for consumers of this library.
4
+ await import("@tanstack/intent/intent-library");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zap-studio/fetch",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -34,12 +34,15 @@
34
34
  "files": [
35
35
  "dist",
36
36
  "CHANGELOG.md",
37
- "LICENSE.md",
38
- "README.md"
37
+ "LICENSE",
38
+ "README.md",
39
+ "skills",
40
+ "bin",
41
+ "!skills/_artifacts"
39
42
  ],
40
43
  "dependencies": {
41
44
  "@standard-schema/spec": "^1.1.0",
42
- "@zap-studio/validation": "0.3.0"
45
+ "@zap-studio/validation": "0.3.2"
43
46
  },
44
47
  "devDependencies": {
45
48
  "@types/node": "^25.0.2",
@@ -50,9 +53,9 @@
50
53
  "valibot": "^1.2.0",
51
54
  "vitest": "^4.0.18",
52
55
  "zod": "^4.2.0",
53
- "@zap-studio/vitest-config": "0.0.0",
54
56
  "@zap-studio/tsdown-config": "0.0.0",
55
- "@zap-studio/typescript-config": "0.0.0"
57
+ "@zap-studio/typescript-config": "0.0.0",
58
+ "@zap-studio/vitest-config": "0.0.0"
56
59
  },
57
60
  "exports": {
58
61
  ".": "./dist/index.mjs",
@@ -65,9 +68,12 @@
65
68
  "main": "./dist/index.mjs",
66
69
  "module": "./dist/index.mjs",
67
70
  "types": "./dist/index.d.mts",
71
+ "bin": {
72
+ "intent": "./bin/intent.js"
73
+ },
68
74
  "scripts": {
69
75
  "build": "tsdown --config tsdown.config.ts",
70
- "check": "tsc --noEmit",
76
+ "typecheck": "tsc --noEmit",
71
77
  "test": "vitest run",
72
78
  "test:watch": "vitest --watch"
73
79
  }
@@ -0,0 +1,152 @@
1
+ ---
2
+ name: zap-fetch-typed-http
3
+ description: >
4
+ Implement type-safe HTTP requests with @zap-studio/fetch using $fetch,
5
+ api.get/post/put/patch/delete, createFetch defaults, searchParams merging,
6
+ and throwOnFetchError/throwOnValidationError return modes.
7
+ type: core
8
+ library: '@zap-studio/fetch'
9
+ library_version: '0.4.6'
10
+ sources:
11
+ - 'zap-studio/monorepo:packages/fetch/README.md'
12
+ - 'zap-studio/monorepo:packages/fetch/src/index.ts'
13
+ - 'zap-studio/monorepo:packages/fetch/src/utils.ts'
14
+ ---
15
+
16
+ # @zap-studio/fetch — Typed HTTP Client
17
+
18
+ ## Setup
19
+
20
+ ```ts
21
+ import { createFetch } from '@zap-studio/fetch';
22
+ import { z } from 'zod';
23
+
24
+ const UserSchema = z.object({
25
+ id: z.number(),
26
+ name: z.string(),
27
+ email: z.string().email(),
28
+ });
29
+
30
+ const { api } = createFetch({
31
+ baseURL: 'https://api.example.com',
32
+ headers: { Authorization: 'Bearer token' },
33
+ searchParams: { locale: 'en' },
34
+ });
35
+
36
+ const user = await api.get('/users/1', UserSchema);
37
+ ```
38
+
39
+ ## Core Patterns
40
+
41
+ ### Return raw `Response` when no schema is provided
42
+
43
+ ```ts
44
+ import { $fetch } from '@zap-studio/fetch';
45
+
46
+ const response = await $fetch('https://api.example.com/health');
47
+ const data = await response.json();
48
+ ```
49
+
50
+ ### Request validated payload via schema
51
+
52
+ ```ts
53
+ import { api } from '@zap-studio/fetch';
54
+ import { z } from 'zod';
55
+
56
+ const PostSchema = z.object({ id: z.number(), title: z.string() });
57
+ const post = await api.get('https://api.example.com/posts/1', PostSchema);
58
+ ```
59
+
60
+ ### Handle non-throw validation mode explicitly
61
+
62
+ ```ts
63
+ import { $fetch } from '@zap-studio/fetch';
64
+ import { z } from 'zod';
65
+
66
+ const UserSchema = z.object({ id: z.number() });
67
+
68
+ const result = await $fetch('/users/1', UserSchema, {
69
+ throwOnValidationError: false,
70
+ });
71
+
72
+ if (result.issues) {
73
+ throw new Error('Invalid response payload');
74
+ }
75
+
76
+ console.log(result.value.id);
77
+ ```
78
+
79
+ ## Common Mistakes
80
+
81
+ ### HIGH Assuming `api.get` returns raw `Response`
82
+
83
+ Wrong:
84
+
85
+ ```ts
86
+ const res = await api.get('/users/1', { headers: { Authorization: token } });
87
+ const body = await res.json();
88
+ ```
89
+
90
+ Correct:
91
+
92
+ ```ts
93
+ const user = await api.get('/users/1', UserSchema, {
94
+ headers: { Authorization: token },
95
+ });
96
+ console.log(user.id);
97
+ ```
98
+
99
+ `api.*` overloads are schema-based helpers; they return validated payloads, not a `Response`.
100
+
101
+ Source: zap-studio/monorepo:packages/fetch/src/index.ts
102
+
103
+ ### HIGH Ignoring validation result branch
104
+
105
+ Wrong:
106
+
107
+ ```ts
108
+ const user = await $fetch('/users/1', UserSchema, {
109
+ throwOnValidationError: false,
110
+ });
111
+ console.log(user.id);
112
+ ```
113
+
114
+ Correct:
115
+
116
+ ```ts
117
+ const result = await $fetch('/users/1', UserSchema, {
118
+ throwOnValidationError: false,
119
+ });
120
+
121
+ if (result.issues) throw new Error('Invalid payload');
122
+ console.log(result.value.id);
123
+ ```
124
+
125
+ With `throwOnValidationError: false`, return type is `{ value?, issues? }` and must be narrowed.
126
+
127
+ Source: zap-studio/monorepo:packages/fetch/src/index.ts
128
+
129
+ ### MEDIUM Relying on implicit body semantics for non-JSON inputs
130
+
131
+ Wrong:
132
+
133
+ ```ts
134
+ await api.post('/users', UserSchema, {
135
+ body: new URLSearchParams({ name: 'Ada' }),
136
+ });
137
+ ```
138
+
139
+ Correct:
140
+
141
+ ```ts
142
+ await api.post('/users', UserSchema, {
143
+ body: { name: 'Ada' },
144
+ headers: { 'Content-Type': 'application/json' },
145
+ });
146
+ ```
147
+
148
+ Only plain object bodies are auto-JSON-stringified; other `BodyInit` forms keep their native encoding behavior.
149
+
150
+ Source: zap-studio/monorepo:packages/fetch/src/utils.ts
151
+
152
+ See also: zap-validation-standard-schema/SKILL.md — response validation result handling.
File without changes