Skip to content

Commit face9b1

Browse files
committed
feat: release
0 parents  commit face9b1

File tree

17 files changed

+1013
-0
lines changed

17 files changed

+1013
-0
lines changed

.github/setup/action.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Setup Workflow
2+
description: Composite action that sets up bun and installs dependencies
3+
runs:
4+
using: "composite"
5+
steps:
6+
- uses: actions/setup-node@v4
7+
with:
8+
node-version: 20.x
9+
10+
- uses: oven-sh/setup-bun@v1
11+
with:
12+
bun-version: 1.0.26
13+
14+
- run: bun install
15+
shell: bash

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
7+
jobs:
8+
test:
9+
name: test
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repo
13+
uses: actions/checkout@v4
14+
- uses: ./.github/setup
15+
16+
- name: Lint
17+
run: bun lint
18+
19+
- name: Typecheck
20+
run: bun typecheck
21+
22+
- name: Test
23+
run: bun test

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
*.log
3+
.DS_Store
4+
coverage
5+
dist
6+
types
7+
.conf*
8+
.env

.releaserc.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"plugins": [
3+
[
4+
"@semantic-release/commit-analyzer",
5+
{
6+
"preset": "angular",
7+
"releaseRules": [
8+
{
9+
"type": "refactor",
10+
"release": false
11+
},
12+
{
13+
"type": "style",
14+
"release": "patch"
15+
},
16+
{
17+
"scope": "no-release",
18+
"release": false
19+
},
20+
{
21+
"scope": "cli",
22+
"release": "patch"
23+
},
24+
{
25+
"scope": "patch",
26+
"release": "patch"
27+
},
28+
{
29+
"scope": "minor",
30+
"release": "patch"
31+
}
32+
]
33+
}
34+
],
35+
"@semantic-release/github",
36+
"@semantic-release/release-notes-generator",
37+
"@semantic-release/npm"
38+
]
39+
}

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"biome.enabled": true,
3+
"editor.defaultFormatter": "biomejs.biome",
4+
"[typescript]": {
5+
"editor.defaultFormatter": "biomejs.biome"
6+
}
7+
}

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Changelog
2+
3+
## v1.1.0
4+
5+
### 🚀 First Release
6+
7+
- Works
8+
9+
### ❤️ Contributors
10+
11+
- Bekacru <[email protected]>

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Bereket Engida
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.

README.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# Better Fetch
2+
3+
A fetch wrapper for typescript that returns the data and the error object. Works on the browser, node (version 18+), workers, deno and bun. Some of the APIs are inspired by [ofetch](https://github.com/unjs/ofetch).
4+
5+
## Installation
6+
7+
```bash
8+
pnpm install @better-tools/fetch
9+
```
10+
11+
## Usage
12+
13+
```typescript
14+
const { data, error } = await fetch<{
15+
userId: number;
16+
id: number;
17+
title: string;
18+
completed: boolean;
19+
}>("https://jsonplaceholder.typicode.com/todos/1");
20+
if (error) {
21+
// handle the error
22+
}
23+
if (data) {
24+
// handle the data
25+
}
26+
```
27+
28+
### ♯ Create a custom fetch
29+
30+
You can create a custom fetch with default options.
31+
32+
```typescript
33+
import { createFetch } from "better-auth";
34+
35+
const $fetch = createFetch({
36+
baseUrl: "https://jsonplaceholder.typicode.com",
37+
retry: 2,
38+
});
39+
40+
const { data, error } = await $fetch<{
41+
userId: number;
42+
id: number;
43+
title: string;
44+
completed: boolean;
45+
}>("/todos/1");
46+
```
47+
48+
### ♯ Parsing the response
49+
50+
Better fetch will smartly parse JSON using JSON.parse and if it fails it will return the response as text.
51+
52+
For binary content types, better fetch will instead return a Blob object.
53+
54+
You can also pass custom parser.
55+
56+
```typescript
57+
//parsed as JSON
58+
const { data, error } = await fetch("/todos/1");
59+
// Get the blob version of the response
60+
const { data, error } = await fetch("/api/image.png");
61+
// Return text as is
62+
await ofetch("/ok");
63+
//custom parser
64+
const { data, error } = await fetch("/todos/1", {
65+
parser: (text) => {
66+
return JSON.parse(text);
67+
},
68+
});
69+
```
70+
71+
### ♯ Handling Errors
72+
73+
By default better fetch will return the error object if the request fails. You can also pass the throw option to throw if the request fails.
74+
75+
```typescript
76+
const { data, error } = await fetch<
77+
{
78+
userId: number;
79+
id: number;
80+
title: string;
81+
completed;
82+
},
83+
{
84+
message: string;
85+
}
86+
>("https://jsonplaceholder.typicode.com/todos/1");
87+
if (error) {
88+
// handle the error
89+
}
90+
```
91+
92+
> throws if the request fails
93+
94+
```typescript
95+
const { data, error } = await fetch<{
96+
userId: number;
97+
id: number;
98+
title: string;
99+
completed;
100+
}>("https://jsonplaceholder.typicode.com/todos/1", {
101+
throw: true,
102+
});
103+
```
104+
105+
### ♯ Auto Retry
106+
107+
You can set the number of retries.
108+
109+
```typescript
110+
const { data, error } = await fetch<{
111+
userId: number;
112+
id: number;
113+
title: string;
114+
completed;
115+
}>("https://jsonplaceholder.typicode.com/todos/1", {
116+
retry: 2,
117+
});
118+
```
119+
120+
### ♯ Timeout
121+
122+
You can set the timeout in milliseconds.
123+
124+
```typescript
125+
const { data, error } = await fetch<{
126+
userId: number;
127+
id: number;
128+
title: string;
129+
completed;
130+
}>("https://jsonplaceholder.typicode.com/todos/1", {
131+
timeout: 5000,
132+
});
133+
```
134+
135+
### ♯ Query Parameters
136+
137+
You can pass the query parameters as an object.
138+
139+
```typescript
140+
const { data, error } = await fetch<{
141+
userId: number;
142+
id: number;
143+
title: string;
144+
completed;
145+
}>("https://jsonplaceholder.typicode.com/todos/1", {
146+
query: {
147+
userId: 1,
148+
},
149+
});
150+
```
151+
152+
### ♯ Callbacks
153+
154+
You can pass callbacks for different events on the request lifecycle.
155+
156+
```typescript
157+
const { data, error } = await fetch<{
158+
userId: number;
159+
id: number;
160+
title: string;
161+
completed;
162+
}>("https://jsonplaceholder.typicode.com/todos/1", {
163+
onRequest: (request) => {
164+
console.log("Requesting", request);
165+
},
166+
onResponse: (response) => {
167+
console.log("Response", response);
168+
},
169+
onError: (error) => {
170+
console.log("Error", error);
171+
},
172+
onSuccess: (data) => {
173+
console.log("Success", data);
174+
},
175+
onRetry: (retry) => {
176+
console.log("Retrying", retry);
177+
},
178+
});
179+
```
180+
181+
### ♯ Native Fetch
182+
183+
You can use the native fetch by calling the native method.
184+
185+
```typescript
186+
import betterFetch from "better-fetch";
187+
const res = await betterFetch.native(
188+
"https://jsonplaceholder.typicode.com/todos/1"
189+
);
190+
```
191+
192+
## License
193+
194+
MIT

biome.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.6.3/schema.json",
3+
"organizeImports": {
4+
"enabled": true
5+
},
6+
"linter": {
7+
"enabled": true,
8+
"rules": {
9+
"recommended": true,
10+
"complexity": {
11+
"useLiteralKeys": "off"
12+
},
13+
"a11y": {
14+
"useKeyWithClickEvents": "off"
15+
},
16+
"suspicious": {
17+
"noExplicitAny": "off"
18+
}
19+
}
20+
}
21+
}

bun.lockb

274 KB
Binary file not shown.

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@better-tools/fetch",
3+
"version": "1.1.0",
4+
"packageManager": "[email protected]",
5+
"main": "./dist/index.cjs",
6+
"module": "./dist/index.js",
7+
"react-native": "./dist/index.js",
8+
"types": "./dist/index.d.ts",
9+
"devDependencies": {
10+
"@types/bun": "^1.1.0",
11+
"@types/node": "^20.11.30",
12+
"biome": "^0.3.3",
13+
"semantic-release": "^23.0.8",
14+
"tsup": "^8.0.2",
15+
"typescript": "^5.4.5"
16+
},
17+
"exports": {
18+
".": {
19+
"import": "./dist/index.js",
20+
"require": "./dist/index.cjs"
21+
}
22+
},
23+
"scripts": {
24+
"build": "tsup",
25+
"lint": "biome check .",
26+
"release": "bun run build && npm publish --access public",
27+
"lint:fix": "biome check . --apply",
28+
"typecheck": "tsc --noEmit"
29+
},
30+
"files": [
31+
"dist"
32+
],
33+
"type": "module"
34+
}

0 commit comments

Comments
 (0)