import { Effect, pipe } from "effect"
interface User {
id: string
name: string
middleName: string
phone: string
role: string
fullName: string
settings: object
twoFactorType: string
email: string
createdAt: string
affiliate: string
}
interface AuthenticateClientResponse {
token: string
user: User
}
class AuthenticationError {
readonly _tag = "AuthenticationError"
}
export async function postAuthenticateClient() {
const url = `${process.env.TELEGRA_URL}/auth/client`
const username = process.env.TELEGRA_USERNAME
const password = process.env.TELEGRA_PASSWORD
const credentials = Buffer.from(`${username}:${password}`).toString("base64")
const response: Effect.Effect<
AuthenticateClientResponse,
AuthenticationError
> = pipe(
Effect.tryPromise(() =>
fetch(url, {
method: "POST",
redirect: "follow",
headers: { Authorization: `Basic ${credentials}` },
}),
),
Effect.flatMap((response) =>
!response.ok
? Effect.fail(new AuthenticationError())
: Effect.tryPromise(() => response.json()),
),
Effect.map((result: AuthenticateClientResponse) => result),
)
return Effect.runPromise(response).then((res) => res.token)
}
postAuthenticateClient().then(console.log)
import { Effect, pipe } from "effect"
interface User {
id: string
name: string
middleName: string
phone: string
role: string
fullName: string
settings: object
twoFactorType: string
email: string
createdAt: string
affiliate: string
}
interface AuthenticateClientResponse {
token: string
user: User
}
class AuthenticationError {
readonly _tag = "AuthenticationError"
}
export async function postAuthenticateClient() {
const url = `${process.env.TELEGRA_URL}/auth/client`
const username = process.env.TELEGRA_USERNAME
const password = process.env.TELEGRA_PASSWORD
const credentials = Buffer.from(`${username}:${password}`).toString("base64")
const response: Effect.Effect<
AuthenticateClientResponse,
AuthenticationError
> = pipe(
Effect.tryPromise(() =>
fetch(url, {
method: "POST",
redirect: "follow",
headers: { Authorization: `Basic ${credentials}` },
}),
),
Effect.flatMap((response) =>
!response.ok
? Effect.fail(new AuthenticationError())
: Effect.tryPromise(() => response.json()),
),
Effect.map((result: AuthenticateClientResponse) => result),
)
return Effect.runPromise(response).then((res) => res.token)
}
postAuthenticateClient().then(console.log)