Skip to content

Commit

Permalink
Merge pull request #211 from tomek14/master
Browse files Browse the repository at this point in the history
Update Angular-Capacitor sample to reflect RxJS and Capacitor updates
  • Loading branch information
mraible authored May 15, 2023
2 parents ceac1d7 + 0122c18 commit 0d0d23e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
26 changes: 11 additions & 15 deletions demos/angular-capacitor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@
"@angular/platform-browser": "14.1.3",
"@angular/platform-browser-dynamic": "14.1.3",
"@angular/router": "14.1.3",
"@capacitor-community/http": "1.4.1",
"@capacitor/android": "4.1.0",
"@capacitor/app": "4.0.1",
"@capacitor/browser": "4.0.1",
"@capacitor/core": "4.1.0",
"@capacitor/ios": "4.1.0",
"@capacitor/preferences": "4.0.1",
"@capacitor/splash-screen": "4.0.1",
"@ionic-native/core": "5.36.0",
"@ionic-native/http": "5.36.0",
"@ionic-native/splash-screen": "5.36.0",
"@ionic-native/status-bar": "5.36.0",
"@capacitor/android": "4.7.3",
"@capacitor/app": "4.1.1",
"@capacitor/browser": "4.1.1",
"@capacitor/core": "4.7.3",
"@capacitor/ios": "4.7.3",
"@capacitor/preferences": "4.0.2",
"@capacitor/splash-screen": "4.2.0",
"@capacitor/status-bar": "4.1.1",
"@ionic/angular": "6.2.4",
"capacitor-secure-storage-plugin": "0.8.0",
"capacitor-secure-storage-plugin": "0.8.1",
"ionic-appauth": "github:wi3land/ionic-appauth",
"rxjs": "6.6.7",
"rxjs": "7.5.7",
"tslib": "2.4.0",
"zone.js": "0.11.8"
},
Expand All @@ -44,7 +40,7 @@
"@angular/compiler": "14.1.3",
"@angular/compiler-cli": "14.1.3",
"@angular/language-service": "14.1.3",
"@capacitor/cli": "4.1.0",
"@capacitor/cli": "4.7.3",
"@ionic/angular-toolkit": "7.0.0",
"@types/jasmine": "4.3.0",
"@types/jasminewd2": "2.0.10",
Expand Down
6 changes: 4 additions & 2 deletions demos/angular-capacitor/src/app/core/cap-http.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Injectable } from '@angular/core';
import { Requestor } from '@openid/appauth';
import { Http, HttpResponse } from '@capacitor-community/http';
import { CapacitorHttp, HttpResponse } from '@capacitor/core';
import { XhrSettings } from 'ionic-appauth/lib/cordova';

// REQUIRES ENABLING CapacitorHttp
// https://capacitorjs.com/docs/apis/http
@Injectable({
providedIn: 'root',
})
Expand All @@ -12,7 +14,7 @@ export class CapacitorHttpService implements Requestor {
settings.method = 'GET';
}

const response: HttpResponse = await Http.request({
const response: HttpResponse = await CapacitorHttp.request({
method: settings.method,
url: settings.url,
headers: settings.headers,
Expand Down
22 changes: 12 additions & 10 deletions demos/angular-capacitor/src/app/core/ng-http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ import { Injectable } from '@angular/core';
import { Requestor } from '@openid/appauth';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { XhrSettings } from 'ionic-appauth/lib/cordova';
import { firstValueFrom } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class NgHttpService implements Requestor {

constructor(private http: HttpClient) {}
constructor(private http: HttpClient) {
}

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

switch (settings.method) {
case 'GET':
return this.http.get<T>(settings.url, { headers : this.getHeaders(settings.headers) }).toPromise();
case 'POST':
return this.http.post<T>(settings.url, settings.data, { headers : this.getHeaders(settings.headers) }).toPromise();
case 'PUT':
return this.http.put<T>(settings.url, settings.data, { headers : this.getHeaders(settings.headers) }).toPromise();
case 'DELETE':
return this.http.delete<T>(settings.url, { headers : this.getHeaders(settings.headers) }).toPromise();
case 'GET':
return firstValueFrom(this.http.get<T>(settings.url, { headers: this.getHeaders(settings.headers) }));
case 'POST':
return firstValueFrom(this.http.post<T>(settings.url, settings.data, { headers: this.getHeaders(settings.headers) }));
case 'PUT':
return firstValueFrom(this.http.put<T>(settings.url, settings.data, { headers: this.getHeaders(settings.headers) }));
case 'DELETE':
return firstValueFrom(this.http.delete<T>(settings.url, { headers: this.getHeaders(settings.headers) }));
}
}

Expand All @@ -38,4 +40,4 @@ export class NgHttpService implements Requestor {

return httpHeaders;
}
}
}

0 comments on commit 0d0d23e

Please sign in to comment.