Skip to content

Commit

Permalink
Merge pull request #142 from mleech/http-serializer-fix
Browse files Browse the repository at this point in the history
CordovaRequester: HTTP serializer fix
  • Loading branch information
Matt Raible authored Aug 26, 2022
2 parents 5b61a72 + e5b2e38 commit f859c5b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/cordova/cordova-requestor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface XhrSettings {
export declare class CordovaRequestor extends Requestor {
constructor();
xhr<T>(settings: XhrSettings): Promise<T>;
private makeRequest;
private get;
private post;
private put;
Expand Down
18 changes: 13 additions & 5 deletions lib/cordova/cordova-requestor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ import { HTTP } from '@ionic-native/http';
// cordova-plugin-advanced-http
export class CordovaRequestor extends Requestor {
constructor() {
CordovaDocument.ready(() => HTTP.setDataSerializer('utf8'));
super();
}
xhr(settings) {
return __awaiter(this, void 0, void 0, function* () {
if (!settings.method)
settings.method = "GET";
yield CordovaDocument.ready();
const previousSerializerType = HTTP.getDataSerializer();
HTTP.setDataSerializer('utf8');
const response = yield this.makeRequest(settings);
HTTP.setDataSerializer(previousSerializerType);
return response;
});
}
makeRequest(settings) {
return __awaiter(this, void 0, void 0, function* () {
switch (settings.method) {
case "GET":
case 'GET':
return this.get(settings.url, settings.headers);
case "POST":
case 'POST':
return this.post(settings.url, settings.data, settings.headers);
case "PUT":
case 'PUT':
return this.put(settings.url, settings.data, settings.headers);
case "DELETE":
case 'DELETE':
return this.delete(settings.url, settings.headers);
}
});
Expand Down
33 changes: 22 additions & 11 deletions src/cordova/cordova-requestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,41 @@ export interface XhrSettings {
headers?: any// {key : string, value: any}
}

type SerializerType = 'json' | 'urlencoded' | 'utf8' | 'multipart' | 'raw';

// REQUIRES CORDOVA PLUGINS
// cordova-plugin-advanced-http
export class CordovaRequestor extends Requestor {

constructor(){
CordovaDocument.ready(() => HTTP.setDataSerializer('utf8'));
super();
}

public async xhr<T>(settings: XhrSettings) : Promise<T> {
if(!settings.method)
if(!settings.method)
settings.method = "GET";

await CordovaDocument.ready();

switch(settings.method){
case "GET":
return this.get(settings.url, settings.headers);
case "POST":
return this.post(settings.url, settings.data, settings.headers);
case "PUT":
return this.put(settings.url, settings.data, settings.headers);
case "DELETE":
return this.delete(settings.url, settings.headers);
const previousSerializerType = HTTP.getDataSerializer() as SerializerType;
HTTP.setDataSerializer('utf8')

const response = await this.makeRequest<T>(settings);

HTTP.setDataSerializer(previousSerializerType);
return response;
}

private async makeRequest<T>(settings: XhrSettings): Promise<T> {
switch (settings.method) {
case 'GET':
return this.get(settings.url, settings.headers);
case 'POST':
return this.post(settings.url, settings.data, settings.headers);
case 'PUT':
return this.put(settings.url, settings.data, settings.headers);
case 'DELETE':
return this.delete(settings.url, settings.headers);
}
}

Expand Down

0 comments on commit f859c5b

Please sign in to comment.