Skip to content

Instantly share code, notes, and snippets.

@kitasuna
Last active October 20, 2020 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitasuna/e27537f9e10ca1aac6893b8a8655bd23 to your computer and use it in GitHub Desktop.
Save kitasuna/e27537f9e10ca1aac6893b8a8655bd23 to your computer and use it in GitHub Desktop.
Some fun with fp-ts2's Reader monad
{
"name": "reader-intro",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fp-ts": "^2.7.0",
"ts-node": "^9.0.0",
"typescript": "^3.9.7"
}
}
type ProductId = string
type ProductJSON = {
productId: string,
productName: string
}
export const getPdpData = (id: ProductId): ProductJSON => {
return cacheOrClient(id)
}
export const cacheOrClient = (id: ProductId): ProductJSON => {
if (false) {
// get from cache
} else {
return getProductFromMicroservice(id)
}
}
export const getProductFromMicroservice = (id: ProductId): ProductJSON => {
console.log(`Making request to https://microservice/products/${id}`)
return {
productId: id,
productName: "Super Cool Godzilla Socks"
}
}
const result = getPdpData("E417963-000")
console.dir(result)
type ProductId = string
type ProductJSON = {
productId: string,
productName: string
}
type Dependencies = {
baseUrl: string
}
const getPdpData = (id: ProductId) => (deps: Dependencies): ProductJSON => {
return cacheOrClient(id)(deps)
}
const cacheOrClient = (id: ProductId) => (deps: Dependencies): ProductJSON => {
if (false) {
// get from cache
} else {
return getProductFromMicroservice(id)(deps)
}
}
const getProductFromMicroservice = (id: ProductId) => (deps: Dependencies): ProductJSON => {
console.log(`Making request to ${deps.baseUrl}/products/${id}`)
return {
productId: id,
productName: "Cool Product"
}
}
const myDeps = {
baseUrl: "https://microservice"
}
const result = getPdpData("E417963-000")(myDeps)
console.dir(result)
import * as Reader from "fp-ts/lib/Reader"
type ProductId = string
type ProductJSON = {
productId: string,
productName: string
}
type Dependencies = {
baseUrl: string
}
// Controller?
export const getPdpData = (id: ProductId) => {
return cacheOrClient(id)
}
export const cacheOrClient = (id: ProductId) => {
if(false) {
// Get from cache
} else {
return getProductFromMicroservice(id)
}
}
// Fake http client
// getProductFromMicroservice :: ProductId -> Dependencies -> ProductJSON
// Reader<Dependencies, ProductJSON> :: Dependencies -> ProductJSON
// getProductFromMicroservice :: ProductId -> Reader<Dependencies, ProductJSON>
export const getProductFromMicroservice = (id: ProductId) => {
const f = (deps: Dependencies) => {
console.log(`Making request to ${deps.baseUrl}/products/${id}`)
return {
productId: id,
productName: "Super Cool Godzilla Socks"
}
}
const getDeps = Reader.ask<Dependencies>()
return Reader.map(f)(getDeps)
}
const myDeps = {
baseUrl: "https://microservice"
}
const result = getPdpData("E417963-000")(myDeps)
console.dir(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment