Created
November 27, 2022 10:53
-
-
Save pigoz/32e740961ac1dd32d1ac21f365aa1d02 to your computer and use it in GitHub Desktop.
playing around with Effect/io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { pipe } from '@fp-ts/data/Function'; | |
import * as E from '@fp-ts/data/Either'; | |
import * as Effect from '@effect/io/Effect'; | |
// import * as Layer from '@effect/io/Layer'; | |
// import * as Context from '@fp-ts/data/Context'; | |
import { z } from 'zod'; | |
import debug_ from 'debug'; | |
export function decode<T>(schema: z.ZodSchema<T>) { | |
return (input: unknown): E.Either<z.ZodError<T>, T> => { | |
const result = schema.safeParse(input); | |
if (result.success) { | |
return E.right(result.data); | |
} else { | |
return E.left(result.error); | |
} | |
}; | |
} | |
export const debug = | |
(namespace: string) => | |
(formatter: any, ...args: any[]) => | |
Effect.sync(() => { | |
debug_(namespace)(formatter, ...args); | |
}); | |
export const httpDebug = debug('http'); | |
export const GetJson = (url: string, schema: z.Schema) => | |
pipe( | |
Effect.tryCatchPromise( | |
() => fetch(url), | |
err => E.left({ message: 'fetch failed', err }), | |
), | |
Effect.flatMap(res => | |
Effect.tryCatchPromise( | |
() => res.json(), | |
err => E.left({ message: 'json parse failed', err }), | |
), | |
), | |
Effect.map(decode(schema)), | |
Effect.flatMap(Effect.fromEither), | |
Effect.flatMap(r => httpDebug('%O', r)), | |
); | |
export const GetJsonGen = (url: string, schema: z.Schema) => | |
Effect.gen(function* ($) { | |
const res = yield* $( | |
Effect.tryCatchPromise( | |
() => fetch(url), | |
err => E.left({ message: 'fetch failed', err }), | |
), | |
); | |
const json = yield* $( | |
Effect.tryCatchPromise( | |
() => res.json(), | |
err => E.left({ message: 'json parse failed', err }), | |
), | |
); | |
const result = yield* $(pipe(json, decode(schema), Effect.fromEither)); | |
// yield* $(httpDebug('%O', result)); | |
// dal momento che siamo dentro un generatore possiamo anche chiamare il | |
// codice sincrono... un bene o un male:? | |
debug_('http')('%O', result); | |
}); | |
const GistsSchema = z.array( | |
z.object({ id: z.string(), description: z.string().nullable() }), | |
); | |
const program = GetJsonGen('https://api.github.com/gists', GistsSchema); | |
Effect.unsafeFork(program); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment