Skip to content

Commit

Permalink
Merge pull request #3 from rubenCodeforges/bugfix/static-method-not-s…
Browse files Browse the repository at this point in the history
…upported-in-module-config

Fix for issue #1
  • Loading branch information
rubenCodeforges authored May 6, 2017
2 parents ce54538 + aea3779 commit cf1142c
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 97 deletions.
58 changes: 16 additions & 42 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ and set the configuration
imports: [
...
GoogleApiModule.setConfig(
new GoogleApiConfig(
CLIENT_ID, DISCOVERY_DOCS, SCOPE
)
{
clientId: "your client id",
discoveryDocs: ["url to discovery docs", "another url"],
scope: "space separated scopes"
}
),
...
]
Expand Down Expand Up @@ -98,47 +100,19 @@ auth popup.

#### Configurations
The GoogleApiConfig class provides the required configuration for the Api
There are 2 ways of providing configs.

1. Default configuration is easy to use. The GoogleApiModule has a static method which sets the configs.
As shown in the example you simply provide a new instance of the `GoogleApiConfig` class. This class accepts 3 parameters
Configuration is easy to use. The GoogleApiModule has a static method which sets the configs.
As shown in the example you simply provide a configuration object of type `GapiInitConfigs`.
```typescript
new GoogleApiConfig(
CLIENT_ID, DISCOVERY_DOCS, SCOPE
)
{
clientId: "your client id",
discoveryDocs: ["url to discovery docs", "another url"],
scope: "space separated scopes"
}
```
Configure them according your google app configurations and resource scope.

2. In case you need to customize your configs you extend from the base `GoogleApiConfig` class
Example:
```typescript
export class GapiConfig extends GoogleApiConfig{
private static readonly CLIENT_ID: string = "your client id from google api";
private static readonly DISCOVERY_DOCS: string[] = [
"https://www.googleapis.com/discovery/v1/apis/tasks/v1/rest"
];
private static readonly SCOPE: string = [
'https://www.googleapis.com/auth/tasks',
'https://www.googleapis.com/auth/tasks.readonly'
].join(" ");

constructor(){
super(GapiConfig.CLIENT_ID, GapiConfig.DISCOVERY_DOCS, GapiConfig.SCOPE);
}
}
```
As you can see those 3 configs are still provided to the super class constructor.
Then in Module imports
```typescript
@NgModule({
imports: [
...
GoogleApiModule.setConfig(
new GapiConfig()
),
...
]
})
export MyModule {}
```
As you can see you dont need now to provide the configs inside the module import, instead you just provide the instance of the class
- To get the clientId see in your [developer console](https://console.developers.google.com/apis/credentials)
- The discoveryDoc is in the resource description, here an example for
[Reporting API v4](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/)
- The scope is also in the documentation of the specific API , example for [Reporting API v4](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#authorization)
1 change: 0 additions & 1 deletion components.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion components.js

This file was deleted.

22 changes: 10 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "ng-gapi",
"version": "0.0.36",
"version": "0.0.38",
"description": "Angular 2 Google api module",
"main": "components.js",
"typings": "lib/index",
"main": "./lib/index.js",
"typings": "./lib/index",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -30,16 +30,14 @@
"url": "https://github.com/rubenCodeforges/angular2-google-api/issues"
},
"homepage": "https://github.com/rubenCodeforges/angular2-google-api#readme",
"devDependencies": {
"typescript": "^2.2.2",
"dependencies": {
"@types/gapi": "0.0.31",
"@types/gapi.auth2": "0.0.35"
},
"dependencies": {
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/http": "^4.0.0",
"rxjs": "^5.3.0"
"devDependencies": {
"@angular/core": "^4.1.1",
"rxjs": "^5.3.1",
"typescript": "^2.2.2",
"zone.js": "^0.8.10"
}
}
}
8 changes: 3 additions & 5 deletions src/GoogleApiModule.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {GoogleApiService} from "./GoogleApiService";
import {ModuleWithProviders, NgModule} from "@angular/core";
import {GoogleAuthService} from "./GoogleAuthService";
import {GoogleApiConfig} from "./config/GoogleApiConfig";
import {GapiInitConfigs} from "./config/GoogleApiConfig";

//TODO: Add authConfig
@NgModule()
export class GoogleApiModule {
static setConfig(apiConfig: GoogleApiConfig, authConfig?: any): ModuleWithProviders {
static setConfig(apiConfig: GapiInitConfigs): ModuleWithProviders {
return {
ngModule: GoogleApiModule,
providers: [
Expand All @@ -22,8 +21,7 @@ export class GoogleApiModule {
provide: GoogleAuthService,
useFactory: GoogleAuthService.factory,
deps: [
GoogleApiService,
authConfig
GoogleApiService
]
}
]
Expand Down
14 changes: 9 additions & 5 deletions src/GoogleApiService.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {Observable} from "rxjs";
import {Injectable} from "@angular/core";
import {GoogleApiConfig} from "./config/GoogleApiConfig";
import {GoogleApiConfig, GapiInitConfigs} from "./config/GoogleApiConfig";

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

public static factory(apiConfig: GoogleApiConfig) {
return new GoogleApiService(apiConfig);
public static factory(configs: GapiInitConfigs) {
return new GoogleApiService(new GoogleApiConfig(configs));
}

constructor(config: GoogleApiConfig) {
Expand All @@ -25,8 +25,12 @@ export class GoogleApiService {
this.loadGapi().subscribe(callback);
}

public getConfig(): GoogleApiConfig {
return this.config;
}

private loadGapi(): Observable<void> {
return Observable.create((observer) => {
return Observable.create((observer: any) => {
let node = document.createElement('script');
node.src = this.gapiUrl;
node.type = 'text/javascript';
Expand All @@ -39,4 +43,4 @@ export class GoogleApiService {
};
});
}
}
}
14 changes: 5 additions & 9 deletions src/GoogleAuthService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import {Injectable} from "@angular/core";
import {Observable} from "rxjs";
import {GoogleApiService} from "./GoogleApiService";
import {GoogleApiConfig} from "./config/GoogleApiConfig";
import GoogleAuth = gapi.auth2.GoogleAuth;


@Injectable()
export class GoogleAuthService {
private GoogleAuth: GoogleAuth = undefined;
private config: GoogleApiConfig;

constructor(googleApi: GoogleApiService, config: GoogleApiConfig) {
this.config = config;

constructor(private googleApi: GoogleApiService) {
googleApi.onLoad(() => {
this.loadGapiAuth()
});
Expand All @@ -26,18 +22,18 @@ export class GoogleAuthService {
}

private loadGapiAuth(): Observable<GoogleAuth> {
return Observable.create((observer) => {
return Observable.create((observer: any) => {
gapi.load('auth2', () => {
let auth = gapi.auth2.init(this.config.getAuthConfig());
let auth = gapi.auth2.init(this.googleApi.getConfig().getConfigs());
observer.next(auth);
this.GoogleAuth = auth;
return auth;
});
});
}

public static factory(googleApi: GoogleApiService, config: GoogleApiConfig) {
return new GoogleAuthService(googleApi, config)
public static factory(googleApi: GoogleApiService) {
return new GoogleAuthService(googleApi)
}

}
5 changes: 0 additions & 5 deletions src/config/GapiAuthInitProperties.ts

This file was deleted.

20 changes: 12 additions & 8 deletions src/config/GoogleApiConfig.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import {GapiAuthInitProperties} from "./GapiAuthInitProperties";

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

constructor(CLIENT_ID: string, DISCOVERY_DOCS: string[], SCOPE: string) {
this.CLIENT_ID = CLIENT_ID;
this.DISCOVERY_DOCS = DISCOVERY_DOCS;
this.SCOPE = SCOPE;
constructor(configs: GapiInitConfigs) {
this.CLIENT_ID = configs.clientId;
this.DISCOVERY_DOCS = configs.discoveryDocs;
this.SCOPE = configs.scope;
}

public getAuthConfig(): GapiAuthInitProperties {
public getConfigs(): GapiInitConfigs {
return {
client_id: this.CLIENT_ID,
clientId: this.CLIENT_ID,
discoveryDocs: this.DISCOVERY_DOCS,
scope: this.SCOPE
}
}
}

export interface GapiInitConfigs {
clientId: string,
discoveryDocs: string[],
scope: string
}
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './';
export * from './GoogleApiModule';
export * from './GoogleAuthService';
export * from './GoogleApiService';
export * from './config/GoogleApiConfig';
23 changes: 16 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"module": "commonjs",
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"removeComments": true,
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"lib": [
"es2015",
"dom"
],
"outDir": "lib/",
"declaration": true
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
"exclude": [
"node_modules"
]
],
"angularCompilerOptions": {
"strictMetadataEmit": true,
"skipTemplateCodegen": true
}
}
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var path = require('path');

module.exports = {
entry: "./src/googleApi/GoogleApiModule.ts",
entry: "./src/GoogleApiModule.ts",
output: {
filename: "bundle.js"
},
Expand Down

0 comments on commit cf1142c

Please sign in to comment.