ionic-native-http-connection-backend
Version:
A solution to CORS problem with Ionic and WKWebView
58 lines • 1.89 kB
JavaScript
import { HttpParams } from '@angular/common/http';
import { bodyToObject } from './http-params';
import { httpParamsToFormData } from './http-params-to-form-data';
import { bodyToUtf8 } from './body-to-utf8';
import { isCompositeHttpParams } from './is-composite-http-params';
var DATA_REQUEST_METHODS = ['post', 'put', 'patch'];
export var detectSerializerAndData = function (req) {
var _a, _b;
if (!DATA_REQUEST_METHODS.includes(req.method.toLowerCase())) {
return {
serializer: 'urlencoded',
};
}
var contentType = (_b = (_a = req.headers.get('Content-Type')) !== null && _a !== void 0 ? _a : req.detectContentTypeHeader()) !== null && _b !== void 0 ? _b : '';
if (contentType.indexOf('text/') === 0) {
return {
serializer: 'utf8',
data: req.body,
};
}
if (contentType.indexOf('multipart/mixed') === 0) {
return {
serializer: 'utf8',
data: req.body,
};
}
if (/^application\/(.*)?json(;.*)?$/gi.test(contentType)) {
return {
serializer: 'utf8',
data: bodyToUtf8(req.body),
};
}
if (contentType.indexOf('application/x-www-form-urlencoded') === 0 &&
req.body instanceof HttpParams &&
isCompositeHttpParams(req.body)) {
return {
serializer: 'multipart',
data: httpParamsToFormData(req.body),
};
}
if (req.body instanceof FormData) {
return {
serializer: 'multipart',
data: req.body,
};
}
if (req.body === null || req.body === undefined) {
return {
serializer: 'urlencoded',
data: {},
};
}
return {
serializer: 'urlencoded',
data: bodyToObject(req.body),
};
};
//# sourceMappingURL=detect-serializer-and-data.js.map