* Detox/E2E: Migrate to typescript * Add jest.config.js * Add moduleMapper to config.json * Add cookie jar to axios client, fix tsconfig.json and default_config.json * Take keyboard into consideration; clean test for now for this migration PR * Revert changes on path_builder * Attempt to fix dep issues * Update detox dep * Added missing @type dev dependencies * Fix dep order * Fix unit tests * Added dynamic year to email.ts
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import fs from 'fs';
|
|
|
|
import FormData from 'form-data';
|
|
|
|
import client from './client';
|
|
|
|
export const getResponseFromError = (err: any) => {
|
|
const {response} = err;
|
|
if (!response) {
|
|
const message = `No response from server at "${err.config.baseURL}".
|
|
If testing against a server other than the default local instance, you may set the server URL via "SITE_URL" environment variable.
|
|
`;
|
|
|
|
// Throw an error instead of failing silently
|
|
throw new Error(message);
|
|
}
|
|
|
|
const {data, status} = response;
|
|
|
|
// Explicitly print out response data from server for ease of debugging
|
|
console.warn(data); // eslint-disable-line no-console
|
|
|
|
return {error: data, status};
|
|
};
|
|
|
|
export const apiUploadFile = async (name: string, absFilePath: string, requestOptions = {}): Promise<any> => {
|
|
if (!fs.existsSync(absFilePath)) {
|
|
return {error: {message: `File upload error. "${name}" file does not exist at ${absFilePath}`}};
|
|
}
|
|
|
|
const formData = new FormData();
|
|
formData.append(name, fs.createReadStream(absFilePath));
|
|
|
|
try {
|
|
return await client.request({
|
|
...requestOptions,
|
|
data: formData,
|
|
headers: formData.getHeaders(),
|
|
});
|
|
} catch (err) {
|
|
return getResponseFromError(err);
|
|
}
|
|
};
|