Skip to content

Commit c32b0e3

Browse files
authored
Merge pull request #19 from tainguyenbui/feature/HASI-000_allow_searching_only_published_data
HASI-000 allow searching only published data
2 parents 2fb51fa + 889b3ae commit c32b0e3

6 files changed

Lines changed: 95 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ const response = await client.getDataverseInformation('myDataverseAlias')
2828

2929
`public async search(options: SearchOptions): Promise<AxiosResponse> {`
3030

31+
`public async searchOnlyPublished(options: SearchOptions): Promise<AxiosResponse> {`
32+
3133
`public async getFile(fileId: string): Promise<AxiosResponse> {`
3234

3335
`public async getFileMetadata(fileId: string, draftVersion: boolean = false): Promise<AxiosResponse> {`
3436

3537
`public async getLatestDatasetInformation(datasetId: string): Promise<AxiosResponse> {`
3638

39+
`public async getLatestPublishedDatasetVersion(datasetId: string): Promise<AxiosResponse> {`
40+
41+
`public async getDraftDatasetVersion(datasetId: string): Promise<AxiosResponse> {`
42+
3743
`public async getLatestDatasetInformationFromDOI(doi: string): Promise<AxiosResponse> {`
3844

3945
`public async getDatasetVersions(datasetId: string): Promise<AxiosResponse> {`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-dataverse",
3-
"version": "1.0.29",
3+
"version": "1.0.32",
44
"description": "A Dataverse module for JavaScript/TypeScript",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/@types/datasetVersionType.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum DatasetVersionType {
2+
LATEST_PUBLISHED = ':latest-published',
3+
LATEST = ':latest',
4+
DRAFT = ':draft'
5+
}

src/@types/dataverseHeaders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export interface DataverseHeaders {
2-
'X-Dataverse-key': string,
2+
'X-Dataverse-key'?: string,
33
'Content-Type'?: string
44
}

src/dataverseClient.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { BasicDatasetInformation } from './@types/basicDataset'
77
import { DatasetUtil } from './utils/datasetUtil'
88
import request from 'request-promise'
99
import { DatasetVersionUpgradeType } from './@types/datasetVersionUpgradeType'
10+
import { DatasetVersionType } from './@types/datasetVersionType'
1011

1112
export class DataverseClient {
1213
private readonly host: string
@@ -44,10 +45,14 @@ export class DataverseClient {
4445
})
4546
}
4647

47-
public async search(options: SearchOptions): Promise<AxiosResponse> {
48+
public async search(options: SearchOptions, headers: DataverseHeaders = this.getHeaders()): Promise<AxiosResponse> {
4849
const url = `${this.host}/api/search`
4950
const requestOptions: DataverseSearchOptions = this.mapSearchOptions(options)
50-
return this.getRequest(url, { params: requestOptions })
51+
return this.getRequest(url, { params: requestOptions, headers: headers })
52+
}
53+
54+
public async searchOnlyPublished(options: SearchOptions): Promise<AxiosResponse> {
55+
return this.search(options, {})
5156
}
5257

5358
public async getFile(fileId: string): Promise<AxiosResponse> {
@@ -68,6 +73,14 @@ export class DataverseClient {
6873
return this.getRequest(url)
6974
}
7075

76+
public async getLatestPublishedDatasetVersion(datasetId: string): Promise<AxiosResponse> {
77+
return this.getDatasetVersion(datasetId, DatasetVersionType.LATEST_PUBLISHED)
78+
}
79+
80+
public async getDraftDatasetVersion(datasetId: string): Promise<AxiosResponse> {
81+
return this.getDatasetVersion(datasetId, DatasetVersionType.DRAFT)
82+
}
83+
7184
public async getLatestDatasetInformationFromDOI(doi: string): Promise<AxiosResponse> {
7285
const url = `${this.host}/api/datasets/:persistentId?persistentId=doi:${doi}`
7386
return this.getRequest(url)

test/dataverseClient.spec.ts

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DataverseClient, DataverseSearchOptions, SearchType } from '../src/index'
1+
import { DataverseClient, DataverseSearchOptions, SearchOptions, SearchType } from '../src/index'
22
import { assert, createSandbox, SinonSandbox, SinonStub } from 'sinon'
33
import { expect } from 'chai'
44
import axios from 'axios'
@@ -338,6 +338,31 @@ describe('DataverseClient', () => {
338338
})
339339
})
340340

341+
describe('searchOnlyPublished', () => {
342+
let searchStub: SinonStub
343+
344+
beforeEach(() => {
345+
searchStub = sandbox
346+
.stub(DataverseClient.prototype, 'search')
347+
})
348+
349+
it('should call search with expected options', async () => {
350+
const mockQuery = random.word()
351+
const type: SearchType = SearchType.DATASET
352+
const mockDataverseAlias = random.word()
353+
const expectedOptions: SearchOptions = {
354+
query: mockQuery,
355+
dataverseAlias: mockDataverseAlias,
356+
type
357+
}
358+
359+
await client.searchOnlyPublished({ query: mockQuery, type, dataverseAlias: mockDataverseAlias })
360+
361+
assert.calledOnce(searchStub)
362+
assert.calledWithExactly(searchStub, expectedOptions, {})
363+
})
364+
})
365+
341366
describe('search', () => {
342367
it('should call axios with expected url and options', async () => {
343368
const query = random.word()
@@ -357,7 +382,10 @@ describe('DataverseClient', () => {
357382

358383
assert.calledOnce(axiosGetStub)
359384
assert.calledWithExactly(axiosGetStub, `${host}/api/search`, {
360-
params: expectedOptions
385+
params: expectedOptions,
386+
headers: {
387+
'X-Dataverse-key': apiToken
388+
}
361389
})
362390
})
363391

@@ -614,6 +642,43 @@ describe('DataverseClient', () => {
614642
})
615643
})
616644

645+
describe('getLatestPublishedDatasetVersion', () => {
646+
let mockDatasetId: string
647+
648+
let getDatasetVersionStub: SinonStub
649+
650+
beforeEach(() => {
651+
mockDatasetId = random.number().toString()
652+
getDatasetVersionStub = sandbox.stub(DataverseClient.prototype, 'getDatasetVersion')
653+
})
654+
655+
it('should call get dataset version with expected input', async () => {
656+
await client.getLatestPublishedDatasetVersion(mockDatasetId)
657+
658+
assert.calledOnce(getDatasetVersionStub)
659+
assert.calledWithExactly(getDatasetVersionStub, mockDatasetId, ':latest-published')
660+
})
661+
})
662+
663+
describe('getDraftDatasetVersion', () => {
664+
let mockDatasetId: string
665+
666+
let getDatasetVersionStub: SinonStub
667+
668+
beforeEach(() => {
669+
mockDatasetId = random.number().toString()
670+
671+
getDatasetVersionStub = sandbox.stub(DataverseClient.prototype, 'getDatasetVersion')
672+
})
673+
674+
it('should call get dataset version with expected input', async () => {
675+
await client.getDraftDatasetVersion(mockDatasetId)
676+
677+
assert.calledOnce(getDatasetVersionStub)
678+
assert.calledWithExactly(getDatasetVersionStub, mockDatasetId, ':draft')
679+
})
680+
})
681+
617682
describe('getLatestDatasetInformationFromDOI', () => {
618683
let doi: string
619684

0 commit comments

Comments
 (0)