Skip to content

Commit

Permalink
chore: overhaul README
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Nov 2, 2023
1 parent 24d7503 commit aefd8ad
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 195 deletions.
57 changes: 29 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
> Minimal, type-safe REST client using JS proxies.
> [!NOTE]
> Thanks to everyone who has [upvoted this feature to be part of `unjs/ofetch`](https://github.com/unjs/ofetch/pull/69)! This library will be part of the upcoming [`api-party`](https://github.com/unjs/api-party) package.
> Thanks to everyone who has [upvoted this feature to be part of `ofetch`](https://github.com/unjs/ofetch/pull/69)! We closed the PR in favor of a separate upcoming package, [`api-party`](https://github.com/unjs/api-party).
## Features

Expand All @@ -21,14 +21,14 @@
Run the following command to add `unrested` to your project.

```bash
# pnpm
pnpm add -D unrested

# npm
npm install -D unrested
npm install unrested

# pnpm
pnpm add unrested

# yarn
yarn add -D unrested
yarn add unrested
```

## Usage
Expand All @@ -53,18 +53,28 @@ const api = createClient({

### Path Segment Chaining

Chain single path segments or path ids by a dot. You can even type the response of your request!
Chain single path segments or path IDs by a dot. You can type the response by passing a generic type to the method:

```ts
// GET request to <baseURL>/users
const users = await api.users.get<UserResponse>()
const response = await api.users.get<UserResponse>()
```

// For GET request you can add search params
For `GET` request, the first parameter is used as query parameters:

```ts
// <baseURL>/users?search=john
const users = await api.users.get<UserResponse>({ search: 'john' })
const response = await api.users.get<UserResponse>({ search: 'john' })
```

For HTTP request methods supporting a payload, the first parameter is used as payload:

```ts
// POST request to <baseURL>/users
const response = await api.users.post({ name: 'foo' })
```

To include dynamic API path segments, you have two options:
To include dynamic API path segments, you can choose between the chain syntax or the bracket syntax:

```ts
// Typed GET request to <baseURL>/users/1
Expand All @@ -77,24 +87,15 @@ const user = await api.users[`${userId}`].get<UserResponse>()

### HTTP Request Methods

Add the appropriate method to the end of your API call. The following methods are supported:

- `get()`
- `post()`
- `put()`
- `delete()`
- `patch()`

### Payload Requests
The following methods are supported as the last method in the chain:

For HTTP request methods supporting a payload, add it to the method call:

```ts
// POST request to <baseURL>/users
const response = await api.users.post({ name: 'foo' })
```
- `get(<query>, <fetchOptions>)`
- `post(<payload>, <fetchOptions>)`
- `put(<payload>, <fetchOptions>)`
- `delete(<payload>, <fetchOptions>)`
- `patch(<payload>, <fetchOptions>)`

### Default Options For `ofetch`
### Default Options for `ofetch`

```ts
import { createClient } from 'unrested'
Expand All @@ -112,7 +113,7 @@ const api = createClient({

### Override Default Options

You can add/overwrite `ofetch` options on a method-level:
Any fetch options on a method-level will override the default options:

```ts
const response = await api.users.get({
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "unrested",
"type": "module",
"version": "1.0.3",
"packageManager": "pnpm@8.9.2",
"packageManager": "pnpm@8.10.2",
"description": "Minimal, type-safe REST client using JS proxies",
"author": "Johann Schopplich <pkg@johannschopplich.com>",
"license": "MIT",
Expand Down Expand Up @@ -55,7 +55,7 @@
"ufo": "^1.3.1"
},
"devDependencies": {
"@antfu/eslint-config": "^1.0.0-beta.28",
"@antfu/eslint-config": "^1.0.0",
"bumpp": "^9.2.0",
"eslint": "^8.52.0",
"h3": "^1.8.2",
Expand Down

0 comments on commit aefd8ad

Please sign in to comment.