Skip to content

Commit

Permalink
Change Next-Fetch-Date from header to part of body
Browse files Browse the repository at this point in the history
  • Loading branch information
raymond-h committed Sep 7, 2024
1 parent 4c88483 commit e87a24e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 47 deletions.
12 changes: 5 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -244,7 +245,7 @@ const commandGroupFetchBodySchema = {
}
}
},
required: ['commands']
required: ['commands', 'nextFetchDate']
};

export async function fetchAndAddCommandsFromGroup({ get, store }, groupId) {
Expand All @@ -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,
Expand Down
36 changes: 4 additions & 32 deletions test/fetch-and-add-commands-from-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down Expand Up @@ -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'
}
});
Expand All @@ -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`);
});
29 changes: 21 additions & 8 deletions test/snapshots/fetch-and-add-commands-from-group.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand All @@ -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',
}
Binary file modified test/snapshots/fetch-and-add-commands-from-group.js.snap
Binary file not shown.

0 comments on commit e87a24e

Please sign in to comment.