Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
#	src/GoogleApiService.ts
  • Loading branch information
rubenCodeforges committed May 6, 2017
2 parents 07a54e2 + cf1142c commit ef16d31
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 80 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.

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 @@ -18,8 +17,7 @@ export class GoogleApiModule {
provide: GoogleAuthService,
useFactory: GoogleAuthService.factory,
deps: [
GoogleApiService,
authConfig
GoogleApiService
]
}
]
Expand Down
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 ef16d31

Please sign in to comment.