lib/tumblr.js
function postRequest(requestPost, credentials, baseUrl, apiPath, requestOptions, params, callback) {
    params = params || {};

    // Sign without multipart data
    const currentRequest = requestPost(extend({
        url: baseUrl + apiPath,
        oauth: credentials,
    }, requestOptions), function(err, response, body) {
        try {
            body = JSON.parse(body);
        } catch (e) {
            body = {
                error: 'Malformed Response: ' + body,
            };
        }
        requestCallback(callback)(err, response, body);
    });

    // Sign it with the non-data parameters
    const dataKeys = ['data'];
    currentRequest.form(omit(params, dataKeys));
    currentRequest.oauth(credentials);

    // Clear the side effects from form(param)
    delete currentRequest.headers['content-type'];
    delete currentRequest.body;

    // if 'data' is an array, rename it with indices
    if ('data' in params && Array.isArray(params.data)) {
        for (let i = 0; i < params.data.length; ++i) {
            params['data[' + i + ']'] = params.data[i];
        }
        delete params.data;
    }

    // And then add the full body
    const form = currentRequest.form();
    for (const key in params) {
        form.append(key, params[key]);
    }

    // Add the form header back
    extend(currentRequest.headers, form.getHeaders());

    return currentRequest;
}
function postRequest(requestPost, credentials, baseUrl, apiPath, requestOptions, params, callback) {
    params = params || {};

    // Sign without multipart data
    const currentRequest = requestPost(extend({
        url: baseUrl + apiPath,
        oauth: credentials,
    }, requestOptions), function(err, response, body) {
        try {
            body = JSON.parse(body);
        } catch (e) {
            body = {
                error: 'Malformed Response: ' + body,
            };
        }
        requestCallback(callback)(err, response, body);
    });

    // Sign it with the non-data parameters
    const dataKeys = ['data'];
    currentRequest.form(omit(params, dataKeys));
    currentRequest.oauth(credentials);

    // Clear the side effects from form(param)
    delete currentRequest.headers['content-type'];
    delete currentRequest.body;

    // if 'data' is an array, rename it with indices
    if ('data' in params && Array.isArray(params.data)) {
        for (let i = 0; i < params.data.length; ++i) {
            params['data[' + i + ']'] = params.data[i];
        }
        delete params.data;
    }

    // And then add the full body
    const form = currentRequest.form();
    for (const key in params) {
        form.append(key, params[key]);
    }

    // Add the form header back
    extend(currentRequest.headers, form.getHeaders());

    return currentRequest;
}
function postRequest ( requestPost , credentials , baseUrl , apiPath , requestOptions , params , callback ) { params = params || { } ; // Sign without multipart data const currentRequest = requestPost ( extend ( { url : baseUrl + apiPath , oauth : credentials , } , requestOptions ) , function ( err , response , body ) { try { body = JSON . parse ( body ) ; } catch ( e ) { body = { error : 'Malformed Response: ' + body , } ; } requestCallback ( callback ) ( err , response , body ) ; } ) ; // Sign it with the non-data parameters const dataKeys = [ 'data' ] ; currentRequest . form ( omit ( params , dataKeys ) ) ; currentRequest . oauth ( credentials ) ; // Clear the side effects from form(param) delete currentRequest . headers [ 'content-type' ] ; delete currentRequest . body ; // if 'data' is an array, rename it with indices if ( 'data' in params && Array . isArray ( params . data ) ) { for ( let i = 0 ; i < params . data . length ; ++ i ) { params [ 'data[' + i + ']' ] = params . data [ i ] ; } delete params . data ; } // And then add the full body const form = currentRequest . form ( ) ; for ( const key in params ) { form . append ( key , params [ key ] ) ; } // Add the form header back extend ( currentRequest . headers , form . getHeaders ( ) ) ; return currentRequest ; }
Create a function to make POST requests to the Tumblr API

@param  {Function} requestPost - function that performs a get request
@param  {Object} credentials - OAuth credentials
@param  {string} baseUrl - base URL for the request
@param  {string} apiPath - URL path for the request
@param  {Object} requestOptions - additional request options
@param  {Object} params - form data
@param  {TumblrClient~callback} callback - request callback

@return {Request} Request object

@private
Create a function to make POST requests to the Tumblr API