Skip to content

Commit

Permalink
feat: add optional query params
Browse files Browse the repository at this point in the history
  • Loading branch information
mttwlkr committed Jun 14, 2022
1 parent a84be47 commit cf780de
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
22 changes: 16 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
FilterParams,
TradeHistory,
OrderHistory,
OptionalParams,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import { del, DELETE, GET, get, POST, post } from "./http_helpers";
Expand Down Expand Up @@ -100,28 +101,32 @@ export class ClobClient {
/**
* Creates a new API key for a user
* @param nonce
* @param optionalParams - query parameters
* @returns ApiKeyCreds
*/
public async createApiKey(nonce?: number): Promise<ApiKeyCreds> {
public async createApiKey(nonce?: number, optionalParams?: OptionalParams): Promise<ApiKeyCreds> {
this.canL1Auth();

const endpoint = `${this.host}${CREATE_API_KEY}`;
const headers = await createL1Headers(this.signer as Wallet | JsonRpcSigner, nonce);
const resp = await post(endpoint, headers);

const resp = await post(endpoint, headers, undefined, optionalParams);
return resp;
}

/**
* Derives an existing API key for a user
* @param nonce
* @param optionalParams - query parameters
* @returns ApiKeyCreds
*/
public async deriveApiKey(nonce?: number): Promise<ApiKeyCreds> {
public async deriveApiKey(nonce?: number, optionalParams?: OptionalParams): Promise<ApiKeyCreds> {
this.canL1Auth();

const endpoint = `${this.host}${DERIVE_API_KEY}`;
const headers = await createL1Headers(this.signer as Wallet | JsonRpcSigner, nonce);
const resp = await get(endpoint, headers);

const resp = await get(endpoint, headers, undefined, optionalParams);
return resp;
}

Expand Down Expand Up @@ -248,13 +253,17 @@ export class ClobClient {
return get(url, headers);
}

public async postOrder(order: LimitOrderAndSignature | MarketOrderAndSignature): Promise<any> {
public async postOrder(
order: LimitOrderAndSignature | MarketOrderAndSignature,
optionalParams?: OptionalParams,
): Promise<any> {
this.canL2Auth();
const endpoint = POST_ORDER;
const orderPayload =
order.orderType === "limit"
? limitOrderToJson(order as LimitOrderAndSignature)
: marketOrderToJson(order as MarketOrderAndSignature);

const l2HeaderArgs = {
method: POST,
requestPath: endpoint,
Expand All @@ -266,7 +275,8 @@ export class ClobClient {
this.creds as ApiKeyCreds,
l2HeaderArgs,
);
return post(`${this.host}${endpoint}`, headers, orderPayload);

return post(`${this.host}${endpoint}`, headers, orderPayload, optionalParams);
}

public async cancelOrder(payload: OrderPayload): Promise<any> {
Expand Down
22 changes: 14 additions & 8 deletions src/http_helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ export const POST = "POST";
export const DELETE = "DELETE";
export const PUT = "PUT";

export const request = async (endpoint: string, method: Method, headers?: any, data?: any): Promise<any> => {
export const request = async (
endpoint: string,
method: Method,
headers?: any,
data?: any,
params?: any,
): Promise<any> => {
try {
const response = await axios({ method, url: endpoint, headers, data });
const response = await axios({ method, url: endpoint, headers, data, params });
return response;
} catch (err) {
if (axios.isAxiosError(err)) {
Expand All @@ -22,17 +28,17 @@ export const request = async (endpoint: string, method: Method, headers?: any, d
}
};

export const post = async (endpoint: string, headers: any, data?: any): Promise<any> => {
const resp = await request(endpoint, POST, headers, data);
export const post = async (endpoint: string, headers: any, data?: any, params?: any): Promise<any> => {
const resp = await request(endpoint, POST, headers, data, params);
return "error" in resp ? resp : resp.data;
};

export const get = async (endpoint: string, headers?: any, data?: any): Promise<any> => {
const resp = await request(endpoint, GET, headers, data);
export const get = async (endpoint: string, headers?: any, data?: any, params?: any): Promise<any> => {
const resp = await request(endpoint, GET, headers, data, params);
return "error" in resp ? resp : resp.data;
};

export const del = async (endpoint: string, headers?: any, data?: any): Promise<any> => {
const resp = await request(endpoint, DELETE, headers, data);
export const del = async (endpoint: string, headers?: any, data?: any, params?: any): Promise<any> => {
const resp = await request(endpoint, DELETE, headers, data, params);
return "error" in resp ? resp : resp.data;
};
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,5 @@ export interface TradeHistory {
export interface OrderHistory {
history: Order[];
}

export type OptionalParams = { [query: string]: string };

0 comments on commit cf780de

Please sign in to comment.