diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f697ff..1342d90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Added - **Tracked-change types in the package entry** - `TrackedChange`, `ProjectReview`, `ListTrackedChangesOptions`, the acceptance and rejection plan types, `TrackedChangeKind`, `TrackedChangeAuthor`, `ShareJsTextOp` and `RejectConflictError` are exported from `@aloth/olcli`, so code that uses `listReview`, `listTrackedChanges` or `rejectTrackedChanges` as a library can name what they return and catch the conflict error +- **Accept tracked changes of one doc with a single request** - `acceptTrackedChangesInDoc(projectId, docId, ids)` sends just the editor's accept request. `acceptTrackedChanges` reads the whole project before and after to resolve the ids and check the result, which is right for the CLI but costs two passes over every doc; an editor integration that already holds the changes and re-reads the project after every write can skip both. `acceptTrackedChanges` now uses it for each doc, so the request is built in one place ## [0.16.2] - 2026-11-11 diff --git a/src/client.ts b/src/client.ts index eb29471..530997d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -2601,16 +2601,7 @@ export class OverleafClient { const byDoc = new Map(); for (const change of selected) byDoc.set(change.docId, [...(byDoc.get(change.docId) ?? []), change]); for (const [docId, changes] of byDoc) { - const response = await this.httpRequest(`${this.baseUrl}/project/${projectId}/doc/${docId}/changes/accept`, { - method: 'POST', - headers: this.getHeaders(true), - body: JSON.stringify({ change_ids: changes.map(change => change.id) }), - expect: 'text' - }); - if (!response.ok) { - throw httpError(`Failed to accept tracked changes in ${changes[0].path}`, response.status); - } - this.applySetCookieHeaders(response.headers['set-cookie'] as string[] | undefined); + await this.acceptTrackedChangesInDoc(projectId, docId, changes.map(change => change.id), changes[0].path); } const after = await this.listTrackedChanges(projectId); @@ -2621,6 +2612,25 @@ export class OverleafClient { return selected; } + /** + * Accept tracked changes of one doc with a single request, the editor's + * route, without listing the project before or after. For callers that + * already hold the changes and their doc id and re-read the project anyway; + * `acceptTrackedChanges` resolves ids and checks the result for you. + */ + async acceptTrackedChangesInDoc(projectId: string, docId: string, ids: string[], label = docId): Promise { + const response = await this.httpRequest(`${this.baseUrl}/project/${projectId}/doc/${docId}/changes/accept`, { + method: 'POST', + headers: this.getHeaders(true), + body: JSON.stringify({ change_ids: ids }), + expect: 'text' + }); + if (!response.ok) { + throw httpError(`Failed to accept tracked changes in ${label}`, response.status); + } + this.applySetCookieHeaders(response.headers['set-cookie'] as string[] | undefined); + } + private selectTrackedChanges(all: TrackedChange[], ids: string[]): TrackedChange[] { const wanted = [...new Set(ids)]; if (wanted.length === 0) throw new Error('No tracked change id given'); diff --git a/test/tracked-changes.test.ts b/test/tracked-changes.test.ts index 6bbe885..e05ec30 100644 --- a/test/tracked-changes.test.ts +++ b/test/tracked-changes.test.ts @@ -151,6 +151,15 @@ test('acceptTrackedChanges posts the ids per doc and verifies they are gone', as assert.deepEqual(state.posts[0].body.change_ids.sort(), ['d', 'i']); }); +test('acceptTrackedChangesInDoc posts once and reads nothing', async () => { + const { client, state } = reviewClient('foo quux baz', replaced); + let listed = 0; + client.joinDocument = async () => { listed++; throw new Error('must not list'); }; + await client.acceptTrackedChangesInDoc('p', 'd1', ['i']); + assert.equal(listed, 0); + assert.deepEqual(state.posts.map((x: any) => [x.url.replace(/^.*\/project/, '/project'), x.body]), [['/project/p/doc/d1/changes/accept', { change_ids: ['i'] }]]); +}); + test('acceptTrackedChanges sends nothing when an id is unknown', async () => { const { client, state } = reviewClient('foo quux baz', replaced); await assert.rejects(client.acceptTrackedChanges('p', ['i', 'nope']), /not found: nope/);