From b409a433dcb7e954d65fef86f93d15a93b976acc Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Tue, 20 Dec 2022 12:07:01 +0000 Subject: [PATCH] Avoid having to overload the fetch decorators The timeoutRequest decorator didn't need overloading as the position of the generic saw TypeScript infer the type correctly. This change wraps the other decorators in a function so they can take advantage of this technique. Refs #536, f76f85283323886b09a7576291341b4c13bbe6f8, https://github.com/gcanti/fp-ts/pull/1761 --- src/app.ts | 2 +- src/crossref.ts | 4 ++-- src/datacite.ts | 4 ++-- src/fetch.ts | 15 +++++---------- src/zenodo.ts | 10 +++++----- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/app.ts b/src/app.ts index f09d197a8..bfd497658 100644 --- a/src/app.ts +++ b/src/app.ts @@ -179,7 +179,7 @@ const getPreprint = (doi: PreprintId['doi']) => const getPreprintTitle = flow( getPreprint, - RTE.local(useStaleCache), + RTE.local(useStaleCache()), RTE.map(preprint => ({ language: preprint.title.language, title: preprint.title.text })), ) diff --git a/src/crossref.ts b/src/crossref.ts index 7ebe89cbe..dc9a38c48 100644 --- a/src/crossref.ts +++ b/src/crossref.ts @@ -60,8 +60,8 @@ export const isCrossrefPreprintDoi: Refinement = export const getPreprintFromCrossref = flow( (doi: CrossrefPreprintId['doi']) => getWork(doi), - RTE.local(revalidateIfStale), - RTE.local(useStaleCache), + RTE.local(revalidateIfStale()), + RTE.local(useStaleCache()), RTE.local(timeoutRequest(2000)), RTE.chainEitherKW(workToPreprint), RTE.mapLeft(error => diff --git a/src/datacite.ts b/src/datacite.ts index 582a981a1..a436af017 100644 --- a/src/datacite.ts +++ b/src/datacite.ts @@ -23,8 +23,8 @@ export const isDatacitePreprintDoi: R.Refinement export const getPreprintFromDatacite = flow( (doi: DatacitePreprintId['doi']) => getWork(doi), - RTE.local(revalidateIfStale), - RTE.local(useStaleCache), + RTE.local(revalidateIfStale()), + RTE.local(useStaleCache()), RTE.local(timeoutRequest(2000)), RTE.chainEitherKW(dataciteWorkToPreprint), RTE.mapLeft(error => diff --git a/src/fetch.ts b/src/fetch.ts index ead1ab102..5963ab18b 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,18 +1,13 @@ import * as F from 'fetch-fp-ts' import { constVoid } from 'fp-ts/function' import * as L from 'logger-fp-ts' -import { ZenodoEnv } from 'zenodo-ts' -export function useStaleCache(env: ZenodoEnv): ZenodoEnv -export function useStaleCache(env: F.FetchEnv): F.FetchEnv -export function useStaleCache(env: E): E { - return { ...env, fetch: (url, init) => env.fetch(url, { cache: 'force-cache', ...init }) } +export function useStaleCache(): (env: E) => E { + return env => ({ ...env, fetch: (url, init) => env.fetch(url, { cache: 'force-cache', ...init }) }) } -export function revalidateIfStale(env: ZenodoEnv): ZenodoEnv -export function revalidateIfStale(env: F.FetchEnv): F.FetchEnv -export function revalidateIfStale(env: E): E { - return { +export function revalidateIfStale(): (env: E) => E { + return env => ({ ...env, fetch: async (url, init) => { const response = await env.fetch(url, init) @@ -26,7 +21,7 @@ export function revalidateIfStale(env: E): E { return response }, - } + }) } export function timeoutRequest(timeout: number): (env: E) => E { diff --git a/src/zenodo.ts b/src/zenodo.ts index 9cdc65099..ae76858cc 100644 --- a/src/zenodo.ts +++ b/src/zenodo.ts @@ -40,8 +40,8 @@ interface GetPreprintTitleEnv { export const getPrereviewFromZenodo = flow( getRecord, - RTE.local(revalidateIfStale), - RTE.local(useStaleCache), + RTE.local(revalidateIfStale()), + RTE.local(useStaleCache()), RTE.local(timeoutRequest(2000)), RTE.filterOrElseW(pipe(isInCommunity, and(isPeerReview)), () => new NotFound()), RTE.chain(recordToPrereview), @@ -57,8 +57,8 @@ export const getPrereviewsFromZenodo = flow( subtype: 'peerreview', }), getRecords, - RTE.local(revalidateIfStale), - RTE.local(useStaleCache), + RTE.local(revalidateIfStale()), + RTE.local(useStaleCache()), RTE.local(timeoutRequest(2000)), RTE.bimap( () => 'unavailable' as const, @@ -167,7 +167,7 @@ const getReviewUrl = flow( const getReviewText = flow( F.Request('GET'), F.send, - RTE.local(useStaleCache), + RTE.local(useStaleCache()), RTE.filterOrElseW(F.hasStatus(Status.OK), () => 'no text'), RTE.chainTaskEitherK(F.getText(identity)), RTE.map(sanitizeHtml),