Created
February 8, 2018 18:02
-
-
Save dfa1234/de66a44d129e346df11313b3f11ab5d7 to your computer and use it in GitHub Desktop.
Woo Angular Module Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ModuleWithProviders, NgModule } from '@angular/core'; | |
import { CommonModule } from '@angular/common'; | |
import { Injectable, Inject } from '@angular/core'; | |
import * as WooCommerceAPI from 'woocommerce-api'; | |
//For exporting the service: | |
//import { WooApiService } from './src/woocommerce.service'; | |
//export * from './src/woocommerce.service'; | |
@Injectable() | |
export class WooApiService { | |
woo: any; | |
constructor(@Inject('config') private config: any) { | |
this.woo = WooCommerceAPI(config); | |
} | |
fetchItems(itemType:string): Promise<any> { | |
return new Promise((resolve, reject) => { | |
this.woo.getAsync(itemType) | |
.then((data:any) => resolve(JSON.parse(data.toJSON().body))) | |
.catch((error:Error) => reject(error)); | |
}); | |
}; | |
} | |
@NgModule({ | |
imports: [ CommonModule ], | |
declarations: [], | |
exports: [] | |
}) | |
export class WooApiModule { | |
static forRoot(config: Object): ModuleWithProviders { | |
return { | |
ngModule: WooApiModule, | |
providers: [ WooApiService, {provide: 'config', useValue: config}] | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment