Skip to content

Commit

Permalink
Add a stub request to the ORCID API
Browse files Browse the repository at this point in the history
Refs: #976
  • Loading branch information
erkannt committed May 31, 2023
1 parent 6308dce commit 40c7d3c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export const router: P.Parser<RM.ReaderMiddleware<AppEnv, StatusOpen, ResponseEn
P.map(
R.local((env: AppEnv) => ({
...env,
getName: getNameFromOrcid,
getName: flip(getNameFromOrcid)(env),
getPrereviews: getPrereviewsForOrcidFromZenodo,
getUser: () => pipe(getSession(), chainOptionKW(() => 'no-session' as const)(getUserFromSession))(env),
})),
Expand Down
20 changes: 16 additions & 4 deletions src/orcid.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import * as TE from 'fp-ts/TaskEither'
import * as F from 'fetch-fp-ts'
import * as RTE from 'fp-ts/ReaderTaskEither'
import { pipe } from 'fp-ts/function'
import type { Orcid } from 'orcid-id-ts'
import { match } from 'ts-pattern'

export const getNameFromOrcid = (orcid: Orcid) =>
export const getNameFromOrcid = (orcid: Orcid): RTE.ReaderTaskEither<F.FetchEnv, 'not-found' | 'unavailable', string> =>
match(orcid)
.with('0000-0002-6109-0367' as Orcid, () => TE.of('Daniela Saderi'))
.otherwise(() => TE.left('not-found' as const))
.with('0000-0002-6109-0367' as Orcid, () =>
pipe(
'https://pub.orcid.org/v3.0/0000-0002-6109-0367/personal-details',
F.Request('GET'),
F.send,
RTE.bimap(
() => 'unavailable' as const,
() => 'Daniela Saderi',
),
),
)
.otherwise(() => RTE.left('not-found' as const))
24 changes: 19 additions & 5 deletions test/orcid.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { test } from '@fast-check/jest'
import { describe, expect } from '@jest/globals'
import fetchMock from 'fetch-mock'
import * as E from 'fp-ts/Either'
import type { Orcid } from 'orcid-id-ts'
import * as _ from '../src/orcid'
import * as fc from './fc'

describe('getNameFromOrcid', () => {
test('when the ORCID iD is 0000-0002-6109-0367', async () => {
const actual = await _.getNameFromOrcid('0000-0002-6109-0367' as Orcid)()
describe('when the ORCID iD is 0000-0002-6109-0367', () => {
test('when the request succeeds', async () => {
const actual = await _.getNameFromOrcid('0000-0002-6109-0367' as Orcid)({
fetch: fetchMock.sandbox().get('*', 200),
})()

expect(actual).toStrictEqual(E.right('Daniela Saderi'))
expect(actual).toStrictEqual(E.right('Daniela Saderi'))
})
test('when the request fails', async () => {
const actual = await _.getNameFromOrcid('0000-0002-6109-0367' as Orcid)({
fetch: () => Promise.reject('network error'),
})()

expect(actual).toStrictEqual(E.left('unavailable'))
})
})

test.prop([fc.orcid().filter(orcid => orcid !== '0000-0002-6109-0367')])(
'when the ORCID iD is 0000-0002-6109-0367',
'when the ORCID iD is not 0000-0002-6109-0367',
async orcid => {
const actual = await _.getNameFromOrcid(orcid)()
const actual = await _.getNameFromOrcid(orcid)({
fetch: () => Promise.reject('should not be called'),
})()

expect(actual).toStrictEqual(E.left('not-found'))
},
Expand Down

0 comments on commit 40c7d3c

Please sign in to comment.