/**
* P2P apps: signalling/relay infrastructure with TURN credentials, an
* offline mailbox, origin control and custom domains.
*/
import type { Client } from './client.js';
import { checkGate } from './gate.js';
import * as http from './http.js';
import type { Body } from './types.js';
/** GET /p2p/apps — List your p2p apps. */
export const listApps = (client: Client) => http.get(client, '/p2p/apps');
/** POST /p2p/apps — Create a p2p app (the offline mailbox is off by default). */
export const createApp = (client: Client, body: Body<'/p2p/apps', 'post'>) => http.post(client, '/p2p/apps', { body });
/** GET /p2p/apps/{id} — P2P app detail. */
export const getApp = (client: Client, id: string) => http.get(client, '/p2p/apps/{id}', { params: { id } });
/** DELETE /p2p/apps/{id} — Delete a p2p app. */
export const deleteApp = (client: Client, id: string) => http.del(client, '/p2p/apps/{id}', { params: { id } });
/** PATCH /p2p/apps/{id} — Update app origins/settings/relay/mailbox. */
export const updateApp = (client: Client, id: string, body: Body<'/p2p/apps/{id}', 'patch'>) =>
http.patch(client, '/p2p/apps/{id}', { params: { id }, body });
/** POST /p2p/apps/{id}/turn-credentials — Mint TURN credentials. */
export const mintTurnCredentials = (
client: Client,
id: string,
body?: Body<'/p2p/apps/{id}/turn-credentials', 'post'>,
) => http.post(client, '/p2p/apps/{id}/turn-credentials', { params: { id }, body });
/**
* POST /p2p/apps/{id}/mailbox-credentials — Mint a short-lived bearer token
* for the app's offline mailbox (requires `mailbox_enabled`). The response's
* `credentials` carry the token and the endpoint to send it to; see
* `mailbox`.
*/
export const mintMailboxCredentials = (
client: Client,
id: string,
body?: Body<'/p2p/apps/{id}/mailbox-credentials', 'post'>,
) => http.post(client, '/p2p/apps/{id}/mailbox-credentials', { params: { id }, body });
/** GET /p2p/apps/{id}/domains — List an app's custom domains. */
export const listAppDomains = (client: Client, id: string) =>
http.get(client, '/p2p/apps/{id}/domains', { params: { id } });
/** POST /p2p/apps/{id}/domains — Attach a custom domain. Gated on `hostname`. */
export const attachDomain = async (client: Client, id: string, body: Body<'/p2p/apps/{id}/domains', 'post'>) => {
await checkGate(client, 'POST /p2p/apps/{id}/domains', { domain: body.hostname });
return http.post(client, '/p2p/apps/{id}/domains', { params: { id }, body });
};
/**
* DELETE /p2p/apps/{id}/domains/{domain-id} — Detach a custom domain. Gated
* on the domain's hostname (resolved via `listAppDomains` when a gate is set).
*/
export const detachDomain = async (client: Client, id: string, domainId: string) => {
if (client.gate) {
const { domains } = await listAppDomains(client, id);
const hostname = domains?.find((d) => String(d.id) === domainId)?.hostname;
await checkGate(client, 'DELETE /p2p/apps/{id}/domains/{domain-id}', { domain: hostname });
}
return http.del(client, '/p2p/apps/{id}/domains/{domain-id}', { params: { id, 'domain-id': domainId } });
};
/** POST /p2p/apps/{id}/domains/{domain-id}/verify-dns — Re-check a domain's DNS. */
export const verifyDomainDns = (client: Client, id: string, domainId: string) =>
http.post(client, '/p2p/apps/{id}/domains/{domain-id}/verify-dns', { params: { id, 'domain-id': domainId } });
/** GET /p2p/usage — P2P usage and cost. */
export const usage = (client: Client) => http.get(client, '/p2p/usage');