-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlens.ts
66 lines (58 loc) · 1.7 KB
/
lens.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { getAddress, isAddress } from '@ethersproject/address';
import { graphQlCall, resize } from '../utils';
import { max } from '../constants.json';
import { fetchHttpImage } from './utils';
const API_URL = 'https://api-v2.lens.dev';
const LENS_IPFS_GATEWAY = 'https://gw.ipfs-lens.dev/ipfs/';
const LENS_EXTENSION = '.lens';
function normalizeImageUrl(url: string) {
if (!url) return false;
// Lens IPFS gateway is returning 403 when accessed directly
if (url.startsWith(LENS_IPFS_GATEWAY)) {
return `https://${process.env.IPFS_GATEWAY || 'cloudflare-ipfs.com'}/ipfs/${
url.split(LENS_IPFS_GATEWAY)[1]
}`;
}
}
export default async function resolve(domainOrAddress: string) {
let request: string;
if (isAddress(domainOrAddress)) {
request = `{ ownedBy: ["${getAddress(domainOrAddress)}"] }`;
} else if (domainOrAddress.endsWith(LENS_EXTENSION)) {
request = `{ handles: ["lens/${domainOrAddress.split(LENS_EXTENSION)[0]}"] }`;
} else {
return false;
}
try {
const {
data: {
data: {
profiles: { items }
}
}
} = await graphQlCall(
`${API_URL}/graphql`,
`query Profile {
profiles(request: { where: ${request}, limit: Ten }) {
items {
metadata {
picture {
... on ImageSet {
raw {
uri
}
}
}
}
}
}
}`
);
const img_url = normalizeImageUrl(items?.[0]?.metadata?.picture?.raw?.uri);
if (!img_url) return false;
const input = await fetchHttpImage(img_url);
return await resize(input, max, max);
} catch (e) {
return false;
}
}