Skip to content

Commit 0efa33f

Browse files
authored
feat(advanced-http): duplicate methods sync to be able to abort requests (#3575)
* initial * finish http-changes
1 parent 182b403 commit 0efa33f

File tree

2 files changed

+267
-1
lines changed

2 files changed

+267
-1
lines changed

src/@ionic-native/core/decorators/interfaces.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export interface CordovaOptions {
3737
* If the method-name of the cordova plugin is different from the wrappers one, it can be defined here
3838
*/
3939
methodName?: string;
40-
4140
/**
4241
* Set to true if the wrapped method is a sync function
4342
*/

src/@ionic-native/plugins/http/index.ts

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export interface HTTPResponse {
2424
error?: string;
2525
}
2626

27+
interface AbortedResponse {
28+
aborted: boolean;
29+
}
30+
2731
/**
2832
* @name HTTP
2933
* @description
@@ -84,6 +88,7 @@ export class HTTP extends IonicNativePlugin {
8488
UNSUPPORTED_URL: number;
8589
NOT_CONNECTED: number;
8690
POST_PROCESSING_FAILED: number;
91+
ABORTED: number;
8792
};
8893

8994
/**
@@ -230,6 +235,29 @@ export class HTTP extends IonicNativePlugin {
230235
return;
231236
}
232237

238+
/**
239+
* Make a sync POST request
240+
* @param url {string} The url to send the request to
241+
* @param body {Object} The body of the request
242+
* @param headers {Object} The headers to set for this request
243+
* @param success {function} A callback that is called when the request succeed
244+
* @param failure {function} A callback that is called when the request failed
245+
* @returns {string} returns a string that represents the requestId
246+
*/
247+
@Cordova({
248+
methodName: 'post',
249+
sync: true,
250+
})
251+
postSync(
252+
url: string,
253+
body: any,
254+
headers: any,
255+
success: (result: HTTPResponse) => void,
256+
failure: (error: any) => void
257+
): string {
258+
return;
259+
}
260+
233261
/**
234262
* Make a GET request
235263
* @param url {string} The url to send the request to
@@ -242,6 +270,29 @@ export class HTTP extends IonicNativePlugin {
242270
return;
243271
}
244272

273+
/**
274+
* Make a sync GET request
275+
* @param url {string} The url to send the request to
276+
* @param parameters {Object} Parameters to send with the request
277+
* @param headers {Object} The headers to set for this request
278+
* @param success {function} A callback that is called when the request succeed
279+
* @param failure {function} A callback that is called when the request failed
280+
* @returns {string} returns a string that represents the requestId
281+
*/
282+
@Cordova({
283+
methodName: 'get',
284+
sync: true,
285+
})
286+
getSync(
287+
url: string,
288+
parameters: any,
289+
headers: any,
290+
success: (result: HTTPResponse) => void,
291+
failure: (error: any) => void
292+
): string {
293+
return;
294+
}
295+
245296
/**
246297
* Make a PUT request
247298
* @param url {string} The url to send the request to
@@ -254,6 +305,29 @@ export class HTTP extends IonicNativePlugin {
254305
return;
255306
}
256307

308+
/**
309+
* Make a sync PUT request
310+
* @param url {string} The url to send the request to
311+
* @param body {Object} The body of the request
312+
* @param headers {Object} The headers to set for this request
313+
* @param success {function} A callback that is called when the request succeed
314+
* @param failure {function} A callback that is called when the request failed
315+
* @returns {string} returns a string that represents the requestId
316+
*/
317+
@Cordova({
318+
methodName: 'put',
319+
sync: true,
320+
})
321+
putSync(
322+
url: string,
323+
body: any,
324+
headers: any,
325+
success: (result: HTTPResponse) => void,
326+
failure: (error: any) => void
327+
): string {
328+
return;
329+
}
330+
257331
/**
258332
* Make a PATCH request
259333
* @param url {string} The url to send the request to
@@ -266,6 +340,29 @@ export class HTTP extends IonicNativePlugin {
266340
return;
267341
}
268342

343+
/**
344+
* Make a sync PATCH request
345+
* @param url {string} The url to send the request to
346+
* @param body {Object} The body of the request
347+
* @param headers {Object} The headers to set for this request
348+
* @param success {function} A callback that is called when the request succeed
349+
* @param failure {function} A callback that is called when the request failed
350+
* @returns {string} returns a string that represents the requestId
351+
*/
352+
@Cordova({
353+
methodName: 'patch',
354+
sync: true,
355+
})
356+
patchSync(
357+
url: string,
358+
body: any,
359+
headers: any,
360+
success: (result: HTTPResponse) => void,
361+
failure: (error: any) => void
362+
): string {
363+
return;
364+
}
365+
269366
/**
270367
* Make a DELETE request
271368
* @param url {string} The url to send the request to
@@ -278,6 +375,29 @@ export class HTTP extends IonicNativePlugin {
278375
return;
279376
}
280377

378+
/**
379+
* Make a sync DELETE request
380+
* @param url {string} The url to send the request to
381+
* @param parameters {Object} Parameters to send with the request
382+
* @param headers {Object} The headers to set for this request
383+
* @param success {function} A callback that is called when the request succeed
384+
* @param failure {function} A callback that is called when the request failed
385+
* @returns {string} returns a string that represents the requestId
386+
*/
387+
@Cordova({
388+
methodName: 'delete',
389+
sync: true,
390+
})
391+
deleteSync(
392+
url: string,
393+
parameters: any,
394+
headers: any,
395+
success: (result: HTTPResponse) => void,
396+
failure: (error: any) => void
397+
): string {
398+
return;
399+
}
400+
281401
/**
282402
* Make a HEAD request
283403
* @param url {string} The url to send the request to
@@ -290,6 +410,29 @@ export class HTTP extends IonicNativePlugin {
290410
return;
291411
}
292412

413+
/**
414+
* Make a sync HEAD request
415+
* @param url {string} The url to send the request to
416+
* @param parameters {Object} Parameters to send with the request
417+
* @param headers {Object} The headers to set for this request
418+
* @param success {function} A callback that is called when the request succeed
419+
* @param failure {function} A callback that is called when the request failed
420+
* @returns {string} returns a string that represents the requestId
421+
*/
422+
@Cordova({
423+
methodName: 'head',
424+
sync: true,
425+
})
426+
headSync(
427+
url: string,
428+
parameters: any,
429+
headers: any,
430+
success: (result: HTTPResponse) => void,
431+
failure: (error: any) => void
432+
): string {
433+
return;
434+
}
435+
293436
/**
294437
* Make an OPTIONS request
295438
* @param url {string} The url to send the request to
@@ -302,6 +445,29 @@ export class HTTP extends IonicNativePlugin {
302445
return;
303446
}
304447

448+
/**
449+
* Make an sync OPTIONS request
450+
* @param url {string} The url to send the request to
451+
* @param parameters {Object} Parameters to send with the request
452+
* @param headers {Object} The headers to set for this request
453+
* @param success {function} A callback that is called when the request succeed
454+
* @param failure {function} A callback that is called when the request failed
455+
* @returns {string} returns a string that represents the requestId
456+
*/
457+
@Cordova({
458+
methodName: 'options',
459+
sync: true,
460+
})
461+
optionsSync(
462+
url: string,
463+
parameters: any,
464+
headers: any,
465+
success: (result: HTTPResponse) => void,
466+
failure: (error: any) => void
467+
): string {
468+
return;
469+
}
470+
305471
/**
306472
*
307473
* @param url {string} The url to send the request to
@@ -316,6 +482,33 @@ export class HTTP extends IonicNativePlugin {
316482
return;
317483
}
318484

485+
/**
486+
*
487+
* @param url {string} The url to send the request to
488+
* @param body {Object} The body of the request
489+
* @param headers {Object} The headers to set for this request
490+
* @param filePath {string} The local path(s) of the file(s) to upload
491+
* @param name {string} The name(s) of the parameter to pass the file(s) along as
492+
* @param success {function} A callback that is called when the request succeed
493+
* @param failure {function} A callback that is called when the request failed
494+
* @returns {string} returns a string that represents the requestId
495+
*/
496+
@Cordova({
497+
methodName: 'uploadFile',
498+
sync: true,
499+
})
500+
uploadFileSync(
501+
url: string,
502+
body: any,
503+
headers: any,
504+
filePath: string | string[],
505+
name: string | string[],
506+
success: (result: any) => void,
507+
failure: (error: any) => void
508+
): string {
509+
return;
510+
}
511+
319512
/**
320513
*
321514
* @param url {string} The url to send the request to
@@ -329,6 +522,31 @@ export class HTTP extends IonicNativePlugin {
329522
return;
330523
}
331524

525+
/**
526+
*
527+
* @param url {string} The url to send the request to
528+
* @param body {Object} The body of the request
529+
* @param headers {Object} The headers to set for this request
530+
* @param filePath {string} The path to download the file to, including the file name.
531+
* @param success {function} A callback that is called when the request succeed
532+
* @param failure {function} A callback that is called when the request failed
533+
* @returns {string} returns a string that represents the requestId
534+
*/
535+
@Cordova({
536+
methodName: 'downloadFile',
537+
sync: true,
538+
})
539+
downloadFileSync(
540+
url: string,
541+
body: any,
542+
headers: any,
543+
filePath: string,
544+
success: (result: any) => void,
545+
failure: (error: any) => void
546+
): string {
547+
return;
548+
}
549+
332550
/**
333551
*
334552
* @param url {string} The url to send the request to
@@ -362,4 +580,53 @@ export class HTTP extends IonicNativePlugin {
362580
): Promise<HTTPResponse> {
363581
return;
364582
}
583+
584+
/**
585+
*
586+
* @param url {string} The url to send the request to
587+
* @param options {Object} options for individual request
588+
* @param options.method {string} request method
589+
* @param options.data {Object} payload to be send to the server (only applicable on post, put or patch methods)
590+
* @param options.params {Object} query params to be appended to the URL (only applicable on get, head, delete, upload or download methods)
591+
* @param options.serializer {string} data serializer to be used (only applicable on post, put or patch methods), defaults to global serializer value, see setDataSerializer for supported values
592+
* @param options.timeout {number} timeout value for the request in seconds, defaults to global timeout value
593+
* @param options.headers {Object} headers object (key value pair), will be merged with global values
594+
* @param options.filePath {string} file path(s) to be used during upload and download see uploadFile and downloadFile for detailed information
595+
* @param options.name {string} name(s) to be used during upload see uploadFile for detailed information
596+
* @param options.responseType {string} response type, defaults to text
597+
* @param success {function} A callback that is called when the request succeed
598+
* @param failure {function} A callback that is called when the request failed
599+
*
600+
* @returns {string} returns a string that represents the requestId
601+
*/
602+
@Cordova({
603+
methodName: 'sendRequest',
604+
sync: true,
605+
})
606+
sendRequestSync(
607+
url: string,
608+
options: {
609+
method: 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'upload' | 'download';
610+
data?: { [index: string]: any };
611+
params?: { [index: string]: string | number };
612+
serializer?: 'json' | 'urlencoded' | 'utf8' | 'multipart';
613+
timeout?: number;
614+
headers?: { [index: string]: string };
615+
filePath?: string | string[];
616+
name?: string | string[];
617+
responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';
618+
},
619+
success: (result: HTTPResponse) => void,
620+
failure: (error: any) => void
621+
): string {
622+
return;
623+
}
624+
625+
/**
626+
* @param requestId {string} The RequestId of the request to abort
627+
*/
628+
@Cordova()
629+
abort(requestId: string): Promise<AbortedResponse> {
630+
return;
631+
}
365632
}

0 commit comments

Comments
 (0)