<commit_before>import 'whatwg-fetch';

import Error from './interfaces/error';

const Qs = require('qs');

export type Header = { [key: string]: string };
export type Credentials = '' | 'same-origin' | 'include';

export default class Client {

    // Properties

    baseUrl: string;
    defaultHeader: Header;
    credentials: Credentials = '';
    headerInterceptor: (header: Header) => Header = (header) => {
        return header
    }

    // Constructors

    constructor() {
        this.defaultHeader = {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        };
    }

    // Option-related methods

    setBaseUrl(baseUrl: string | any) {
        if (typeof baseUrl === 'string') {
            this.baseUrl = baseUrl;
        } else {
            this.baseUrl = baseUrl[process.env.NODE_ENV];
        }
    }

    setDefaultHeader(header: Header) {
        this.defaultHeader = header;
    }

    setHeaderInterceptor(interceptor: (header: Header) => Header) {
        this.headerInterceptor = interceptor;
    }

    // REST methods

    get(url: string, query?: Object, body?: Object) {
        if (query) {
            return this.fetch(`${this.baseUrl}${url}?${Qs.stringify(query, { arrayFormat: 'repeat' })}`, {
                method: 'get',
                body: JSON.stringify(body)
            })
        }
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'get',
            body: JSON.stringify(body)
        })
    }

    post(url: String, body?: Object, encoder?: (body: any) => any) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'post',
            body: encoder ? encoder(body) : JSON.stringify(body),
        })
    }

    postByForm(url: String, body: any) {
        // TODO: check type of body
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'post',
            body: body,
            headers: this.headerInterceptor({})
        })
    }

    postByUrlEncoding(url: String, body?: Object) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'post',
            body: Qs.stringify(body, { arrayFormat: 'repeat' }),
            headers: this.getHeaderUrlEncodedContentType()
        })
    }

    put(url: String, body?: Object) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'put',
            body: JSON.stringify(body),
        })
    }

    putByForm(url: String, body: any) {
        // TODO: check type of body
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'put',
            body: body,
            headers: this.headerInterceptor({})
        })
    }

    putByUrlEncoding(url: String, body?: Object) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'put',
            body: Qs.stringify(body, { arrayFormat: 'repeat' }),
            headers: this.getHeaderUrlEncodedContentType()
        })
    }

    delete(url: String) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'delete',
        })
    }

    /**
     * Base fetch method with default tasks (check status, parse json)
     */
    fetch(url: string, options?: Object) {
        return fetch(url, {
            headers: this.getHeader(),<commit_msg>postByForm, putByForm 에 Accept 헤더를 json 을 기본으로 수정<commit_after>import 'whatwg-fetch';

import Error from './interfaces/error';

const Qs = require('qs');

export type Header = { [key: string]: string };
export type Credentials = '' | 'same-origin' | 'include';

export default class Client {

    // Properties

    baseUrl: string;
    defaultHeader: Header;
    credentials: Credentials = '';
    headerInterceptor: (header: Header) => Header = (header) => {
        return header
    }

    // Constructors

    constructor() {
        this.defaultHeader = {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        };
    }

    // Option-related methods

    setBaseUrl(baseUrl: string | any) {
        if (typeof baseUrl === 'string') {
            this.baseUrl = baseUrl;
        } else {
            this.baseUrl = baseUrl[process.env.NODE_ENV];
        }
    }

    setDefaultHeader(header: Header) {
        this.defaultHeader = header;
    }

    setHeaderInterceptor(interceptor: (header: Header) => Header) {
        this.headerInterceptor = interceptor;
    }

    // REST methods

    get(url: string, query?: Object, body?: Object) {
        if (query) {
            return this.fetch(`${this.baseUrl}${url}?${Qs.stringify(query, { arrayFormat: 'repeat' })}`, {
                method: 'get',
                body: JSON.stringify(body)
            })
        }
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'get',
            body: JSON.stringify(body)
        })
    }

    post(url: String, body?: Object, encoder?: (body: any) => any) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'post',
            body: encoder ? encoder(body) : JSON.stringify(body),
        })
    }

    postByForm(url: String, body: any) {
        // TODO: check type of body
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'post',
            body: body,
            headers: this.headerInterceptor({
                'Accept': 'application/json'
            })
        })
    }

    postByUrlEncoding(url: String, body?: Object) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'post',
            body: Qs.stringify(body, { arrayFormat: 'repeat' }),
            headers: this.getHeaderUrlEncodedContentType()
        })
    }

    put(url: String, body?: Object) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'put',
            body: JSON.stringify(body),
        })
    }

    putByForm(url: String, body: any) {
        // TODO: check type of body
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'put',
            body: body,
            headers: this.headerInterceptor({
                'Accept': 'application/json'
            })
        })
    }

    putByUrlEncoding(url: String, body?: Object) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'put',
            body: Qs.stringify(body, { arrayFormat: 'repeat' }),
            headers: this.getHeaderUrlEncodedContentType()
        })
    }

    delete(url: String) {
        return this.fetch(`${this.baseUrl}${url}`, {
            method: 'delete',
        })
    }

    /**
     * Base fetch method with default tasks (check status, parse json)
     */
    fetch(url: string, options?: Object) {
        return fetch(url, {
            headers: this.getHeader(),
{'fraction_non_alphanumeric': 0.14945087563075096, 'fraction_numerical': 0.0, 'mean_word_length': 1.9492341356673961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1592956', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1961, 'n_words': 421}