Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

npmRelease.ts

38 lines1.4 KB

1import { useAtomValue } from "@effect/atom-react";
2import * as Effect from "effect/Effect";
3import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/http";
4import { AsyncResult, Atom } from "effect/reactivity";
5​
6export const npmReleasePublishedAtom = Atom.family((version: string) =>
7 Atom.make(
8 Effect.gen(function* () {
9 const response = yield* HttpClient.get(
10 `https://registry.npmjs.org/t3/${encodeURIComponent(version)}`,
11 );
12 if (response.status === 404) return false;
13 const body = yield* HttpClientResponse.filterStatusOk(response).pipe(
14 Effect.flatMap((ok) => ok.json),
15 );
16 return typeof body === "object" && body !== null && "version" in body
17 ? body.version === version
18 : false;
19 }).pipe(
20 // Tracing headers would make the browser send a CORS preflight, which npm rejects.
21 Effect.provideService(HttpClient.TracerPropagationEnabled, false),
22 Effect.provide(FetchHttpClient.layer),
23 ),
24 ).pipe(
25 Atom.withRefresh("5 minutes"),
26 Atom.keepAlive,
27 Atom.withLabel(`npm-release-published:${version}`),
28 ),
29);
30​
31const NO_VERSION_ATOM = Atom.make(AsyncResult.success(false)).pipe(
32 Atom.withLabel("npm-release-published:none"),
33);
34​
35export function useNpmReleasePublished(version: string | null): boolean {
36 const result = useAtomValue(version ? npmReleasePublishedAtom(version) : NO_VERSION_ATOM);
37 return AsyncResult.getOrElse(result, () => false);