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/src/GoogleApiModule.ts b/src/GoogleApiModule.ts index 5104af5..9f3c893 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: [ @@ -18,8 +17,7 @@ export class GoogleApiModule { provide: GoogleAuthService, useFactory: GoogleAuthService.factory, deps: [ - GoogleApiService, - authConfig + GoogleApiService ] } ] 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" },