Skip to content

Instantly share code, notes, and snippets.

@dfa1234
Created February 8, 2018 18:02
Show Gist options
  • Save dfa1234/de66a44d129e346df11313b3f11ab5d7 to your computer and use it in GitHub Desktop.
Save dfa1234/de66a44d129e346df11313b3f11ab5d7 to your computer and use it in GitHub Desktop.
Woo Angular Module Wrapper
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