Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install dependencies
run: bun install

- name: Lint
- name: Lint package
run: bun run lint

typecheck:
Expand All @@ -32,7 +32,10 @@ jobs:
- name: Install dependencies
run: bun install

- name: Typecheck Module
- name: Build package
run: bun run build

- name: Typecheck package
run: bun run typecheck

release:
Expand All @@ -51,7 +54,7 @@ jobs:
- name: Install dependencies
run: bun install

- name: Build
- name: Build package
run: bun run build

- name: Release module
Expand Down
21 changes: 6 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@ jobs:
- name: Install dependencies
run: bun install

- name: Lint
- name: Lint package
run: bun run lint

typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: oven-sh/setup-bun@v2

- name: Install dependencies
run: bun install

- name: Typecheck Module
run: bun run typecheck

build:
runs-on: ubuntu-latest
needs: [lint, typecheck]
needs: lint

steps:
- uses: actions/checkout@v5
Expand All @@ -42,5 +30,8 @@ jobs:
- name: Install dependencies
run: bun install

- name: Build
- name: Build package
run: bun run build

- name: Run typecheck
run: bun run typecheck
95 changes: 87 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
[![NPM last update][npm-last-update-src]][npm-last-update-href]
[![License][license-src]][license-href]

Use legacy APIs with confidence.

Discofetch is a type-safe fetch client that automatically discovers and generates TypeScript
types for REST APIs that lack OpenAPI specifications.
Instead of manually writing types or dealing with `any`, Discofetch probes your API endpoints
Expand Down Expand Up @@ -35,14 +33,14 @@ This gives you autocompletion, type checking, and IntelliSense for legacy APIs w

## Installation

### Nuxt

Install the module:

```sh
npm install discofetch
```

### Nuxt

Add it to your Nuxt config:

```ts
Expand All @@ -54,11 +52,19 @@ export default defineNuxtConfig({

### Vite

Coming soon...
Update your tsconfig.json:

```json
{
"include": [
"node_modules/.discofetch/index.d.ts"
]
}
```

## Usage

### Basic Setup (Nuxt)
### Basic Nuxt Setup

Configure the module with your API base URL and define probes for the endpoints you want to discover:

Expand Down Expand Up @@ -91,13 +97,13 @@ export default defineNuxtConfig({
},
},

// Whether the generated client should only be available server-side (nitro)
// Whether the generated client should only be available server-side (nitro) - default: false
private: false,
},
})
```

### Using the Generated Client
### Using the Generated Client with Nuxt

Once configured, use the `dfetch` composable anywhere in your Nuxt app:

Expand Down Expand Up @@ -139,6 +145,79 @@ console.log(todo.title) // ✅ Fully typed!
The `useDfetch` composable provides methods for all HTTP verbs you defined probes for, complete with type safety and autocompletion.
It is also available on the server side during SSR and in Nitro API routes.

### Basic Vite Setup

Add the plugin to your Vite config:

```ts
// vite.config.ts
import discofetch from 'discofetch/vite'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [
discofetch({
// Base URL for your API
baseUrl: 'https://jsonplaceholder.typicode.com',

// Define endpoints to probe
probes: {
get: {
'/todos': {},
'/todos/{id}': {
params: { id: 1 },
},
'/comments': {
query: { postId: 1 },
},
},
post: {
'/todos': {
body: {
title: 'Sample Todo',
completed: false,
userId: 1,
},
},
},
},

// Whether the generated client should exclude headers given in the vite config - default: false
private: false,
})
]
})
```

### Using the generated client with Vite

Once configured, you can import a preconfigured `dfetch` instance or create your own with custom configs:

```ts
import { createDfetch, dfetch } from 'discofetch/client'

// GET request with path parameters
const { data: todo } = await dfetch.GET('/todos/{id}', {
params: {
path: { id: 10 },
},
})

const customDfetchClient = createDfetch({
headers: {
'my-custom-header': 'my custom header value!'
}
})

const { data: newTodo } = await customDfetchClient.POST('/todos', {
body: {
title: 'New Todo Item',
completed: true,
userId: 2,
},
})
```

## Configuration

### Probe Configuration
Expand Down
5 changes: 3 additions & 2 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ export default defineBuildConfig({

entries: [
'src/module.ts',
'src/adapters/vite.ts',

{
builder: 'mkdist',
input: 'src/client',
outDir: 'dist/client',
input: 'src/types',
outDir: 'dist/types',
},
],
})
Loading