import API from './APIUtils' ; import { IArticle } from '../types' ; const encode = encodeURIComponent ; type Articles = { articles : Array < IArticle > ; } & { articlesCount : number ; } ; type Article = { article : IArticle ; } ; function limit ( count , p ) { return `limit=${ count }&offset=${ p ? p * count : 0 }` ; } function omitSlug ( article ) { return Object . assign ( { } , article , { slug : undefined } ) ; } export function getArticles ( page ) { return API . get < Articles > ( `/articles?${ limit ( 10 , page ) }` ) ; } export function getArticlesByAuthor ( username , page ) { return API . get < Articles > ( `/articles?author=${ encode ( username ) }&${ limit ( 5 , page ) }` ) ; } export function getArticlesByTag ( tag , page ) { return API . get < Articles > ( `/articles?tag=${ encode ( tag ) }&${ limit ( 10 , page ) }` ) ; } export function deleteArticle ( slug ) { return API . delete < null > ( `/articles/${ slug }` ) ; } export function favoriteArticle ( slug ) { return API . post < Article > ( `/articles/${ slug }/favorite` ) ; } export function getArticlesFavoritedBy ( username , page ) { return API . get < Articles > ( `/articles?favorited=${ encode ( username ) }&${ limit ( 5 , page ) }` ) ; } export function getFeedArticles ( ) { return API . get < Articles > ( '/articles/feed?limit=10&offset=0' ) ; } export function getArticle ( slug ) { return API . get < Article > ( `/articles/${ slug }` ) ; } export function unfavoriteArticle ( slug ) { return API . delete < Article > ( `/articles/${ slug }/favorite` ) ; } export function updateArticle ( article ) { return API . put < Article > ( `/articles/${ article . slug }` , { article : omitSlug ( article ) , } ) ; } export function createArticle ( article ) { return API . post < Article > ( '/articles' , { article } ) ; }