diff --git a/integration/finding-a-profile.spec.ts b/integration/finding-a-profile.spec.ts index 81a47f14c..952f134e1 100644 --- a/integration/finding-a-profile.spec.ts +++ b/integration/finding-a-profile.spec.ts @@ -1,8 +1,9 @@ -import { Status } from 'hyper-ts' import { expect, test } from './base' test('can find and view a profile', async ({ fetch, javaScriptEnabled, page }) => { - fetch.getOnce('https://pub.orcid.org/v3.0/0000-0002-6109-0367/personal-details', Status.OK) + fetch.getOnce('https://pub.orcid.org/v3.0/0000-0002-6109-0367/personal-details', { + body: { name: { 'given-names': { value: 'Daniela' }, 'family-name': { value: 'Saderi' } } }, + }) await page.goto('/profiles/0000-0002-6109-0367') diff --git a/src/orcid.ts b/src/orcid.ts index 908ab57fa..fa48db43d 100644 --- a/src/orcid.ts +++ b/src/orcid.ts @@ -1,9 +1,32 @@ import * as F from 'fetch-fp-ts' +import * as E from 'fp-ts/Either' +import * as J from 'fp-ts/Json' import * as RTE from 'fp-ts/ReaderTaskEither' import { identity, pipe } from 'fp-ts/function' import { Status } from 'hyper-ts' +import * as D from 'io-ts/Decoder' import type { Orcid } from 'orcid-id-ts' import { match } from 'ts-pattern' +import { NonEmptyStringC } from './string' + +const JsonD = { + decode: (s: string) => + pipe( + J.parse(s), + E.mapLeft(() => D.error(s, 'JSON')), + ), +} + +const PersonalDetailsD = D.struct({ + name: D.struct({ + 'given-names': D.struct({ + value: NonEmptyStringC, + }), + 'family-name': D.struct({ + value: NonEmptyStringC, + }), + }), +}) export const getNameFromOrcid = (orcid: Orcid): RTE.ReaderTaskEither => match(orcid) @@ -11,11 +34,16 @@ export const getNameFromOrcid = (orcid: Orcid): RTE.ReaderTaskEither 'unavailable' as const, - () => 'Daniela Saderi', + personalDetails => + `${personalDetails.name['given-names'].value} ${personalDetails.name['family-name'].value}`, ), ), ) diff --git a/test/orcid.test.ts b/test/orcid.test.ts index 73adae907..c915ae4c4 100644 --- a/test/orcid.test.ts +++ b/test/orcid.test.ts @@ -11,7 +11,9 @@ describe('getNameFromOrcid', () => { 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), + fetch: fetchMock.sandbox().get('*', { + body: { name: { 'given-names': { value: 'Daniela' }, 'family-name': { value: 'Saderi' } } }, + }), })() expect(actual).toStrictEqual(E.right('Daniela Saderi'))