Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass custom headers to http-transport in niljs #180

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
15 changes: 15 additions & 0 deletions niljs/src/utils/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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",
);

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

export { isValidHttpHeaders };