Skip to content

Commit 5301eda

Browse files
committed
feat: release
0 parents  commit 5301eda

File tree

16 files changed

+1024
-0
lines changed

16 files changed

+1024
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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
24+
release:
25+
name: Release
26+
runs-on: ubuntu-latest
27+
needs: ["test"]
28+
if: ${{!contains(github.event.head_commit.message, 'skip-release') && !contains(github.event.head_commit.message, 'skip-ci') && github.event_name != 'pull_request' }}
29+
permissions:
30+
contents: write
31+
issues: write
32+
pull-requests: write
33+
id-token: write
34+
steps:
35+
- name: Checkout repo
36+
uses: actions/checkout@v4
37+
- uses: ./.github/setup
38+
39+
- name: Build
40+
run: bun run build
41+
42+
- name: Create Release
43+
id: semantic
44+
run: bunx semantic-release --branches main
45+
env:
46+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
47+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
48+
NPM_CONFIG_PROVENANCE: true

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.vscode
2+
node_modules
3+
*.log
4+
.DS_Store
5+
coverage
6+
dist
7+
types
8+
.conf*
9+
.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+
}

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.0.0
4+
5+
### 🚀 First Release
6+
7+
- Release (1457244)
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: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
## Why?
6+
7+
While working on a project, I began using ofetch. I appreciated many of its features, but I was particularly drawn to the concept of receiving both data and error objects with each call. This led me to create a fetch wrapper that incorporated this functionality , along with some features borrowed from ofetch.
8+
9+
## Installation
10+
11+
```bash
12+
pnpm install @better-tools/fetch
13+
```
14+
15+
## Usage
16+
17+
```typescript
18+
const { data, error } = await fetch<{
19+
userId: number;
20+
id: number;
21+
title: string;
22+
completed;
23+
}>("https://jsonplaceholder.typicode.com/todos/1");
24+
if (error) {
25+
// handle the error
26+
}
27+
if (data) {
28+
// handle the data
29+
}
30+
```
31+
32+
### ♯ Create a custom fetch
33+
34+
You can create a custom fetch with default options.
35+
36+
```typescript
37+
import { createFetch } from "better-auth";
38+
39+
const $fetch = createFetch({
40+
baseUrl: "https://jsonplaceholder.typicode.com",
41+
retry: 2,
42+
});
43+
44+
const { data, error } = await $fetch<{
45+
userId: number;
46+
id: number;
47+
title: string;
48+
completed: boolean;
49+
}>("/todos/1");
50+
```
51+
52+
### ♯ Parsing the response
53+
54+
Better fetch will smartly parse JSON using JSON.parse and if it fails it will return the response as text.
55+
56+
For binary content types, better fetch will instead return a Blob object.
57+
58+
You can also pass custom parser.
59+
60+
```typescript
61+
//parsed as JSON
62+
const { data, error } = await fetch("/todos/1");
63+
// Get the blob version of the response
64+
const { data, error } = await fetch("/api/image.png");
65+
// Return text as is
66+
await ofetch("/ok");
67+
//custom parser
68+
const { data, error } = await fetch("/todos/1", {
69+
parser: (text) => {
70+
return JSON.parse(text);
71+
},
72+
});
73+
```
74+
75+
### ♯ Handling Errors
76+
77+
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.
78+
79+
```typescript
80+
const { data, error } = await fetch<
81+
{
82+
userId: number;
83+
id: number;
84+
title: string;
85+
completed;
86+
},
87+
{
88+
message: string;
89+
}
90+
>("https://jsonplaceholder.typicode.com/todos/1");
91+
if (error) {
92+
// handle the error
93+
}
94+
```
95+
96+
> throws if the request fails
97+
98+
```typescript
99+
const { data, error } = await fetch<{
100+
userId: number;
101+
id: number;
102+
title: string;
103+
completed;
104+
}>("https://jsonplaceholder.typicode.com/todos/1", {
105+
throw: true,
106+
});
107+
```
108+
109+
### ♯ Auto Retry
110+
111+
You can set the number of retries.
112+
113+
```typescript
114+
const { data, error } = await fetch<{
115+
userId: number;
116+
id: number;
117+
title: string;
118+
completed;
119+
}>("https://jsonplaceholder.typicode.com/todos/1", {
120+
retry: 2,
121+
});
122+
```
123+
124+
### ♯ Timeout
125+
126+
You can set the timeout in milliseconds.
127+
128+
```typescript
129+
const { data, error } = await fetch<{
130+
userId: number;
131+
id: number;
132+
title: string;
133+
completed;
134+
}>("https://jsonplaceholder.typicode.com/todos/1", {
135+
timeout: 5000,
136+
});
137+
```
138+
139+
### ♯ Query Parameters
140+
141+
You can pass the query parameters as an object.
142+
143+
```typescript
144+
const { data, error } = await fetch<{
145+
userId: number;
146+
id: number;
147+
title: string;
148+
completed;
149+
}>("https://jsonplaceholder.typicode.com/todos/1", {
150+
query: {
151+
userId: 1,
152+
},
153+
});
154+
```
155+
156+
### ♯ Callbacks
157+
158+
You can pass callbacks for different events on the request lifecycle.
159+
160+
```typescript
161+
const { data, error } = await fetch<{
162+
userId: number;
163+
id: number;
164+
title: string;
165+
completed;
166+
}>("https://jsonplaceholder.typicode.com/todos/1", {
167+
onRequest: (request) => {
168+
console.log("Requesting", request);
169+
},
170+
onResponse: (response) => {
171+
console.log("Response", response);
172+
},
173+
onError: (error) => {
174+
console.log("Error", error);
175+
},
176+
onSuccess: (data) => {
177+
console.log("Success", data);
178+
},
179+
onRetry: (retry) => {
180+
console.log("Retrying", retry);
181+
},
182+
});
183+
```
184+
185+
### ♯ Native Fetch
186+
187+
You can use the native fetch by calling the native method.
188+
189+
```typescript
190+
import betterFetch from "better-fetch";
191+
const res = await betterFetch.native(
192+
"https://jsonplaceholder.typicode.com/todos/1"
193+
);
194+
```
195+
196+
## License
197+
198+
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@better-tools/fetch",
3+
"version": "1.0.0",
4+
"packageManager": "[email protected]",
5+
"devDependencies": {
6+
"@types/bun": "^1.1.0",
7+
"@types/node": "^20.11.30",
8+
"biome": "^0.3.3",
9+
"semantic-release": "^23.0.8",
10+
"tsup": "^8.0.2",
11+
"typescript": "^5.4.5"
12+
},
13+
"exports": {
14+
".": "./dist/index.js"
15+
},
16+
"scripts": {
17+
"build": "tsup",
18+
"lint": "biome check .",
19+
"release": "bun test && changelogen --release && npm publish && git push --follow-tags",
20+
"lint:fix": "biome check . --apply",
21+
"typecheck": "tsc --noEmit"
22+
}
23+
}

0 commit comments

Comments
 (0)