-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sismo api in sdk class (#9)
* feat: add sismo api in sdk class * feat: fetch group on sismo api directly from zk connect * feat: create sdk from an url instead of an env * fix: remove typo --------- Co-authored-by: Dev <[email protected]>
- Loading branch information
Showing
12 changed files
with
293 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,8 @@ | |
"author": "[email protected]", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@apollo/client": "^3.7.10", | ||
"graphql": "^16.6.0", | ||
"@ethersproject/bignumber": "5.7.0" | ||
}, | ||
"lint-staged": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ApolloClient, InMemoryCache, QueryOptions } from "@apollo/client"; | ||
|
||
export class ApiFetcher { | ||
private _url: string; | ||
private _client: ApolloClient<any>; | ||
|
||
constructor(sismoApiUrl: string) { | ||
this._url = sismoApiUrl; | ||
this._client = new ApolloClient({ | ||
uri: this._url, | ||
cache: new InMemoryCache(), | ||
}); | ||
} | ||
|
||
public async getWithQuery<T>(opts: QueryOptions) { | ||
const res = await this._client.query<T>(opts); | ||
return res.data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./api-fetcher"; | ||
export * from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const sismoApiUrls: Record<string, string> = { | ||
staging: "https://api.staging.zikies.io", | ||
testnets: "https://api.testnets.sismo.io", | ||
prod: "https://api.sismo.io", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./queries"; | ||
export * from "./sdk"; | ||
export * from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { gql } from "@apollo/client"; | ||
|
||
export const getGroupFromIdQuery = gql` | ||
query getGroup($id: ID!) { | ||
group(id: $id) { | ||
id | ||
name | ||
description | ||
specs | ||
generationFrequency | ||
snapshots { | ||
timestamp | ||
dataUrl | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const getGroupFromNameQuery = gql` | ||
query getGroup($name: String!) { | ||
group(name: $name) { | ||
id | ||
name | ||
description | ||
specs | ||
generationFrequency | ||
snapshots { | ||
timestamp | ||
dataUrl | ||
} | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ApiFetcher, sismoApiUrls } from "../api"; | ||
import { getGroupFromIdQuery, getGroupFromNameQuery } from "./queries"; | ||
import { FetchedData, GetGroupQueryOutput, GroupParams } from "./types"; | ||
|
||
export class Sdk { | ||
private _apiFetcher: ApiFetcher; | ||
|
||
constructor(sismoApiUrl?: string) { | ||
this._apiFetcher = new ApiFetcher(sismoApiUrl ?? sismoApiUrls.prod); | ||
} | ||
|
||
public async getGroup({ id, name, timestamp }: GroupParams) { | ||
if (!id && !name) { | ||
throw new Error( | ||
"Either id or name must be provided for the group. You can view all groups at https://factory.sismo.io/groups-explorer." | ||
); | ||
} | ||
|
||
const group: GetGroupQueryOutput = id | ||
? ( | ||
await this._apiFetcher.getWithQuery<{ group: GetGroupQueryOutput }>({ | ||
query: getGroupFromIdQuery, | ||
variables: { id }, | ||
}) | ||
).group | ||
: ( | ||
await this._apiFetcher.getWithQuery<{ group: GetGroupQueryOutput }>({ | ||
query: getGroupFromNameQuery, | ||
variables: { name }, | ||
}) | ||
).group; | ||
|
||
let dataUrl: string; | ||
if (timestamp) { | ||
const snapshot = group.snapshots.filter( | ||
(s: { timestamp: string }) => s.timestamp === timestamp | ||
)[0]; | ||
({ dataUrl } = snapshot); | ||
if (!snapshot) { | ||
({ dataUrl } = group.snapshots[0]); | ||
} | ||
} else { | ||
({ dataUrl } = group.snapshots[0]); | ||
} | ||
|
||
const data: FetchedData = await fetch(dataUrl).then((res) => res.json()); | ||
return { ...group, data } as GetGroupQueryOutput & { data: FetchedData }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export type GroupParams = { id?: string; name?: string; timestamp?: string }; | ||
export type FetchedData = Record<string, number>; | ||
|
||
// queries types | ||
export type GetGroupQueryOutput = { | ||
id: string; | ||
name: string; | ||
description: string; | ||
specs: string; | ||
generationFrequency: string; | ||
snapshots: { | ||
timestamp: string; | ||
dataUrl: string; | ||
}[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.