Skip to content

Commit

Permalink
Merge pull request #12 from rubenCodeforges/feature/client-config
Browse files Browse the repository at this point in the history
Feature/client config
  • Loading branch information
rubenCodeforges authored Jul 31, 2017
2 parents fe53c94 + 9f51c5d commit d79e698
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
28 changes: 23 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ npm install ng-gapi
#### Usage

To use the `ng-gapi` simply add `GoogleApiModule` to your module imports
and set the configuration
and set the configuration.

##### ClientConfig interface
Bellow are all available parameters that can be provided in the `forRoot()` method.
```typescript
interface ClientConfig {
clientId: string;
discoveryDocs: string[];
scope: string;
ux_mode?: string;
fetch_basic_profile?: boolean;
cookie_policy?: string;
hosted_domain?: string;
redirect_uri?: string;
}
```
##### Example:
```typescript
let gapiConfig = {
let gapiClientConfig: ClientConfig = {
clientId: "CLIENT_ID",
discoveryDocs: ["https://analyticsreporting.googleapis.com/$discovery/rest?version=v4"],
scope: [
Expand All @@ -31,17 +47,19 @@ let gapiConfig = {

@NgModule({
imports: [
...
//...
GoogleApiModule.forRoot({
provide: NG_GAPI_CONFIG,
useValue: gapiConfig
useValue: gapiClientConfig
}),
...
//...
]
})
export MyModule {}
```



Now you will have Access to the GoogleApi service.
The service has a a event method `onLoad(callback)`
This event will fire when the gapi script is loaded.
Expand Down
5 changes: 3 additions & 2 deletions examples/gapiReporting/GapiReportingModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {BrowserModule} from "@angular/platform-browser";
import {FormsModule} from "@angular/forms";
import {GapiReportingModel} from "./services/GapiReportingModel";
import {NG_GAPI_CONFIG} from "../../src/GoogleApiService";
import {ClientConfig} from "../../lib/config/GoogleApiConfig";

let gapiConfig = {
let gapiClientConfig: ClientConfig = {
clientId: "CLIENT_ID",
discoveryDocs: ["https://analyticsreporting.googleapis.com/$discovery/rest?version=v4"],
scope: [
Expand All @@ -23,7 +24,7 @@ let gapiConfig = {
FormsModule,
GoogleApiModule.forRoot({
provide: NG_GAPI_CONFIG,
useValue: gapiConfig
useValue: gapiClientConfig
})
],
declarations: [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-gapi",
"version": "0.0.41",
"version": "0.0.42-1",
"description": "Angular 4 Google api module ng-gapi",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions src/GoogleApiService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {Observable} from "rxjs";
import {Inject, Injectable, InjectionToken} from "@angular/core";
import {GapiInitConfigs, GoogleApiConfig} from "./config/GoogleApiConfig";
import {ClientConfig, GoogleApiConfig} from "./config/GoogleApiConfig";


export let NG_GAPI_CONFIG: InjectionToken<GapiInitConfigs> =
new InjectionToken<GapiInitConfigs>("ng-gapi.config");
export let NG_GAPI_CONFIG: InjectionToken<ClientConfig> =
new InjectionToken<ClientConfig>("ng-gapi.config");

@Injectable()
export class GoogleApiService {
private readonly gapiUrl: string = 'https://apis.google.com/js/platform.js';
private isLoaded: boolean = false;
private config: GoogleApiConfig;

constructor(@Inject(NG_GAPI_CONFIG) config: GapiInitConfigs) {
constructor(@Inject(NG_GAPI_CONFIG) config: ClientConfig) {
this.config = new GoogleApiConfig(config);
this.loadGapi();
}
Expand Down
2 changes: 1 addition & 1 deletion src/GoogleAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class GoogleAuthService {
private loadGapiAuth(): Observable<GoogleAuth> {
return Observable.create((observer: any) => {
gapi.load('auth2', () => {
let auth = gapi.auth2.init(this.googleApi.getConfig().getConfigs());
let auth = gapi.auth2.init(this.googleApi.getConfig().getClientConfig());
observer.next(auth);
this.GoogleAuth = auth;
return auth;
Expand Down
34 changes: 17 additions & 17 deletions src/config/GoogleApiConfig.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
export interface GapiInitConfigs {
clientId: string,
discoveryDocs: string[],
scope: string
/**
* @description https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiauth2clientconfig
*/
export interface ClientConfig {
clientId: string;
discoveryDocs: string[];
scope: string;
ux_mode?: string;
fetch_basic_profile?: boolean;
cookie_policy?: string;
hosted_domain?: string;
redirect_uri?: string;
}

export class GoogleApiConfig {
protected CLIENT_ID: string;
protected DISCOVERY_DOCS: string[];
protected SCOPE: string;
protected clientConfig: ClientConfig;

constructor(configs: GapiInitConfigs) {
this.CLIENT_ID = configs.clientId;
this.DISCOVERY_DOCS = configs.discoveryDocs;
this.SCOPE = configs.scope;
constructor(clientConfig: ClientConfig) {
this.clientConfig = clientConfig
}

public getConfigs(): GapiInitConfigs {
return {
clientId: this.CLIENT_ID,
discoveryDocs: this.DISCOVERY_DOCS,
scope: this.SCOPE
}
public getClientConfig(): ClientConfig {
return this.clientConfig;
}
}

0 comments on commit d79e698

Please sign in to comment.