UNPKG

node-libcurl

Version:

The fastest http(s) client (and much more) for Node.js - Node.js bindings for libcurl

128 lines 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Easy = void 0; /** * Copyright (c) Jonathan Cardoso Machado. All Rights Reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ require("./moduleSetup"); const CurlMime_1 = require("./CurlMime"); const CurlPause_1 = require("./enum/CurlPause"); const bindings = require('../lib/binding/node_libcurl.node'); // @ts-expect-error - we are abusing TS merging here to have sane types for the addon classes const Easy = bindings.Easy; exports.Easy = Easy; /** * Build and set a MIME structure from a declarative configuration. * * This is a high-level method that accepts an array of MIME part specifications * and internally builds a {@link CurlMime} structure, then sets it using the * `MIMEPOST` option. This is the recommended way to create multipart form data. * * @remarks * For stream-based parts, you must provide the unpause callback that will be * called when more data is available. The callback should unpause the transfer * using `handle.pause(handle.pauseFlags & ~CurlPause.Recv)`. * * Available since libcurl 7.56.0. * * @example * ```typescript * import { Easy, CurlPause } from 'node-libcurl' * import { createReadStream } from 'fs' * * const easy = new Easy() * easy.setOpt('URL', 'https://httpbin.org/post') * * easy.setMimePost([ * { * type: 'data', * name: 'username', * data: 'john_doe' * }, * { * type: 'file', * name: 'document', * file: '/path/to/document.pdf', * mimeType: 'application/pdf' * }, * { * type: 'stream', * name: 'logfile', * stream: createReadStream('/path/to/log.txt'), * unpause: () => { * easy.pause(easy.pauseFlags & ~CurlPause.Recv) * }, * size: 12345 * }, * { * type: 'subparts', * name: 'metadata', * parts: [ * { * type: 'data', * name: 'description', * data: 'A nested part containing metadata fields.' * }, * { * type: 'data', * name: 'notes', * data: 'Additional details about the upload' * } * ] * } * ]) * ``` * * @param parts Array of MIME part specifications * @returns This Easy instance for method chaining */ Easy.prototype.setMimePost = function (parts) { const mime = new CurlMime_1.CurlMime(this); // Helper function to recursively build MIME parts const buildMimePart = (parentMime, partSpec) => { const part = parentMime.addPart(); part.setName(partSpec.name); // Set common properties if (partSpec.mimeType) part.setType(partSpec.mimeType); if (partSpec.fileName) part.setFileName(partSpec.fileName); if (partSpec.encoder) part.setEncoder(partSpec.encoder); if (partSpec.headers) part.setHeaders(partSpec.headers); // Handle type-specific properties switch (partSpec.type) { case 'data': part.setData(partSpec.data); break; case 'file': part.setFileData(partSpec.file); break; case 'stream': part.setDataStream(partSpec.stream, () => { this.pause(this.pauseFlags & ~CurlPause_1.CurlPause.Recv); }, partSpec.size); break; case 'subparts': // Recursive case: create nested MIME structure const subMime = new CurlMime_1.CurlMime(this); for (const subPartSpec of partSpec.parts) { buildMimePart(subMime, subPartSpec); } part.setSubparts(subMime); break; } }; // Build all MIME parts for (const partSpec of parts) { buildMimePart(mime, partSpec); } // Set the MIME structure on the handle this.setOpt('MIMEPOST', mime); return this; }; //# sourceMappingURL=Easy.js.map