1
0
Fork 0
sortable-recipes/app/adapters/meal.js
2019-12-22 11:47:00 -06:00

40 lines
954 B
JavaScript

import ApplicationAdapter from './application';
import { dasherize } from '@ember/string';
export default class MealAdapter extends ApplicationAdapter {
async findRecord(store, model, id) {
let result = await fetch(`${this.host}/lookup.php?i=${id}`);
let json = await result.json();
return normalize(json.meals[0]);
}
async queryRecord(store, model, query) {
if (query === 'random') {
let result = await fetch(`${this.host}/random.php`);
let json = await result.json();
return normalize(json.meals[0]);
}
}
}
function normalize(obj) {
let attributes = {};
Object.keys(obj).forEach(key => attributes[cleanKey(key)] = obj[key]);
return {
data: {
type: 'meal',
id: attributes.id,
attributes,
},
};
}
function cleanKey(key) {
key = dasherize(key);
key = key.replace('str-', '');
if (key === 'id-meal') key = 'id';
if (key === 'meal') key = 'name';
return key;
}