-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@moralisweb3/common-streams-utils': minor | ||
'@moralisweb3/streams': minor | ||
--- | ||
|
||
Implement getLogs endpoint at `Streams.getLogs` |
27 changes: 27 additions & 0 deletions
27
packages/common/streamsUtils/src/operations/history/getLogsOperation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import MoralisCore from '@moralisweb3/common-core'; | ||
import { getLogsOperation, GetLogsRequest } from './getLogsOperation'; | ||
|
||
describe('getLogsOperation', () => { | ||
let core: MoralisCore; | ||
|
||
beforeAll(() => { | ||
core = MoralisCore.create(); | ||
}); | ||
|
||
it('serializeRequest() serializes correctly and deserializeRequest() deserializes correctly', () => { | ||
const request: Required<GetLogsRequest> = { | ||
limit: 100, | ||
cursor: 'CURSOR1', | ||
}; | ||
|
||
const serializedRequest = getLogsOperation.serializeRequest(request, core); | ||
|
||
expect(serializedRequest.limit).toBe(request.limit); | ||
expect(serializedRequest.cursor).toBe(request.cursor); | ||
|
||
const deserializedRequest = getLogsOperation.deserializeRequest(serializedRequest, core); | ||
|
||
expect(deserializedRequest.limit).toBe(request.limit); | ||
expect(deserializedRequest.cursor).toBe(request.cursor); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
packages/common/streamsUtils/src/operations/history/getLogsOperation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Camelize, maybe, PaginatedOperation } from '@moralisweb3/common-core'; | ||
import { EvmChain } from '@moralisweb3/common-evm-utils'; | ||
import { operations } from '../openapi'; | ||
|
||
type OperationId = 'GetLogs'; | ||
|
||
type QueryParams = operations[OperationId]['parameters']['query']; | ||
type RequestParams = QueryParams; | ||
|
||
type SuccessResponse = operations[OperationId]['responses']['200']['content']['application/json']; | ||
|
||
// Exports | ||
|
||
export interface GetLogsRequest extends Camelize<RequestParams> {} | ||
|
||
export type GetLogsJSONRequest = ReturnType<typeof serializeRequest>; | ||
|
||
export type GetLogsJSONResponse = SuccessResponse; | ||
|
||
export type GetLogsResponse = ReturnType<typeof deserializeResponse>; | ||
|
||
export const getLogsOperation: PaginatedOperation< | ||
GetLogsRequest, | ||
GetLogsJSONRequest, | ||
GetLogsResponse, | ||
GetLogsJSONResponse['result'] | ||
> = { | ||
method: 'GET', | ||
name: 'getLogs', | ||
id: 'GetLogs', | ||
groupName: 'history', | ||
urlPathPattern: '/history/logs', | ||
urlSearchParamNames: ['limit', 'cursor'], | ||
firstPageIndex: 0, | ||
|
||
getRequestUrlParams, | ||
deserializeResponse, | ||
serializeRequest, | ||
deserializeRequest, | ||
}; | ||
|
||
// Methods | ||
|
||
function getRequestUrlParams(request: GetLogsRequest) { | ||
return { | ||
limit: maybe(request.limit, String), | ||
cursor: request.cursor, | ||
}; | ||
} | ||
|
||
function deserializeResponse(jsonResponse: GetLogsJSONResponse) { | ||
return (jsonResponse.result ?? []).map((result) => ({ | ||
...result, | ||
chain: EvmChain.create(result.chain), | ||
})); | ||
} | ||
|
||
function serializeRequest(request: GetLogsRequest) { | ||
return request; | ||
} | ||
|
||
function deserializeRequest(jsonRequest: GetLogsJSONRequest) { | ||
return jsonRequest; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './getHistoryOperation'; | ||
export * from './replayHistoryOperation'; | ||
export * from './getLogsOperation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { MockScenarios } from '@moralisweb3/test-utils'; | ||
import { createPaginatedWebhookLogResponse } from '../response/webhookLogResponse'; | ||
|
||
export const mockGetLogs = MockScenarios.create( | ||
{ | ||
method: 'get', | ||
name: 'mockGetLogs', | ||
url: `/history/logs`, | ||
getParams: ({ req }) => ({ | ||
limit: req.url.searchParams.get('limit'), | ||
cursor: req.url.searchParams.get('cursor'), | ||
}), | ||
}, | ||
[ | ||
{ | ||
condition: { | ||
limit: '20', | ||
}, | ||
response: createPaginatedWebhookLogResponse(Array(20).fill('logs-test-id-1'), 20), | ||
}, | ||
{ | ||
condition: { | ||
limit: '5', | ||
}, | ||
response: createPaginatedWebhookLogResponse(Array(5).fill('logs-test-id-2'), 20, 'cursor'), | ||
}, | ||
{ | ||
condition: { | ||
limit: '5', | ||
cursor: 'cursor', | ||
}, | ||
response: createPaginatedWebhookLogResponse(Array(5).fill('logs-test-id-3'), 20, 'cursor-2'), | ||
}, | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/streams/integration/mocks/response/webhookLogResponse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const defaultWebhookLogResponse = { | ||
streamId: 'aa1d4d4f-549a-4235-a97d-367e7909f657', | ||
chain: '0x1', | ||
webhookUrl: 'https://webhook.site/9b9a8773-2129-431d-983a-f32c89854ee7', | ||
tag: 'demo', | ||
retries: 0, | ||
deliveryStatus: 'failed', | ||
blockNumber: 16866964, | ||
errorMessage: 'Request failed with status code 500', | ||
type: 'evm', | ||
createdAt: '2023-03-20T06:03:27.060Z', | ||
updatedAt: '2023-03-20T06:03:27.060Z', | ||
id: 'e5578320-44a1-47c3-95d5-0ae6874157cf', | ||
}; | ||
|
||
export const createWebhookLogResponse = (id: string) => ({ | ||
...defaultWebhookLogResponse, | ||
id, | ||
}); | ||
|
||
export const createPaginatedWebhookLogResponse = (ids: string[], total: number, cursor?: string) => ({ | ||
result: ids.map(createWebhookLogResponse), | ||
total, | ||
cursor, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Streams } from '../../src/Streams'; | ||
import { cleanStreamsApi, setupStreamApi } from '../setup'; | ||
|
||
describe('getLogs', () => { | ||
let StreamApi: Streams; | ||
|
||
beforeAll(() => { | ||
StreamApi = setupStreamApi(); | ||
}); | ||
|
||
afterAll(() => { | ||
cleanStreamsApi(); | ||
}); | ||
|
||
it('should get all logs', async () => { | ||
const result = await StreamApi.getLogs({ | ||
limit: 20, | ||
}); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result.result).toBeDefined(); | ||
expect(result.pagination.total).toEqual(20); | ||
expect(result.result.length).toEqual(20); | ||
expect(result.result[0].id).toEqual('logs-test-id-1'); | ||
}); | ||
|
||
it('should get a cursor', async () => { | ||
const result = await StreamApi.getLogs({ | ||
limit: 5, | ||
}); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result.result).toBeDefined(); | ||
expect(result.pagination.total).toEqual(20); | ||
expect(result.result.length).toEqual(5); | ||
expect(result.result[0].id).toEqual('logs-test-id-2'); | ||
expect(result.pagination.cursor).toEqual('cursor'); | ||
expect(result.next).toBeDefined(); | ||
expect(result.hasNext).toBeTruthy(); | ||
}); | ||
|
||
it('should use a provided cursor', async () => { | ||
const result = await StreamApi.getLogs({ | ||
limit: 5, | ||
cursor: 'cursor', | ||
}); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result.result).toBeDefined(); | ||
expect(result.pagination.total).toEqual(20); | ||
expect(result.result.length).toEqual(5); | ||
expect(result.result[0].id).toEqual('logs-test-id-3'); | ||
expect(result.pagination.cursor).toEqual('cursor-2'); | ||
expect(result.next).toBeDefined(); | ||
expect(result.hasNext).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters