Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ http://demo.dataverse.org/api/datasets/389608/versions/1

`public async getMetricByCountry(datasetId: string, metricType: DataverseMetricType, countryCode?: string, yearMonth?: string) {`

`public async replaceFile(fileId: string, filename: string, fileBuffer: Buffer): Promise<any> {`
`public async replaceFile(fileId: string, filename: string, fileBuffer: Buffer, jsonData: object = {}): Promise<any> {`

`public async publishDataset(datasetId: string, versionUpgradeType: DatasetVersionUpgradeType = DatasetVersionUpgradeType.MAJOR): Promise<AxiosResponse> {`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-dataverse",
"version": "1.0.33",
"version": "1.0.34",
"description": "A Dataverse module for JavaScript/TypeScript",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
9 changes: 5 additions & 4 deletions src/dataverseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class DataverseClient {
return this.getRequest(url)
}

public async replaceFile(fileId: string, filename: string, fileBuffer: Buffer): Promise<any> {
public async replaceFile(fileId: string, filename: string, fileBuffer: Buffer, jsonData: object = {}): Promise<any> {
const url = `${this.host}/api/files/${fileId}/replace`

const options = {
Expand All @@ -155,14 +155,15 @@ export class DataverseClient {
value: fileBuffer,
options: {
filename: filename
}
}
},
},
jsonData: JSON.stringify(jsonData)
},
resolveWithFullResponse: true
}

return request.post(options).catch(error => {
throw new DataverseException(error.response.statusCode, error.response.body ? JSON.parse(error.response.body).message : '')
throw new DataverseException(error.response.statusCode, error.response.body ? error.response.body.message : '')
})
}

Expand Down
57 changes: 46 additions & 11 deletions test/dataverseClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ describe('DataverseClient', () => {
await client.searchOnlyPublished({ query: mockQuery, type, dataverseAlias: mockDataverseAlias })

assert.calledOnce(searchStub)
assert.calledWithExactly(searchStub, expectedOptions, {'X-Dataverse-key': ''})
assert.calledWithExactly(searchStub, expectedOptions, { 'X-Dataverse-key': '' })
})
})

Expand Down Expand Up @@ -1147,10 +1147,15 @@ describe('DataverseClient', () => {
const testFile = fs.readFileSync(path.resolve(__dirname, '../test/assets/test-file.csv'), 'base64')
const fileBuffer = Buffer.from(testFile, 'base64')

it('should call request with expected url', async () => {
const mockFileId: string = random.number().toString()
const mockFilename: string = system.fileName()
let mockFileId: string
let mockFilename: string

beforeEach(() => {
mockFileId = random.number().toString()
mockFilename = system.fileName()
})

it('should call request with expected input', async () => {
const expectedRequest = {
url: `${host}/api/files/${mockFileId}/replace`,
headers: { 'X-Dataverse-key': apiToken },
Expand All @@ -1160,7 +1165,8 @@ describe('DataverseClient', () => {
options: {
filename: mockFilename
}
}
},
jsonData: JSON.stringify({})
},
resolveWithFullResponse: true
}
Expand All @@ -1171,9 +1177,39 @@ describe('DataverseClient', () => {
assert.calledWithExactly(requestPostStub, expectedRequest)
})

describe('with json data', () => {
it('should call request with expected input', async () => {
const mockDescription: string = random.words()

const expectedRequest = {
url: `${host}/api/files/${mockFileId}/replace`,
headers: { 'X-Dataverse-key': apiToken },
formData: {
file: {
value: fileBuffer,
options: {
filename: mockFilename
}
},
jsonData: JSON.stringify({
forceReplace: true,
description: mockDescription
})
},
resolveWithFullResponse: true
}

await client.replaceFile(mockFileId, mockFilename, fileBuffer, {
forceReplace: true,
description: mockDescription
})

assert.calledOnce(requestPostStub)
assert.calledWithExactly(requestPostStub, expectedRequest)
})
})

it('should return expected response', async () => {
const mockFileId: string = random.number().toString()
const mockFilename: string = system.fileName()
const randomValue = random.word()
const expectedResponse = {
...mockResponse,
Expand All @@ -1189,7 +1225,8 @@ describe('DataverseClient', () => {
options: {
filename: mockFilename
}
}
},
jsonData: JSON.stringify({})
},
resolveWithFullResponse: true
}
Expand All @@ -1202,11 +1239,9 @@ describe('DataverseClient', () => {
})

it('should throw expected error', async () => {
const mockFileId: string = random.number().toString()
const mockFilename: string = system.fileName()
const errorMessage = random.words()
const errorCode = random.number()
requestPostStub.rejects({ response: { statusCode: errorCode, body: JSON.stringify({ message: errorMessage }) } })
requestPostStub.rejects({ response: { statusCode: errorCode, body: { message: errorMessage } } })

let error: DataverseException = undefined

Expand Down