Skip to content

Commit

Permalink
feat: add glob to webhook definition
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp committed Jan 22, 2024
1 parent bb1fd11 commit 15920b8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/providers/json-webhook-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class JsonWebhookProvider implements WebhookProvider {
}

export function findEndpointsMatchingFilter(filter: OrderFilter, definition: WebhookDefinition): Webhook[] {
let endpoints: Webhook[] = []
let endpoints: Webhook[] = definition['*'] ?? []

const filterKeys = Object.keys(filter) as FILTER_FIELD[]
const filterMapping = definition.filter
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export enum FILTER_FIELD {

export type WebhookDefinition = {
filter: WebhookFilterMapping
registeredWebhook: { [key: string]: string }
'*'?: Webhook[]
}

export type Webhook = {
Expand Down
90 changes: 58 additions & 32 deletions test/providers/s3-webhook-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { S3Client } from '@aws-sdk/client-s3';
import { WebhookDefinition } from '../../lib/providers/types'
import { S3Client } from '@aws-sdk/client-s3'
import { S3WebhookConfigurationProvider } from '../../lib/providers/s3-webhook-provider'
import { WebhookDefinition } from '../../lib/providers/types'

describe('S3WebhookProvider test', () => {
const bucket = 'test-bucket';
const key = 'test-key';
const bucket = 'test-bucket'
const key = 'test-key'

function applyMock(endpoints: WebhookDefinition) {
jest.spyOn(S3Client.prototype, 'send').mockImplementationOnce(() =>
Expand All @@ -13,10 +13,9 @@ describe('S3WebhookProvider test', () => {
transformToString: () => Promise.resolve(JSON.stringify(endpoints)),
},
})
);
)
}


const mockEndpoints = {
filter: {
filler: {
Expand All @@ -25,7 +24,8 @@ describe('S3WebhookProvider test', () => {
orderStatus: { open: [{ url: 'webhook.com/2' }, { url: 'webhook.com/1' }] },
offerer: { '0x2': [{ url: 'webhook.com/4' }] },
},
registeredWebhook: {}
['*']: [{ url: 'webhook.com/0' }],
registeredWebhook: {},
}

const mockEndpoints2 = {
Expand All @@ -36,62 +36,88 @@ describe('S3WebhookProvider test', () => {
orderStatus: { open: [{ url: 'webhook2.com/2' }, { url: 'webhook2.com/1' }] },
offerer: { '0x2': [{ url: 'webhook2.com/4' }] },
},
registeredWebhook: {}
['*']: [{ url: 'webhook.com/0' }],
registeredWebhook: {},
}

it('Fetches endpoints', async () => {
applyMock(mockEndpoints);
const provider = new S3WebhookConfigurationProvider(bucket, key);
applyMock(mockEndpoints)
const provider = new S3WebhookConfigurationProvider(bucket, key)
const endpoints = await provider.getEndpoints({
filler: '0x1',
orderStatus: 'open',
offerer: '0x2',
} as any)
expect(endpoints).toEqual([{ url: 'webhook.com/1' }, { url: 'webhook.com/2' }, { url: 'webhook.com/4' }])
});
})
expect(endpoints).toEqual([
{ url: 'webhook.com/0' },
{ url: 'webhook.com/1' },
{ url: 'webhook.com/2' },
{ url: 'webhook.com/4' },
])
})

it('Caches fetched endpoints', async () => {
applyMock(mockEndpoints);
const provider = new S3WebhookConfigurationProvider(bucket, key);
let endpoints = await provider.getEndpoints({
applyMock(mockEndpoints)
const provider = new S3WebhookConfigurationProvider(bucket, key)
const endpoints = await provider.getEndpoints({
filler: '0x1',
orderStatus: 'open',
offerer: '0x2',
} as any)
expect(endpoints).toEqual([{ url: 'webhook.com/1' }, { url: 'webhook.com/2' }, { url: 'webhook.com/4' }])
});
})
expect(endpoints).toEqual([
{ url: 'webhook.com/0' },
{ url: 'webhook.com/1' },
{ url: 'webhook.com/2' },
{ url: 'webhook.com/4' },
])
})

it('Refetches after cache expires', async () => {
applyMock(mockEndpoints);
const provider = new S3WebhookConfigurationProvider(bucket, key);
applyMock(mockEndpoints)
const provider = new S3WebhookConfigurationProvider(bucket, key)
let endpoints = await provider.getEndpoints({
filler: '0x1',
orderStatus: 'open',
offerer: '0x2',
} as any)
expect(endpoints).toEqual([{ url: 'webhook.com/1' }, { url: 'webhook.com/2' }, { url: 'webhook.com/4' }])
})
expect(endpoints).toEqual([
{ url: 'webhook.com/0' },
{ url: 'webhook.com/1' },
{ url: 'webhook.com/2' },
{ url: 'webhook.com/4' },
])

// update mock endpoints and skip a small bit of time forward
// should still use the old ones
applyMock(mockEndpoints2);
jest.useFakeTimers().setSystemTime(Date.now() + 100);
applyMock(mockEndpoints2)
jest.useFakeTimers().setSystemTime(Date.now() + 100)
endpoints = await provider.getEndpoints({
filler: '0x1',
orderStatus: 'open',
offerer: '0x2',
} as any)
})
// should still equal old ones
expect(endpoints).toEqual([{ url: 'webhook.com/1' }, { url: 'webhook.com/2' }, { url: 'webhook.com/4' }])
expect(endpoints).toEqual([
{ url: 'webhook.com/0' },
{ url: 'webhook.com/1' },
{ url: 'webhook.com/2' },
{ url: 'webhook.com/4' },
])

// skip farther forward
applyMock(mockEndpoints2);
jest.useFakeTimers().setSystemTime(Date.now() + 1000000);
applyMock(mockEndpoints2)
jest.useFakeTimers().setSystemTime(Date.now() + 1000000)
endpoints = await provider.getEndpoints({
filler: '0x1',
orderStatus: 'open',
offerer: '0x2',
} as any)
})
// should still equal old ones
expect(endpoints).toEqual([{ url: 'webhook2.com/1' }, { url: 'webhook2.com/2' }, { url: 'webhook2.com/4' }])
});
expect(endpoints).toEqual([
{ url: 'webhook.com/0' },
{ url: 'webhook2.com/1' },
{ url: 'webhook2.com/2' },
{ url: 'webhook2.com/4' },
])
})
})

0 comments on commit 15920b8

Please sign in to comment.