From aea3779aee85959446a6d102c439df4fbef8e677 Mon Sep 17 00:00:00 2001 From: Rubenator Date: Sat, 6 May 2017 08:23:21 +0100 Subject: [PATCH] Fix for issue #1 - refactored npm structure should fix the issue - updated readme for new configurations --- README.MD | 58 ++++++++-------------------- components.d.ts | 1 - components.js | 1 - package.json | 22 +++++------ src/GoogleApiModule.ts | 8 ++-- src/GoogleApiService.ts | 14 ++++--- src/GoogleAuthService.ts | 14 +++---- src/config/GapiAuthInitProperties.ts | 5 --- src/config/GoogleApiConfig.ts | 20 ++++++---- src/index.ts | 5 ++- tsconfig.json | 23 +++++++---- webpack.config.js | 2 +- 12 files changed, 76 insertions(+), 97 deletions(-) delete mode 100644 components.d.ts delete mode 100644 components.js delete mode 100644 src/config/GapiAuthInitProperties.ts diff --git a/README.MD b/README.MD index 2460daa..36a525e 100644 --- a/README.MD +++ b/README.MD @@ -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" + } ), ... ] @@ -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) \ No newline at end of file diff --git a/components.d.ts b/components.d.ts deleted file mode 100644 index f3c80e0..0000000 --- a/components.d.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './src/index'; \ No newline at end of file diff --git a/components.js b/components.js deleted file mode 100644 index f0da413..0000000 --- a/components.js +++ /dev/null @@ -1 +0,0 @@ -exports.GoogleApiModule = require('./src/index').GoogleApiModule; \ No newline at end of file diff --git a/package.json b/package.json index 0a79fa9..1785d00 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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" } -} \ No newline at end of file +} diff --git a/src/GoogleApiModule.ts b/src/GoogleApiModule.ts index afa8d4c..c8c48f9 100644 --- a/src/GoogleApiModule.ts +++ b/src/GoogleApiModule.ts @@ -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: [ @@ -22,8 +21,7 @@ export class GoogleApiModule { provide: GoogleAuthService, useFactory: GoogleAuthService.factory, deps: [ - GoogleApiService, - authConfig + GoogleApiService ] } ] diff --git a/src/GoogleApiService.ts b/src/GoogleApiService.ts index 6126a92..96b5051 100644 --- a/src/GoogleApiService.ts +++ b/src/GoogleApiService.ts @@ -1,6 +1,6 @@ import {Observable} from "rxjs"; import {Injectable} from "@angular/core"; -import {GoogleApiConfig} from "./config/GoogleApiConfig"; +import {GoogleApiConfig, GapiInitConfigs} from "./config/GoogleApiConfig"; @Injectable() export class GoogleApiService { @@ -8,8 +8,8 @@ export class GoogleApiService { 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) { @@ -25,8 +25,12 @@ export class GoogleApiService { this.loadGapi().subscribe(callback); } + public getConfig(): GoogleApiConfig { + return this.config; + } + private loadGapi(): Observable { - return Observable.create((observer) => { + return Observable.create((observer: any) => { let node = document.createElement('script'); node.src = this.gapiUrl; node.type = 'text/javascript'; @@ -39,4 +43,4 @@ export class GoogleApiService { }; }); } -} \ No newline at end of file +} diff --git a/src/GoogleAuthService.ts b/src/GoogleAuthService.ts index ebfff11..114db46 100644 --- a/src/GoogleAuthService.ts +++ b/src/GoogleAuthService.ts @@ -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() }); @@ -26,9 +22,9 @@ export class GoogleAuthService { } private loadGapiAuth(): Observable { - 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; @@ -36,8 +32,8 @@ export class GoogleAuthService { }); } - public static factory(googleApi: GoogleApiService, config: GoogleApiConfig) { - return new GoogleAuthService(googleApi, config) + public static factory(googleApi: GoogleApiService) { + return new GoogleAuthService(googleApi) } } \ No newline at end of file diff --git a/src/config/GapiAuthInitProperties.ts b/src/config/GapiAuthInitProperties.ts deleted file mode 100644 index e8ad211..0000000 --- a/src/config/GapiAuthInitProperties.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface GapiAuthInitProperties { - client_id: string; - discoveryDocs: string[]; - scope: string; -} diff --git a/src/config/GoogleApiConfig.ts b/src/config/GoogleApiConfig.ts index 3389341..fecb7c7 100644 --- a/src/config/GoogleApiConfig.ts +++ b/src/config/GoogleApiConfig.ts @@ -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 } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 8a72944..3f369ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,4 @@ -export * from './'; \ No newline at end of file +export * from './GoogleApiModule'; +export * from './GoogleAuthService'; +export * from './GoogleApiService'; +export * from './config/GoogleApiConfig'; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 7f6345d..3be7449 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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 + } } \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index dee8df1..9282070 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,7 +1,7 @@ var path = require('path'); module.exports = { - entry: "./src/googleApi/GoogleApiModule.ts", + entry: "./src/GoogleApiModule.ts", output: { filename: "bundle.js" },