Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created October 2, 2025 21:52
Show Gist options
  • Select an option

  • Save subtleGradient/33d012d8985285c401b9b591db3084fe to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/33d012d8985285c401b9b591db3084fe to your computer and use it in GitHub Desktop.
FaxMachine.effect.ts
import {FileSystem, HttpClient} from '@effect/platform'
import {formData} from '@effect/platform/HttpBody'
import {Config} from 'effect'
import * as Console from 'effect/Console'
import * as Effect from 'effect/Effect'
const testFaxNumber = '+19898989898'
export class FaxMachine extends Effect.Service<FaxMachine>()('FaxMachine', {
accessors: true,
effect: Effect.gen(function* () {
const [projectId, username, password] = [
yield* Config.nonEmptyString('SINCH_PROJECT_ID'),
yield* Config.nonEmptyString('SINCH_USERNAME'),
yield* Config.nonEmptyString('SINCH_PASSWORD'),
]
const auth = Buffer.from(`${username}:${password}`).toString('base64')
const http = yield* HttpClient.HttpClient
return {
sendFax: Effect.fnUntraced(function* ({faxTo = testFaxNumber, pathToPDF = '', dryRun = true}) {
const actuallyFaxTo = dryRun ? testFaxNumber : faxTo
yield* Console.log({dryRun, projectId, faxTo, actuallyFaxTo, pathToPDF})
const fs = yield* FileSystem.FileSystem
yield* fs
.exists(pathToPDF)
.pipe(
Effect.flatMap(exists =>
exists
? Console.log(`Found file at path: ${pathToPDF}`)
: Effect.fail(new Error(`File does not exist at path: ${pathToPDF}`)),
),
)
const form = new FormData()
form.append('to', actuallyFaxTo)
form.append('file', Bun.file(pathToPDF))
for (let i = 10; i >= 1; i--) {
yield* Console.log(`Sending fax to ${actuallyFaxTo} via Sinch API in ${i}s...`)
if (i > 1) yield* Effect.sleep(1000)
}
yield* Console.log(`Sending fax to ${actuallyFaxTo} via Sinch API...`)
// https://developers.sinch.com/docs/fax/api-reference/fax/tag/Faxes/#tag/Faxes/operation/sendFax
const resp = yield* http.post(`https://fax.api.sinch.com/v3/projects/${projectId}/faxes`, {
headers: {Authorization: `Basic ${auth}`},
body: formData(form),
})
return yield* resp.json
}),
}
}),
}) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment