diff --git a/src/handlers/delete.js b/src/handlers/delete.js index ba77e3a..e6b0b77 100644 --- a/src/handlers/delete.js +++ b/src/handlers/delete.js @@ -11,10 +11,10 @@ */ import { deleteSource } from '../routes/source.js'; -export default async function deleteHandler({ env, daCtx }) { +export default async function deleteHandler({ req, env, daCtx }) { const { path } = daCtx; - if (path.startsWith('/source')) return deleteSource({ env, daCtx }); + if (path.startsWith('/source')) return deleteSource({ req, env, daCtx }); return undefined; } diff --git a/src/index.js b/src/index.js index 0a4b225..6dc1671 100644 --- a/src/index.js +++ b/src/index.js @@ -42,7 +42,7 @@ export default { respObj = await postHandler({ req, env, daCtx }); break; case 'DELETE': - respObj = await deleteHandler({ env, daCtx }); + respObj = await deleteHandler({ req, env, daCtx }); break; default: respObj = unkownHandler(); diff --git a/src/routes/source.js b/src/routes/source.js index 6174ea3..b974a28 100644 --- a/src/routes/source.js +++ b/src/routes/source.js @@ -16,13 +16,8 @@ import deleteObject from '../storage/object/delete.js'; import putHelper from '../helpers/source.js'; import { postObjectVersion } from '../storage/version/put.js'; -export async function deleteSource({ env, daCtx }) { - await postObjectVersion(env, daCtx); - return deleteObject(env, daCtx); -} - -async function invalidateCollab(url, env) { - const invPath = `/api/v1/syncadmin?doc=${url}`; +async function invalidateCollab(api, url, env) { + const invPath = `/api/v1/${api}?doc=${url}`; if (env.dacollab) { // service binding is configured, hostname is not relevant console.log('Using service binding'); @@ -39,6 +34,19 @@ async function invalidateCollab(url, env) { } } +export async function deleteSource({ req, env, daCtx }) { + await postObjectVersion(env, daCtx); + const resp = await deleteObject(env, daCtx); + + if (resp.status === 204) { + const initiator = req.headers.get('x-da-initiator'); + if (initiator !== 'collab') { + await invalidateCollab('deleteadmin', req.url, env); + } + } + return resp; +} + export async function postSource({ req, env, daCtx }) { const obj = await putHelper(req, env, daCtx); const resp = await putObject(env, daCtx, obj); @@ -46,7 +54,7 @@ export async function postSource({ req, env, daCtx }) { if (resp.status === 201 || resp.status === 200) { const initiator = req.headers.get('x-da-initiator'); if (initiator !== 'collab') { - await invalidateCollab(req.url, env); + await invalidateCollab('syncadmin', req.url, env); } } return resp; diff --git a/src/storage/object/delete.js b/src/storage/object/delete.js index cbcfcae..bb0944f 100644 --- a/src/storage/object/delete.js +++ b/src/storage/object/delete.js @@ -71,5 +71,5 @@ export default async function deleteObjects(env, daCtx) { } } while (ContinuationToken); - return { body: '', status: 204 }; + return { body: null, status: 204 }; } diff --git a/test/routes/source.test.js b/test/routes/source.test.js index bb80e22..950a814 100644 --- a/test/routes/source.test.js +++ b/test/routes/source.test.js @@ -186,7 +186,15 @@ describe('Source Route', () => { }); it('Test deleteSource', async () => { - const env = {}; + const req = { + headers: new Map(), + url: 'http://somehost.com/somedoc.html' + }; + + const daCalled = [] + const dacollab = { fetch: (u) => daCalled.push(u) }; + + const env = { dacollab }; const daCtx = {}; const called = []; @@ -204,8 +212,10 @@ describe('Source Route', () => { } } ); - const resp = await deleteSource({env, daCtx}); + const resp = await deleteSource({req, env, daCtx}); assert.equal(204, resp.status); assert.deepStrictEqual(called, ['deleteObject']); + assert.deepStrictEqual(daCalled, + ['https://localhost/api/v1/deleteadmin?doc=http://somehost.com/somedoc.html']); }); }); \ No newline at end of file