fleet/frontend/services/index.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

import axios, { isAxiosError, ResponseType as AxiosResponseType } from "axios";
import URL_PREFIX from "router/url_prefix";
import { authToken } from "utilities/local";
export const sendRequest = async (
method: "GET" | "POST" | "PATCH" | "DELETE" | "HEAD",
path: string,
data?: unknown,
UI: 9274 unenroll mdm modal (#9539) # Addresses #9274 https://www.loom.com/share/2edd946cbd424af2b960801cc505ac85 ## Button and permissions: - no permission, enrolled, online: <img width="1131" alt="no permission, enrolled, online" src="https://user-images.githubusercontent.com/61553566/215197330-abc1606d-bf0a-44ec-b2de-2ef687bd529b.png"> - permission, enrolled, online: <img width="1131" alt="permission, enrolled, online" src="https://user-images.githubusercontent.com/61553566/215197443-a1353b9b-10dd-408b-8295-56029f2df4c3.png"> - permission, enrolled, offline: <img width="1131" alt="permission, enrolled, offline" src="https://user-images.githubusercontent.com/61553566/215197544-b2a997a7-09e5-4f8a-b723-af587b61a90d.png"> - not enrolled: <img width="1131" alt="not enrolled" src="https://user-images.githubusercontent.com/61553566/215197630-87f99cb3-63a9-45ce-bc85-57a45d54cae0.png"> ## Modal - <img width="672" alt="modal" src="https://user-images.githubusercontent.com/61553566/215214640-96670a23-d927-4213-a8fa-89411279c075.png"> - <img width="672" alt="Screenshot 2023-01-27 at 2 12 42 PM" src="https://user-images.githubusercontent.com/61553566/215215098-40d29556-3b73-4f52-a4ae-cc8b09122f5d.png"> - <img width="672" alt="Screenshot 2023-01-27 at 2 17 48 PM" src="https://user-images.githubusercontent.com/61553566/215216304-b9362b13-f37f-4454-81b5-423f6fc72280.png"> - <img width="787" alt="success-shot" src="https://user-images.githubusercontent.com/61553566/215236373-be7b1970-662d-47e6-ac59-f51eff344fcd.png"> # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/` - [x] Updated test inventory - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2023-01-30 23:59:02 +00:00
responseType: AxiosResponseType = "json",
timeout?: number,
skipParseError?: boolean
) => {
const { origin } = global.window.location;
const url = `${origin}${URL_PREFIX}/api${path}`;
const token = authToken();
try {
const response = await axios({
method,
url,
data,
responseType,
UI: 9274 unenroll mdm modal (#9539) # Addresses #9274 https://www.loom.com/share/2edd946cbd424af2b960801cc505ac85 ## Button and permissions: - no permission, enrolled, online: <img width="1131" alt="no permission, enrolled, online" src="https://user-images.githubusercontent.com/61553566/215197330-abc1606d-bf0a-44ec-b2de-2ef687bd529b.png"> - permission, enrolled, online: <img width="1131" alt="permission, enrolled, online" src="https://user-images.githubusercontent.com/61553566/215197443-a1353b9b-10dd-408b-8295-56029f2df4c3.png"> - permission, enrolled, offline: <img width="1131" alt="permission, enrolled, offline" src="https://user-images.githubusercontent.com/61553566/215197544-b2a997a7-09e5-4f8a-b723-af587b61a90d.png"> - not enrolled: <img width="1131" alt="not enrolled" src="https://user-images.githubusercontent.com/61553566/215197630-87f99cb3-63a9-45ce-bc85-57a45d54cae0.png"> ## Modal - <img width="672" alt="modal" src="https://user-images.githubusercontent.com/61553566/215214640-96670a23-d927-4213-a8fa-89411279c075.png"> - <img width="672" alt="Screenshot 2023-01-27 at 2 12 42 PM" src="https://user-images.githubusercontent.com/61553566/215215098-40d29556-3b73-4f52-a4ae-cc8b09122f5d.png"> - <img width="672" alt="Screenshot 2023-01-27 at 2 17 48 PM" src="https://user-images.githubusercontent.com/61553566/215216304-b9362b13-f37f-4454-81b5-423f6fc72280.png"> - <img width="787" alt="success-shot" src="https://user-images.githubusercontent.com/61553566/215236373-be7b1970-662d-47e6-ac59-f51eff344fcd.png"> # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/` - [x] Updated test inventory - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2023-01-30 23:59:02 +00:00
timeout,
headers: {
Authorization: `Bearer ${token}`,
},
});
return response.data;
} catch (error) {
if (skipParseError) {
return Promise.reject(error);
}
let reason: unknown | undefined;
if (isAxiosError(error)) {
reason = error.response || error.message || error.code;
}
return Promise.reject(
reason || `send request: parse server error: ${error}`
);
}
};
export default sendRequest;