-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpds.js
42 lines (38 loc) · 955 Bytes
/
pds.js
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
// @ts-check
import { useQuery } from '@tanstack/react-query';
/** @typedef {{ alsoKnownAs: Array<string>; service: Array<{ id: string; type: string; serviceEndpoint: string }> }} PlcRecord */
/**
*
* @param {string} did
* @returns {import('@tanstack/react-query').UseQueryResult<PlcRecord>}
*/
export function usePlcRecord(did) {
return useQuery({
enabled: !!did,
queryKey: ['plc.directory', did],
queryFn: () =>
fetch(`https://plc.directory/${encodeURIComponent(did)}`).then((x) =>
x.json()
),
});
}
/**
*
* @param {string} did
*/
export function usePdsUrl(did) {
const { status, error, data } = usePlcRecord(did);
/** @type {string | undefined} */
let pdsUrl;
if (status === 'success') {
const pds = data.service.find(
(s) => s.type === 'AtprotoPersonalDataServer' && !!s.serviceEndpoint
);
pdsUrl = pds?.serviceEndpoint;
}
return {
status,
error,
pdsUrl,
};
}