Skip to content

Commit c1e2962

Browse files
authored
Merge pull request #21 from tainguyenbui/feature/HASI-000_allow_specifying_json_data
HASI-000 allow specifying json data
2 parents 082682d + 0fc1754 commit c1e2962

4 files changed

Lines changed: 53 additions & 17 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ http://demo.dataverse.org/api/datasets/389608/versions/1
5757

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

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

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

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.33",
3+
"version": "1.0.34",
44
"description": "A Dataverse module for JavaScript/TypeScript",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/dataverseClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class DataverseClient {
144144
return this.getRequest(url)
145145
}
146146

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

150150
const options = {
@@ -155,14 +155,15 @@ export class DataverseClient {
155155
value: fileBuffer,
156156
options: {
157157
filename: filename
158-
}
159-
}
158+
},
159+
},
160+
jsonData: JSON.stringify(jsonData)
160161
},
161162
resolveWithFullResponse: true
162163
}
163164

164165
return request.post(options).catch(error => {
165-
throw new DataverseException(error.response.statusCode, error.response.body ? JSON.parse(error.response.body).message : '')
166+
throw new DataverseException(error.response.statusCode, error.response.body ? error.response.body.message : '')
166167
})
167168
}
168169

test/dataverseClient.spec.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ describe('DataverseClient', () => {
359359
await client.searchOnlyPublished({ query: mockQuery, type, dataverseAlias: mockDataverseAlias })
360360

361361
assert.calledOnce(searchStub)
362-
assert.calledWithExactly(searchStub, expectedOptions, {'X-Dataverse-key': ''})
362+
assert.calledWithExactly(searchStub, expectedOptions, { 'X-Dataverse-key': '' })
363363
})
364364
})
365365

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

1150-
it('should call request with expected url', async () => {
1151-
const mockFileId: string = random.number().toString()
1152-
const mockFilename: string = system.fileName()
1150+
let mockFileId: string
1151+
let mockFilename: string
1152+
1153+
beforeEach(() => {
1154+
mockFileId = random.number().toString()
1155+
mockFilename = system.fileName()
1156+
})
11531157

1158+
it('should call request with expected input', async () => {
11541159
const expectedRequest = {
11551160
url: `${host}/api/files/${mockFileId}/replace`,
11561161
headers: { 'X-Dataverse-key': apiToken },
@@ -1160,7 +1165,8 @@ describe('DataverseClient', () => {
11601165
options: {
11611166
filename: mockFilename
11621167
}
1163-
}
1168+
},
1169+
jsonData: JSON.stringify({})
11641170
},
11651171
resolveWithFullResponse: true
11661172
}
@@ -1171,9 +1177,39 @@ describe('DataverseClient', () => {
11711177
assert.calledWithExactly(requestPostStub, expectedRequest)
11721178
})
11731179

1180+
describe('with json data', () => {
1181+
it('should call request with expected input', async () => {
1182+
const mockDescription: string = random.words()
1183+
1184+
const expectedRequest = {
1185+
url: `${host}/api/files/${mockFileId}/replace`,
1186+
headers: { 'X-Dataverse-key': apiToken },
1187+
formData: {
1188+
file: {
1189+
value: fileBuffer,
1190+
options: {
1191+
filename: mockFilename
1192+
}
1193+
},
1194+
jsonData: JSON.stringify({
1195+
forceReplace: true,
1196+
description: mockDescription
1197+
})
1198+
},
1199+
resolveWithFullResponse: true
1200+
}
1201+
1202+
await client.replaceFile(mockFileId, mockFilename, fileBuffer, {
1203+
forceReplace: true,
1204+
description: mockDescription
1205+
})
1206+
1207+
assert.calledOnce(requestPostStub)
1208+
assert.calledWithExactly(requestPostStub, expectedRequest)
1209+
})
1210+
})
1211+
11741212
it('should return expected response', async () => {
1175-
const mockFileId: string = random.number().toString()
1176-
const mockFilename: string = system.fileName()
11771213
const randomValue = random.word()
11781214
const expectedResponse = {
11791215
...mockResponse,
@@ -1189,7 +1225,8 @@ describe('DataverseClient', () => {
11891225
options: {
11901226
filename: mockFilename
11911227
}
1192-
}
1228+
},
1229+
jsonData: JSON.stringify({})
11931230
},
11941231
resolveWithFullResponse: true
11951232
}
@@ -1202,11 +1239,9 @@ describe('DataverseClient', () => {
12021239
})
12031240

12041241
it('should throw expected error', async () => {
1205-
const mockFileId: string = random.number().toString()
1206-
const mockFilename: string = system.fileName()
12071242
const errorMessage = random.words()
12081243
const errorCode = random.number()
1209-
requestPostStub.rejects({ response: { statusCode: errorCode, body: JSON.stringify({ message: errorMessage }) } })
1244+
requestPostStub.rejects({ response: { statusCode: errorCode, body: { message: errorMessage } } })
12101245

12111246
let error: DataverseException = undefined
12121247

0 commit comments

Comments
 (0)