Skip to content

Commit

Permalink
feat: release
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekacru committed Apr 22, 2024
0 parents commit 5301eda
Show file tree
Hide file tree
Showing 16 changed files with 1,024 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Setup Workflow
description: Composite action that sets up bun and installs dependencies
runs:
using: "composite"
steps:
- uses: actions/setup-node@v4
with:
node-version: 20.x

- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.26

- run: bun install
shell: bash
48 changes: 48 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CI
on:
push:
branches:
- main

jobs:
test:
name: test
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- uses: ./.github/setup

- name: Lint
run: bun lint

- name: Typecheck
run: bun typecheck

- name: Test
run: bun test
release:
name: Release
runs-on: ubuntu-latest
needs: ["test"]
if: ${{!contains(github.event.head_commit.message, 'skip-release') && !contains(github.event.head_commit.message, 'skip-ci') && github.event_name != 'pull_request' }}
permissions:
contents: write
issues: write
pull-requests: write
id-token: write
steps:
- name: Checkout repo
uses: actions/checkout@v4
- uses: ./.github/setup

- name: Build
run: bun run build

- name: Create Release
id: semantic
run: bunx semantic-release --branches main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_CONFIG_PROVENANCE: true
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.vscode
node_modules
*.log
.DS_Store
coverage
dist
types
.conf*
.env
39 changes: 39 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "angular",
"releaseRules": [
{
"type": "refactor",
"release": false
},
{
"type": "style",
"release": "patch"
},
{
"scope": "no-release",
"release": false
},
{
"scope": "cli",
"release": "patch"
},
{
"scope": "patch",
"release": "patch"
},
{
"scope": "minor",
"release": "patch"
}
]
}
],
"@semantic-release/github",
"@semantic-release/release-notes-generator",
"@semantic-release/npm"
]
}
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

## v1.0.0

### 🚀 First Release

- Release (1457244)

### ❤️ Contributors

- Bekacru <[email protected]>
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Bereket Engida

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
198 changes: 198 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# Better Fetch

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).

## Why?

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.

## Installation

```bash
pnpm install @better-tools/fetch
```

## Usage

```typescript
const { data, error } = await fetch<{
userId: number;
id: number;
title: string;
completed;
}>("https://jsonplaceholder.typicode.com/todos/1");
if (error) {
// handle the error
}
if (data) {
// handle the data
}
```

### ♯ Create a custom fetch

You can create a custom fetch with default options.

```typescript
import { createFetch } from "better-auth";

const $fetch = createFetch({
baseUrl: "https://jsonplaceholder.typicode.com",
retry: 2,
});

const { data, error } = await $fetch<{
userId: number;
id: number;
title: string;
completed: boolean;
}>("/todos/1");
```

### ♯ Parsing the response

Better fetch will smartly parse JSON using JSON.parse and if it fails it will return the response as text.

For binary content types, better fetch will instead return a Blob object.

You can also pass custom parser.

```typescript
//parsed as JSON
const { data, error } = await fetch("/todos/1");
// Get the blob version of the response
const { data, error } = await fetch("/api/image.png");
// Return text as is
await ofetch("/ok");
//custom parser
const { data, error } = await fetch("/todos/1", {
parser: (text) => {
return JSON.parse(text);
},
});
```

### ♯ Handling Errors

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.

```typescript
const { data, error } = await fetch<
{
userId: number;
id: number;
title: string;
completed;
},
{
message: string;
}
>("https://jsonplaceholder.typicode.com/todos/1");
if (error) {
// handle the error
}
```

> throws if the request fails
```typescript
const { data, error } = await fetch<{
userId: number;
id: number;
title: string;
completed;
}>("https://jsonplaceholder.typicode.com/todos/1", {
throw: true,
});
```

### ♯ Auto Retry

You can set the number of retries.

```typescript
const { data, error } = await fetch<{
userId: number;
id: number;
title: string;
completed;
}>("https://jsonplaceholder.typicode.com/todos/1", {
retry: 2,
});
```

### ♯ Timeout

You can set the timeout in milliseconds.

```typescript
const { data, error } = await fetch<{
userId: number;
id: number;
title: string;
completed;
}>("https://jsonplaceholder.typicode.com/todos/1", {
timeout: 5000,
});
```

### ♯ Query Parameters

You can pass the query parameters as an object.

```typescript
const { data, error } = await fetch<{
userId: number;
id: number;
title: string;
completed;
}>("https://jsonplaceholder.typicode.com/todos/1", {
query: {
userId: 1,
},
});
```

### ♯ Callbacks

You can pass callbacks for different events on the request lifecycle.

```typescript
const { data, error } = await fetch<{
userId: number;
id: number;
title: string;
completed;
}>("https://jsonplaceholder.typicode.com/todos/1", {
onRequest: (request) => {
console.log("Requesting", request);
},
onResponse: (response) => {
console.log("Response", response);
},
onError: (error) => {
console.log("Error", error);
},
onSuccess: (data) => {
console.log("Success", data);
},
onRetry: (retry) => {
console.log("Retrying", retry);
},
});
```

### ♯ Native Fetch

You can use the native fetch by calling the native method.

```typescript
import betterFetch from "better-fetch";
const res = await betterFetch.native(
"https://jsonplaceholder.typicode.com/todos/1"
);
```

## License

MIT
21 changes: 21 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://biomejs.dev/schemas/1.6.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"complexity": {
"useLiteralKeys": "off"
},
"a11y": {
"useKeyWithClickEvents": "off"
},
"suspicious": {
"noExplicitAny": "off"
}
}
}
}
Binary file added bun.lockb
Binary file not shown.
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@better-tools/fetch",
"version": "1.0.0",
"packageManager": "[email protected]",
"devDependencies": {
"@types/bun": "^1.1.0",
"@types/node": "^20.11.30",
"biome": "^0.3.3",
"semantic-release": "^23.0.8",
"tsup": "^8.0.2",
"typescript": "^5.4.5"
},
"exports": {
".": "./dist/index.js"
},
"scripts": {
"build": "tsup",
"lint": "biome check .",
"release": "bun test && changelogen --release && npm publish && git push --follow-tags",
"lint:fix": "biome check . --apply",
"typecheck": "tsc --noEmit"
}
}
Loading

0 comments on commit 5301eda

Please sign in to comment.