From d6d14566947df0d843134be2c1807d5602f0f684 Mon Sep 17 00:00:00 2001 From: codeforges Date: Thu, 13 Jul 2017 16:49:33 +0200 Subject: [PATCH 1/2] Fixes issue #3 and #4 - upgraded to ts v 2.3 - upgraded to Angualr4 - Changed config setup via provider value rather then factory - Readme updated - examples updated --- README.MD | 31 +++++++++++++------ examples/gapiReporting/GapiReportingModule.ts | 22 ++++++++----- package.json | 6 ++-- src/GoogleApiModule.ts | 12 +++---- src/GoogleApiService.ts | 14 ++++----- src/GoogleAuthService.ts | 2 +- src/config/GoogleApiConfig.ts | 12 +++---- 7 files changed, 57 insertions(+), 42 deletions(-) diff --git a/README.MD b/README.MD index b93bf4a..b655e7e 100644 --- a/README.MD +++ b/README.MD @@ -1,8 +1,13 @@ -### Angular 2 Google api module (ng-gapi) +### Angular 4 Google api module (ng-gapi) This module will add the google api to your project. It wraps the Gapi in to a service layer allowing to work with Gapi -in a Angular 2+ project. +in a Angular 4+ project. + +#### Latest change + +- Requires now Typescript version 2.3 or higher +- Requires Angular4 or higher #### Installation @@ -15,16 +20,22 @@ npm install ng-gapi To use the `ng-gapi` simply add `GoogleApiModule` to your module imports and set the configuration ```typescript +let gapiConfig = { + clientId: "CLIENT_ID", + discoveryDocs: ["https://analyticsreporting.googleapis.com/$discovery/rest?version=v4"], + scope: [ + "https://www.googleapis.com/auth/analytics.readonly", + "https://www.googleapis.com/auth/analytics" + ].join(" ") +}; + @NgModule({ imports: [ ... - GoogleApiModule.setConfig( - { - clientId: "your client id", - discoveryDocs: ["url to discovery docs", "another url"], - scope: "space separated scopes" - } - ), + GoogleApiModule.forRoot({ + provide: NG_GAPI_CONFIG, + useValue: gapiConfig + }), ... ] }) @@ -48,6 +59,8 @@ export class FooService { } ``` +Also check the example folder with a google api reports module + #### GoogleAuthService The module has a GoogleAuth service which allows you to work with the google auth diff --git a/examples/gapiReporting/GapiReportingModule.ts b/examples/gapiReporting/GapiReportingModule.ts index c9276e5..b5ac632 100644 --- a/examples/gapiReporting/GapiReportingModule.ts +++ b/examples/gapiReporting/GapiReportingModule.ts @@ -6,18 +6,24 @@ import {GapiUserService} from "./services/GapiUserService"; import {BrowserModule} from "@angular/platform-browser"; import {FormsModule} from "@angular/forms"; import {GapiReportingModel} from "./services/GapiReportingModel"; +import {NG_GAPI_CONFIG} from "../../src/GoogleApiService"; + +let gapiConfig = { + clientId: "CLIENT_ID", + discoveryDocs: ["https://analyticsreporting.googleapis.com/$discovery/rest?version=v4"], + scope: [ + "https://www.googleapis.com/auth/analytics.readonly", + "https://www.googleapis.com/auth/analytics" + ].join(" ") +}; @NgModule({ imports: [ BrowserModule, FormsModule, - GoogleApiModule.setConfig({ - clientId: "CLIENT_ID", - discoveryDocs: ["https://analyticsreporting.googleapis.com/$discovery/rest?version=v4"], - scope: [ - "https://www.googleapis.com/auth/analytics.readonly", - "https://www.googleapis.com/auth/analytics" - ].join(" ") + GoogleApiModule.forRoot({ + provide: NG_GAPI_CONFIG, + useValue: gapiConfig }) ], declarations: [ @@ -33,4 +39,4 @@ import {GapiReportingModel} from "./services/GapiReportingModel"; ] }) export class GapiReportingModule { -} \ No newline at end of file +} diff --git a/package.json b/package.json index d2970d6..2030056 100644 --- a/package.json +++ b/package.json @@ -31,13 +31,13 @@ }, "homepage": "https://github.com/rubenCodeforges/angular2-google-api#readme", "dependencies": { - "@types/gapi": "0.0.31", - "@types/gapi.auth2": "0.0.35" + "@types/gapi": "0.0.33", + "@types/gapi.auth2": "0.0.40" }, "devDependencies": { "@angular/core": "^4.1.1", "rxjs": "^5.3.1", - "typescript": "^2.2.2", + "typescript": "^2.3.0", "zone.js": "^0.8.10" } } diff --git a/src/GoogleApiModule.ts b/src/GoogleApiModule.ts index 9f3c893..4a5ef77 100644 --- a/src/GoogleApiModule.ts +++ b/src/GoogleApiModule.ts @@ -1,18 +1,14 @@ import {GoogleApiService} from "./GoogleApiService"; -import {ModuleWithProviders, NgModule} from "@angular/core"; +import {ModuleWithProviders, NgModule, Provider} from "@angular/core"; import {GoogleAuthService} from "./GoogleAuthService"; -import {GapiInitConfigs} from "./config/GoogleApiConfig"; @NgModule() export class GoogleApiModule { - static setConfig(apiConfig: GapiInitConfigs): ModuleWithProviders { + static forRoot(gapiConfigProvider: Provider): ModuleWithProviders { return { ngModule: GoogleApiModule, providers: [ - { - provide: GoogleApiService, - useFactory: () => GoogleApiService.factory(apiConfig), - }, + gapiConfigProvider, { provide: GoogleAuthService, useFactory: GoogleAuthService.factory, @@ -23,4 +19,4 @@ export class GoogleApiModule { ] } } -} \ No newline at end of file +} diff --git a/src/GoogleApiService.ts b/src/GoogleApiService.ts index 4ff5009..2a1dcbd 100644 --- a/src/GoogleApiService.ts +++ b/src/GoogleApiService.ts @@ -1,6 +1,10 @@ import {Observable} from "rxjs"; -import {Injectable} from "@angular/core"; -import {GoogleApiConfig, GapiInitConfigs} from "./config/GoogleApiConfig"; +import {Inject, Injectable, InjectionToken} from "@angular/core"; +import {GapiInitConfigs, GoogleApiConfig} from "./config/GoogleApiConfig"; + + +export let NG_GAPI_CONFIG: InjectionToken = + new InjectionToken("ng-gapi.config"); @Injectable() export class GoogleApiService { @@ -8,11 +12,7 @@ export class GoogleApiService { private isLoaded: boolean = false; private config: GoogleApiConfig; - public static factory(configs: GapiInitConfigs) { - return new GoogleApiService(configs); - } - - constructor(config: GapiInitConfigs) { + constructor(@Inject(NG_GAPI_CONFIG) config: GapiInitConfigs) { this.config = new GoogleApiConfig(config); this.loadGapi(); } diff --git a/src/GoogleAuthService.ts b/src/GoogleAuthService.ts index 114db46..338c511 100644 --- a/src/GoogleAuthService.ts +++ b/src/GoogleAuthService.ts @@ -36,4 +36,4 @@ export class GoogleAuthService { return new GoogleAuthService(googleApi) } -} \ No newline at end of file +} diff --git a/src/config/GoogleApiConfig.ts b/src/config/GoogleApiConfig.ts index fecb7c7..40c47ad 100644 --- a/src/config/GoogleApiConfig.ts +++ b/src/config/GoogleApiConfig.ts @@ -1,3 +1,9 @@ +export interface GapiInitConfigs { + clientId: string, + discoveryDocs: string[], + scope: string +} + export class GoogleApiConfig { protected CLIENT_ID: string; protected DISCOVERY_DOCS: string[]; @@ -17,9 +23,3 @@ export class GoogleApiConfig { } } } - -export interface GapiInitConfigs { - clientId: string, - discoveryDocs: string[], - scope: string -} \ No newline at end of file From 3f453b2e21fddeebd518b590248a7e3c514cd653 Mon Sep 17 00:00:00 2001 From: codeforges Date: Fri, 14 Jul 2017 15:38:05 +0200 Subject: [PATCH 2/2] -npm issues and compilation fixes --- .gitignore | 2 ++ .npmignore | 4 +++- package.json | 19 ++++++++++++------- src/GoogleApiModule.ts | 9 +-------- src/GoogleAuthService.ts | 6 ------ tsconfig.json | 30 +++++++++++++++++++++++------- 6 files changed, 41 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index a8f2eda..c09f675 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ testem.log #System Files .DS_Store Thumbs.db +/compiled +*.metadata.json \ No newline at end of file diff --git a/.npmignore b/.npmignore index b34948c..2187fd4 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,5 @@ node_modules src -.idea \ No newline at end of file +.idea +examples +compiled \ No newline at end of file diff --git a/package.json b/package.json index 2030056..d96028c 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,12 @@ { "name": "ng-gapi", - "version": "0.0.40", - "description": "Angular 2 Google api module", + "version": "0.0.40-3", + "description": "Angular 4 Google api module ng-gapi", "main": "./lib/index.js", - "typings": "./lib/index", + "typings": "./lib/index.d.ts", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "tsc": "ngc" }, "repository": { "type": "git", @@ -31,13 +32,17 @@ }, "homepage": "https://github.com/rubenCodeforges/angular2-google-api#readme", "dependencies": { - "@types/gapi": "0.0.33", - "@types/gapi.auth2": "0.0.40" + "@types/gapi": "0.0.33", + "@types/gapi.auth2": "0.0.40" }, "devDependencies": { + "@angular/compiler": "^4.2.6", + "@angular/compiler-cli": "^4.2.6", "@angular/core": "^4.1.1", + "@angular/platform-server": "^4.2.6", + "@types/node": "7.0.10", "rxjs": "^5.3.1", - "typescript": "^2.3.0", + "typescript": "^2.4.1", "zone.js": "^0.8.10" } } diff --git a/src/GoogleApiModule.ts b/src/GoogleApiModule.ts index 4a5ef77..3398afc 100644 --- a/src/GoogleApiModule.ts +++ b/src/GoogleApiModule.ts @@ -1,4 +1,3 @@ -import {GoogleApiService} from "./GoogleApiService"; import {ModuleWithProviders, NgModule, Provider} from "@angular/core"; import {GoogleAuthService} from "./GoogleAuthService"; @@ -9,13 +8,7 @@ export class GoogleApiModule { ngModule: GoogleApiModule, providers: [ gapiConfigProvider, - { - provide: GoogleAuthService, - useFactory: GoogleAuthService.factory, - deps: [ - GoogleApiService - ] - } + GoogleAuthService, ] } } diff --git a/src/GoogleAuthService.ts b/src/GoogleAuthService.ts index 338c511..e9b4b05 100644 --- a/src/GoogleAuthService.ts +++ b/src/GoogleAuthService.ts @@ -3,7 +3,6 @@ import {Observable} from "rxjs"; import {GoogleApiService} from "./GoogleApiService"; import GoogleAuth = gapi.auth2.GoogleAuth; - @Injectable() export class GoogleAuthService { private GoogleAuth: GoogleAuth = undefined; @@ -31,9 +30,4 @@ export class GoogleAuthService { }); }); } - - public static factory(googleApi: GoogleApiService) { - return new GoogleAuthService(googleApi) - } - } diff --git a/tsconfig.json b/tsconfig.json index 3be7449..8a5863c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,25 +1,41 @@ { "compilerOptions": { - "target": "es5", + "noImplicitAny": true, "module": "es2015", - "moduleResolution": "node", - "sourceMap": true, + "target": "es5", "emitDecoratorMetadata": true, "experimentalDecorators": true, "declaration": true, + "moduleResolution": "node", + "noUnusedLocals": true, + "types": [ + "node", + "gapi", + "gapi.auth2" + ], + "typeRoots": [ + "./node_modules/@types" + ], "lib": [ "es2015", "dom" ], - "outDir": "lib/", - "noImplicitAny": true, - "suppressImplicitAnyIndexErrors": true + "outDir": "lib/" }, + "files": [ + "./src/index.ts", + "./src/GoogleApiModule.ts", + "./src/GoogleApiService.ts", + "./src/GoogleAuthService.ts", + "./src/config/GoogleApiConfig.ts" + ], "exclude": [ - "node_modules" + "node_modules", + "examples" ], "angularCompilerOptions": { "strictMetadataEmit": true, + "genDir": "compiled", "skipTemplateCodegen": true } } \ No newline at end of file