From e87a24ecbcc465266b22148f342bf213ee14a59f Mon Sep 17 00:00:00 2001 From: Raymond Hammarling Date: Sat, 7 Sep 2024 05:46:27 +0200 Subject: [PATCH] Change Next-Fetch-Date from header to part of body --- lib/index.js | 12 +++--- test/fetch-and-add-commands-from-group.js | 36 ++---------------- .../fetch-and-add-commands-from-group.js.md | 29 ++++++++++---- .../fetch-and-add-commands-from-group.js.snap | Bin 755 -> 862 bytes 4 files changed, 30 insertions(+), 47 deletions(-) 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 aff568002edc7d5d987df619b9d2da0cec4865f2..e2a2505ca3afdba98eae18eba43b6cf144bc1987 100644 GIT binary patch literal 862 zcmV-k1EKsuRzVzdgndBnV8&x? z$ClmS5+92Q00000000A}Rl9D~KomXK?>u*-1SB4X6$zvufv9*ih)5(9An_E4a7Z()Vo~}$O-1U9#E)5XMx-$h{j*b7{glg=NFYCkePuzzYK267Z9N0i%OmyT+PP$W$bfsf;!mBjjATImZK4uh(;|IRj`Jz#{{A zV*o!4V9W##nZOkjm^Zh=va47_AP-c-C>dj9B3t%YI=c|_L=}O)kSTsxMKRYiqeLo} z1iT2?i()a-v)q_V$!5DrheE-np9tj#BFVh)mGD)A`{Ug{<9-%rN<*>|7I$HsB4wE9 zwFz{ST1+)`qODZ4c&e6*!0v+ik%TX)X|96C8K!s+_N4^o$e_Vvroq`#v0%RVH@P{M zzmsil#|AGDam;pR3z(6!#_vXQp>IN5t1s6lG#lG zt37)Tcuk6!UuH=t;wx%lW~SRK5Tcobo4N7N?RDe(I~GQ~eVjj+)=HagV5gyVB8&5spg`PINL1EhN*ls8BFYe{~f)xn>0El^*@Bjb+ literal 755 zcmVRLg1f=t9z#NoD5MAbkPQMWmK|J@Bx8DT`5E(K30|P znoOJNu1QrzlZAq!polK~gJhwb?A?n?L0q}=CkzPgWV)v((=|yHdsThwKF&R-?%ixG zwPd)WpKS>fo0eEVX_`tt(t%J)sz9be!dnfg5)lPg=Whi{=(bF?h;_=_x+#r*x*0{> z@VnjzhAKKA`Q)4eFauzcDs}Q^mEs#j#i>@Sfrdc@8X>w^1P#3e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