Constructing Request Chains with Effect and TaskEither in TypeScript
Hello,guys.
I'm new to Effect and now and wrting codes to consrtuct a chain of request.In
The flow code like these:
The request function like this:
I'm new to Effect and now and wrting codes to consrtuct a chain of request.In
fp-tsfp-ts , it always done by TaskEitherTaskEither and its tryCatchtryCatch function.The flow code like these:
import { Effect, flow } from "effect"
import { getEmbeddingsByFetch } from "./googleGenimi.ts"
import { createEntity } from "./zilliz.ts"
export const sampleFlow = flow(
getEmbeddingsByFetch,
Effect.andThen(res => {
return res.embeddings.value
}),
Effect.andThen(vector =>
createEntity('Blog_test',{
en_name:'test',
vector
})
),
Effect.map(res => {
return Effect.runPromise(res)
}),
)import { Effect, flow } from "effect"
import { getEmbeddingsByFetch } from "./googleGenimi.ts"
import { createEntity } from "./zilliz.ts"
export const sampleFlow = flow(
getEmbeddingsByFetch,
Effect.andThen(res => {
return res.embeddings.value
}),
Effect.andThen(vector =>
createEntity('Blog_test',{
en_name:'test',
vector
})
),
Effect.map(res => {
return Effect.runPromise(res)
}),
)The request function like this:
export const getEmbeddingsByFetch = (text:string) => {
return Effect.tryPromise({
try:() => fetch(`https://generativelanguage.googleapis.com/v1beta/models/text-embedding-004:embedContent?key=${GENIMI_API_KEY}`,{
method:'POST',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify({
"model":"models/text-embedding-004",
"content":{
"parts":[
{
text
}
]
}
})
}).then(res => res.json() as Promise<GoogleSingleEmbeddingsResponse>),
catch:(err) => err
})
// return await Effect.runPromiseExit(result)
}export const getEmbeddingsByFetch = (text:string) => {
return Effect.tryPromise({
try:() => fetch(`https://generativelanguage.googleapis.com/v1beta/models/text-embedding-004:embedContent?key=${GENIMI_API_KEY}`,{
method:'POST',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify({
"model":"models/text-embedding-004",
"content":{
"parts":[
{
text
}
]
}
})
}).then(res => res.json() as Promise<GoogleSingleEmbeddingsResponse>),
catch:(err) => err
})
// return await Effect.runPromiseExit(result)
}