Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Created July 27, 2021 08:17
Show Gist options
  • Select an option

  • Save MeetMartin/000f06642e53633dc23a19495b2c42c4 to your computer and use it in GitHub Desktop.

Select an option

Save MeetMartin/000f06642e53633dc23a19495b2c42c4 to your computer and use it in GitHub Desktop.
// /src/store/effects/NetlifyFunction.js
import { AsyncEffect, isEqual, Maybe, map } from '@7urtle/lambda';
import axios from 'axios';
const api_url = isEqual('development')(process.env.NODE_ENV) ? 'http://localhost:5000' : '/.netlify/functions';
/**
* Ansynchronous post request to a Netlify Function on an input path
* that maybe returns a returns response data based on input data.
*
* @HindleyMilner postToFunction :: string -> object -> AsyncEffect.of(Maybe.of(object))
*
* @pure
* @param {string} path path to the Netlify Function added to /.netlify/functions
* @param {object} data data to be posted to the function
* @returns {AsyncEffect} AsyncEffect of Maybe of quote object
*
* @example
* postToFunction('/my-function')({something: 'something'})
* .trigger
* (error => console.log(error))
* (MaybeData =>
* maybe
* (() => console.log('Response data is Nothing.'))
* (data => console.log('Response data are: ', data))
* (MaybeData)
* );
*/
export const postToFunction = path => data =>
map(response => Maybe.of(response.data))
(
AsyncEffect.ofPromise(() =>
axios.post(
api_url + path,
data
)
)
);
// /src/store/effects/QuoteEffect.js
import { postToFunction } from './NetlifyFunction';
/**
* Ansynchronous request that maybe returns a random quote.
* It calls Netlify function to obtain the data by partial application of postToFunction function.
*
* @HindleyMilner requestQuote :: object -> AsyncEffect.of(Maybe.of(object))
*
* @pure
* @param {object} action global state action
* @returns {AsyncEffect} AsyncEffect of Maybe of quote object
*
* @example
* requestQuote()
* .trigger
* (error => console.log(error))
* (MaybeQuote =>
* maybe
* (() => console.log('Quote is Nothing.'))
* (quote => console.log(`"${quote.quote}" by ${quote.author}`))
* (MaybeQuote)
* );
*/
export const getQuoteFromNetlify = postToFunction('/quote');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment