Skip to content

Commit

Permalink
Ensure getLinks() and getMeta() is consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Jul 27, 2024
1 parent 09ad972 commit 785f9fe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ApiResponse {
* @return {?number} Version of the content in response
*/
getVersion() {
return parseIntHeaders(this.response?.headers, 'Last-Modified-Version');
return parseIntHeaders(this.response.headers, 'Last-Modified-Version');
}
}

Expand Down Expand Up @@ -146,15 +146,15 @@ module:zotero-api-client~MultiReadResponse#getData}
* @return {string} Total number of results
*/
getTotalResults() {
return parseIntHeaders(this.response?.headers, 'Total-Results');
return parseIntHeaders(this.response.headers, 'Total-Results');
}

/**
* @return {object} Parsed content of "Link" header as object where value of "rel" is a key and
the URL is the value, contains values for "next", "last" etc.
*/
getRelLinks() {
const links = this.response?.headers.get('link') ?? '';
const links = this.response.headers.get('link') ?? '';
const matches = Array.from(links.matchAll(/<(.*?)>;\s+rel="(.*?)"/ig));
return Array.from(matches).reduce((acc, [_match, url, rel]) => { // eslint-disable-line no-unused-vars
acc[rel] = url;
Expand Down Expand Up @@ -239,7 +239,7 @@ class MultiWriteResponse extends ApiResponse {
if("successful" in this.raw) {
const entry = this.raw.successful[index.toString()];
if(entry) {
return entry.links || {}
return entry.links || null;
}
}
return null;
Expand All @@ -254,7 +254,7 @@ class MultiWriteResponse extends ApiResponse {
if("successful" in this.raw) {
const entry = this.raw.successful[index.toString()];
if(entry) {
return entry.meta || {}
return entry.meta || null;
}
}
return null;
Expand Down Expand Up @@ -378,7 +378,7 @@ class FileUploadResponse extends ApiResponse {
// will be in obtained from the initial response
return parseIntHeaders(this.registerResponse?.headers, 'Last-Modified-Version') ??
parseIntHeaders(this.uploadResponse?.headers, 'Last-Modified-Version') ??
parseIntHeaders(this.response?.headers, 'Last-Modified-Version');
parseIntHeaders(this.response.headers, 'Last-Modified-Version');
}
}

Expand Down Expand Up @@ -477,7 +477,7 @@ class ErrorResponse extends Error {
* @return {?number} Version of the content in response
*/
getVersion() {
return parseIntHeaders(this.response?.headers, 'Last-Modified-Version');
return parseIntHeaders(this.response.headers, 'Last-Modified-Version');
}

/**
Expand Down
52 changes: 52 additions & 0 deletions test/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ describe('ZoteroJS request', () => {
}).catch(async error => {
assert.instanceOf(error, ErrorResponse);
assert.strictEqual(error.getResponseType(), 'ErrorResponse');
assert.isNull(error.getVersion());
assert.strictEqual(error.message, '404: Not Found');
assert.strictEqual(error.reason, 'These aren\'t the droids You are looking for');
assert.strictEqual(error.response.bodyUsed, false);
Expand Down Expand Up @@ -984,6 +985,8 @@ describe('ZoteroJS request', () => {
assert.strictEqual(response.getData()[0].title, 'My Amazing Book');
assert.strictEqual(response.getData()[0].itemType, 'book');
assert.strictEqual(response.getData()[0].version, 1337);
assert.deepEqual(response.getLinks()[0], null);
assert.deepEqual(response.getMeta()[0], null);
});
});

Expand Down Expand Up @@ -1049,6 +1052,55 @@ describe('ZoteroJS request', () => {
});
});

it('should accept response with new data but no meta or links', async () => {
const item = {
'version': 0,
'itemType': 'book',
'title': 'My Amazing Book'
};

fetchMock.post((url) => {
assert.isOk(url.startsWith('https://api.zotero.org/users/475425/items'));
return true;
}, {
headers: {
'Last-Modified-Version': 1337
},
body: {
...multiSuccessWriteResponseFixture,
successful: {
"0": {
data: {
...item,
version: 1337,
key: 'AZBCAADA',
dateAdded: "2018-07-05T09:24:36Z",
dateModified: "2018-07-05T09:24:36Z",
tags: [],
relations: {}
}
}
}
}
});

const response = await request({
method: 'post',
body: [item],
resource: {
library: 'u475425',
items: null
}
});
assert.instanceOf(response, MultiWriteResponse);
assert.strictEqual(response.getResponseType(), 'MultiWriteResponse');
assert.isOk(response.isSuccess());
assert.strictEqual(response.getData()[0].key, 'AZBCAADA');
assert.strictEqual(response.getData()[0].title, 'My Amazing Book');
assert.isNull(response.getMeta()[0]);
assert.isNull(response.getLinks()[0]);
});

it('should post multiple items and handle mixed response', () => {
const book = {
'key': 'ABCD1111',
Expand Down

0 comments on commit 785f9fe

Please sign in to comment.