Skip to content

Commit

Permalink
add: usePromise hook for sending http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktorrudzki committed Apr 13, 2024
1 parent 688de8c commit 8ff5415
Show file tree
Hide file tree
Showing 16 changed files with 21,742 additions and 436 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
API_URL=http://localhost:5096/
API_URL=http://localhost:3000/
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ module.exports = {
],
'react/react-in-jsx-scope': 'off',
'react-hooks/exhaustive-deps': 'warn',
indent: ['warn', 2, { SwitchCase: 1 }],
quotes: ['warn', 'single'],
semi: ['warn', 'always'],
},
Expand Down
1 change: 1 addition & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createNativeStackNavigator } from '@react-navigation/native-stack';
import i18next from 'i18next';
import commonEN from 'public/locales/en/common.json';
import commonPL from 'public/locales/pl/common.json';
import React from 'react';
import { initReactI18next } from 'react-i18next';

import { ThemeProvider } from '@/hooks';
Expand Down
32 changes: 29 additions & 3 deletions api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import axios from 'axios';
import { API_URL } from '@env';
import axios, { AxiosRequestConfig } from 'axios';

const api = axios.create({
baseURL: process.env.API_URL,
baseURL: API_URL + '/api',
});

export default api;
const get = <T extends object>(
url: string,
config: AxiosRequestConfig<any> | undefined = undefined,
): Promise<T> => api.get(url, config);

const del = <T extends object>(
url: string,
config: AxiosRequestConfig<any> | undefined = undefined,
): Promise<T> => api.delete(url, config);

const post = <T extends object>(
url: string,
data: unknown | undefined = undefined,
config: AxiosRequestConfig<any> | undefined = undefined,
): Promise<T> => api.post(url, data, config);

const put = <T extends object>(
url: string,
data: unknown | undefined = undefined,
config: AxiosRequestConfig<any> | undefined = undefined,
): Promise<T> => api.put(url, data, config);

const patch = <T extends object>(url: string, data: unknown): Promise<T> =>
api.patch(url, data);

export { get, del, post, put, patch };
11 changes: 11 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ module.exports = function (api) {
},
},
],
[
'module:react-native-dotenv',
{
envName: 'APP_ENV',
moduleName: '@env',
path: '.env',
safe: false,
allowUndefined: true,
verbose: false,
},
],
],
};
};
3 changes: 3 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '@env' {
export const API_URL: string;
}
Loading

0 comments on commit 8ff5415

Please sign in to comment.