@mapomodule/core
Advanced tools
Comparing version 1.0.0-alpha.9 to 1.0.0-alpha.10
@@ -0,2 +1,10 @@ | ||
const transformRequestInMultipart = require('@mapomodule/core/api/multipart') | ||
const trimslashes = (str) => (str || "").replace(/^\/|\/$/g, '') | ||
const preparePayload = (payload, options) => { | ||
const { multipart } = (options || {}) | ||
if (!(payload instanceof FormData) && (typeof multipart === 'string') && multipart !== 'disable') { | ||
payload = transformRequestInMultipart(payload, multipart); | ||
} | ||
return payload | ||
} | ||
const endpointError = "This crud is badly configured. No endpoint was passed" | ||
@@ -29,6 +37,8 @@ | ||
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted. | ||
* @param {MapoRequestConfig} options Additional option configuration. Here you can configure some mapo reserved options. Like the multipart autodiscover politic. | ||
* @returns {Promise<external:AxiosResponse>} | ||
*/ | ||
create(payload, config) { | ||
const func = $axios.$post(`/${trimslashes(endpoint)}/`, payload, { ...higherConf, ...config }) | ||
create(payload, config, options) { | ||
const func = $axios.$post(`/${trimslashes(endpoint)}/`, preparePayload(payload, options), { ...higherConf, ...config }) | ||
return endpoint !== null ? func : Promise.reject(endpointError) | ||
@@ -54,6 +64,7 @@ }, | ||
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted. | ||
* @param {MapoRequestConfig} options Additional option configuration. Here you can configure some mapo reserved options. Like the multipart autodiscover politic. | ||
* @returns {Promise<external:AxiosResponse>} | ||
*/ | ||
updateOrCreate(payload, config) { | ||
const func = payload.id ? this.update(payload.id, payload, { ...higherConf, ...config }) : this.create(payload, { ...higherConf, ...config }) | ||
updateOrCreate(payload, config, options) { | ||
const func = payload.id ? this.update(payload.id, payload, { ...higherConf, ...config }, options) : this.create(payload, { ...higherConf, ...config }, options) | ||
return endpoint !== null ? func : Promise.reject(endpointError) | ||
@@ -68,6 +79,7 @@ }, | ||
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted. | ||
* @param {MapoRequestConfig} options Additional option configuration. Here you can configure some mapo reserved options. Like the multipart autodiscover politic. | ||
* @returns {Promise<external:AxiosResponse>} | ||
*/ | ||
update(id, payload, config) { | ||
const func = $axios.$put(`/${trimslashes(endpoint)}/${id}/`, payload, { ...higherConf, ...config }) | ||
update(id, payload, config, options) { | ||
const func = $axios.$put(`/${trimslashes(endpoint)}/${id}/`, preparePayload(payload, options), { ...higherConf, ...config }) | ||
return endpoint !== null ? func : Promise.reject(endpointError) | ||
@@ -82,6 +94,7 @@ }, | ||
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted. | ||
* @param {MapoRequestConfig} options Additional option configuration. Here you can configure some mapo reserved options. Like the multipart autodiscover politic. | ||
* @returns {Promise<external:AxiosResponse>} | ||
*/ | ||
partialUpdate(id, payload, config) { | ||
const func = $axios.$patch(`/${trimslashes(endpoint)}/${id}/`, payload, { ...higherConf, ...config }) | ||
partialUpdate(id, payload, config, options) { | ||
const func = $axios.$patch(`/${trimslashes(endpoint)}/${id}/`, preparePayload(payload, options), { ...higherConf, ...config }) | ||
return endpoint !== null ? func : Promise.reject(endpointError) | ||
@@ -111,2 +124,8 @@ }, | ||
/** | ||
* Additional option configuration. Here you can configure some mapo reserved options. | ||
* @typedef {MapoRequestConfig} MapoRequestConfig | ||
* @property {string} multipart - Set the multipart politic. Accepts `'auto'|'force|'disable'`. If auto is set the request is transformed in multipart if any file is in the payload. If set to force the request is transformed in multipart no matter if files are found. If set to `'disable'` the request is never transformed in multipart. | ||
*/ | ||
module.exports = createRepository |
@@ -1,2 +0,2 @@ | ||
const deepClone = require('@mapomodule/utils/helpers/objHelpers').deepClone | ||
const { deepClone, getPointed, setPointed, filesInObject } = require('@mapomodule/utils/helpers/objHelpers') | ||
@@ -9,29 +9,25 @@ /** | ||
* @param {Object} payload The payload of the request that needs to be trasformed. | ||
* @param {String[]} file_attributes The list of dotted path of all the file attributes that need to be attached to the request. | ||
* @param {String | String[]} file_attributes The list of dotted path of all the file attributes that need to be attached to the request. If unset autodiscovery is applyed. If set to 'fallback' if no files are found it returns the payload as it is. | ||
* @returns {external:FormData} | ||
*/ | ||
function transformRequestInMultipart(payload, file_attributes = []) { | ||
const cleanObject = (object, file_attributes, base = '', files = {}) => { | ||
for (const key in object) { | ||
if (file_attributes.includes(key) && object[key] && typeof object[key] != 'string') { | ||
files[base + key] = object[key]; | ||
delete object[key]; | ||
} | ||
if (typeof object[key] == 'object') { | ||
cleanObject(object[key], file_attributes, base + key + '.', files); | ||
} | ||
if (file_attributes.includes(key) && object[key] && typeof object[key] == 'string') { | ||
delete object[key]; | ||
} | ||
function transformRequestInMultipart(payload, file_attributes = "auto") { | ||
var file_paths = [] | ||
if (["auto", "force"].includes(file_attributes)) { | ||
file_paths = filesInObject(payload) | ||
if (file_attributes == "auto" && !file_paths.length){ | ||
return payload | ||
} | ||
return { data: object, files: files }; | ||
} else if (Array.isArray(file_attributes)) { | ||
file_paths = file_attributes | ||
} | ||
const tmp_obj = deepClone(payload); | ||
const blob = cleanObject(tmp_obj, file_attributes); | ||
const data = deepClone(payload); | ||
const files = file_paths.reduce((acc, path) => { | ||
const file = getPointed(data, path, undefined) | ||
setPointed(data, path, undefined); | ||
return { [path]: file, ...acc } | ||
}, {}) | ||
const formData = new FormData(); | ||
formData.append('data', JSON.stringify(blob.data)); | ||
for (const key in blob.files) { | ||
formData.append(key, blob.files[key]); | ||
formData.append('data', JSON.stringify(data)); | ||
for (const key in files) { | ||
formData.append(key, files[key]); | ||
} | ||
@@ -38,0 +34,0 @@ return formData; |
@@ -6,2 +6,18 @@ # Change Log | ||
# [1.0.0-alpha.10](https://github.com/lotrekagency/mapo/compare/v1.0.0-alpha.9...v1.0.0-alpha.10) (2022-02-11) | ||
### Bug Fixes | ||
* fix crud requests without multipart policy ([5875833](https://github.com/lotrekagency/mapo/commit/5875833209872a2609b653a089fff80c447b14e9)) | ||
### Features | ||
* **core:** Completely rewritten multipart capabilities, now the crud helper autodiscovers whet to use a multipart request. ([0a6546c](https://github.com/lotrekagency/mapo/commit/0a6546c9adeaf1fcf57a88105101e476b8b786dc)) | ||
# [1.0.0-alpha.9](https://github.com/lotrekagency/mapo/compare/v1.0.0-alpha.8...v1.0.0-alpha.9) (2022-02-08) | ||
@@ -8,0 +24,0 @@ |
{ | ||
"name": "@mapomodule/core", | ||
"version": "1.0.0-alpha.9", | ||
"version": "1.0.0-alpha.10", | ||
"description": "This is part of Mapo nuxt module. Injects the core mapo plugin helpers.", | ||
@@ -16,3 +16,3 @@ "author": "bnznamco <gabriele.baldi.01@gmail.com>", | ||
"dependencies": { | ||
"@mapomodule/utils": "^1.0.0-alpha.9" | ||
"@mapomodule/utils": "^1.0.0-alpha.10" | ||
}, | ||
@@ -22,3 +22,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "a75142247a9371981aea57b76828bd354b375aea" | ||
"gitHead": "283cad977741523d3a0c300ce08edf1a6855e11c" | ||
} |
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
21159
355