Skip to content

Commit

Permalink
Read the name from the ORCID response
Browse files Browse the repository at this point in the history
This assumes the response has both a given and a family name and that
the given name should precede the family name.

Refs: #976
  • Loading branch information
erkannt committed May 31, 2023
1 parent 28c5bb0 commit 5b64ba4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 3 additions & 2 deletions integration/finding-a-profile.spec.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand Down
30 changes: 29 additions & 1 deletion src/orcid.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
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<F.FetchEnv, 'not-found' | 'unavailable', string> =>
match(orcid)
.with('0000-0002-6109-0367' as Orcid, () =>
pipe(
'https://pub.orcid.org/v3.0/0000-0002-6109-0367/personal-details',
F.Request('GET'),
F.setHeader('Accept', 'application/json'),
F.send,
RTE.filterOrElseW(F.hasStatus(Status.OK), identity),
RTE.chainTaskEitherK(F.getText(identity)),
RTE.chainEitherKW(JsonD.decode),
RTE.chainEitherKW(PersonalDetailsD.decode),
RTE.bimap(
() => 'unavailable' as const,
() => 'Daniela Saderi',
personalDetails =>
`${personalDetails.name['given-names'].value} ${personalDetails.name['family-name'].value}`,
),
),
)
Expand Down
4 changes: 3 additions & 1 deletion test/orcid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit 5b64ba4

Please sign in to comment.