Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CordovaRequester: HTTP serializer fix #142

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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