diff --git a/lib/index.js b/lib/index.js index 78c8998..a4fb23a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -232,6 +232,7 @@ export async function runUserCommand(post, client, store, msg, data) { const commandGroupFetchBodySchema = { type: 'object', properties: { + nextFetchDate: { type: 'string', format: 'date-time' }, commands: { type: 'array', items: { @@ -244,7 +245,7 @@ const commandGroupFetchBodySchema = { } } }, - required: ['commands'] + required: ['commands', 'nextFetchDate'] }; export async function fetchAndAddCommandsFromGroup({ get, store }, groupId) { @@ -254,14 +255,11 @@ export async function fetchAndAddCommandsFromGroup({ get, store }, groupId) { console.log(`Fetching commands from group ${groupId} with URL ${url}`); - const { headers, body } = await get(url); + const { body } = await get(url); - const { instance: { commands } } = validate(body, commandGroupFetchBodySchema, { required: true, throwAll: true }); + const { instance: { nextFetchDate: nextFetchDateStr, commands } } = validate(body, commandGroupFetchBodySchema, { required: true, throwAll: true }); - const nextFetchDate = new Date(headers.get('Next-Fetch-Date')); - if (Number.isNaN(+nextFetchDate)) { - throw new Error('Unable to parse date from header Next-Fetch-Date'); - } + const nextFetchDate = new Date(nextFetchDateStr); await store.set('command-groups', { ...groups, diff --git a/test/fetch-and-add-commands-from-group.js b/test/fetch-and-add-commands-from-group.js index f386284..defc0b0 100644 --- a/test/fetch-and-add-commands-from-group.js +++ b/test/fetch-and-add-commands-from-group.js @@ -14,10 +14,9 @@ test('works', async t => { const { get, store } = t.context; get.resolves({ - headers: new Headers({ - 'Next-Fetch-Date': '2024-05-09T01:02:03.000Z' - }), + headers: new Headers(), body: { + nextFetchDate: '2024-05-09T01:02:03.000Z', commands: [ { name: 'some-command', url: '/some-command' }, { name: 'another-command', url: '/another-command' }, @@ -52,10 +51,9 @@ test('throws error on malformed GET response', async t => { const { get, store } = t.context; get.resolves({ - headers: new Headers({ - 'Next-Fetch-Date': '2024-05-09T01:02:03.000Z' - }), + headers: new Headers(), body: { + nextFetchDate: 'wowie', someRandomStuff: 'cool' } }); @@ -71,29 +69,3 @@ test('throws error on malformed GET response', async t => { t.true(err.message.startsWith(`When running fetchAndAddCommandsFromGroup for group ${groupId}:`)); t.snapshot(err.cause.errors); }); - -test('throws error on malformed date in Next-Fetch-Date header', async t => { - const { get, store } = t.context; - - get.resolves({ - headers: new Headers({ - 'Next-Fetch-Date': 'something random' - }), - body: { - commands: [ - { name: 'some-command', url: '/some-command' }, - { name: 'another-command', url: '/another-command' }, - ] - } - }); - - await store.set('command-groups', { - [groupId]: { - url: 'https://example.com/.commands', - commands: [] - } - }); - - const err = await t.throwsAsync(() => fetchAndAddCommandsFromGroup({ get, store }, groupId)); - t.is(err.message, `When running fetchAndAddCommandsFromGroup for group ${groupId}: Unable to parse date from header Next-Fetch-Date`); -}); diff --git a/test/snapshots/fetch-and-add-commands-from-group.js.md b/test/snapshots/fetch-and-add-commands-from-group.js.md index f46eca7..a85d9ea 100644 --- a/test/snapshots/fetch-and-add-commands-from-group.js.md +++ b/test/snapshots/fetch-and-add-commands-from-group.js.md @@ -9,9 +9,25 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 [ + ValidationError { + argument: 'date-time', + instance: 'wowie', + message: 'does not conform to the "date-time" format', + name: 'format', + path: [ + 'nextFetchDate', + ], + property: 'instance.nextFetchDate', + schema: { + format: 'date-time', + type: 'string', + }, + stack: 'instance.nextFetchDate does not conform to the "date-time" format', + }, ValidationError { argument: 'commands', instance: { + nextFetchDate: 'wowie', someRandomStuff: 'cool', }, message: 'requires property "commands"', @@ -38,20 +54,17 @@ Generated by [AVA](https://avajs.dev). }, type: 'array', }, + nextFetchDate: { + format: 'date-time', + type: 'string', + }, }, required: [ 'commands', + 'nextFetchDate', ], type: 'object', }, stack: 'instance requires property "commands"', }, ] - -## throws error on malformed date in Next-Fetch-Date header - -> Snapshot 1 - - Error { - message: 'Unable to parse date from header Next-Fetch-Date', - } diff --git a/test/snapshots/fetch-and-add-commands-from-group.js.snap b/test/snapshots/fetch-and-add-commands-from-group.js.snap index aff5680..e2a2505 100644 Binary files a/test/snapshots/fetch-and-add-commands-from-group.js.snap and b/test/snapshots/fetch-and-add-commands-from-group.js.snap differ