Skip to content

Commit

Permalink
feat(client): accept request init options
Browse files Browse the repository at this point in the history
  • Loading branch information
javadkh2 committed Nov 18, 2024
1 parent bcdb600 commit 6fae1a1
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 135 deletions.
43 changes: 22 additions & 21 deletions packages/libs/chainweb-node-client/etc/chainweb-node-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export type ChainwebChainId = (typeof CHAINS)[number];
// @alpha
export type ChainwebNetworkId = 'mainnet01' | 'testnet04' | 'testnet05' | 'development';

// @alpha (undocumented)
export type ClientRequestInit = Omit<RequestInit, 'method' | 'body'>;

// @alpha (undocumented)
export function convertIUnsignedTransactionToNoSig(transaction: IUnsignedCommand): ICommand;

Expand Down Expand Up @@ -86,9 +89,7 @@ export interface ILocalCommandResult {
}

// @alpha (undocumented)
export interface ILocalOptions {
// (undocumented)
headers?: Record<string, string>;
export interface ILocalOptions extends ClientRequestInit {
// (undocumented)
preflight?: boolean;
// (undocumented)
Expand Down Expand Up @@ -155,9 +156,7 @@ export interface ISPVRequestBody {
}

// @alpha
export function listen(requestBody: IListenRequestBody, apiHost: string, { headers }?: {
headers?: Record<string, string>;
}): Promise<ICommandResult>;
export function listen(requestBody: IListenRequestBody, apiHost: string, requestInit?: ClientRequestInit): Promise<ICommandResult>;

// @alpha (undocumented)
export type ListenResponse = ICommandResult;
Expand All @@ -166,10 +165,9 @@ export type ListenResponse = ICommandResult;
export function local<T extends ILocalOptions>(requestBody: LocalRequestBody, apiHost: string, options?: T): Promise<LocalResponse<T>>;

// @alpha
export function localRaw(requestBody: LocalRequestBody, apiHost: string, { preflight, signatureVerification, headers, }: {
export function localRaw(requestBody: LocalRequestBody, apiHost: string, { preflight, signatureVerification, ...requestInit }: ILocalOptions & {
signatureVerification: boolean;
preflight: boolean;
headers?: Record<string, string>;
}): Promise<IPreflightResult | ICommandResult>;

// @alpha (undocumented)
Expand All @@ -196,33 +194,36 @@ export function parseResponse<T>(response: Response): Promise<T>;
export function parseResponseTEXT(response: Response): Promise<string>;

// @alpha
export function poll(requestBody: IPollRequestBody, apiHost: string, confirmationDepth?: number, { headers }?: {
headers?: Record<string, string>;
}): Promise<IPollResponse>;
export function poll(requestBody: IPollRequestBody, apiHost: string, confirmationDepth?: number, requestInit?: ClientRequestInit): Promise<IPollResponse>;

// @alpha
export function send(requestBody: ISendRequestBody, apiHost: string, { headers }?: {
headers?: Record<string, string>;
}): Promise<SendResponse>;
export function send(requestBody: ISendRequestBody, apiHost: string, requestInit?: ClientRequestInit): Promise<SendResponse>;

// @alpha
export type SendResponse = IRequestKeys;

// @alpha
export function spv(requestBody: ISPVRequestBody, apiHost: string, { headers }?: {
headers?: Record<string, string>;
}): Promise<SPVResponse | Response>;
export function spv(requestBody: ISPVRequestBody, apiHost: string, requestInit?: ClientRequestInit): Promise<SPVResponse | Response>;

// @alpha
export type SPVResponse = SPVProof;

// @alpha
export function stringifyAndMakePOSTRequest<T>(body: T, headers?: Record<string, string>): {
headers: {
'Content-Type': string;
};
export function stringifyAndMakePOSTRequest<T>(body: T, requestInit?: ClientRequestInit): {
method: string;
body: string;
cache?: RequestCache | undefined;
credentials?: RequestCredentials | undefined;
headers?: HeadersInit | undefined;
integrity?: string | undefined;
keepalive?: boolean | undefined;
mode?: RequestMode | undefined;
priority?: RequestPriority | undefined;
redirect?: RequestRedirect | undefined;
referrer?: string | undefined;
referrerPolicy?: ReferrerPolicy | undefined;
signal?: AbortSignal | null | undefined;
window?: null | undefined;
};

// (No @packageDocumentation comment for this package)
Expand Down
5 changes: 3 additions & 2 deletions packages/libs/chainweb-node-client/src/listen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ICommandResult, IListenRequestBody } from './interfaces/PactAPI';
import type { ClientRequestInit } from './local';
import { parseResponse } from './parseResponse';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';
Expand All @@ -13,9 +14,9 @@ import { fetch } from './utils/fetch';
export async function listen(
requestBody: IListenRequestBody,
apiHost: string,
{ headers }: { headers?: Record<string, string> } = {},
requestInit?: ClientRequestInit,
): Promise<ICommandResult> {
const request = stringifyAndMakePOSTRequest(requestBody, headers);
const request = stringifyAndMakePOSTRequest(requestBody, requestInit);
const listenUrl = new URL(`${apiHost}/api/v1/listen`);

const response = await fetch(listenUrl.toString(), request);
Expand Down
19 changes: 11 additions & 8 deletions packages/libs/chainweb-node-client/src/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import { fetch } from './utils/fetch';
/**
* @alpha
*/
export interface ILocalOptions {
export type ClientRequestInit = Omit<RequestInit, 'method' | 'body'>;

/**
* @alpha
*/
export interface ILocalOptions extends ClientRequestInit {
preflight?: boolean;
signatureVerification?: boolean;
headers?: Record<string, string>;
}

/**
Expand Down Expand Up @@ -45,7 +49,7 @@ export async function local<T extends ILocalOptions>(
const {
signatureVerification = true,
preflight = true,
headers = {},
...requestInit
} = options ?? {};

if (!signatureVerification) {
Expand All @@ -56,7 +60,7 @@ export async function local<T extends ILocalOptions>(
const result = await localRaw(body, apiHost, {
preflight,
signatureVerification,
headers,
...requestInit,
});

return parsePreflight(result);
Expand All @@ -78,14 +82,13 @@ export async function localRaw(
{
preflight,
signatureVerification,
headers = {},
}: {
...requestInit
}: ILocalOptions & {
signatureVerification: boolean;
preflight: boolean;
headers?: Record<string, string>;
},
): Promise<IPreflightResult | ICommandResult> {
const request = stringifyAndMakePOSTRequest(requestBody, headers);
const request = stringifyAndMakePOSTRequest(requestBody, requestInit);
const localUrlWithQueries = new URL(`${apiHost}/api/v1/local`);

localUrlWithQueries.searchParams.append('preflight', preflight.toString());
Expand Down
5 changes: 3 additions & 2 deletions packages/libs/chainweb-node-client/src/poll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IPollRequestBody, IPollResponse } from './interfaces/PactAPI';
import type { ClientRequestInit } from './local';
import { parseResponse } from './parseResponse';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';
Expand All @@ -18,9 +19,9 @@ export async function poll(
requestBody: IPollRequestBody,
apiHost: string,
confirmationDepth = 0,
{ headers }: { headers?: Record<string, string> } = {},
requestInit?: ClientRequestInit,
): Promise<IPollResponse> {
const request = stringifyAndMakePOSTRequest(requestBody, headers);
const request = stringifyAndMakePOSTRequest(requestBody, requestInit);
const pollUrl = new URL(`${apiHost}/api/v1/poll`);
if (confirmationDepth > 0) {
pollUrl.searchParams.append(
Expand Down
5 changes: 3 additions & 2 deletions packages/libs/chainweb-node-client/src/send.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ISendRequestBody, SendResponse } from './interfaces/PactAPI';
import type { ClientRequestInit } from './local';
import { parseResponse } from './parseResponse';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';
Expand All @@ -16,9 +17,9 @@ import { fetch } from './utils/fetch';
export async function send(
requestBody: ISendRequestBody,
apiHost: string,
{ headers }: { headers?: Record<string, string> } = {},
requestInit?: ClientRequestInit,
): Promise<SendResponse> {
const request = stringifyAndMakePOSTRequest(requestBody, headers);
const request = stringifyAndMakePOSTRequest(requestBody, requestInit);
const sendUrl = new URL(`${apiHost}/api/v1/send`);

const response = await fetch(sendUrl.toString(), request);
Expand Down
5 changes: 3 additions & 2 deletions packages/libs/chainweb-node-client/src/spv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ISPVRequestBody, SPVResponse } from './interfaces/PactAPI';
import type { ClientRequestInit } from './local';
import { parseResponseTEXT } from './parseResponseTEXT';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';
Expand All @@ -14,9 +15,9 @@ import { fetch } from './utils/fetch';
export async function spv(
requestBody: ISPVRequestBody,
apiHost: string,
{ headers }: { headers?: Record<string, string> } = {},
requestInit?: ClientRequestInit,
): Promise<SPVResponse | Response> {
const request = stringifyAndMakePOSTRequest(requestBody, headers);
const request = stringifyAndMakePOSTRequest(requestBody, requestInit);
const spvUrl = new URL(`${apiHost}/spv`);

const response = await fetch(spvUrl.toString(), request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ClientRequestInit } from './local';

/**
* Formats API request body to use with `fetch` function.
*
Expand All @@ -7,13 +9,10 @@
*/
export function stringifyAndMakePOSTRequest<T>(
body: T,
headers: Record<string, string> = {},
requestInit?: ClientRequestInit,
) {
return {
headers: {
'Content-Type': 'application/json',
...headers,
},
...requestInit,
method: 'POST',
body: JSON.stringify(body),
};
Expand Down
35 changes: 18 additions & 17 deletions packages/libs/client/etc/client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { ChainId } from '@kadena/types';
import type Client from '@walletconnect/sign-client';
import type { ClientRequestInit } from '@kadena/chainweb-node-client';
import { ICap } from '@kadena/types';
import { ICommand } from '@kadena/types';
import { ICommandResult } from '@kadena/chainweb-node-client';
Expand Down Expand Up @@ -82,9 +83,9 @@ export const getHostUrl: (hostBaseUrl: string) => ({ networkId, chainId }: INetw

// @public (undocumented)
export interface IBaseClient {
createSpv: (transactionDescriptor: ITransactionDescriptor, targetChainId: ChainId) => Promise<string>;
getStatus: (transactionDescriptors: ITransactionDescriptor[] | ITransactionDescriptor) => Promise<IPollResponse>;
listen: (transactionDescriptor: ITransactionDescriptor) => Promise<ICommandResult>;
createSpv: (transactionDescriptor: ITransactionDescriptor, targetChainId: ChainId, options?: ClientRequestInit) => Promise<string>;
getStatus: (transactionDescriptors: ITransactionDescriptor[] | ITransactionDescriptor, options?: ClientRequestInit) => Promise<IPollResponse>;
listen: (transactionDescriptor: ITransactionDescriptor, options?: ClientRequestInit) => Promise<ICommandResult>;
local: <T extends ILocalOptions>(transaction: LocalRequestBody, options?: T) => Promise<LocalResponse<T>>;
pollCreateSpv: (transactionDescriptor: ITransactionDescriptor, targetChainId: ChainId, options?: IPollOptions) => Promise<string>;
pollStatus: (transactionDescriptors: ITransactionDescriptor[] | ITransactionDescriptor, options?: IPollOptions) => IPollRequestPromise<ICommandResult>;
Expand Down Expand Up @@ -118,16 +119,16 @@ export type ICapabilityItem = ICap;

// @public
export interface IClient extends IBaseClient {
dirtyRead: (transaction: IUnsignedCommand) => Promise<ICommandResult>;
dirtyRead: (transaction: IUnsignedCommand, options?: ClientRequestInit) => Promise<ICommandResult>;
// @deprecated
getPoll: (transactionDescriptors: ITransactionDescriptor[] | ITransactionDescriptor) => Promise<IPollResponse>;
getPoll: (transactionDescriptors: ITransactionDescriptor[] | ITransactionDescriptor, options?: ClientRequestInit) => Promise<IPollResponse>;
pollOne: (transactionDescriptor: ITransactionDescriptor, options?: IPollOptions) => Promise<ICommandResult>;
preflight: (transaction: ICommand | IUnsignedCommand) => Promise<ILocalCommandResult>;
runPact: (code: string, data: Record<string, unknown>, option: INetworkOptions) => Promise<ICommandResult>;
preflight: (transaction: ICommand | IUnsignedCommand, options?: ClientRequestInit) => Promise<ILocalCommandResult>;
runPact: (code: string, data: Record<string, unknown>, option: ClientRequestInit & INetworkOptions) => Promise<ICommandResult>;
// @deprecated
send: ISubmit;
signatureVerification: (transaction: ICommand) => Promise<ICommandResult>;
submitOne: (transaction: ICommand) => Promise<ITransactionDescriptor>;
signatureVerification: (transaction: ICommand, options?: ClientRequestInit) => Promise<ICommandResult>;
submitOne: (transaction: ICommand, options?: ClientRequestInit) => Promise<ITransactionDescriptor>;
}

export { ICommand }
Expand Down Expand Up @@ -168,8 +169,8 @@ export interface ICreateClient {
networkId: string;
type?: 'local' | 'send' | 'poll' | 'listen' | 'spv';
}) => string | {
host: string;
headers: Record<string, string>;
hostUrl: string;
requestInit: ClientRequestInit;
}, defaults?: {
confirmationDepth?: number;
}): IClient;
Expand Down Expand Up @@ -281,17 +282,17 @@ export interface IPartialPactCommand extends AllPartial<IPactCommand> {
}

// @public
export interface IPollOptions {
export interface IPollOptions extends ClientRequestInit {
// (undocumented)
confirmationDepth?: number;
// (undocumented)
headers?: Record<string, string>;
// Warning: (ae-incompatible-release-tags) The symbol "interval" is marked as @public, but its signature references "Milliseconds" which is marked as @alpha
//
// (undocumented)
interval?: Milliseconds;
// (undocumented)
onPoll?: (id: string) => void;
onPoll?: (id: string | undefined, error: any) => void;
// (undocumented)
onResult?: (requestKey: string, result: ICommandResult) => void;
// Warning: (ae-incompatible-release-tags) The symbol "timeout" is marked as @public, but its signature references "Milliseconds" which is marked as @alpha
//
// (undocumented)
Expand Down Expand Up @@ -441,8 +442,8 @@ export function isSignedTransaction(command: IUnsignedCommand | ICommand): comma

// @public (undocumented)
export interface ISubmit {
(transaction: ICommand): Promise<ITransactionDescriptor>;
(transactionList: ICommand[]): Promise<ITransactionDescriptor[]>;
(transaction: ICommand, options?: ClientRequestInit): Promise<ITransactionDescriptor>;
(transactionList: ICommand[], options?: ClientRequestInit): Promise<ITransactionDescriptor[]>;
}

// @public (undocumented)
Expand Down
9 changes: 6 additions & 3 deletions packages/libs/client/src/client/api/runPact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { ICommandResult } from '@kadena/chainweb-node-client';
import type {
ClientRequestInit,
ICommandResult,
} from '@kadena/chainweb-node-client';
import { local } from '@kadena/chainweb-node-client';
import { hash as blackHash } from '@kadena/cryptography-utils';
import { composePactCommand, execution } from '../../composePactCommand';
Expand All @@ -7,7 +10,7 @@ export function runPact(
hostUrl: string,
code: string,
data: Record<string, unknown> = {},
requestOptions: { headers?: Record<string, string> } = {},
requestInit?: ClientRequestInit,
): Promise<ICommandResult> {
const pactCommand = composePactCommand(execution(code), {
payload: { exec: { data } },
Expand All @@ -24,7 +27,7 @@ export function runPact(
{
preflight: false,
signatureVerification: false,
...requestOptions,
...requestInit,
},
);
}
11 changes: 7 additions & 4 deletions packages/libs/client/src/client/api/spv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { SPVResponse } from '@kadena/chainweb-node-client';
import type {
ClientRequestInit,
SPVResponse,
} from '@kadena/chainweb-node-client';
import { spv } from '@kadena/chainweb-node-client';
import type { ChainId } from '@kadena/types';
import type { IPollOptions } from '../interfaces/interfaces';
Expand All @@ -8,9 +11,9 @@ export async function getSpv(
host: string,
requestKey: string,
targetChainId: ChainId,
requestOptions: { headers?: Record<string, string> } = {},
requestInit: ClientRequestInit = {},
): Promise<SPVResponse> {
const proof = await spv({ requestKey, targetChainId }, host, requestOptions);
const proof = await spv({ requestKey, targetChainId }, host, requestInit);
if (typeof proof !== 'string') throw new Error('PROOF_IS_NOT_AVAILABLE');
return proof;
}
Expand All @@ -22,7 +25,7 @@ export const pollSpv = (
pollingOptions?: IPollOptions,
): Promise<SPVResponse> => {
const task = async (): Promise<SPVResponse> =>
getSpv(host, requestKey, targetChainId);
getSpv(host, requestKey, targetChainId, pollingOptions);

const retrySpv = retry(task);

Expand Down
Loading

0 comments on commit 6fae1a1

Please sign in to comment.