Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typings improvements + remove axios #41

Merged
merged 4 commits into from
Aug 9, 2024
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
45 changes: 20 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,31 +428,26 @@ Check Examples section below for more information.

## Comparison with another libraries

| Feature | axios-multi-api | ofetch() | Wretch() | Axios | SWR | React Query | Native fetch() |
| --------------------------------------- | --------------- | ------------ | ------------ | ------------ | ------------ | ------------ | -------------- |
| **Unified API Client** | ✅ | -- | -- | -- | -- | -- | -- |
| **Customizable Error Handling** | ✅ | -- | ✅ | ✅ | ✅ | ✅ | -- |
| **Retries with exponential backoff** | ✅ | -- | -- | -- | ✅ | ✅ | -- |
| **Easy Timeouts** | ✅ | ✅ | ✅ | ✅ | -- | -- | -- |
| **Easy Cancellation** | ✅ | -- | -- | -- | ✅ | ✅ | -- |
| **Default Responses** | ✅ | -- | -- | -- | -- | -- | -- |
| **Global Configuration** | ✅ | -- | ✅ | ✅ | ✅ | ✅ | -- |
| **TypeScript Support** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| **Interceptors** | ✅ | ✅ | ✅ | ✅ | -- | -- | -- |
| **Request and Response Transformation** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | -- |
| **Integration with Libraries** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | -- |
| **Request Queuing** | ✅ | -- | -- | -- | -- | -- | -- |
| **Multiple Fetching Strategies** | ✅ | -- | -- | -- | -- | -- | -- |
| **Dynamic URLs** | ✅ | -- | ✅ | -- | -- | -- | -- |
| **Automatic Retry on Failure** | ✅ | ✅ | -- | ✅ | ✅ | ✅ | -- |
| **Server-Side Rendering (SSR) Support** | ✅ | ✅ | -- | -- | ✅ | ✅ | -- |
| **Pagination Handling** | -- | -- | -- | -- | ✅ | ✅ | -- |
| **Caching** | -- | -- | -- | -- | ✅ | ✅ | -- |
| **Optimistic Updates** | -- | -- | -- | -- | ✅ | ✅ | -- |
| **Data Synchronization** | -- | -- | -- | -- | ✅ | ✅ | -- |
| **Local State Management** | -- | -- | -- | -- | ✅ | ✅ | -- |
| **Minimal Installation Size** | 🟢 (2.83 KB) | 🟡 (6.51 KB) | 🟢 (2.16 KB) | 🔴 (13.9 KB) | 🟡 (4.57 KB) | 🔴 (13.3 KB) | 🟢 (0 KB) |
| **Built-in AbortController Support** | ✅ | -- | -- | -- | ✅ | ✅ | -- |
| Feature | axios-multi-api | ofetch() | Wretch() | Axios | Native fetch() |
| --------------------------------------- | --------------- | ------------ | ------------ | ------------ | -------------- |
| **Unified API Client** | ✅ | -- | -- | -- | -- |
| **Customizable Error Handling** | ✅ | -- | ✅ | ✅ | -- |
| **Retries with exponential backoff** | ✅ | -- | -- | -- | -- |
| **Easy Timeouts** | ✅ | ✅ | ✅ | ✅ | -- |
| **Easy Cancellation** | ✅ | -- | -- | -- | -- |
| **Default Responses** | ✅ | -- | -- | -- | -- |
| **Global Configuration** | ✅ | -- | ✅ | ✅ | -- |
| **TypeScript Support** | ✅ | ✅ | ✅ | ✅ | ✅ |
| **Interceptors** | ✅ | ✅ | ✅ | ✅ | -- |
| **Request and Response Transformation** | ✅ | ✅ | ✅ | ✅ | -- |
| **Integration with Libraries** | ✅ | ✅ | ✅ | ✅ | -- |
| **Request Queuing** | ✅ | -- | -- | -- | -- |
| **Multiple Fetching Strategies** | ✅ | -- | -- | -- | -- |
| **Dynamic URLs** | ✅ | -- | ✅ | -- | -- |
| **Automatic Retry on Failure** | ✅ | ✅ | -- | ✅ | -- |
| **Server-Side Rendering (SSR) Support** | ✅ | ✅ | -- | -- | -- |
| **Minimal Installation Size** | 🟢 (2.83 KB) | 🟡 (6.51 KB) | 🟢 (2.16 KB) | 🔴 (13.9 KB) | 🟢 (0 KB) |
| **Built-in AbortController Support** | ✅ | -- | -- | -- | -- |

Please mind that this table is for informational purposes only. All of these solutions differ. For example `swr` and `react-query` are more focused on React, re-rendering, query caching and keeping data in sync, while fetch wrappers like `axios-multi-api` or `ofetch` aim to extend functionalities of native `fetch` so to reduce complexity of having to maintain various wrappers.

Expand Down
7 changes: 4 additions & 3 deletions docs/examples/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,16 @@ async function example4() {

// createApiFetcher() - direct API request() call to a custom endpoint with flattenResponse == false
async function example5() {
interface Endpoints {
interface Endpoints5 {
fetchBooks: Endpoint<Books, BooksQueryParams>;
}

const api = createApiFetcher<Endpoints, typeof endpoints>({
const api = createApiFetcher<Endpoints5, typeof endpoints>({
apiUrl: '',
endpoints,
});

const { data: books2 } = await api.fetchBooks();
const { data: books } = await api.request<Books>('fetchBooks', {});
const { data: data1 } = await api.request(
'https://example.com/api/custom-endpoint',
Expand All @@ -190,7 +191,7 @@ async function example5() {
'https://example.com/api/custom-endpoint',
);

console.log('Example 5', books);
console.log('Example 5', books, books2);
console.log('Example 5', data1, data2);
}

Expand Down
111 changes: 0 additions & 111 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,5 @@
"tsup": "8.2.4",
"typescript": "5.5.4",
"typescript-eslint": "8.0.1"
},
"peerDependencies": {
"axios": "^1"
}
}
12 changes: 7 additions & 5 deletions src/types/api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ export declare type Endpoint<

type EndpointDefaults = Endpoint<never>;

type Fn = (...args: unknown[]) => unknown;

type EndpointsRecord<EndpointsMethods> = {
[K in keyof EndpointsMethods]: EndpointsMethods[K] extends Fn
? EndpointsMethods[K]
: EndpointDefaults;
[K in keyof EndpointsMethods]: EndpointsMethods[K] extends Endpoint<
infer ResponseData,
infer QueryParams,
infer UrlPathParams
>
? Endpoint<ResponseData, QueryParams, UrlPathParams>
: Endpoint<never>;
};

type DefaultEndpoints<EndpointsCfg> = {
Expand Down
7 changes: 5 additions & 2 deletions test/api-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios from 'axios';
import { endpoints } from './mocks/endpoints';
import { createApiFetcher } from '../src';

describe('API Handler', () => {
const fetcher = {
create: jest.fn().mockReturnValue({ request: jest.fn() }),
};

const apiUrl = 'http://example.com/api/';
const config = {
fetcher: axios,
fetcher,
apiUrl,
endpoints,
onError: jest.fn(),
Expand Down
Loading
Loading