-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add axios and react-native-dotenv dependencies
- Loading branch information
1 parent
45ed105
commit 1495015
Showing
7 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,5 @@ yarn-error.* | |
|
||
# typescript | ||
*.tsbuildinfo | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
module.exports = function(api) { | ||
module.exports = function (api) { | ||
api.cache(true); | ||
return { | ||
presets: ['babel-preset-expo'], | ||
plugins: [ | ||
[ | ||
'module:react-native-dotenv', | ||
{ | ||
moduleName: 'react-native-dotenv', | ||
verbose: false, | ||
}, | ||
], | ||
], | ||
}; | ||
}; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import axios, { AxiosRequestConfig, AxiosError } from "axios"; | ||
|
||
const BASE_URL = `http://api.openweathermap.org/data/2.5/weather/?q=`; | ||
const FIND_URL = `https://openweathermap.org/find`; | ||
|
||
type Response<T> = { | ||
statusCode: string | null; | ||
data: T | null; | ||
}; | ||
|
||
const Config: AxiosRequestConfig = { | ||
baseURL: BASE_URL, | ||
timeout: 10000, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}; | ||
|
||
async function GET<T>( | ||
url: string, | ||
params: Record<string, unknown> | ||
): Promise<Response<T>> { | ||
try { | ||
const controller = new AbortController(); | ||
Config.signal = controller.signal; | ||
Config.params = params; | ||
const response = await axios.get<T>(`${BASE_URL}${url}`, Config); | ||
return { | ||
statusCode: response.status.toString(), | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
if (axios.isAxiosError(error)) { | ||
const axiosError: AxiosError = error; | ||
return { | ||
statusCode: axiosError.response?.status.toString() || ("500" as string), | ||
data: null, | ||
}; | ||
} else { | ||
return { | ||
statusCode: "500" as string, | ||
data: null, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
async function POST<T>( | ||
url: string, | ||
data: Record<string, unknown> | ||
): Promise<Response<T>> { | ||
try { | ||
const controller = new AbortController(); | ||
Config.signal = controller.signal; | ||
const response = await axios.post<T>(`${BASE_URL}${url}`, data, Config); | ||
return { | ||
statusCode: response.status.toString(), | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
if (axios.isAxiosError(error)) { | ||
const axiosError: AxiosError = error; | ||
return { | ||
statusCode: axiosError.response?.status.toString() || ("500" as string), | ||
data: null, | ||
}; | ||
} else { | ||
return { | ||
statusCode: "500" as string, | ||
data: null, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
export default { GET, POST }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Api from ".."; | ||
|
||
class WeatherApi { | ||
private endpoints = {}; | ||
} | ||
|
||
export default new WeatherApi(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters