Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Api {
- constructor(apiUrl) {
- this.apiUrl = apiUrl;
- }
- getPurchases () {
- return fetch(`/purchases`, {
- headers: {
- 'Content-Type': 'application/json'
- }
- })
- .then( e => {
- if(e.ok) {
- return e.json()
- }
- return Promise.reject(e.statusText)
- })
- }
- addPurchases (id) {
- return fetch(`/purchases`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({
- id: id
- })
- })
- .then( e => {
- if(e.ok) {
- return e.json()
- }
- return Promise.reject(e.statusText)
- })
- }
- removePurchases (id){
- return fetch(`/purchases/${id}`, {
- method: 'DELETE',
- headers: {
- 'Content-Type': 'application/json'
- }
- })
- .then( e => {
- if(e.ok) {
- return e.json()
- }
- return Promise.reject(e.statusText)
- })
- }
- addSubscriptions(id) {
- return fetch(`/subscriptions`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({
- id: id
- })
- })
- .then( e => {
- if(e.ok) {
- return e.json()
- }
- return Promise.reject(e.statusText)
- })
- }
- removeSubscriptions (id) {
- return fetch(`/subscriptions/${id}`, {
- method: 'DELETE',
- headers: {
- 'Content-Type': 'application/json'
- }
- })
- .then( e => {
- if(e.ok) {
- return e.json()
- }
- return Promise.reject(e.statusText)
- })
- }
- addFavorites (id) {
- return fetch(`/favorites`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({
- id: id
- })
- })
- .then( e => {
- if(e.ok) {
- return e.json()
- }
- return Promise.reject(e.statusText)
- })
- }
- removeFavorites (id) {
- return fetch(`/favorites/${id}`, {
- method: 'DELETE',
- headers: {
- 'Content-Type': 'application/json'
- }
- })
- .then( e => {
- if(e.ok) {
- return e.json()
- }
- return Promise.reject(e.statusText)
- })
- }
- getIngredients (text) {
- return fetch(`/ingredients?query=${text}`, {
- headers: {
- 'Content-Type': 'application/json'
- }
- })
- .then( e => {
- if(e.ok) {
- return e.json()
- }
- return Promise.reject(e.statusText)
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement