desfetch 1.0.0
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/LICENSE +21 -0
- package/README.md +193 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +1 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NotZero
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# desfetch
|
|
2
|
+
|
|
3
|
+
A tiny type-safe `fetch` wrapper for destructured { data, error } results, with automatic JSON parsing, custom parsers, and no `try/catch`.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Automatically parses JSON responses
|
|
8
|
+
- Supports custom response parsers
|
|
9
|
+
- TypeScript-friendly
|
|
10
|
+
- Returns `{ data, error }` instead of throwing
|
|
11
|
+
- Uses the native `fetch` API
|
|
12
|
+
- Allows passing a custom `fetch` implementation
|
|
13
|
+
- Zero dependencies
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add desfetch
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or with npm:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install desfetch
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Basic usage
|
|
30
|
+
|
|
31
|
+
By default, `desfetch` parses the response as JSON:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import desfetch from "desfetch";
|
|
35
|
+
|
|
36
|
+
const { data, error } = await desfetch<User>("/api/user");
|
|
37
|
+
|
|
38
|
+
if (error) {
|
|
39
|
+
console.error(error);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
console.log(data.name);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### POST request
|
|
47
|
+
|
|
48
|
+
`desfetch` accepts all standard `RequestInit` options:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const { data, error } = await desfetch<User>("/api/user", {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: {
|
|
54
|
+
"Content-Type": "application/json",
|
|
55
|
+
},
|
|
56
|
+
body: JSON.stringify({
|
|
57
|
+
name: "John",
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Custom parsing
|
|
63
|
+
|
|
64
|
+
You can provide your own parser when the response isn't JSON.
|
|
65
|
+
|
|
66
|
+
For example, parsing plain text:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const { data, error } = await desfetch<string>("/api/message", {
|
|
70
|
+
parse: (response) => response.text(),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
console.log(data);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Or parsing a `Blob`:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const { data, error } = await desfetch<Blob>("/api/file", {
|
|
80
|
+
parse: (response) => response.blob(),
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Custom `fetch`
|
|
85
|
+
|
|
86
|
+
The third argument allows you to provide a custom `fetch` implementation:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const { data, error } = await desfetch<User>(
|
|
90
|
+
"/api/user",
|
|
91
|
+
{},
|
|
92
|
+
customFetch,
|
|
93
|
+
);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
This can be useful with frameworks that provide their own `fetch` implementation:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
const { data, error } = await desfetch<User>("/api/user", {}, fetch);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Error handling
|
|
103
|
+
|
|
104
|
+
`desfetch` never throws instead, it returns one of these shapes:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
{
|
|
108
|
+
data: T;
|
|
109
|
+
error: null;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
or:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
{
|
|
117
|
+
data: null;
|
|
118
|
+
error: Error;
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
For example:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
const { data, error } = await desfetch<User>("/api/user");
|
|
126
|
+
|
|
127
|
+
if (error) {
|
|
128
|
+
console.error("Request failed:", error);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
console.log(data.name);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
HTTP responses outside the `2xx` range are treated as errors:
|
|
136
|
+
|
|
137
|
+
```text
|
|
138
|
+
HTTP 404: Not Found
|
|
139
|
+
HTTP 500: Internal Server Error
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## API
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
type DesFetchOptions<T> = RequestInit & {
|
|
146
|
+
parse?: (response: Response) => Promise<T>;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const desfetch = async <T = unknown>(
|
|
150
|
+
url: string | URL,
|
|
151
|
+
options?: DesFetchOptions<T>,
|
|
152
|
+
fetchInstance?: typeof globalThis.fetch,
|
|
153
|
+
): Promise<
|
|
154
|
+
| { data: T; error: null }
|
|
155
|
+
| { data: null; error: Error }
|
|
156
|
+
>;
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### `url`
|
|
160
|
+
|
|
161
|
+
The URL to request.
|
|
162
|
+
|
|
163
|
+
Accepts either a `string` or `URL`.
|
|
164
|
+
|
|
165
|
+
### `options`
|
|
166
|
+
|
|
167
|
+
All standard `RequestInit` options are supported, plus:
|
|
168
|
+
|
|
169
|
+
#### `parse`
|
|
170
|
+
|
|
171
|
+
A function responsible for converting the `Response` into the desired type.
|
|
172
|
+
|
|
173
|
+
The default parser is:
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
(response) => response.json();
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### `fetchInstance`
|
|
180
|
+
|
|
181
|
+
Optional custom `fetch` implementation.
|
|
182
|
+
|
|
183
|
+
Defaults to:
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
globalThis.fetch
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Simple fetch, typed data, explicit errors.
|
|
190
|
+
|
|
191
|
+
## Source Code
|
|
192
|
+
|
|
193
|
+
Since this lib is MIT licensed, you can also contribute to it at it's repo on [GitHub](https://github.com/yspoof/desfetch)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var o=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var i=Object.prototype.hasOwnProperty;var T=(t,r)=>{for(var s in r)o(t,s,{get:r[s],enumerable:!0})},l=(t,r,s,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let e of c(r))!i.call(t,e)&&e!==s&&o(t,e,{get:()=>r[e],enumerable:!(n=a(r,e))||n.enumerable});return t};var p=t=>l(o({},"__esModule",{value:!0}),t);var w={};T(w,{default:()=>f});module.exports=p(w);var u=async t=>await t.json(),h=async(t,{parse:r=u,...s}={},n=globalThis.fetch)=>{try{let e=await n(t,s);if(!e.ok)throw new Error(`HTTP ${e.status}: ${e.statusText}`);return{data:await r(e),error:null}}catch(e){return{data:null,error:e instanceof Error?e:new Error(String(e))}}},f=h;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type DesFetchOptions<T> = RequestInit & {
|
|
2
|
+
parse?: (response: Response) => Promise<T>;
|
|
3
|
+
};
|
|
4
|
+
declare const desfetch: <T = unknown>(url: string | URL, { parse, ...options }?: DesFetchOptions<T>, fetchInstance?: typeof globalThis.fetch) => Promise<{
|
|
5
|
+
data: Awaited<T>;
|
|
6
|
+
error: null;
|
|
7
|
+
} | {
|
|
8
|
+
data: null;
|
|
9
|
+
error: Error;
|
|
10
|
+
}>;
|
|
11
|
+
export default desfetch;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var o=async t=>await t.json(),a=async(t,{parse:r=o,...s}={},n=globalThis.fetch)=>{try{let e=await n(t,s);if(!e.ok)throw new Error(`HTTP ${e.status}: ${e.statusText}`);return{data:await r(e),error:null}}catch(e){return{data:null,error:e instanceof Error?e:new Error(String(e))}}},c=a;export{c as default};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "desfetch",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A tiny type-safe fetch wrapper for destructured { data, error } results, with automatic JSON parsing, custom parsers, and no try/catch.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"api",
|
|
7
|
+
"client",
|
|
8
|
+
"fetch",
|
|
9
|
+
"http",
|
|
10
|
+
"request"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "n0tz3r0",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/index.cjs",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"require": "./dist/index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"tsup": "^8.5.1",
|
|
29
|
+
"typescript": "^7.0.2"
|
|
30
|
+
},
|
|
31
|
+
"devEngines": {
|
|
32
|
+
"packageManager": {
|
|
33
|
+
"name": "pnpm",
|
|
34
|
+
"version": "11.24.0",
|
|
35
|
+
"onFail": "download"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup && tsc",
|
|
40
|
+
"dev": "tsup --watch"
|
|
41
|
+
}
|
|
42
|
+
}
|