fix: failing native http requests with body

This commit is contained in:
Rainer Killinger
2022-10-07 16:40:15 +02:00
parent c712fa17f7
commit fed4f20c3c
10 changed files with 177 additions and 140 deletions

View File

@@ -1,5 +1,5 @@
import {Requestor} from '@openid/appauth';
import {Http, HttpHeaders, HttpResponse} from '@capacitor-community/http';
import {CapacitorHttp, HttpHeaders, HttpResponse} from '@capacitor/core';
import {XhrSettings} from 'ionic-appauth/lib/cordova';
// REQUIRES CAPACITOR PLUGIN
@@ -25,28 +25,46 @@ export class CapacitorRequestor extends Requestor {
}
private async get<T>(url: string, headers: HttpHeaders) {
return Http.get({url, headers}).then(
return CapacitorHttp.get({url, headers}).then(
(response: HttpResponse) => response.data as T,
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async post<T>(url: string, data: any, headers: HttpHeaders) {
return Http.post({url, data: data, headers}).then(
(response: HttpResponse) => response.data as T,
);
return CapacitorHttp.post({
url,
data: this.decodeURLSearchParams(data),
headers,
}).then((response: HttpResponse) => {
return response.data as T;
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async put<T>(url: string, data: any, headers: HttpHeaders) {
return Http.put({url, data, headers}).then(
return CapacitorHttp.put({
url,
data: this.decodeURLSearchParams(data),
headers,
}).then((response: HttpResponse) => response.data as T);
}
private async delete<T>(url: string, headers: HttpHeaders) {
return CapacitorHttp.delete({url, headers}).then(
(response: HttpResponse) => response.data as T,
);
}
private async delete<T>(url: string, headers: HttpHeaders) {
return Http.del({url, headers}).then(
(response: HttpResponse) => response.data as T,
private decodeURLSearchParams(parameters: string): Record<string, unknown> {
const searchParameters = new URLSearchParams(parameters);
return Object.fromEntries(
[...searchParameters.keys()].map(k => [
k,
searchParameters.getAll(k).length === 1
? searchParameters.get(k)
: searchParameters.getAll(k),
]),
);
}
}