Guest User

Untitled

a guest
Feb 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import { ModuleWithProviders, NgModule } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3.  
  4. import { Injectable, Inject } from '@angular/core';
  5. import * as WooCommerceAPI from 'woocommerce-api';
  6.  
  7. //For exporting the service:
  8. //import { WooApiService } from './src/woocommerce.service';
  9. //export * from './src/woocommerce.service';
  10.  
  11. @Injectable()
  12. export class WooApiService {
  13.  
  14. woo: any;
  15.  
  16. constructor(@Inject('config') private config: any) {
  17. this.woo = WooCommerceAPI(config);
  18. }
  19.  
  20. fetchItems(itemType:string): Promise<any> {
  21. return new Promise((resolve, reject) => {
  22. this.woo.getAsync(itemType)
  23. .then((data:any) => resolve(JSON.parse(data.toJSON().body)))
  24. .catch((error:Error) => reject(error));
  25. });
  26. };
  27.  
  28. }
  29.  
  30.  
  31. @NgModule({
  32. imports: [ CommonModule ],
  33. declarations: [],
  34. exports: []
  35. })
  36. export class WooApiModule {
  37. static forRoot(config: Object): ModuleWithProviders {
  38. return {
  39. ngModule: WooApiModule,
  40. providers: [ WooApiService, {provide: 'config', useValue: config}]
  41. };
  42. }
  43. }
Add Comment
Please, Sign In to add comment