-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduces custom Account fragment
- Loading branch information
1 parent
f6db0b1
commit 0d3fd5f
Showing
12 changed files
with
188 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Custom Fragments | ||
|
||
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/lens-protocol/lens-sdk/tree/next/examples/custom-fragments) |
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,18 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css"> | ||
</head> | ||
<body> | ||
<h1>Custom Fragments</h1> | ||
<div id="app">Loading...</div> | ||
<script type="module"> | ||
import out from './index.ts'; | ||
document.querySelector('#app').innerHTML = Array.isArray(out) | ||
? out.map((x) => `<div style="margin-bottom: 16px;">${x}</div>`).join('') | ||
: out; | ||
</script> | ||
</body> | ||
</html> |
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,54 @@ | ||
import 'viem/window'; | ||
|
||
import { | ||
type FragmentOf, | ||
PublicClient, | ||
UsernameFragment, | ||
evmAddress, | ||
graphql, | ||
testnet as protocolTestnet, | ||
} from '@lens-protocol/client'; | ||
import { fetchAccount } from '@lens-protocol/client/actions'; | ||
|
||
const MyAccountMetadataFragment = graphql( | ||
`fragment AccountMetadata on AccountMetadata { | ||
__typename | ||
name | ||
picture | ||
}`, | ||
); | ||
|
||
const MyAccountFragment = graphql( | ||
`fragment Account on Account { | ||
__typename | ||
address | ||
username { | ||
...Username | ||
} | ||
metadata { | ||
...AccountMetadata | ||
} | ||
}`, | ||
[UsernameFragment, MyAccountMetadataFragment], | ||
); | ||
|
||
type MyAccount = FragmentOf<typeof MyAccountFragment>; | ||
|
||
const client = PublicClient.create({ | ||
environment: protocolTestnet, | ||
accountFragment: MyAccountFragment, | ||
}); | ||
|
||
const account: MyAccount | null = await fetchAccount(client, { | ||
address: evmAddress('0x57b62a1571F4F09CDB4C3d93dA542bfe142D9F81'), | ||
}).match( | ||
(account) => account, | ||
(error) => { | ||
throw error; | ||
}, | ||
); | ||
|
||
export default [ | ||
`<h2>${account?.username?.value}</h2>`, | ||
`<pre>${JSON.stringify(account, null, 2)}</pre>`, | ||
]; |
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 @@ | ||
{ | ||
"name": "example-custom-fragments", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite" | ||
}, | ||
"dependencies": { | ||
"@lens-network/sdk": "canary", | ||
"@lens-protocol/client": "file:../../packages/client", | ||
"@lens-protocol/metadata": "next", | ||
"@lens-protocol/storage-node-client": "next", | ||
"viem": "^2.21.55" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.6.3", | ||
"vite": "^5.4.11" | ||
} | ||
} |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"lib": ["ESNext", "DOM"], | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"strict": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"esModuleInterop": true, | ||
"noEmit": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["./"] | ||
} |
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
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 |
---|---|---|
@@ -1,27 +1,40 @@ | ||
import type { EnvironmentConfig } from '@lens-protocol/env'; | ||
import type { Account } from '@lens-protocol/graphql'; | ||
import type { FragmentDocumentFor } from '@lens-protocol/graphql'; | ||
import { type IStorageProvider, InMemoryStorageProvider } from '@lens-protocol/storage'; | ||
import type { ClientConfig } from './config'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export type Context = { | ||
export type Context<TAccount extends Account = Account> = { | ||
environment: EnvironmentConfig; | ||
cache: boolean; | ||
debug: boolean; | ||
origin?: string; | ||
storage: IStorageProvider; | ||
accountFragment: FragmentDocumentFor<TAccount>; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function configureContext(from: ClientConfig): Context { | ||
export type ContextFrom<TConfig extends ClientConfig> = TConfig extends ClientConfig<infer TAccount> | ||
? Context<TAccount> | ||
: never; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function configureContext<TConfig extends ClientConfig>( | ||
from: TConfig, | ||
): ContextFrom<TConfig> { | ||
return { | ||
environment: from.environment, | ||
cache: from.cache ?? false, | ||
debug: from.debug ?? false, | ||
origin: from.origin, | ||
storage: from.storage ?? new InMemoryStorageProvider(), | ||
}; | ||
accountFragment: from.accountFragment, | ||
} as ContextFrom<TConfig>; | ||
} |
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
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