on 4e38358 help sign in
94 lines 2.6 kB history raw
1import UrbitApi from '@urbit/http-api';
2import type { ContactUpdate, Patp, Group, Contact, Rolodex } from '@urbit/api';
3import _get from 'lodash/get';
4import { compose } from 'lodash/fp';
6import { normalizeId } from './id';
8console.log(`Initializing Urbit API at ${Date()}`);
9const api: UrbitApi = new UrbitApi('', '', window.desk);
10api.ship = window.ship;
11// api.connect();
13export { api };
15export function scry<T = any>(path: string): Promise<T> {
16 return api.scry<T>({ app: 'astrolabe', path });
19export function getPoint(patp: string): Promise<any> {
20 return scry<any>(`/point/${patp}`);
23export function getSpawnedPoints(patp: string): Promise<any> {
24 return scry<any>(`/point/${patp}/spawned`);
27export function getPeers(): Promise<any> {
28 return scry<any>(`/peers`);
31export async function searchPoints(search: string, mode: ('patp' | 'sigil')): Promise<Patp[]> {
32 const { points } = await scry<any>(`/search/${mode}/${search}`);
33 return points.map(normalizeId);
36export function getChartData(old = false): Promise<any> {
37 return scry<any>('/chart-data' + (old ? '-old' : ''));
40window['gcd'] = getChartData;
42export function getDoc(path: string): Promise<string> {
43 return scry<string>(`/doc/${path}`);
46export function initialContacts(json: ContactUpdate, contacts: Rolodex): Rolodex {
47 const data = _get(json, 'initial', false);
48 if (data) {
49 contacts = data.rolodex;
50 }
51 return contacts;
52};
54export function addContact(json: ContactUpdate, contacts: Rolodex): Rolodex {
55 const data = _get(json, 'add', false);
56 if (data) {
57 contacts[data.ship] = data.contact;
58 }
59 return contacts;
60};
62export function removeContact(json: ContactUpdate, contacts: Rolodex): Rolodex {
63 const data = _get(json, 'remove', false);
64 if (
65 data &&
66 (data.ship in contacts)
67 ) {
68 delete contacts[data.ship];
69 }
70 return contacts;
71};
73export function handleContactUpdateEvent(updateContacts) {
74 return ({ 'contact-update': contactUpdate }) => {
75 console.log(`received contact-update: ${Object.keys(contactUpdate)}`)
76 const reducers = [initialContacts, addContact, removeContact];
77 const reducer = compose(reducers.map(r => sta => r(contactUpdate, sta)));
78 updateContacts(reducer);
79 };
82export function subscribeToContacts(e: (data: any) => void): Promise<any> {
83 return api.subscribe({
84 app: 'contact-store',
85 path: '/all',
86 event: handleContactUpdateEvent(e),
87 err: () => {
88 console.error(`Subscription to contact-store/all just get "err"`);
89 },
90 quit: () => {
91 console.error(`Subscription to contact-store/all just get "quit"`)
92 }
93 })