@zhiguang-gastrofy/capi
Version:
comany apis, including Northfork api and Gastrofy api
68 lines (54 loc) • 1.83 kB
text/typescript
import { NorthforkService } from './northfork.service';
import { GastrofyService } from './gastrofy.service';
function attachMeta( recipe, metas:any ) {
if( !metas || !metas.length ) return;
for( let meta of metas ) {
if( recipe.gastrofy_id == meta.id ) {
recipe.rating = meta.rating;
recipe.nutrition = meta.nutrition;
recipe.chef = meta.chefs && meta.chefs[0] ? meta.chefs[0] : {};
break;
}
}
}
// GastrofyService with NorthforkService
export class GNService {
_NFHttp: any;
_GFHttp: any;
constructor( config:any ) {
this._NFHttp = new NorthforkService( {
customerToken: config.customerToken
} );
this._GFHttp = new GastrofyService();
}
getRecipe( id:any, params?:any ) {
return this._NFHttp.getRecipe( id, params ).then( ( recipe:any ) => {
return this._GFHttp.getRecipeMeta( recipe.gastrofy_id ).then( ( metas:any ) => {
attachMeta( recipe, metas );
return recipe;
} );
} );
}
getRecipes( params?:any ) {
let promise = this._NFHttp.getRecipes( params );
promise.then( ( recipesResponse:any ) => {
if( !recipesResponse.data || !recipesResponse.data.length ) return recipesResponse;
let gastrofyIds = recipesResponse.data.map( ( recipe:any ) => {
return recipe.gastrofy_id;
} ).filter( ( gastrofyId:any ) => {
return !!gastrofyId;
} );
if( !gastrofyIds.length ) return recipesResponse;
return this._GFHttp.getRecipeMeta( gastrofyIds ).then( ( metaResponse:any ) => {
recipesResponse.data = recipesResponse.data.map( ( recipe:any ) => {
attachMeta( recipe, metaResponse );
return recipe;
} );
return recipesResponse;
} );
}, ( error:any ) => {
return error;
} );
return promise;
}
}