Skip to content

Commit

Permalink
Improve carp-client exports
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Mar 19, 2024
1 parent d57f499 commit a025b71
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion webserver/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@dcspark/carp-client",
"version": "3.0.0",
"description": "Web client for the Carp server",
"main": "app/index.js",
"main": "client/src/index.js",
"scripts": {
"eslint": "eslint src",
"clean": "rm -rf ./dist",
Expand Down
39 changes: 3 additions & 36 deletions webserver/client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,3 @@
import type { EndpointTypes } from "../../shared/routes";
import type { ErrorShape } from "../../shared/errors";
import type { AxiosError, AxiosResponse } from "axios";
import axios from "axios";
import type { Routes } from "../../shared/routes";

export type {
RelationFilter,
RelationFilterType,
Pagination,
UtxoPointer,
} from "../../shared/models/common";
export * from "../../shared/routes";
export * from "../../shared/errors";
export type { EndpointTypes } from "../../shared/routes";
export type { ErrorShape } from "../../shared/errors";

export async function query<T extends Routes>(
urlBase: string,
route: T,
data: EndpointTypes[T]["input"]
): Promise<EndpointTypes[T]["response"]> {
const result = await axios.post<
EndpointTypes[T]["response"],
AxiosResponse<EndpointTypes[T]["response"]>,
EndpointTypes[T]["input"]
>(`${urlBase}/${route}`, data);
return result.data;
}

export function getErrorResponse(
err: AxiosError<ErrorShape, unknown>
): AxiosResponse<ErrorShape, unknown> {
if (err.response == null) throw new Error(`Unexpected null response`);
return err.response;
}
export * from './template';
export * from './paginated';
export * from "../../shared";
6 changes: 3 additions & 3 deletions webserver/client/src/paginated.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { query } from "./index";
import type { EndpointTypes } from "./index";
import { Routes } from "./index";
import { query } from "./template";
import type { EndpointTypes } from "../../shared/routes";
import { Routes } from "../../shared/routes";
import type { Pagination } from "../../shared/models/common";
import type { TxAndBlockInfo } from "../../shared/models/TransactionHistory";
import type {
Expand Down
25 changes: 25 additions & 0 deletions webserver/client/src/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { EndpointTypes } from "../../shared/routes";
import type { ErrorShape } from "../../shared/errors";
import type { AxiosError, AxiosResponse } from "axios";
import axios from "axios";
import type { Routes } from "../../shared/routes";

export async function query<T extends Routes>(
urlBase: string,
route: T,
data: EndpointTypes[T]["input"]
): Promise<EndpointTypes[T]["response"]> {
const result = await axios.post<
EndpointTypes[T]["response"],
AxiosResponse<EndpointTypes[T]["response"]>,
EndpointTypes[T]["input"]
>(`${urlBase}/${route}`, data);
return result.data;
}

export function getErrorResponse(
err: AxiosError<ErrorShape, unknown>
): AxiosResponse<ErrorShape, unknown> {
if (err.response == null) throw new Error(`Unexpected null response`);
return err.response;
}
2 changes: 1 addition & 1 deletion webserver/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "carp",
"version": "3.0.0",
"description": "API for the Postgres database generated by Carp",
"main": "app/index.js",
"main": "client/src/index.js",
"scripts": {
"eslint": "eslint app",
"typecheck": "tsc --project tsconfig.json",
Expand Down
4 changes: 4 additions & 0 deletions webserver/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './constants';
export * from './errors';
export * from './routes';
export * from './models';
17 changes: 17 additions & 0 deletions webserver/shared/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export * from './Address';
export * from './AddressUsed';
export * from './AssetUtxos';
export * from './BlockLatest';
export * from './common';
export * from './CredentialAddress';
export * from './DelegationForAddress';
export * from './DelegationForPool';
export * from './DexLastPrice';
export * from './DexMeanPrice';
export * from './DexSwap';
export * from './MintBurn';
export * from './PolicyIdAssetMap';
export * from './Pool';
export * from './ProjectedNftRange';
export * from './TransactionHistory';
export * from './TransactionOutput';

0 comments on commit a025b71

Please sign in to comment.