Files
team/e2e-tests/cypress/tests/plugins/external_request.ts
Harrison HealeyandGitHub b7873d148a MM-69003 Switch to using @stylistic/eslint-plugin for deprecated ESLint rules (#36770)
* MM-69003 Switch to using @stylistic/eslint-plugin for deprecated ESLint rules

* Run --fix on all web app packages

These changes are mostly around TypeScript linting where the @stylistic
versions are more accurate and therefore stricter about spacing and
semicolons in TS types.

* Run --fix on Cypress tests
2026-06-11 17:31:15 -04:00

84 lines
2.7 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import type {AxiosError} from 'axios';
import axios from 'axios';
import * as timeouts from '../fixtures/timeouts';
export interface ExternalRequestUser {
username: string;
password: string;
}
interface ExternalRequestArg {
baseUrl: string;
user: ExternalRequestUser;
method: 'get' | 'post' | 'put' | 'delete' | 'patch';
path: string;
data: any;
}
type ExternalRequestResult = {status: number; statusText: string; data: any; isError?: boolean} | {data: {id: string; isTimeout: boolean}; status?: undefined; statusText?: undefined; isError?: undefined};
export default async function externalRequest(arg: ExternalRequestArg): Promise<ExternalRequestResult> {
const {baseUrl, user, method = 'get', path, data = {}} = arg;
const loginUrl = `${baseUrl}/api/v4/users/login`;
// First we need to login with our external user to get cookies/tokens
let cookieString = '';
try {
const response = await axios({
url: loginUrl,
headers: {'X-Requested-With': 'XMLHttpRequest'},
method: 'post',
timeout: timeouts.TEN_SEC,
data: {login_id: user.username, password: user.password},
});
const setCookie = response.headers['set-cookie'];
(setCookie as any).forEach((cookie: string) => {
const nameAndValue = cookie.split(';')[0];
cookieString += nameAndValue + ';';
});
} catch (error) {
return getErrorResponse(error as AxiosError);
}
try {
const response = await axios({
method,
url: `${baseUrl}/api/v4/${path}`,
headers: {
'Content-Type': 'text/plain',
Cookie: cookieString,
'X-Requested-With': 'XMLHttpRequest',
},
timeout: timeouts.TEN_SEC,
data,
});
return {
status: response.status,
statusText: response.statusText,
data: response.data,
};
} catch (error) {
// If we have a response for the error, pull out the relevant parts
return getErrorResponse(error as AxiosError);
}
}
function getErrorResponse(error: AxiosError) {
if (error.response) {
return {
status: error.response.status,
statusText: error.response.statusText,
data: error.response.data,
isError: true,
};
} else if (error.code === 'ECONNABORTED') {
return {data: {id: error.code, isTimeout: true}};
}
// If we get here something else went wrong, so throw
throw error;
}