Skip to content

Commit

Permalink
Merge pull request #34 from Atralupus/feat/node-type
Browse files Browse the repository at this point in the history
Handle networkType and nodeType
  • Loading branch information
Atralupus authored May 17, 2024
2 parents 5d854cd + 6619f0c commit 4a7f45a
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 176 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NETWORK_CONF_MAP="<network-name>=<graphql-endpoint>,<network-name>=<graphql-endpoint>"
NETWORK_CONF_MAP="<networkType-nodeType>=<graphql-endpoint>,heimdall-internal=http://url/graphql,heimdall-main=http://url/graphql"
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# 9c-board

[![Netlify Status](https://api.netlify.com/api/v1/badges/a99a7121-aa14-4b2a-b45a-6f874370dc93/deploy-status)](https://app.netlify.com/sites/planetarium-9c-board/deploys)

A web application to provide useful tools. You can access it in https://planetarium-9c-board.netlify.app/.
A web application to provide useful tools. You can access it in https://9c-board.nine-chronicles.dev/.

## Devlopment

Expand All @@ -12,7 +10,7 @@ A web application to provide useful tools. You can access it in https://planetar

```
# Set network config map e.g.,
NETWORK_CONF_MAP="odin=http://9c-main-rpc-1.nine-chronicles.com/graphql,heimdall=http://heimdall-rpc-1.nine-chronicles.com/graphql"
NETWORK_CONF_MAP="<networkType-nodeType>=<graphql-endpoint>,heimdall-internal=http://url/graphql,heimdall-main=http://url/graphql"
```

### Run
Expand Down
37 changes: 28 additions & 9 deletions apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { NetworkType, NodeType } from "./constants/network";

export const BASE_URL = "https://mimir.nine-chronicles.dev/";
export const INTERNAL_BASE_URL = "https://mimir-internal.nine-chronicles.dev/";

const defaultHeaders: HeadersInit = {
"Content-Type": "application/json",
Expand All @@ -11,6 +14,7 @@ interface FetchOptions<TBody = undefined> {
}

async function fetchAPI<TResponse, TBody = undefined>(
nodeType: NodeType,
endpoint: string,
options: FetchOptions<TBody> = {}
): Promise<TResponse> {
Expand All @@ -23,7 +27,9 @@ async function fetchAPI<TResponse, TBody = undefined>(
body: body ? JSON.stringify(body) : null,
};

const url = `${BASE_URL}${endpoint}`;
const url =
(nodeType === NodeType.Internal ? INTERNAL_BASE_URL : BASE_URL) +
endpoint;

const response = await fetch(url, config);

Expand All @@ -45,21 +51,34 @@ async function fetchAPI<TResponse, TBody = undefined>(
}
}

export async function getSheetNames(network: string): Promise<string[]> {
return await fetchAPI<string[]>(`${network}/sheets/names`);
export async function getSheetNames(
nodeType: NodeType,
networkType: NetworkType
): Promise<string[]> {
return await fetchAPI<string[]>(nodeType, `${networkType}/sheets/names`);
}

export async function getSheet(network: string, name: string): Promise<string> {
return await fetchAPI<string>(`${network}/sheets/${name}`, {
export async function getSheet(
nodeType: NodeType,
networkType: NetworkType,
name: string
): Promise<string> {
return await fetchAPI<string>(nodeType, `${networkType}/sheets/${name}`, {
headers: { accept: "text/csv" },
});
}

export async function getAvatarInventory(network: string, avatarAddress: string): Promise<any | null> {
export async function getAvatarInventory(
nodeType: NodeType,
networkType: NetworkType,
avatarAddress: string
): Promise<any | null> {
try {
return await fetchAPI<any>(`${network}/avatars/${avatarAddress}/inventory`);
}
catch (error) {
return await fetchAPI<any>(
nodeType,
`${networkType}/avatars/${avatarAddress}/inventory`
);
} catch (error) {
return null;
}
}
9 changes: 9 additions & 0 deletions constants/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum NetworkType {
Odin = "odin",
Heimdall = "heimdall",
}

export enum NodeType {
Internal = "internal",
Main = "main",
}
Loading

0 comments on commit 4a7f45a

Please sign in to comment.