Skip to content

Commit

Permalink
support mediaType in request (#8977)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan authored Jul 6, 2023
1 parent d62901d commit c9b4e4a
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 174 deletions.
2 changes: 2 additions & 0 deletions ecosystem/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to the Aptos Node SDK will be captured in this file. This ch

## Unreleased

- Support `contentType` in request so custom headers would not override required headers

## 1.13.0 (2023-07-05)

- Use client that is not generated by openAPI
Expand Down
6 changes: 4 additions & 2 deletions ecosystem/typescript/sdk/src/client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ async function axiosRequest<Request, Response>(
url: string,
method: "GET" | "POST",
body?: Request,
contentType?: string,
params?: Record<string, string | AnyNumber | boolean | undefined>,
overrides?: ClientConfig,
): Promise<AxiosResponse<Response>> {
const headers: Record<string, string | number | boolean> = {
...overrides?.HEADERS,
"x-aptos-client": `aptos-ts-sdk/${VERSION}`,
"content-type": contentType ?? "application/json",
};

if (overrides?.TOKEN) {
Expand Down Expand Up @@ -66,9 +68,9 @@ async function axiosRequest<Request, Response>(
* @returns the response or AptosApiError
*/
export async function aptosRequest<Req, Res>(options: AptosRequest): Promise<AptosResponse<Req, Res>> {
const { url, endpoint, method, body, params, overrides } = options;
const { url, endpoint, method, body, contentType, params, overrides } = options;
const fullEndpoint = `${url}/${endpoint ?? ""}`;
const response = await axiosRequest<Req, Res>(fullEndpoint, method, body, params, overrides);
const response = await axiosRequest<Req, Res>(fullEndpoint, method, body, contentType, params, overrides);

const result: AptosResponse<Req, Res> = {
status: response.status,
Expand Down
14 changes: 14 additions & 0 deletions ecosystem/typescript/sdk/src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,25 @@ export type ClientConfig = {
WITH_CREDENTIALS?: boolean;
};

/**
* The API request type
*
* @param url - the url to make the request to, i.e https://fullnode.aptoslabs.devnet.com/v1
* @param method - the request method "GET" | "POST"
* @param endpoint (optional) - the endpoint to make the request to, i.e transactions
* @param body (optional) - the body of the request
* @param contentType (optional) - the content type to set the `content-type` header to,
* by default is set to `application/json`
* @param params (optional) - query params to add to the request
* @param originMethod (optional) - the local method the request came from
* @param overrides (optional) - a `ClientConfig` object type to override request data
*/
export type AptosRequest = {
url: string;
method: "GET" | "POST";
endpoint?: string;
body?: any;
contentType?: string;
params?: Record<string, string | AnyNumber | boolean | undefined>;
originMethod?: string;
overrides?: ClientConfig;
Expand Down
10 changes: 6 additions & 4 deletions ecosystem/typescript/sdk/src/providers/aptos_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ export class AptosClient {
body: signedTxn,
endpoint: "transactions",
originMethod: "submitSignedBCSTransaction",
overrides: { HEADERS: { "content-type": "application/x.aptos.signed_transaction+bcs" }, ...this.config },
contentType: "application/x.aptos.signed_transaction+bcs",
overrides: { ...this.config },
});
return data;
}
Expand Down Expand Up @@ -495,7 +496,8 @@ export class AptosClient {
endpoint: "transactions/simulate",
params: queryParams,
originMethod: "submitBCSSimulation",
overrides: { HEADERS: { "content-type": "application/x.aptos.signed_transaction+bcs" }, ...this.config },
contentType: "application/x.aptos.signed_transaction+bcs",
overrides: { ...this.config },
});
return data;
}
Expand Down Expand Up @@ -735,7 +737,7 @@ export class AptosClient {
endpoint: `tables/${handle}/item`,
originMethod: "getTableItem",
params: { ledger_version: query?.ledgerVersion?.toString() },
overrides: { HEADERS: { "content-type": "application/json" }, ...this.config },
overrides: { ...this.config },
});
return response.data;
}
Expand Down Expand Up @@ -1056,7 +1058,7 @@ export class AptosClient {
endpoint: "view",
originMethod: "getTableItem",
params: { ledger_version },
overrides: { HEADERS: { "content-type": "application/json" }, ...this.config },
overrides: { ...this.config },
});
return data;
}
Expand Down
Loading

0 comments on commit c9b4e4a

Please sign in to comment.