<commit_before>import axios from 'axios';
import FormData = require('form-data');
import * as request from 'request';

export function jsonToFormData(json, formData: FormData) {
  Object.keys(json).forEach(key => {
    const value = json[key];
    switch (typeof value) {
      case 'string':
      case 'number':
      case 'boolean':
        formData.append(key, value.toString());
        break;
      default:
        formData.append(key, JSON.stringify(value));
    }
  });
}

export interface PostFormResponse<T> {
  status: number;
  statusText: string;
  data: Buffer | string | any;
}

export function postMultipartFormData<T>(
  url: string,
  json,
): Promise<PostFormResponse<T>> {
  if (typeof window === 'undefined') {
    /* node.js */
    return new Promise((resolve, reject) => {
      const req = request.post(url, (error, response, body) => {
        if (error) {
          return reject(error);
        }
        resolve({
          status: response.statusCode,
          statusText: response.statusMessage,
          data: body,
        });
      });
      const formData = req.form();
      jsonToFormData(json, formData);
    });
  }
  /* web browser */
  const formData = new FormData();
  jsonToFormData(json, formData);
  return axios({
    method: 'post',
    url,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  }).then(res => ({
    status: res.status,
    statusText: res.statusText,
    data: res.data,
  }));
}
<commit_msg>support file in multipart form post<commit_after>import axios from 'axios';
import FormData = require('form-data');
import * as request from 'request';

export function jsonToFormData(json, formData: FormData) {
  Object.keys(json).forEach(key => {
    const value = json[key];
    switch (typeof value) {
      case 'string':
      case 'number':
      case 'boolean':
        formData.append(key, value.toString());
        break;
      default:
        if (value instanceof File) {
          formData.append(key, value);
        } else {
          formData.append(key, JSON.stringify(value));
        }
    }
  });
}

export interface PostFormResponse<T> {
  status: number;
  statusText: string;
  data: Buffer | string | any;
}

export function postMultipartFormData<T>(
  url: string,
  json,
): Promise<PostFormResponse<T>> {
  if (typeof window === 'undefined') {
    /* node.js */
    return new Promise((resolve, reject) => {
      const req = request.post(url, (error, response, body) => {
        if (error) {
          return reject(error);
        }
        resolve({
          status: response.statusCode,
          statusText: response.statusMessage,
          data: body,
        });
      });
      const formData = req.form();
      jsonToFormData(json, formData);
    });
  }
  /* web browser */
  const formData = new FormData();
  jsonToFormData(json, formData);
  return axios({
    method: 'post',
    url,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  }).then(res => ({
    status: res.status,
    statusText: res.statusText,
    data: res.data,
  }));
}

{'fraction_non_alphanumeric': 0.1426739339531901, 'fraction_numerical': 0.0, 'mean_word_length': 2.5454545454545454, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5642429', 'n_tokens_mistral': 955, 'n_tokens_neox': 956, 'n_words': 232}