Skip to content

Commit

Permalink
feat(api): add localApi instance, request wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mere1y committed Jul 9, 2023
1 parent d4b1f2f commit 1e72d9e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/shared/api/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import axios from 'axios';
import {createEffect} from 'effector';

import {API_URL} from '../config';

export const localApi = axios.create({
baseURL: API_URL,
withCredentials: true,
validateStatus: (status) => status >= 200 && status < 300,
});

interface Request {
path: string;
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
body?: unknown;
params?: unknown;
}

export const requestFx = createEffect<Request, any>((request) => {
return localApi({
method: request.method,
url: request.path,
data: request.body,
params: request.params,
})
.then((response) => response.data)
.catch((response) => Promise.reject(response.response.data));
});

0 comments on commit 1e72d9e

Please sign in to comment.