Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/web3-providers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RaribleAPI } from './rarible'
import { NFTScanAPI } from './NFTScan'
import { NativeExplorerAPI } from './explorer'
import { RSS3API } from './rss3'
import { KeyValueAPI } from './kv'

export * from './types'
export * from './opensea/utils'
Expand All @@ -14,6 +15,7 @@ export const NFTScan = new NFTScanAPI()
export const CoinGecko = new CoinGeckoAPI()
export const Explorer = new NativeExplorerAPI()
export const RSS3 = new RSS3API()
export const KeyValue = new KeyValueAPI()

// Method for provider proxy
export { getOpenSeaNFTList, getOpenSeaCollectionList } from './opensea'
Expand Down
16 changes: 16 additions & 0 deletions packages/web3-providers/src/kv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Key-Value storage

```typescript
// create a JSON storage
const storage = KeyValue.createJSON_Storage('hello_world')

// set value
await storage.set('foo', {
key: 'value',
})

// get value
const response = await storage.get('foo')

console.assert(response?.key === 'value', 'ASSERTION FAILED')
```
43 changes: 17 additions & 26 deletions packages/web3-providers/src/kv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,42 @@ export class JSON_Storage implements StorageAPI.Storage {
constructor(private prefix: string) {}

async set<T extends {}>(key: string, value: T) {
return fetchJSON<void>(
await fetch(
urlcat(KV_ROOT_URL, 'api/:name', {
name: `${this.prefix}_${key}`,
}),
{
method: 'PUT',
mode: 'cors',
credentials: 'omit',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(value),
},
)
}
async get<T>(key: string) {
const response = await fetchJSON<string>(
urlcat(KV_ROOT_URL, 'api/:name', {
name: `${this.prefix}_${key}`,
}),
{
method: 'GET',
mode: 'cors',
credentials: 'omit',
},
)
try {
return JSON.parse(response) as T
return fetchJSON<T>(
urlcat(KV_ROOT_URL, 'api/:name', {
name: `${this.prefix}_${key}`,
}),
{
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
},
)
} catch {
return
}
}
async delete(key: string) {
await fetchJSON<void>(
urlcat(KV_ROOT_URL, 'api/:name', {
name: `${this.prefix}_${key}`,
}),
{
method: 'DELETE',
mode: 'cors',
credentials: 'omit',
},
)
}
}

export class KeyValueAPI implements StorageAPI.Provider {
createStorage(key: string): StorageAPI.Storage {
createJSON_Storage(key: string): StorageAPI.Storage {
return new JSON_Storage(key)
}
}
5 changes: 3 additions & 2 deletions packages/web3-providers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,11 @@ export namespace StorageAPI {
export interface Storage {
set<T extends {}>(key: string, value: T): Promise<void>
get<T>(key: string): Promise<T | void>
delete(key: string): Promise<void>
delete?: (key: string) => Promise<void>
}

export interface Provider {
createStorage(key: string): Storage
createJSON_Storage?: (key: string) => Storage
createBinaryStorage?: (key: string) => Storage
}
}