Skip to content

Commit

Permalink
Avoid having to overload the fetch decorators
Browse files Browse the repository at this point in the history
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, f76f852, gcanti/fp-ts#1761
  • Loading branch information
thewilkybarkid committed Dec 20, 2022
1 parent 38cb931 commit b409a43
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })),
)

Expand Down
4 changes: 2 additions & 2 deletions src/crossref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export const isCrossrefPreprintDoi: Refinement<Doi, CrossrefPreprintId['doi']> =

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 =>
Expand Down
4 changes: 2 additions & 2 deletions src/datacite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const isDatacitePreprintDoi: R.Refinement<Doi, DatacitePreprintId['doi']>

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 =>
Expand Down
15 changes: 5 additions & 10 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -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<E extends F.FetchEnv>(env: E): E {
return { ...env, fetch: (url, init) => env.fetch(url, { cache: 'force-cache', ...init }) }
export function useStaleCache<E extends F.FetchEnv>(): (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<E extends F.FetchEnv>(env: E): E {
return {
export function revalidateIfStale<E extends F.FetchEnv>(): (env: E) => E {
return env => ({
...env,
fetch: async (url, init) => {
const response = await env.fetch(url, init)
Expand All @@ -26,7 +21,7 @@ export function revalidateIfStale<E extends F.FetchEnv>(env: E): E {

return response
},
}
})
}

export function timeoutRequest<E extends F.FetchEnv>(timeout: number): (env: E) => E {
Expand Down
10 changes: 5 additions & 5 deletions src/zenodo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit b409a43

Please sign in to comment.