This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
fromJson
and toJson
options to HTTP transport
- Loading branch information
1 parent
0571c4c
commit ea59901
Showing
5 changed files
with
82 additions
and
2 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,5 @@ | ||
--- | ||
'@solana/rpc-transport-http': patch | ||
--- | ||
|
||
Add `fromJson` and `toJson` options to the HTTP transport |
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
29 changes: 29 additions & 0 deletions
29
packages/rpc-transport-http/src/__tests__/http-transport-from-json-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,29 @@ | ||
import { RpcTransport } from '@solana/rpc-spec'; | ||
|
||
describe('createHttpTransport and `fromJson` function', () => { | ||
let fromJson: jest.Mock; | ||
let fetchSpy: jest.SpyInstance; | ||
let makeHttpRequest: RpcTransport; | ||
beforeEach(async () => { | ||
await jest.isolateModulesAsync(async () => { | ||
fromJson = jest.fn(); | ||
fetchSpy = jest.spyOn(globalThis, 'fetch'); | ||
fetchSpy.mockResolvedValue({ ok: true, text: () => '{"ok":true}' }); | ||
const { createHttpTransport } = | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
await import('../http-transport'); | ||
makeHttpRequest = createHttpTransport({ fromJson, url: 'http://localhost' }); | ||
}); | ||
}); | ||
it('uses the `fromJson` function to parse the response from a JSON string', async () => { | ||
expect.assertions(1); | ||
await makeHttpRequest({ payload: { foo: 123 } }); | ||
expect(fromJson).toHaveBeenCalledWith('{"ok":true}', { foo: 123 }); | ||
}); | ||
it('returns the value parsed by `fromJson`', async () => { | ||
expect.assertions(1); | ||
fromJson.mockReturnValueOnce({ result: 456 }); | ||
await expect(makeHttpRequest({ payload: { foo: 123 } })).resolves.toEqual({ result: 456 }); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/rpc-transport-http/src/__tests__/http-transport-to-json-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,33 @@ | ||
import { RpcTransport } from '@solana/rpc-spec'; | ||
|
||
describe('createHttpTransport and `toJson` function', () => { | ||
let toJson: jest.Mock; | ||
let fetchSpy: jest.SpyInstance; | ||
let makeHttpRequest: RpcTransport; | ||
beforeEach(async () => { | ||
await jest.isolateModulesAsync(async () => { | ||
toJson = jest.fn(value => JSON.stringify(value)); | ||
fetchSpy = jest.spyOn(globalThis, 'fetch'); | ||
fetchSpy.mockResolvedValue({ json: () => ({ ok: true }), ok: true }); | ||
const { createHttpTransport } = | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
await import('../http-transport'); | ||
makeHttpRequest = createHttpTransport({ toJson, url: 'http://localhost' }); | ||
}); | ||
}); | ||
it('uses the `toJson` function to transform the payload to a JSON string', () => { | ||
makeHttpRequest({ payload: { foo: 123 } }); | ||
expect(toJson).toHaveBeenCalledWith({ foo: 123 }); | ||
}); | ||
it('uses passes the JSON string to the fetch API', () => { | ||
toJson.mockReturnValueOnce('{"someAugmented":"jsonString"}'); | ||
makeHttpRequest({ payload: { foo: 123 } }); | ||
expect(fetchSpy).toHaveBeenCalledWith( | ||
expect.anything(), | ||
expect.objectContaining({ | ||
body: '{"someAugmented":"jsonString"}', | ||
}), | ||
); | ||
}); | ||
}); |
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