npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ballatech/effect-oauth-client

v0.3.2

Published

[![npm version](https://img.shields.io/npm/v/%40ballatech%2Feffect-oauth-client)](https://www.npmjs.com/package/@ballatech/effect-oauth-client) [![Checked with Biome](https://img.shields.io/badge/Checked_with-Biome-60a5fa?style=flat&logo=biome)](https://b

Readme

effect-oauth-client

npm version Checked with Biome

Effect-first OAuth 2.0 Client Credentials helper for @effect/platform's HttpClient.

  • Fetches access tokens using the client credentials grant
  • Caches tokens and auto-refreshes near expiry
  • Transparently attaches Authorization: Bearer <token> to outgoing requests
  • Retries once on 401 responses

Installation

pnpm add @ballatech/effect-oauth-client

This package expects effect and @effect/platform to be available as peers.

pnpm add effect @effect/platform

API

import { OAuthClient } from "@ballatech/effect-oauth-client"
  • OAuthClient.make(credentials)Effect<HttpClient>
    • Builds an HttpClient that automatically obtains and injects access tokens.

Credentials

type Credentials = {
  clientId: string
  clientSecret: Redacted.Redacted<string>
  tokenUrl: string
  scope?: string
  audience?: string
  ttl?: Duration.Duration // default: 3600 seconds
  expiryBuffer?: Duration.Duration // default: 300 seconds (refresh ~5m early)
}

Notes:

  • ttl controls the cache TTL for the token effect. Actual token expiry is respected via the expires_in value and refreshed ~10 seconds early.
  • scope and audience are optional and sent as URL-encoded form parameters.

Errors

OAuthClient can fail with AuthorizationError (a tagged error) with code:

  • credentials_error: parsing or validation of the token response failed
  • client_error: HTTP client or response error while obtaining a token
  • unauthorized: downstream API responded with 401

Usage

Basic request

import { OAuthClient } from "@ballatech/effect-oauth-client"
import { Effect, Redacted, Schema } from "effect"
import { FetchHttpClient, HttpClientResponse } from "@effect/platform"

const FooSchema = Schema.Struct({ foo: Schema.String })

const program = Effect.gen(function* () {
  const client = yield* OAuthClient.make({
    clientId: "my-client-id",
    clientSecret: Redacted.make("my-secret"),
    tokenUrl: "https://auth.example.com/oauth/token",
    scope: "read:foo",
  })

  // The client now automatically includes a Bearer token
  const result = yield* client
    .get("https://api.example.com/secret-foo")
    .pipe(
      Effect.flatMap(HttpClientResponse.schemaBodyJson(FooSchema)),
      Effect.scoped
    )

  return result
})

// Provide an HttpClient implementation (Fetch)
Effect.runPromise(program.pipe(Effect.provide(FetchHttpClient.layer)))

With Layer and service composition

import { Context, Effect, Layer, Redacted } from "effect"
import { OAuthClient } from "@ballatech/effect-oauth-client"
import { FetchHttpClient, HttpClientResponse } from "@effect/platform"

const makeService = Effect.gen(function* () {
  const client = yield* OAuthClient.make({
    clientId: "id123",
    clientSecret: Redacted.make("secret"),
    tokenUrl: "https://auth.example.com/oauth/token",
  })
  const getFoo = () =>
    client.get("https://api.example.com/secret-foo").pipe(Effect.scoped)
  return { getFoo }
})

class MyService extends Context.Tag("MyService")<
  MyService,
  Effect.Effect.Success<typeof makeService>
>() {}

export const MyServiceLayer = Layer.effect(MyService, makeService).pipe(
  Layer.provide(FetchHttpClient.layer)
)

Testing (mocking Fetch)

import { beforeEach, describe, expect, it, vi } from "@effect/vitest"
import { Duration, Effect, Layer, ManagedRuntime, Redacted } from "effect"
import { FetchHttpClient } from "@effect/platform"
import { OAuthClient } from "@ballatech/effect-oauth-client"

describe("OAuthClient", () => {
  let rt: ManagedRuntime.ManagedRuntime<never, never>
  const fetch = vi.fn()

  beforeEach(() => {
    fetch.mockReset()
    const FetchTest = Layer.succeed(FetchHttpClient.Fetch, fetch)
    rt = ManagedRuntime.make(FetchHttpClient.layer.pipe(Layer.provide(FetchTest)))
  })

  it("fetches and reuses token", async () => {
    fetch.mockImplementation(async (url: URL) => {
      if (url.href.includes("/oauth/token")) {
        return new Response(JSON.stringify({ access_token: "t", token_type: "Bearer", expires_in: 3600 }), { status: 200 })
      }
      return new Response(JSON.stringify({ ok: true }), { status: 200 })
    })

    const prog = Effect.gen(function* () {
      const client = yield* OAuthClient.make({
        clientId: "id",
        clientSecret: Redacted.make("secret"),
        tokenUrl: "https://auth.example.com/oauth/token",
        ttl: Duration.seconds(3600),
      })
      yield* client.get("https://api.example.com/foo").pipe(Effect.scoped)
      yield* client.get("https://api.example.com/foo").pipe(Effect.scoped)
    })

    await rt.runPromise(prog)
  })
})

Requirements

  • Provide an HttpClient layer, e.g. FetchHttpClient.layer
  • effect and @effect/platform must be installed (peer dependencies)

Build

pnpm -w build