on a64ee97 help sign in
90 lines 2.5 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 function scry<T = any>(path: string): Promise<T> {
14 return api.scry<T>({ app: 'astrolabe', path });
17export function getPoint(patp: string): Promise<any> {
18 return scry<any>(`/point/${patp}`);
21export function getSpawnedPoints(patp: string): Promise<any> {
22 return scry<any>(`/point/${patp}/spawned`);
25export function getPeers(): Promise<any> {
26 return scry<any>(`/peers`);
29export async function searchPoints(search: string): Promise<Patp[]> {
30 const { points } = await scry<any>(`/search/${search}`);
31 return points.map(normalizeId);
34export function getChartData(): Promise<any> {
35 return scry<any>(`/chart-data`);
38export function getDoc(path: string): Promise<string> {
39 return scry<string>(`/doc/${path}`);
42export function initialContacts(json: ContactUpdate, contacts: Rolodex): Rolodex {
43 const data = _get(json, 'initial', false);
44 if (data) {
45 contacts = data.rolodex;
46 }
47 return contacts;
48};
50export function addContact(json: ContactUpdate, contacts: Rolodex): Rolodex {
51 const data = _get(json, 'add', false);
52 if (data) {
53 contacts[data.ship] = data.contact;
54 }
55 return contacts;
56};
58export function removeContact(json: ContactUpdate, contacts: Rolodex): Rolodex {
59 const data = _get(json, 'remove', false);
60 if (
61 data &&
62 (data.ship in contacts)
63 ) {
64 delete contacts[data.ship];
65 }
66 return contacts;
67};
69export function handleContactUpdateEvent(updateContacts) {
70 return ({ 'contact-update': contactUpdate }) => {
71 console.log(`received contact-update: ${Object.keys(contactUpdate)}`)
72 const reducers = [initialContacts, addContact, removeContact];
73 const reducer = compose(reducers.map(r => sta => r(contactUpdate, sta)));
74 updateContacts(reducer);
75 };
78export function subscribeToContacts(e: (data: any) => void): Promise<any> {
79 return api.subscribe({
80 app: 'contact-store',
81 path: '/all',
82 event: handleContactUpdateEvent(e),
83 err: () => {
84 console.error(`Subscription to contact-store/all just get "err"`);
85 },
86 quit: () => {
87 console.error(`Subscription to contact-store/all just get "quit"`)
88 }
89 })