Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
runjuu committed Jun 11, 2024
1 parent 0fe6236 commit 5762543
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"typescript": "^5.4.5"
},
"dependencies": {
"openapi-fetch": "^0.9.7"
"camelcase": "^8.0.0",
"openapi-fetch": "^0.9.7",
"ts-case-convert": "^2.0.7"
}
}
2 changes: 1 addition & 1 deletion packages/core/scripts/generate-openapi-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ const ast = await openapiTS(openapi as unknown as OpenAPI3);
const contents = astToString(ast);

fs.writeFileSync(
new URL("../src/openapi-schema.ts", import.meta.url),
new URL("../src/types/openapi-schema.ts", import.meta.url),
contents,
);
7 changes: 7 additions & 0 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import createClient from "openapi-fetch";

import type { paths } from "./types/openapi-schema.js";

export type Client = typeof client;

export const client = createClient<paths>();
5 changes: 4 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "./types.js";
export * from "./types/components-alias.js";
export * from "./types/openapi-schema.js";
export * from "./client.js";
export * from "./requests.js";
149 changes: 149 additions & 0 deletions packages/core/src/requests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import type { FetchResponse, MaybeOptionalInit } from "openapi-fetch";
import { objectToCamel, objectToSnake } from "ts-case-convert";

import { type Client, client as defaultClient } from "./client.js";
import type { paths } from "./types/openapi-schema.js";
import type {
CamelCaseData,
CommonQuery,
HttpMethod,
} from "./types/utilities.js";

function createRequest<
Path extends keyof paths,
Method extends HttpMethod,
FetchInit extends MaybeOptionalInit<paths[Path], Method>,
Params,
>(path: Path, method: Method, getInit: (params: Params) => FetchInit) {
return async (
params: Params,
init?: Omit<MaybeOptionalInit<paths[Path], Method>, "params"> | null,
client: Client = defaultClient,
): Promise<
CamelCaseData<
FetchResponse<paths[Path][Method], FetchInit, `${string}/${string}`>
>
> => {
// biome-ignore lint/suspicious/noExplicitAny: Too complex to type
const res = await (client as any)[
method.toUpperCase() as Uppercase<Method>
](path, { ...getInit(params), ...init });

if (res.data && typeof res.data === "object") {
res.data = objectToCamel(res.data);
}

return res;
};
}

export const getActivity = createRequest(
"/decentralized/tx/{id}",
"get",
({ id, ...query }: CommonQuery<"/decentralized/tx/{id}", "get">) => ({
params: { path: { id }, query: objectToSnake(query) },
}),
);

export const getAccountActivities = createRequest(
"/decentralized/{account}",
"get",
({ account, ...query }: CommonQuery<"/decentralized/{account}", "get">) => ({
params: { path: { account }, query: objectToSnake(query) },
}),
);

export const getRssActivity = createRequest(
"/rss/{path}",
"get",
(path: CommonQuery<"/rss/{path}", "get">) => ({
params: { path },
}),
);

const o = {
"/nta/bridge/transactions": {
get: "getBridgingTransactions",
},
"/nta/bridge/transactions/{transaction_hash}": {
get: "getBridgingTransactionByHash",
},
"/nta/stake/transactions": {
get: "getStakingTransactions",
},
"/nta/stake/transactions/{transaction_hash}": {
get: "getStakingTransactionByHash",
},
"/nta/stake/stakings": {
get: "getListofStakersAndNodes",
},
"/nta/stake/{staker_address}/profit": {
get: "getStakingProfitByStaker",
},
"/nta/chips": {
get: "getAllChips",
},
"/nta/chips/{chip_id}": {
get: "getChipById",
},
"/nta/chips/{chip_id}/image.svg": {
get: "getChipImageById",
},
"/nta/snapshots/nodes/count": {
get: "getNodeCountSnapshot",
},
"/nta/snapshots/nodes/min_tokens_to_stake": {
post: "minimumStakingAmountSnapshot",
},
"/nta/snapshots/stakers/count": {
get: "getStakerCountSnapshot",
},
"/nta/snapshots/stakers/profit": {
get: "getStakerProfitSnapshot",
},
"/nta/snapshots/nodes/operation/profit": {
get: "getOperationProfitSnapshot",
},
"/nta/snapshots/epochs/apy": {
get: "getEpochsApySnapshot",
},
"/nta/nodes": {
get: "getAllNodes",
},
"/nta/nodes/{address}": {
get: "getNodeByAddress",
},
"/nta/nodes/{address}/avatar.svg": {
get: "getNodeAvatarByAddress",
},
"/nta/nodes/{address}/events": {
get: "getNodeEventsByAddress",
},
"/nta/nodes/{node_address}/operation/profit": {
get: "getNodeOperationProfitByAddress",
},
"/nta/epochs": {
get: "getAllEpochs",
},
"/nta/epochs/{epoch_id}": {
get: "getEpochById",
},
"/nta/epochs/distributions/{transaction_hash}": {
get: "getEpochTransactionByHash",
},
"/nta/epochs/{node_address}/rewards": {
get: "getNodeRewardsByEpoch",
},
"/nta/epochs/apy": {
get: "getAverageEpochsApy",
},
"/nta/networks": {
get: "getAllCompatibleNetworks",
},
"/nta/networks/{network_name}/list_workers": {
get: "getAvailableWorkersByNetwork",
},
"/nta/networks/{network_name}/workers/{worker_name}": {
get: "getConfigByNetworkAndWorker",
},
};
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions packages/core/src/types/utilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { ObjectToCamel } from "ts-case-convert/lib/caseConvert.js";

import type { paths } from "./openapi-schema.js";

export type CamelCaseKeys<T> = T extends object ? ObjectToCamel<T> : T;

export type CamelCaseData<T> = T extends { data: infer D }
? Omit<T, "data"> & { data: CamelCaseKeys<D> }
: T;

export type HttpMethod =
| "get"
| "put"
| "post"
| "delete"
| "options"
| "head"
| "patch"
| "trace";

type MapNeverTo<T, R> = [T] extends [never] ? R : T;

type PathParameters<Method> = MapNeverTo<
Method extends { parameters: { path: infer P } } ? P : never,
object
>;

type QueryParameters<Method> = MapNeverTo<
Method extends { parameters: { query?: infer Q } }
? Q extends object
? Q
: never
: never,
object
>;

export type CommonQuery<
Path extends keyof paths,
Method extends HttpMethod,
> = CamelCaseKeys<
PathParameters<paths[Path][Method]> & QueryParameters<paths[Path][Method]>
>;
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5762543

Please sign in to comment.