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

Feature/client config #12

Merged
merged 2 commits into from
Jul 31, 2017
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
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;
}
}