import fetch from 'node-fetch' ; const flow = require ( 'lodash/fp/flow' ) ; const get = require ( 'lodash/fp/get' ) ; const invoke = require ( 'lodash/fp/invoke' ) ; const isEqual = require ( 'lodash/fp/isEqual' ) ; const map = require ( 'lodash/fp/map' ) ; const remove = require ( 'lodash/fp/remove' ) ; import { Category , Pallet } from './types' ; import { GRAPHQL_API_ENDPOINT } from '../../constants' ; returns { Promise < any > } const gq = ( query ) => ( variables ? ) : Promise < any > => fetch ( GRAPHQL_API_ENDPOINT , { method : 'POST' , headers : { 'Accept' : 'application/json' , 'Content-Type' : 'application/json' } , body : JSON . stringify ( { query , variables } ) } ) . then ( invoke ( 'json' ) ) . then ( ( d ) => d . data || Promise . reject ( map ( 'message' ) ( d . errors ) ) ) ; /**
 * Fetches all the pallets for a given Marketplace category.
 */ const fetchPallets = ( category ) : Promise < Pallet [ ] > => gq ( `query($category: String!){
			search(type: PALLET query: "" category: $category) {
				results {
				...on Pallet {
						id
						name
						description
						documentation
						homepage
						github: repository
						icon
						version
						repository
					}
				}
			}
		}` ) ( { category } ) . then ( get ( 'search.results' ) ) ; /**
 * Fetches a category with all its pallets.
 */ const fetchCategory = ( category ) : Promise < Category > => fetchPallets ( category ) . then ( ( pallets ) => ( { category , pallets } ) ) ; /**
 * Fetches the list of all categories and their pallets.
 */ const fetchCategories = ( ) : Promise < Category [ ] > => gq ( '{ marketplaceCategories(type:PALLET) {name} }' ) ( ) . then ( flow ( get ( 'marketplaceCategories' ) , map ( 'name' ) , remove ( isEqual ( 'tutorial' ) ) , map ( fetchCategory ) , Promise . all . bind ( Promise ) ) ) ; export default fetchCategories ;