Skip to content

Commit

Permalink
chore: add helper method (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh authored Apr 13, 2024
1 parent 9670b5e commit 1054f8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
42 changes: 27 additions & 15 deletions packages/karfia-testcontainers/karfia-testcontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export class KarfiaTestContainer extends AbstractStartedContainer {
return this.getMappedPort(endpoint.port);
}

async rpc(options: {
method: string;
params?: any[];
headers?: Record<string, string>;
endpoint?: string;
}): Promise<Response> {
const name = options.endpoint ?? 'rpc';
/**
* Get the host endpoint for a given name.
* @param {string} name of the endpoint to get
* @param {string} host to use, defaults to the container host,
* use `host.docker.internal` if you need to access the host from a container
*/
getHostEndpoint(name: string, host = this.getHost()): string {
const endpoint = this.container.endpoints?.[name];
if (endpoint === undefined) {
throw new Error(`Endpoint not found, please define a '${name}' endpoint to use rpc()`);
Expand All @@ -51,19 +51,31 @@ export class KarfiaTestContainer extends AbstractStartedContainer {

const jsonRpc = endpoint as ContainerEndpointHttpJsonRpc;
const scheme = jsonRpc.protocol.startsWith('HTTPS') ? 'https' : 'http';
const jsonRpcVersion = jsonRpc.protocol.endsWith('2.0') ? '2.0' : '1.0';

const hostPort = this.getMappedPort(endpoint.port);
const hostEndpoint = `${scheme}://${this.getHost()}:${hostPort}${jsonRpc.path ?? ''}`;
const headers: Record<string, string> = {
'Content-Type': 'application/json',
...(jsonRpc.authorization ? this.getHttpAuthorizationHeaders(jsonRpc.authorization) : {}),
...(options.headers ?? {}),
};
return `${scheme}://${host}:${hostPort}${jsonRpc.path ?? ''}`;
}

async rpc(options: {
method: string;
params?: any[];
headers?: Record<string, string>;
endpoint?: string;
}): Promise<Response> {
const name = options.endpoint ?? 'rpc';
const hostEndpoint = this.getHostEndpoint(name);

const endpoint = this.container.endpoints?.[name];
const jsonRpc = endpoint as ContainerEndpointHttpJsonRpc;
const jsonRpcVersion = jsonRpc.protocol.endsWith('2.0') ? '2.0' : '1.0';

return await fetch(hostEndpoint, {
method: 'POST',
headers: headers,
headers: {
'Content-Type': 'application/json',
...(jsonRpc.authorization ? this.getHttpAuthorizationHeaders(jsonRpc.authorization) : {}),
...(options.headers ?? {}),
},
body: JSON.stringify({
jsonrpc: jsonRpcVersion,
method: options.method,
Expand Down
3 changes: 1 addition & 2 deletions workspace/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const tseslint = require('typescript-eslint');
const globals = require('globals');

module.exports = tseslint.config(
{
languageOptions: {
globals: globals.node,
globals: require('globals').node,
},
},
{
Expand Down

0 comments on commit 1054f8c

Please sign in to comment.