|
| 1 | +# Usage |
| 2 | + |
| 3 | +This example uses the [Ramda library](https://ramdajs.com) - for simplification - but you should be able to use any |
| 4 | +library that implements the [Fantasy-land specifications](https://github.com/fantasyland/fantasy-land). |
| 5 | + |
| 6 | +```js |
| 7 | +import { __, ap, chain, compose, lift, map, match, path, prop, useWith } from "https://deno.land/x/[email protected]/mod.ts"; |
| 8 | +import Task from "https://deno.land/x/[email protected]/library/Task.js"; |
| 9 | +import { safeExtract } from "https://deno.land/x/[email protected]/library/utilities.js"; |
| 10 | +import Request from "https://deno.land/x/[email protected]/library/Request.js"; |
| 11 | +import { factorizeFile } from "https://deno.land/x/[email protected]/library/File.js"; |
| 12 | +import { fetch } from "https://deno.land/x/[email protected]/library/browser_safe.js"; |
| 13 | +import { writeFile } from "https://deno.land/x/[email protected]/library/fs.js"; |
| 14 | + |
| 15 | +const fetchBacon = compose( |
| 16 | + chain(writeFile({})), |
| 17 | + ap( |
| 18 | + useWith( |
| 19 | + lift(factorizeFile(__, __, 0)), |
| 20 | + [ |
| 21 | + compose( |
| 22 | + Task.of, |
| 23 | + name => `${Deno.cwd()}/${name}.html`, |
| 24 | + prop(1), |
| 25 | + match(/\?type=([A-Za-z\-]+)/), |
| 26 | + path([ "headers", "url" ]) |
| 27 | + ), |
| 28 | + map(prop("raw")) |
| 29 | + ] |
| 30 | + ), |
| 31 | + fetch |
| 32 | + ) |
| 33 | +); |
| 34 | + |
| 35 | +const container = fetchBacon( |
| 36 | + Request.get("https://baconipsum.com/api/?type=all-meat¶s=3&start-with-lorem=1&format=html") |
| 37 | +); |
| 38 | + |
| 39 | +// Calling `fetchBacon` results in an instance of `Task` keeping the function pure. |
| 40 | +assert(Task.is(container)); |
| 41 | + |
| 42 | +const file = safeExtract("Failed to write file.", await container.run()); |
| 43 | + |
| 44 | +assert(File.is(file)); |
| 45 | +``` |
| 46 | + |
| 47 | +### Using the bundle |
| 48 | + |
| 49 | +As a convenience, when using Functional IO in the browser, you can use the **unminified** bundled copy (18KB gzipped). |
| 50 | + |
| 51 | +```js |
| 52 | +import { Task, safeExtract } from "https://deno.land/x/[email protected]/functional.js"; |
| 53 | +import { Request, Response, fetch } from "https://deno.land/x/[email protected]/functional-io.js"; |
| 54 | + |
| 55 | +const container = fetch( |
| 56 | + Request.get("https://baconipsum.com/api/?type=all-meat¶s=3&start-with-lorem=1&format=html") |
| 57 | +); |
| 58 | + |
| 59 | +assert(Task.is(container)); |
| 60 | + |
| 61 | +const response = safeExtract("Failed to fetch resource.", await container.run()); |
| 62 | + |
| 63 | +assert(Response.is(response)); |
| 64 | +``` |
0 commit comments