-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
protocol: add MockClient and refactor VCR cassette file reading (#85)
* Move `VcrClient` to `@apibara/protocol` and rename to `MockClient`. * Abstract cassette file reading to `loadCassette` function
- Loading branch information
Showing
8 changed files
with
96 additions
and
95 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from "./client"; | ||
export * from "./config"; | ||
export * from "./helper"; | ||
export * from "./record"; | ||
|
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
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,49 @@ | ||
import assert from "node:assert"; | ||
|
||
import type { Client, ClientCallOptions, StreamDataOptions } from "../client"; | ||
import type { StatusRequest, StatusResponse } from "../status"; | ||
import type { StreamDataRequest, StreamDataResponse } from "../stream"; | ||
|
||
export class MockClient<TFilter, TBlock> implements Client<TFilter, TBlock> { | ||
constructor( | ||
private messages: StreamDataResponse<TBlock>[], | ||
private filter: TFilter[], | ||
) {} | ||
|
||
async status( | ||
request?: StatusRequest, | ||
options?: ClientCallOptions, | ||
): Promise<StatusResponse> { | ||
throw new Error("Client.status is not implemented for VcrClient"); | ||
} | ||
|
||
streamData(request: StreamDataRequest<TFilter>, options?: StreamDataOptions) { | ||
assert.deepStrictEqual( | ||
this.filter, | ||
request.filter, | ||
"Request and Cassette filter mismatch", | ||
); | ||
|
||
return new StreamDataIterable(this.messages); | ||
} | ||
} | ||
|
||
export class StreamDataIterable<TBlock> { | ||
constructor(private messages: StreamDataResponse<TBlock>[]) {} | ||
|
||
[Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>> { | ||
let index = 0; | ||
const messages = this.messages; | ||
|
||
return { | ||
async next() { | ||
if (index >= messages.length) { | ||
return { done: true, value: undefined }; | ||
} | ||
|
||
const message = messages[index++]; | ||
return { done: false, value: message }; | ||
}, | ||
}; | ||
} | ||
} |
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 @@ | ||
export * from "./client"; |