Skip to content

Commit

Permalink
Merge pull request #5 from decentldotland/switch-net
Browse files Browse the repository at this point in the history
feat: v0.0.6 -- read function's KV object
  • Loading branch information
charmful0x authored Jan 29, 2024
2 parents 181a50e + a3570ae commit 02073be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ const OVERWRITE_INIT_STATE = btoa('{"new": "state"}'); // optional

const id = await mem.fork(MAINNET_FUNCTION_ID, FORK_IT_TO, OVERWRITE_INIT_STATE?);
```
### Get a function's KV object (only `mainnet`)
```ts
const MAINNET_FUNCTION_ID = "...";
const kv = await mem.kvGet(MAINNET_FUNCTION_ID);
```
### Named functions resolving DX (only `mainnet`)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mem-sdk",
"version": "0.0.5",
"version": "0.0.6",
"description": "JS SDK for MEM Serverless Functions",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class Actions extends Base {
const res = await this.forkFunction(id, net, state);
return res;
}

async kvGet<T>(id: string): Promise<T> {
const res = await this.kvFunction(id);
return res;
}
}

class Mem extends Actions {}
Expand Down
20 changes: 20 additions & 0 deletions src/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,24 @@ export abstract class Base {
throw new Error(response.statusText);
}
}

async kvFunction(id: string): Promise<any> {
if (this.network === "mainnet") {
const url = `https://mem-api.com/kv/${id}`;

const headers = {
"Content-Type": "application/json",
};

const response = await fetch(url, { headers });

if (response.ok) {
const data = await response.json();
return data as any;
}

throw new Error(response.statusText);
}
}

}

0 comments on commit 02073be

Please sign in to comment.