Skip to content

Commit

Permalink
Use axios instance for all http request (microsoft#1343)
Browse files Browse the repository at this point in the history
  • Loading branch information
lei9444 authored and boydc2014 committed Oct 30, 2019
1 parent 6b6bbc3 commit 511b107
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 51 deletions.
29 changes: 14 additions & 15 deletions Composer/packages/client/src/store/action/bot.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import axios from 'axios';

import { ActionCreator } from '../types';

import { BASEURL, ActionTypes } from './../../constants';
import { ActionTypes } from './../../constants';
import httpClient from './../../utils/httpUtil';

export const connectBot: ActionCreator = async (store, settings) => {
const state = store.getState();
const { botEnvironment } = state;
const path = `${BASEURL}/launcher/connect?botEnvironment=${botEnvironment}`;
const path = `/launcher/connect?botEnvironment=${botEnvironment}`;

try {
const res = await axios.get(path);
const res = await httpClient.get(path);
store.dispatch({
type: ActionTypes.CONNECT_BOT_SUCCESS,
payload: {
Expand All @@ -36,10 +35,10 @@ export const getConnect: ActionCreator = async (store, env) => {
if (env) {
botEnvironment = env;
}
const path = `${BASEURL}/launcher/connect?botEnvironment=${botEnvironment}`;
const path = `/launcher/connect?botEnvironment=${botEnvironment}`;

try {
const res = await axios.get(path);
const res = await httpClient.get(path);
store.dispatch({
type: ActionTypes.GET_ENDPOINT_SUCCESS,
payload: {
Expand All @@ -54,11 +53,11 @@ export const getConnect: ActionCreator = async (store, env) => {

export const reloadBot: ActionCreator = async ({ dispatch, getState }, settings) => {
const { botEnvironment } = getState();
const path = `${BASEURL}/launcher/sync`;
const path = `/launcher/sync`;
try {
const targetEnvironment = botEnvironment === 'integration' ? 'production' : 'integration';

await axios.post(path, { ...settings, targetEnvironment });
await httpClient.post(path, { ...settings, targetEnvironment });
dispatch({
type: ActionTypes.RELOAD_BOT_SUCCESS,
payload: {
Expand All @@ -71,9 +70,9 @@ export const reloadBot: ActionCreator = async ({ dispatch, getState }, settings)
};

export const getPublishHistory: ActionCreator = async ({ dispatch }) => {
const path = `${BASEURL}/launcher/publishHistory`;
const path = `/launcher/publishHistory`;
try {
const res = await axios.get(path);
const res = await httpClient.get(path);
dispatch({
type: ActionTypes.GET_PUBLISH_VERSIONS_SUCCESS,
payload: {
Expand All @@ -86,7 +85,7 @@ export const getPublishHistory: ActionCreator = async ({ dispatch }) => {
};

export const publish: ActionCreator = async ({ dispatch }) => {
const path = `${BASEURL}/launcher/publish`;
const path = `/launcher/publish`;

dispatch({
type: ActionTypes.PUBLISH_BEGIN,
Expand All @@ -96,7 +95,7 @@ export const publish: ActionCreator = async ({ dispatch }) => {
});

try {
const res = await axios.post(path);
const res = await httpClient.post(path);

// test for error
dispatch({
Expand All @@ -116,7 +115,7 @@ export const publish: ActionCreator = async ({ dispatch }) => {
};

export const publishVersion: ActionCreator = async ({ dispatch }, version) => {
const path = `${BASEURL}/launcher/publish/${version}`;
const path = `/launcher/publish/${version}`;

dispatch({
type: ActionTypes.PUBLISH_BEGIN,
Expand All @@ -126,7 +125,7 @@ export const publishVersion: ActionCreator = async ({ dispatch }, version) => {
});

try {
const res = await axios.post(path);
const res = await httpClient.post(path);
dispatch({
type: ActionTypes.PUBLISH_SUCCESS,
payload: {
Expand Down
11 changes: 5 additions & 6 deletions Composer/packages/client/src/store/action/lg.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import axios from 'axios';

import * as lgUtil from '../../utils/lgUtil';
import { ActionCreator } from '../types';

import { BASEURL, ActionTypes } from './../../constants';
import { ActionTypes } from './../../constants';
import httpClient from './../../utils/httpUtil';

export function textFromTemplates(templates) {
let text = '';
Expand Down Expand Up @@ -78,7 +77,7 @@ export function checkLgContent(content) {

export const updateLgFile: ActionCreator = async ({ dispatch }, { id, content }) => {
try {
const response = await axios.put(`${BASEURL}/projects/opened/lgFiles/${id}`, { id, content });
const response = await httpClient.put(`/projects/opened/lgFiles/${id}`, { id, content });
dispatch({
type: ActionTypes.UPDATE_LG_SUCCESS,
payload: { response },
Expand All @@ -94,7 +93,7 @@ export const updateLgFile: ActionCreator = async ({ dispatch }, { id, content })

export const createLgFile: ActionCreator = async ({ dispatch }, { id, content }) => {
try {
const response = await axios.post(`${BASEURL}/projects/opened/lgFiles`, { id, content });
const response = await httpClient.post(`/projects/opened/lgFiles`, { id, content });
dispatch({
type: ActionTypes.CREATE_LG_SUCCCESS,
payload: { response },
Expand All @@ -110,7 +109,7 @@ export const createLgFile: ActionCreator = async ({ dispatch }, { id, content })

export const removeLgFile: ActionCreator = async ({ dispatch }, { id }) => {
try {
const response = await axios.delete(`${BASEURL}/projects/opened/lgFiles/${id}`);
const response = await httpClient.delete(`/projects/opened/lgFiles/${id}`);
dispatch({
type: ActionTypes.REMOVE_LG_SUCCCESS,
payload: { response },
Expand Down
13 changes: 6 additions & 7 deletions Composer/packages/client/src/store/action/lu.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import axios from 'axios';

import { ActionCreator } from '../types';

import { BASEURL, ActionTypes } from './../../constants/index';
import { ActionTypes } from './../../constants/index';
import httpClient from './../../utils/httpUtil';

export const updateLuFile: ActionCreator = async ({ dispatch }, { id, content }) => {
try {
const response = await axios.put(`${BASEURL}/projects/opened/luFiles/${id}`, { id, content });
const response = await httpClient.put(`/projects/opened/luFiles/${id}`, { id, content });
dispatch({
type: ActionTypes.UPDATE_LU_SUCCESS,
payload: { response },
Expand All @@ -26,7 +25,7 @@ export const updateLuFile: ActionCreator = async ({ dispatch }, { id, content })

export const createLuFile: ActionCreator = async ({ dispatch }, { id, content }) => {
try {
const response = await axios.post(`${BASEURL}/projects/opened/luFiles`, { id, content });
const response = await httpClient.post(`/projects/opened/luFiles`, { id, content });
dispatch({
type: ActionTypes.CREATE_LU_SUCCCESS,
payload: { response },
Expand All @@ -44,7 +43,7 @@ export const createLuFile: ActionCreator = async ({ dispatch }, { id, content })

export const removeLuFile: ActionCreator = async ({ dispatch }, { id }) => {
try {
const response = await axios.delete(`${BASEURL}/projects/opened/luFiles/${id}`);
const response = await httpClient.delete(`/projects/opened/luFiles/${id}`);
dispatch({
type: ActionTypes.REMOVE_LU_SUCCCESS,
payload: { response },
Expand All @@ -60,7 +59,7 @@ export const removeLuFile: ActionCreator = async ({ dispatch }, { id }) => {

export const publishLuis: ActionCreator = async ({ dispatch }, authoringKey) => {
try {
const response = await axios.post(`${BASEURL}/projects/opened/luFiles/publish`, { authoringKey });
const response = await httpClient.post(`/projects/opened/luFiles/publish`, { authoringKey });
dispatch({
type: ActionTypes.PUBLISH_LU_SUCCCESS,
payload: { response },
Expand Down
16 changes: 8 additions & 8 deletions Composer/packages/client/src/store/action/project.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import axios from 'axios';
import { navigate } from '@reach/router';

import { ActionCreator } from '../types';

import { BASEURL, ActionTypes, BASEPATH } from './../../constants/index';
import { ActionTypes, BASEPATH } from './../../constants/index';
import { navigateTo } from './../../utils/navigation';
import { startBot } from './bot';
import { navTo } from './navigation';
import settingStorage from './../../utils/dialogSettingStorage';
import httpClient from './../../utils/httpUtil';

export const setCreationFlowStatus: ActionCreator = ({ dispatch }, creationFlowStatus) => {
dispatch({
Expand All @@ -32,7 +32,7 @@ export const saveTemplateId: ActionCreator = ({ dispatch }, templateId) => {

export const fetchProject: ActionCreator = async store => {
try {
const response = await axios.get(`${BASEURL}/projects/opened`);
const response = await httpClient.get(`/projects/opened`);
store.dispatch({
type: ActionTypes.GET_PROJECT_SUCCESS,
payload: {
Expand All @@ -48,7 +48,7 @@ export const fetchProject: ActionCreator = async store => {

export const fetchRecentProjects: ActionCreator = async ({ dispatch }) => {
try {
const response = await axios.get(`${BASEURL}/projects/recent`);
const response = await httpClient.get(`/projects/recent`);
dispatch({
type: ActionTypes.GET_RECENT_PROJECTS_SUCCESS,
payload: {
Expand All @@ -68,7 +68,7 @@ export const openBotProject: ActionCreator = async (store, absolutePath) => {
storageId,
path: absolutePath,
};
const response = await axios.put(`${BASEURL}/projects/opened`, data);
const response = await httpClient.put(`/projects/opened`, data);
const dialogs = response.data.dialogs;
store.dispatch({
type: ActionTypes.GET_PROJECT_SUCCESS,
Expand Down Expand Up @@ -107,7 +107,7 @@ export const saveProjectAs: ActionCreator = async (store, name, description) =>
name,
description,
};
const response = await axios.post(`${BASEURL}/projects/opened/project/saveAs`, data);
const response = await httpClient.post(`/projects/opened/project/saveAs`, data);
const dialogs = response.data.dialogs;
store.dispatch({
type: ActionTypes.GET_PROJECT_SUCCESS,
Expand Down Expand Up @@ -141,7 +141,7 @@ export const createProject: ActionCreator = async (
description,
location,
};
const response = await axios.post(`${BASEURL}/projects`, data);
const response = await httpClient.post(`/projects`, data);
const dialogs = response.data.dialogs;
settingStorage.remove(name);
store.dispatch({
Expand All @@ -161,7 +161,7 @@ export const createProject: ActionCreator = async (

export const getAllProjects = async () => {
try {
return (await axios.get(`${BASEURL}/projects`)).data.children;
return (await httpClient.get(`/projects`)).data.children;
} catch (err) {
return err;
}
Expand Down
10 changes: 5 additions & 5 deletions Composer/packages/client/src/store/action/setting.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import axios from 'axios';
import { get } from 'lodash';

import { ActionCreator, DialogSetting } from '../types';
import settingsStorage from '../../utils/dialogSettingStorage';
import { SensitiveProperties } from '../../constants';

import { BASEURL, ActionTypes } from './../../constants/index';
import { ActionTypes } from './../../constants/index';
import { BotEnvironments } from './../../utils/envUtil';
import httpClient from './../../utils/httpUtil';

export const setSettings: ActionCreator = async (
{ dispatch },
Expand All @@ -32,7 +32,7 @@ export const setSettings: ActionCreator = async (
}
// set value to server
const suffix = slot ? `/${slot}` : '';
await axios.post(`${BASEURL}/projects/opened/settings${suffix}`, { settings });
await httpClient.post(`/projects/opened/settings${suffix}`, { settings });
} catch (err) {
dispatch({
type: ActionTypes.SET_ERROR,
Expand All @@ -47,10 +47,10 @@ export const setSettings: ActionCreator = async (
export const setDialogSettingsSlot = async ({ dispatch }, editing: boolean, slot?: BotEnvironments) => {
const suffix = slot ? `/${slot}` : '';
const query = editing ? '' : '?obfuscate=true';
const url = `${BASEURL}/projects/opened/settings${suffix}${query}`;
const url = `/projects/opened/settings${suffix}${query}`;

try {
const response = await axios.get(`${BASEURL}/projects/opened/settings${suffix}`);
const response = await httpClient.get(`/projects/opened/settings${suffix}`);
const settings = response.data;
dispatch({
type: ActionTypes.SYNC_ENV_SETTING,
Expand Down
15 changes: 7 additions & 8 deletions Composer/packages/client/src/store/action/storage.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import axios from 'axios';

import { ActionCreator } from '../types';

import { BASEURL, ActionTypes } from './../../constants';
import { ActionTypes } from './../../constants';
import httpClient from './../../utils/httpUtil';

export const fetchStorages: ActionCreator = async ({ dispatch }) => {
try {
const response = await axios.get(`${BASEURL}/storages`);
const response = await httpClient.get(`/storages`);
dispatch({
type: ActionTypes.GET_STORAGE_SUCCESS,
payload: {
Expand All @@ -24,7 +23,7 @@ export const fetchStorages: ActionCreator = async ({ dispatch }) => {

export async function fetchTemplates({ dispatch }) {
try {
const response = await axios.get(`${BASEURL}/assets/projectTemplates`);
const response = await httpClient.get(`/assets/projectTemplates`);

dispatch({
type: ActionTypes.GET_TEMPLATE_PROJECTS_SUCCESS,
Expand All @@ -42,7 +41,7 @@ export async function fetchTemplates({ dispatch }) {

export const addNewStorage: ActionCreator = async ({ dispatch }, storageData) => {
try {
const response = await axios.post(`${BASEURL}/storages`, storageData);
const response = await httpClient.post(`/storages`, storageData);
dispatch({
type: ActionTypes.GET_STORAGE_SUCCESS,
payload: {
Expand All @@ -57,7 +56,7 @@ export const addNewStorage: ActionCreator = async ({ dispatch }, storageData) =>
// todo: enable this if we have more storage, currently we only have one.
export const fetchStorageByName: ActionCreator = async ({ dispatch }, fileName) => {
try {
const response = await axios.get(`${BASEURL}/storage/${fileName}`);
const response = await httpClient.get(`/storage/${fileName}`);
dispatch({
type: ActionTypes.GET_STORAGE_SUCCESS,
payload: {
Expand All @@ -77,7 +76,7 @@ export const fetchFolderItemsByPath: ActionCreator = async ({ dispatch }, id, pa
status: 'pending',
},
});
const response = await axios.get(`${BASEURL}/storages/${id}/blobs/${path}`);
const response = await httpClient.get(`/storages/${id}/blobs/${path}`);
dispatch({
type: ActionTypes.GET_STORAGEFILE_SUCCESS,
payload: {
Expand Down
5 changes: 3 additions & 2 deletions Composer/packages/client/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { USER_TOKEN_STORAGE_KEY, BASEURL, ActionTypes } from '../constants';
import { Store } from '../store/types';

import storage from './storage';
import httpClient from './httpUtil';

export function isTokenExpired(token: string): boolean {
try {
Expand Down Expand Up @@ -60,7 +61,7 @@ export function prepareAxios(store: Store) {
if (process.env.COMPOSER_REQUIRE_AUTH) {
const cancelSource = axios.CancelToken.source();

axios.interceptors.request.use(config => {
httpClient.interceptors.request.use(config => {
// only attach cancellation token to api requests
if (config.url && config.url.includes('/api/')) {
config.cancelToken = cancelSource.token;
Expand All @@ -73,7 +74,7 @@ export function prepareAxios(store: Store) {
return config;
});

axios.interceptors.response.use(
httpClient.interceptors.response.use(
res => {
return res;
},
Expand Down

0 comments on commit 511b107

Please sign in to comment.