@apia/api 4.0.79 → 4.0.81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +11 -0
- package/dist/index.js +61 -1
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/dist/index.d.ts
CHANGED
|
@@ -28,6 +28,14 @@ type TApiaApiAxiosResponse<LoadType> = AxiosResponse<LoadType | null> & {
|
|
|
28
28
|
hasError: boolean;
|
|
29
29
|
hasMessages: boolean;
|
|
30
30
|
};
|
|
31
|
+
declare function postMultiPart<LoadType extends Record<string, unknown> = Record<string, unknown>>(url: string | TModify<IApiaApiRequestConfig<LoadType>, {
|
|
32
|
+
postData?: TFormData;
|
|
33
|
+
}>, config?: TModify<IApiaApiRequestConfig<LoadType>, {
|
|
34
|
+
postData?: TFormData;
|
|
35
|
+
}>): TApiaApiResult<LoadType>;
|
|
36
|
+
declare function postMultiPart<LoadType extends Record<string, unknown> = Record<string, unknown>>(config: TModify<IApiaApiRequestConfig<LoadType>, {
|
|
37
|
+
postData?: TFormData;
|
|
38
|
+
}>): TApiaApiResult<LoadType>;
|
|
31
39
|
/**
|
|
32
40
|
* IMPORTANT!! The ApiaApi is programmed under the slogan that
|
|
33
41
|
* **should never throw an exception**. **Errors** coming from
|
|
@@ -137,6 +145,7 @@ declare const ApiaApi: {
|
|
|
137
145
|
get: typeof get;
|
|
138
146
|
getConfig: typeof getConfig;
|
|
139
147
|
post: typeof post;
|
|
148
|
+
postMultiPart: typeof postMultiPart;
|
|
140
149
|
};
|
|
141
150
|
/**
|
|
142
151
|
* Creates an url based on the general requirements of Apia.
|
|
@@ -205,6 +214,8 @@ interface IApiaApiRequestConfig<DataType> extends IApiaApiConsoleConfig {
|
|
|
205
214
|
stringifyOptions?: QueryString.IStringifyOptions;
|
|
206
215
|
validateResponse?: (response: AxiosResponse<DataType | null>) => Promise<boolean | string> | boolean | string;
|
|
207
216
|
}
|
|
217
|
+
type TFormDataValue = string | number | boolean | Blob | File | null | undefined | Record<string, unknown> | Array<string | number | boolean | Blob | File | null | undefined | Record<string, unknown>>;
|
|
218
|
+
type TFormData = Record<string, TFormDataValue>;
|
|
208
219
|
|
|
209
220
|
type TMethod = {
|
|
210
221
|
name: string;
|
package/dist/index.js
CHANGED
|
@@ -361,6 +361,65 @@ async function handleResponse(result, currentUrl, outerBehaveConfig = defaultCon
|
|
|
361
361
|
}
|
|
362
362
|
return null;
|
|
363
363
|
}
|
|
364
|
+
async function postMultiPart(par1, par2) {
|
|
365
|
+
const actualUrl = typeof par1 !== "string" ? makeApiaUrl() : par1;
|
|
366
|
+
const configParameter = typeof par1 === "string" ? par2 ?? defaultConfig : par1;
|
|
367
|
+
const behaveConfig = {
|
|
368
|
+
...getConfig(configParameter)
|
|
369
|
+
};
|
|
370
|
+
const parsedUrl = makeUrl(
|
|
371
|
+
actualUrl,
|
|
372
|
+
behaveConfig.queryData,
|
|
373
|
+
behaveConfig.stringifyOptions
|
|
374
|
+
);
|
|
375
|
+
const formData = new FormData();
|
|
376
|
+
const appendValue = (key, value) => {
|
|
377
|
+
if (value === null || value === void 0)
|
|
378
|
+
return;
|
|
379
|
+
if (value instanceof Blob || value instanceof File) {
|
|
380
|
+
formData.append(key, value);
|
|
381
|
+
} else if (Array.isArray(value)) {
|
|
382
|
+
value.forEach((v, i) => {
|
|
383
|
+
appendValue(`${key}[${i}]`, v);
|
|
384
|
+
});
|
|
385
|
+
} else if (typeof value === "object") {
|
|
386
|
+
formData.append(key, JSON.stringify(value));
|
|
387
|
+
} else {
|
|
388
|
+
formData.append(key, String(value));
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
if (configParameter.postData) {
|
|
392
|
+
Object.entries(configParameter.postData).forEach(([key, value]) => {
|
|
393
|
+
appendValue(key, value);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
if (behaveConfig.debug) {
|
|
397
|
+
const action = getURLActionName(actualUrl);
|
|
398
|
+
console.log(
|
|
399
|
+
`%cApiaApi.postMultiPart ${action}`,
|
|
400
|
+
`color: ${getColor("message", behaveConfig.colors)}`,
|
|
401
|
+
{
|
|
402
|
+
url: parsedUrl,
|
|
403
|
+
queryData: behaveConfig.queryData,
|
|
404
|
+
formDataKeys: [...formData.keys()]
|
|
405
|
+
}
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
const response = await axios.post(parsedUrl, formData, {
|
|
409
|
+
...behaveConfig.axiosConfig,
|
|
410
|
+
headers: {
|
|
411
|
+
...behaveConfig.axiosConfig?.headers
|
|
412
|
+
// IMPORTANTE: no seteamos Content-Type
|
|
413
|
+
}
|
|
414
|
+
}).catch((e) => {
|
|
415
|
+
handleWrongResponse(e instanceof AxiosError ? e : new Error(e));
|
|
416
|
+
});
|
|
417
|
+
if (response) {
|
|
418
|
+
const result = handleResponse(response, actualUrl, behaveConfig);
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
364
423
|
async function post(par1, par2) {
|
|
365
424
|
const actualUrl = typeof par1 !== "string" ? makeApiaUrl() : par1;
|
|
366
425
|
const configParameter = typeof par1 === "string" ? par2 ?? defaultConfig : par1;
|
|
@@ -434,7 +493,8 @@ async function get(par1, par2) {
|
|
|
434
493
|
const ApiaApi = {
|
|
435
494
|
get,
|
|
436
495
|
getConfig,
|
|
437
|
-
post
|
|
496
|
+
post,
|
|
497
|
+
postMultiPart
|
|
438
498
|
};
|
|
439
499
|
function makeApiaUrl(props) {
|
|
440
500
|
let actualQueryData = {};
|