Skip to content

Commit

Permalink
allow to pass custom headers to http-transport in niljs
Browse files Browse the repository at this point in the history
  • Loading branch information
ukorvl committed Feb 5, 2025
1 parent 20cdfd5 commit 8875383
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
18 changes: 18 additions & 0 deletions niljs/src/rpc/rpcClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,21 @@ test("creates a new RPC client with the correct endpoint", () => {

expect(actualClient).not.toBeUndefined();
});

test("creates a new RPC client with the correct endpoint and headers", () => {
const actualClient = createRPCClient(endpoint, {
headers: {
"My-header": "my-value",
},
});

expect(actualClient).not.toBeUndefined();
});

test("throws an error when the headers are invalid (helps in JavaScript)", () => {
const headers = {
"Invalid-header": 333,
} as unknown as Record<string, string>;

expect(() => createRPCClient(endpoint, { headers })).toThrow();
});
7 changes: 6 additions & 1 deletion niljs/src/rpc/rpcClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Client, HTTPTransport, RequestManager } from "@open-rpc/client-js";
import fetch from "isomorphic-fetch";
import { isValidHttpHeaders } from "../utils/rpc.js";
import { version } from "../version.js";

/**
* The options for the RPC client.
*/
type RPCClientOptions = {
signal?: AbortSignal;
headers?: Record<string, string>;
};

/**
Expand All @@ -15,14 +17,17 @@ type RPCClientOptions = {
* HTTP is currently the only supported transport.
* @example const client = createRPCClient(RPC_ENDPOINT);
*/
const createRPCClient = (endpoint: string, { signal }: RPCClientOptions = {}) => {
const createRPCClient = (endpoint: string, { signal, headers = {} }: RPCClientOptions = {}) => {
const fetcher: typeof fetch = (url, options) => {
return fetch(url, { ...options, signal });
};

isValidHttpHeaders(headers);

const transport = new HTTPTransport(endpoint, {
headers: {
"Client-Version": version,
...headers,
},
fetcher,
});
Expand Down
4 changes: 2 additions & 2 deletions niljs/src/transport/HttpTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class HttpTransport implements ITransport {
* @constructor
* @param {IHttpTransportConfig} config The transport config. See {@link IHttpTransportConfig}.
*/
constructor({ endpoint, signal, timeout = 20000 }: IHttpTransportConfig) {
this.rpcClient = createRPCClient(endpoint, { signal });
constructor({ endpoint, signal, timeout = 20000, headers }: IHttpTransportConfig) {
this.rpcClient = createRPCClient(endpoint, { signal, headers });
this.timeout = timeout;
}

Expand Down
6 changes: 6 additions & 0 deletions niljs/src/transport/types/IHttpTransportConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type IHttpTransportConfig = {
* @default undefined
*/
signal?: AbortSignal;
/**
* The headers to be sent with the request.
* @example { 'My-header': 'my-value' }
* @default {}
*/
headers?: Record<string, string>;
};

export type { IHttpTransportConfig };
18 changes: 18 additions & 0 deletions niljs/src/utils/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function isValidHttpHeaders(headers: unknown) {
if (headers === null || typeof headers !== "object" || Array.isArray(headers)) {
throw new Error("Invalid headers provided to the RPC client.");
}

const isValidObj = Object.entries(headers).every(
([key, value]) =>
typeof key === "string" &&
(typeof value === "string" ||
(Array.isArray(value) && value.every((v) => typeof v === "string"))),
);

if (!isValidObj) {
throw new Error("Invalid http headers provided to the RPC client.");
}
}

export { isValidHttpHeaders };

0 comments on commit 8875383

Please sign in to comment.