-
Notifications
You must be signed in to change notification settings - Fork 12
/
oai-pmh-list.js
52 lines (45 loc) · 1.29 KB
/
oai-pmh-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { get } from 'lodash'
import { parseOaiPmhXml } from './oai-pmh-xml'
function getResumptionToken (result, listSize) {
const token = result.resumptionToken
if (!token) return undefined
if (typeof token === 'string') return token
const cursor = get(token, '$.cursor')
const completeListSize = get(token, '$.completeListSize')
if (
cursor &&
completeListSize &&
parseInt(cursor, 10) + listSize >= parseInt(completeListSize, 10)
) return undefined
return token._
}
export async function * getOaiListItems (oaiPmh, verb, field, options) {
const initialResponse = await oaiPmh.request({
url: oaiPmh.baseUrl,
qs: {
...options,
verb
}
})
const initialParsedResponse = await parseOaiPmhXml(initialResponse.body)
const initialResult = initialParsedResponse[verb]
for (const item of initialResult[field]) {
yield item
}
let result = initialResult
let resumptionToken
while ((resumptionToken = getResumptionToken(result, result[field].length))) {
const response = await oaiPmh.request({
url: oaiPmh.baseUrl,
qs: {
verb,
resumptionToken
}
})
const parsedResponse = await parseOaiPmhXml(response.body)
result = parsedResponse[verb]
for (const item of result[field]) {
yield item
}
}
}