@trayio/commons
Advanced tools
Comparing version 4.46.0 to 4.47.0
@@ -22,3 +22,3 @@ /// <reference types="node" /> | ||
export interface FileReferenceInterface { | ||
fromUnknown(value: unknown): value is FileReference; | ||
isFileReference(value: unknown): value is FileReference; | ||
fromFile(file: File): FileReference; | ||
@@ -25,0 +25,0 @@ } |
@@ -33,3 +33,3 @@ "use strict"; | ||
exports.FileReference = { | ||
fromUnknown(value) { | ||
isFileReference(value) { | ||
return (typeof value === 'object' && | ||
@@ -36,0 +36,0 @@ value !== null && |
/// <reference types="node" /> | ||
import * as O from 'fp-ts/lib/Option'; | ||
import * as TE from 'fp-ts/lib/TaskEither'; | ||
import { Readable } from 'stream'; | ||
import { File } from '../file/File'; | ||
import { File, FileStorage } from '../file/File'; | ||
import { SerializationFormat } from '../serialization/Serialization'; | ||
@@ -17,2 +18,8 @@ import { DynamicObject } from '../dynamictype/DynamicType'; | ||
}; | ||
export interface MultipartHttpRequestBodyInterface { | ||
create: (value: unknown, fileStorage: FileStorage) => TE.TaskEither<Error, MultipartHttpRequestBody>; | ||
} | ||
export declare const MultipartHttpRequestBody: { | ||
create: (value: unknown, fileStorage: FileStorage) => TE.TaskEither<Error, MultipartHttpRequestBody>; | ||
}; | ||
export type HttpRequestBody = RegularHttpRequestBody | MultipartHttpRequestBody; | ||
@@ -19,0 +26,0 @@ export type HttpRequest = { |
@@ -26,5 +26,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isMultiPartBody = exports.serializationFormatToHttpContentType = exports.HttpHeaders = exports.HttpHeader = exports.HttpContentType = exports.HttpMethod = void 0; | ||
exports.isMultiPartBody = exports.serializationFormatToHttpContentType = exports.HttpHeaders = exports.HttpHeader = exports.HttpContentType = exports.HttpMethod = exports.MultipartHttpRequestBody = void 0; | ||
const O = __importStar(require("fp-ts/lib/Option")); | ||
const TE = __importStar(require("fp-ts/lib/TaskEither")); | ||
const function_1 = require("fp-ts/lib/function"); | ||
const _ = __importStar(require("lodash")); | ||
const File_1 = require("../file/File"); | ||
const Serialization_1 = require("../serialization/Serialization"); | ||
@@ -38,2 +41,29 @@ const getHeader = (headers, name) => { | ||
}; | ||
exports.MultipartHttpRequestBody = { | ||
create: (value, fileStorage) => { | ||
if (value === null || typeof value !== 'object') { | ||
return TE.left(Error('Multipart body must be an object')); | ||
} | ||
const fields = {}; | ||
const fileReferences = []; | ||
Object.entries(value).forEach(([key, val]) => { | ||
if (File_1.FileReference.isFileReference(val)) { | ||
fileReferences.push(val); | ||
} | ||
else if (!_.isObject(val)) { | ||
fields[key] = val; | ||
} | ||
else { | ||
return TE.left(Error(`Multipart body field '${key}' must be a string, number, boolean or file reference`)); | ||
} | ||
}); | ||
return (0, function_1.pipe)(TE.sequenceArray(fileReferences.map((fileReference) => (0, function_1.pipe)(fileStorage.read(fileReference.key), TE.map((file) => ({ key: file.key, file }))))), TE.map((files) => { | ||
const filesRecord = {}; | ||
files.forEach(({ key, file }) => { | ||
filesRecord[key] = file; | ||
}); | ||
return { fields, files: filesRecord }; | ||
})); | ||
}, | ||
}; | ||
var HttpMethod; | ||
@@ -40,0 +70,0 @@ (function (HttpMethod) { |
{ | ||
"name": "@trayio/commons", | ||
"version": "4.46.0", | ||
"version": "4.47.0", | ||
"description": "Extensions to the standard/core libraries and basic features", | ||
@@ -5,0 +5,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
204191
4209