Skip to content

Commit 997f262

Browse files
Replace fetch with Axios including an interceptor
1 parent aaff1d5 commit 997f262

File tree

5 files changed

+57
-12
lines changed

5 files changed

+57
-12
lines changed

02-Calling-an-API/package-lock.json

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

02-Calling-an-API/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@fortawesome/fontawesome-svg-core": "~1.2.36",
1616
"@fortawesome/free-solid-svg-icons": "^5.15.4",
1717
"@fortawesome/vue-fontawesome": "^3.0.0-5",
18+
"axios": "^0.26.1",
1819
"core-js": "^3.20.3",
1920
"cors": "^2.8.5",
2021
"express": "^4.17.2",

02-Calling-an-API/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { library } from "@fortawesome/fontawesome-svg-core";
88
import { faLink, faUser, faPowerOff } from "@fortawesome/free-solid-svg-icons";
99
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
1010
import { domain, clientId as client_id, audience } from "../auth_config.json";
11+
import { createAxios } from './plugins/axios';
1112

1213
const app = createApp(App);
1314

@@ -24,5 +25,6 @@ app
2425
audience
2526
})
2627
)
28+
.use(createAxios())
2729
.component("font-awesome-icon", FontAwesomeIcon)
2830
.mount("#app");
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { App, inject } from "vue";
2+
import axios, { AxiosInstance, AxiosRequestConfig, AxiosStatic } from "axios";
3+
import { useAuth0 } from "@auth0/auth0-vue";
4+
5+
export function createAxios() {
6+
return {
7+
install(app: App) {
8+
app.config.globalProperties["axios"] = axios;
9+
app.provide("axios", axios);
10+
},
11+
};
12+
}
13+
14+
export function useAxios(config?: AxiosRequestConfig): AxiosInstance {
15+
const axios = inject("axios") as AxiosStatic;
16+
const { getAccessTokenSilently } = useAuth0();
17+
const instance = axios.create(config);
18+
19+
instance.interceptors.request.use(async (config) => {
20+
const token = await getAccessTokenSilently();
21+
22+
config.headers = {
23+
Authorization: `Bearer ${token}`,
24+
...config.headers,
25+
};
26+
27+
return config;
28+
});
29+
30+
return instance;
31+
}

02-Calling-an-API/src/views/ExternalApi.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,20 @@
2121
</template>
2222

2323
<script>
24-
import { useAuth0 } from "@auth0/auth0-vue";
24+
import { useAxios } from "./../plugins/axios";
2525
import { ref } from "@vue/reactivity";
2626
export default {
2727
name: "Api",
2828
setup() {
29-
const { getAccessTokenSilently } = useAuth0();
29+
const api = useAxios({
30+
baseURL: '/api'
31+
});
3032
const apiMessage = ref();
3133
return {
3234
apiMessage,
3335
async callApi() {
34-
const accessToken = await getAccessTokenSilently();
3536
try {
36-
const response = await fetch("/api/external", {
37-
headers: {
38-
Authorization: `Bearer ${accessToken}`,
39-
},
40-
});
41-
const data = await response.json();
37+
const { data } = await api.get("/external");
4238
apiMessage.value = data;
4339
} catch (e) {
4440
apiMessage.value = `Error: the server responded with '${e.response.status}: ${e.response.statusText}'`;

0 commit comments

Comments
 (0)