@raucheacho/phono-web 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/assets/goul-audio.wasm +0 -0
- package/assets/goul-ptt-worklets.js +256 -0
- package/dist/announcements.d.ts +23 -0
- package/dist/audio.d.ts +79 -0
- package/dist/client.d.ts +265 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +1204 -0
- package/dist/phone.d.ts +110 -0
- package/dist/react.d.ts +13 -0
- package/dist/react.js +18 -0
- package/dist/realtime.d.ts +39 -0
- package/dist/talkie.d.ts +85 -0
- package/dist/tones.d.ts +14 -0
- package/dist/types.d.ts +284 -0
- package/dist/types.js +0 -0
- package/package.json +53 -4
- package/src/announcements.ts +57 -0
- package/src/audio.ts +313 -0
- package/src/client.ts +433 -0
- package/src/index.ts +13 -0
- package/src/phone.ts +340 -0
- package/src/react.ts +34 -0
- package/src/realtime.ts +174 -0
- package/src/talkie.ts +342 -0
- package/src/tones.ts +58 -0
- package/src/types.ts +339 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { Phone, type PhoneOptions } from "./phone";
|
|
2
|
+
import { Realtime } from "./realtime";
|
|
3
|
+
import { Talkie } from "./talkie";
|
|
4
|
+
import type {
|
|
5
|
+
Activity,
|
|
6
|
+
Announcement,
|
|
7
|
+
Announcements,
|
|
8
|
+
Group,
|
|
9
|
+
SearchHit,
|
|
10
|
+
Transfer,
|
|
11
|
+
AudioCredentials,
|
|
12
|
+
Call,
|
|
13
|
+
CallEnvelope,
|
|
14
|
+
ContactRequest,
|
|
15
|
+
ContactRequests,
|
|
16
|
+
Contacts,
|
|
17
|
+
Conversation,
|
|
18
|
+
LookedUpHandle,
|
|
19
|
+
Me,
|
|
20
|
+
Message,
|
|
21
|
+
PublicHandle,
|
|
22
|
+
Relation,
|
|
23
|
+
Channel,
|
|
24
|
+
Transmission,
|
|
25
|
+
} from "./types";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* D'où vient le jeton : une chaîne, ou une fonction qui en demande un à votre
|
|
29
|
+
* backend (`POST /v1/identities/{ref}/tokens`, côté serveur). La fonction est
|
|
30
|
+
* rappelée avant l'expiration du jeton, et après un refus.
|
|
31
|
+
*/
|
|
32
|
+
export type TokenSource = string | (() => string | Promise<string>);
|
|
33
|
+
|
|
34
|
+
export interface PhonoOptions {
|
|
35
|
+
/** L'URL de l'API Phono, sans /v1. */
|
|
36
|
+
baseUrl: string;
|
|
37
|
+
/** Un jeton client `pht_…` (ou toute clé Phono). */
|
|
38
|
+
token?: TokenSource;
|
|
39
|
+
/** En-têtes ajoutés à chaque requête. */
|
|
40
|
+
headers?: Record<string, string>;
|
|
41
|
+
/** `include` pour une session par cookie plutôt qu'un jeton. */
|
|
42
|
+
credentials?: RequestCredentials;
|
|
43
|
+
/** Où le navigateur trouve le cœur audio : voir le README. */
|
|
44
|
+
audio?: AudioAssets;
|
|
45
|
+
fetch?: typeof fetch;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface AudioAssets {
|
|
49
|
+
/** `goul-ptt-worklets.js`. Par défaut `/goul-ptt-worklets.js`. */
|
|
50
|
+
workletUrl?: string;
|
|
51
|
+
/** `goul-audio.wasm`. Par défaut `/goul-audio.wasm`. */
|
|
52
|
+
wasmUrl?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Une erreur de l'API : `code` est stable (voir la référence des erreurs). */
|
|
56
|
+
export class PhonoError extends Error {
|
|
57
|
+
constructor(
|
|
58
|
+
readonly status: number,
|
|
59
|
+
readonly code: string,
|
|
60
|
+
message: string,
|
|
61
|
+
/** Un appel qui n'aboutit pas : l'annonce d'opérateur à jouer. */
|
|
62
|
+
readonly announcement?: Announcement
|
|
63
|
+
) {
|
|
64
|
+
super(message);
|
|
65
|
+
this.name = "PhonoError";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Un jeton est renouvelé quand il lui reste moins que cela. */
|
|
70
|
+
const REFRESH_MARGIN_MS = 60_000;
|
|
71
|
+
|
|
72
|
+
/** L'expiration d'un jeton client (un JWT derrière `pht_`), si on peut la lire. */
|
|
73
|
+
function expiryOf(token: string): number | null {
|
|
74
|
+
const jwt = token.startsWith("pht_") ? token.slice(4) : null;
|
|
75
|
+
const payload = jwt?.split(".")[1];
|
|
76
|
+
if (!payload) return null;
|
|
77
|
+
try {
|
|
78
|
+
const json = JSON.parse(atob(payload.replace(/-/g, "+").replace(/_/g, "/")));
|
|
79
|
+
return typeof json.exp === "number" ? json.exp * 1000 : null;
|
|
80
|
+
} catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Le client Phono : l'API `/v1` au nom d'une identité.
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* const phono = new Phono({
|
|
90
|
+
* baseUrl: "https://phonoapi.raucheacho.com",
|
|
91
|
+
* token: () => fetch("/api/phono-token").then((r) => r.text()),
|
|
92
|
+
* });
|
|
93
|
+
* await phono.messages.send({ to: "jean.acme", text: "Bonjour" });
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export class Phono {
|
|
97
|
+
readonly options: PhonoOptions;
|
|
98
|
+
private token: string | null = null;
|
|
99
|
+
private tokenExpiry: number | null = null;
|
|
100
|
+
private pendingToken: Promise<string> | null = null;
|
|
101
|
+
private realtimeInstance: Realtime | null = null;
|
|
102
|
+
|
|
103
|
+
constructor(options: PhonoOptions) {
|
|
104
|
+
this.options = { ...options, baseUrl: options.baseUrl.replace(/\/+$/, "") };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// --- les requêtes -----------------------------------------------------------
|
|
108
|
+
|
|
109
|
+
/** Une requête à l'API, authentifiée. `path` commence par `/`. */
|
|
110
|
+
async request<T>(path: string, init: RequestInit = {}): Promise<T> {
|
|
111
|
+
const res = await this.send(path, init, false);
|
|
112
|
+
if (res.status === 204) return undefined as T;
|
|
113
|
+
return res.json() as Promise<T>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private async send(path: string, init: RequestInit, retried: boolean): Promise<Response> {
|
|
117
|
+
const headers = new Headers(this.options.headers);
|
|
118
|
+
new Headers(init.headers).forEach((v, k) => headers.set(k, v));
|
|
119
|
+
if (init.body && !(init.body instanceof FormData) && !headers.has("Content-Type")) {
|
|
120
|
+
headers.set("Content-Type", "application/json");
|
|
121
|
+
}
|
|
122
|
+
const token = await this.currentToken(false);
|
|
123
|
+
if (token) headers.set("Authorization", `Bearer ${token}`);
|
|
124
|
+
|
|
125
|
+
const doFetch = this.options.fetch ?? fetch;
|
|
126
|
+
const res = await doFetch(`${this.options.baseUrl}${path}`, {
|
|
127
|
+
...init,
|
|
128
|
+
headers,
|
|
129
|
+
credentials: this.options.credentials ?? init.credentials,
|
|
130
|
+
});
|
|
131
|
+
// Un jeton refusé : peut-être expiré entre-temps. Un seul nouvel essai,
|
|
132
|
+
// avec un jeton neuf, si la source sait en fournir.
|
|
133
|
+
if (res.status === 401 && !retried && typeof this.options.token === "function") {
|
|
134
|
+
await this.currentToken(true);
|
|
135
|
+
return this.send(path, init, true);
|
|
136
|
+
}
|
|
137
|
+
if (!res.ok) {
|
|
138
|
+
const body = (await res.json().catch(() => null)) as {
|
|
139
|
+
error?: string;
|
|
140
|
+
message?: string;
|
|
141
|
+
announcement?: Announcement;
|
|
142
|
+
} | null;
|
|
143
|
+
throw new PhonoError(
|
|
144
|
+
res.status,
|
|
145
|
+
body?.error ?? "unknown",
|
|
146
|
+
body?.message ?? `HTTP ${res.status}`,
|
|
147
|
+
body?.announcement
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
return res;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private async currentToken(force: boolean): Promise<string | null> {
|
|
154
|
+
const source = this.options.token;
|
|
155
|
+
if (source === undefined) return null;
|
|
156
|
+
if (typeof source === "string") return source;
|
|
157
|
+
|
|
158
|
+
const fresh =
|
|
159
|
+
this.token && (this.tokenExpiry === null || this.tokenExpiry - Date.now() > REFRESH_MARGIN_MS);
|
|
160
|
+
if (fresh && !force) return this.token;
|
|
161
|
+
|
|
162
|
+
// Une seule demande à la fois, même si dix requêtes partent ensemble.
|
|
163
|
+
this.pendingToken ??= Promise.resolve(source())
|
|
164
|
+
.then((t) => {
|
|
165
|
+
this.token = t.trim();
|
|
166
|
+
this.tokenExpiry = expiryOf(this.token);
|
|
167
|
+
return this.token;
|
|
168
|
+
})
|
|
169
|
+
.finally(() => {
|
|
170
|
+
this.pendingToken = null;
|
|
171
|
+
});
|
|
172
|
+
return this.pendingToken;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// --- les ressources -----------------------------------------------------------
|
|
176
|
+
|
|
177
|
+
/** L'identité au nom de laquelle ce client agit. */
|
|
178
|
+
me(): Promise<Me> {
|
|
179
|
+
return this.request("/v1/me");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Les annonces d'opérateur à jouer quand un appel n'aboutit pas : celles de
|
|
184
|
+
* votre organisation, sinon celles de Phono. `lang` : `fr`, `en`…
|
|
185
|
+
*/
|
|
186
|
+
announcements(lang?: string): Promise<Announcements> {
|
|
187
|
+
return this.request(`/v1/announcements${lang ? `?lang=${encodeURIComponent(lang)}` : ""}`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Une identité, par son identifiant (`jean.acme`). */
|
|
191
|
+
lookup(handle: string): Promise<LookedUpHandle> {
|
|
192
|
+
return this.request(`/v1/handles/${encodeURIComponent(handle.replace(/^@/, ""))}`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
contacts(): Promise<Contacts> {
|
|
196
|
+
return this.request("/v1/contacts");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Les demandes de contact et le blocage. Hors de votre organisation, écrire
|
|
201
|
+
* ou appeler exige un contact établi : sinon l'API répond `contact_required`.
|
|
202
|
+
*/
|
|
203
|
+
readonly contactRequests = {
|
|
204
|
+
list: (): Promise<ContactRequests> => this.request("/v1/contact-requests"),
|
|
205
|
+
|
|
206
|
+
/** Demander à joindre `to`. Rend `outgoing`, ou `contact` s'il vous avait déjà demandé. */
|
|
207
|
+
send: (input: { to: string; note?: string }): Promise<{ handle: PublicHandle; relation: Relation }> =>
|
|
208
|
+
this.request("/v1/contact-requests", { method: "POST", body: JSON.stringify(input) }),
|
|
209
|
+
|
|
210
|
+
accept: (handle: string): Promise<{ handle: PublicHandle; relation: Relation }> =>
|
|
211
|
+
this.request(`/v1/contact-requests/${encodeURIComponent(bare(handle))}/accept`, { method: "POST" }),
|
|
212
|
+
|
|
213
|
+
/** Refuser : l'autre n'en est pas prévenu. */
|
|
214
|
+
decline: (handle: string): Promise<void> =>
|
|
215
|
+
this.request(`/v1/contact-requests/${encodeURIComponent(bare(handle))}/decline`, { method: "POST" }),
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/** Retirer un contact, ou annuler sa demande. */
|
|
219
|
+
removeContact(handle: string): Promise<void> {
|
|
220
|
+
return this.request(`/v1/contacts/${encodeURIComponent(bare(handle))}`, { method: "DELETE" });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
readonly blocks = {
|
|
224
|
+
list: (): Promise<ContactRequest[]> => this.request("/v1/blocks"),
|
|
225
|
+
/** Plus rien de `handle` : ni message, ni appel, ni demande. Il n'en sait rien. */
|
|
226
|
+
add: (handle: string): Promise<void> =>
|
|
227
|
+
this.request(`/v1/blocks/${encodeURIComponent(bare(handle))}`, { method: "PUT" }),
|
|
228
|
+
remove: (handle: string): Promise<void> =>
|
|
229
|
+
this.request(`/v1/blocks/${encodeURIComponent(bare(handle))}`, { method: "DELETE" }),
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
readonly messages = {
|
|
233
|
+
/** Un texte, à `to` ou dans un `group` ; `reply_to` pour répondre à un message précis. */
|
|
234
|
+
/** `urgent` : passe la sourdine et le mode Concentration — dans l'organisation seulement. */
|
|
235
|
+
send: (input: { to?: string; group?: string; text: string; reply_to?: string; urgent?: boolean }): Promise<Message> =>
|
|
236
|
+
this.request("/v1/messages", { method: "POST", body: JSON.stringify(input) }),
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Un vocal : Ogg Opus tel quel, ou ce que produit le navigateur
|
|
240
|
+
* (MediaRecorder : WebM, MP4) — Phono le convertit.
|
|
241
|
+
*/
|
|
242
|
+
sendVoice: (input: { to?: string; group?: string; audio: Blob; reply_to?: string }): Promise<Message> => {
|
|
243
|
+
const form = new FormData();
|
|
244
|
+
if (input.group) form.set("group", input.group);
|
|
245
|
+
else if (input.to) form.set("to", input.to);
|
|
246
|
+
if (input.reply_to) form.set("reply_to", input.reply_to);
|
|
247
|
+
const ext = input.audio.type.includes("mp4") ? "m4a" : input.audio.type.includes("webm") ? "webm" : "ogg";
|
|
248
|
+
form.set("audio", input.audio, `voice.${ext}`);
|
|
249
|
+
return this.request("/v1/messages/voice", { method: "POST", body: form });
|
|
250
|
+
},
|
|
251
|
+
|
|
252
|
+
/** Une photo (affichée dans le fil) ou un document, 25 Mo au plus. */
|
|
253
|
+
sendFile: (input: {
|
|
254
|
+
to?: string;
|
|
255
|
+
group?: string;
|
|
256
|
+
file: Blob;
|
|
257
|
+
filename: string;
|
|
258
|
+
caption?: string;
|
|
259
|
+
reply_to?: string;
|
|
260
|
+
width?: number;
|
|
261
|
+
height?: number;
|
|
262
|
+
}): Promise<Message> => {
|
|
263
|
+
const form = new FormData();
|
|
264
|
+
if (input.group) form.set("group", input.group);
|
|
265
|
+
else if (input.to) form.set("to", input.to);
|
|
266
|
+
if (input.caption) form.set("caption", input.caption);
|
|
267
|
+
if (input.reply_to) form.set("reply_to", input.reply_to);
|
|
268
|
+
if (input.width) form.set("width", String(Math.round(input.width)));
|
|
269
|
+
if (input.height) form.set("height", String(Math.round(input.height)));
|
|
270
|
+
form.set("file", input.file, input.filename);
|
|
271
|
+
return this.request("/v1/messages/attachment", { method: "POST", body: form });
|
|
272
|
+
},
|
|
273
|
+
|
|
274
|
+
/** Une position, une fois — pas un suivi. */
|
|
275
|
+
sendLocation: (input: { to?: string; group?: string; lat: number; lng: number; label?: string; reply_to?: string }): Promise<Message> =>
|
|
276
|
+
this.request("/v1/messages/location", { method: "POST", body: JSON.stringify(input) }),
|
|
277
|
+
|
|
278
|
+
/** Corrige l'un de vos textes, dans les 15 minutes. */
|
|
279
|
+
edit: (messageId: string, text: string): Promise<Message> =>
|
|
280
|
+
this.request(`/v1/messages/${messageId}`, { method: "PATCH", body: JSON.stringify({ text }) }),
|
|
281
|
+
|
|
282
|
+
/** Supprime l'un de vos messages pour tout le monde. */
|
|
283
|
+
remove: (messageId: string): Promise<Message> => this.request(`/v1/messages/${messageId}`, { method: "DELETE" }),
|
|
284
|
+
|
|
285
|
+
/** Cherche dans vos conversations (textes et transcriptions). */
|
|
286
|
+
search: (q: string, limit = 30): Promise<SearchHit[]> =>
|
|
287
|
+
this.request(`/v1/messages/search?${query({ q, limit })}`),
|
|
288
|
+
|
|
289
|
+
/** Le fil d'un groupe. */
|
|
290
|
+
group: (groupId: string, input: { before?: string; limit?: number } = {}): Promise<{ conversation_id: string; messages: Message[] }> =>
|
|
291
|
+
this.request(`/v1/messages?${query({ group: groupId, ...input })}`),
|
|
292
|
+
|
|
293
|
+
/** La conversation avec `with`, la plus récente d'abord. */
|
|
294
|
+
list: (input: {
|
|
295
|
+
with: string;
|
|
296
|
+
before?: string;
|
|
297
|
+
limit?: number;
|
|
298
|
+
}): Promise<{ conversation_id: string; messages: Message[] }> =>
|
|
299
|
+
this.request(`/v1/messages?${query(input)}`),
|
|
300
|
+
|
|
301
|
+
/** Pose (ou remplace) ma réaction — un emoji — sur un message. */
|
|
302
|
+
react: (messageId: string, emoji: string): Promise<Message> =>
|
|
303
|
+
this.request(`/v1/messages/${messageId}/reaction`, { method: "PUT", body: JSON.stringify({ emoji }) }),
|
|
304
|
+
unreact: (messageId: string): Promise<Message> =>
|
|
305
|
+
this.request(`/v1/messages/${messageId}/reaction`, { method: "DELETE" }),
|
|
306
|
+
|
|
307
|
+
/** « J'écris », « j'enregistre un vocal », « j'ai fini » : à répéter toutes les 3 s tant que ça dure. */
|
|
308
|
+
activity: (input: { to?: string; group?: string; state: Activity }): Promise<void> =>
|
|
309
|
+
this.request("/v1/activity", { method: "POST", body: JSON.stringify(input) }),
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
readonly conversations = {
|
|
313
|
+
list: (input: { limit?: number; archived?: boolean } = {}): Promise<Conversation[]> =>
|
|
314
|
+
this.request(`/v1/conversations?${query(input)}`),
|
|
315
|
+
markRead: (conversationId: string): Promise<{ marked: number }> =>
|
|
316
|
+
this.request(`/v1/conversations/${conversationId}/read`, { method: "POST" }),
|
|
317
|
+
/** Vous avez reçu les messages (✓✓ chez l'auteur). */
|
|
318
|
+
delivered: (conversationId: string): Promise<{ delivered: number }> =>
|
|
319
|
+
this.request(`/v1/conversations/${conversationId}/delivered`, { method: "POST" }),
|
|
320
|
+
mute: (conversationId: string, until?: string): Promise<void> =>
|
|
321
|
+
this.request(`/v1/conversations/${conversationId}/mute`, { method: "PUT", body: JSON.stringify(until ? { until } : {}) }),
|
|
322
|
+
unmute: (conversationId: string): Promise<void> =>
|
|
323
|
+
this.request(`/v1/conversations/${conversationId}/mute`, { method: "DELETE" }),
|
|
324
|
+
archive: (conversationId: string, archived = true): Promise<void> =>
|
|
325
|
+
this.request(`/v1/conversations/${conversationId}/archive`, { method: archived ? "PUT" : "DELETE" }),
|
|
326
|
+
/** Passe votre conversation avec `with` à `to`, de votre organisation. */
|
|
327
|
+
transfer: (input: { with: string; to: string; note?: string }): Promise<Transfer> =>
|
|
328
|
+
this.request("/v1/transfers", { method: "POST", body: JSON.stringify(input) }),
|
|
329
|
+
transfers: (withHandle: string): Promise<Transfer[]> => this.request(`/v1/transfers?${query({ with: withHandle })}`),
|
|
330
|
+
transferHistory: (id: string): Promise<{ messages: Message[] }> => this.request(`/v1/transfers/${id}/messages`),
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
/** Les petits groupes fermés : 12 membres au plus, joignables par vous. */
|
|
334
|
+
readonly groups = {
|
|
335
|
+
create: (input: { title: string; members: string[] }): Promise<Group> =>
|
|
336
|
+
this.request("/v1/groups", { method: "POST", body: JSON.stringify(input) }),
|
|
337
|
+
get: (groupId: string): Promise<Group> => this.request(`/v1/groups/${groupId}`),
|
|
338
|
+
rename: (groupId: string, title: string): Promise<Group> =>
|
|
339
|
+
this.request(`/v1/groups/${groupId}`, { method: "PATCH", body: JSON.stringify({ title }) }),
|
|
340
|
+
add: (groupId: string, handle: string): Promise<Group> =>
|
|
341
|
+
this.request(`/v1/groups/${groupId}/members`, { method: "POST", body: JSON.stringify({ handle }) }),
|
|
342
|
+
remove: (groupId: string, handle: string): Promise<void> =>
|
|
343
|
+
this.request(`/v1/groups/${groupId}/members/${encodeURIComponent(bare(handle))}`, { method: "DELETE" }),
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
readonly calls = {
|
|
347
|
+
/** Faire sonner `to`. Pour l'audio dans le navigateur, préférez `phone().dial()`. */
|
|
348
|
+
create: (input: { to: string } | { connect: [string, string] }): Promise<CallEnvelope> =>
|
|
349
|
+
this.request("/v1/calls", { method: "POST", body: JSON.stringify(input) }),
|
|
350
|
+
get: (callId: string): Promise<Call> => this.request(`/v1/calls/${callId}`),
|
|
351
|
+
list: (input: { limit?: number } = {}): Promise<Call[]> => this.request(`/v1/calls?${query(input)}`),
|
|
352
|
+
answer: (callId: string): Promise<CallEnvelope> =>
|
|
353
|
+
this.request(`/v1/calls/${callId}/answer`, { method: "POST" }),
|
|
354
|
+
reject: (callId: string): Promise<CallEnvelope> =>
|
|
355
|
+
this.request(`/v1/calls/${callId}/reject`, { method: "POST" }),
|
|
356
|
+
hangup: (callId: string): Promise<CallEnvelope> =>
|
|
357
|
+
this.request(`/v1/calls/${callId}/hangup`, { method: "POST" }),
|
|
358
|
+
/** De nouveaux identifiants audio pour un appel en cours (reconnexion). */
|
|
359
|
+
audio: (callId: string): Promise<AudioCredentials> =>
|
|
360
|
+
this.request(`/v1/calls/${callId}/audio`, { method: "POST" }),
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/** Une URL lisible par un `<audio>` pour un vocal ou un enregistrement. */
|
|
364
|
+
async mediaUrl(mediaId: string, speed?: 1.5 | 2): Promise<string> {
|
|
365
|
+
const res = await this.send(`/v1/media/${mediaId}${speed ? `?speed=${speed}` : ""}`, {}, false);
|
|
366
|
+
return URL.createObjectURL(await res.blob());
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** Les événements de cette identité, en direct. Une seule connexion par client. */
|
|
370
|
+
realtime(): Realtime {
|
|
371
|
+
this.realtimeInstance ??= new Realtime(this);
|
|
372
|
+
return this.realtimeInstance;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** Un téléphone : sonner, appeler, décrocher, parler. */
|
|
376
|
+
phone(options?: PhoneOptions): Phone {
|
|
377
|
+
return new Phone(this, options);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Le talkie d'un canal : écouter, appuyer pour parler. */
|
|
381
|
+
talkie(channelId: string): Talkie {
|
|
382
|
+
return new Talkie(this, channelId, this.options.audio);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Les canaux de talkie : fermés, dans l'organisation, une voix à la fois. */
|
|
386
|
+
readonly channels = {
|
|
387
|
+
list: (): Promise<Channel[]> => this.request("/v1/channels"),
|
|
388
|
+
get: (id: string): Promise<Channel> => this.request(`/v1/channels/${encodeURIComponent(id)}`),
|
|
389
|
+
create: (input: { name: string; members: string[]; record?: boolean }): Promise<Channel> =>
|
|
390
|
+
this.request("/v1/channels", { method: "POST", body: JSON.stringify(input) }),
|
|
391
|
+
update: (id: string, input: { name?: string; record?: boolean }): Promise<Channel> =>
|
|
392
|
+
this.request(`/v1/channels/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify(input) }),
|
|
393
|
+
add: (id: string, handle: string): Promise<Channel> =>
|
|
394
|
+
this.request(`/v1/channels/${encodeURIComponent(id)}/members`, { method: "POST", body: JSON.stringify({ handle }) }),
|
|
395
|
+
/** Retire un membre ; avec son propre identifiant, on part. */
|
|
396
|
+
remove: (id: string, handle: string): Promise<Channel | undefined> =>
|
|
397
|
+
this.request(`/v1/channels/${encodeURIComponent(id)}/members/${encodeURIComponent(handle.replace(/^@/, ""))}`, {
|
|
398
|
+
method: "DELETE",
|
|
399
|
+
}),
|
|
400
|
+
delete: (id: string): Promise<void> => this.request(`/v1/channels/${encodeURIComponent(id)}`, { method: "DELETE" }),
|
|
401
|
+
/** Accepter l'invitation à un canal (`channel.invited`) : on l'entend désormais. */
|
|
402
|
+
accept: (id: string): Promise<Channel> =>
|
|
403
|
+
this.request(`/v1/channels/${encodeURIComponent(id)}/accept`, { method: "POST" }),
|
|
404
|
+
/** Refuser l'invitation. */
|
|
405
|
+
decline: (id: string): Promise<void> =>
|
|
406
|
+
this.request(`/v1/channels/${encodeURIComponent(id)}/decline`, { method: "POST" }),
|
|
407
|
+
/** Les prises de parole, la plus récente d'abord. */
|
|
408
|
+
transmissions: (id: string, options: { limit?: number; before?: string } = {}): Promise<Transmission[]> => {
|
|
409
|
+
const q = new URLSearchParams({ limit: String(options.limit ?? 50) });
|
|
410
|
+
if (options.before) q.set("before", options.before);
|
|
411
|
+
return this.request(`/v1/channels/${encodeURIComponent(id)}/transmissions?${q}`);
|
|
412
|
+
},
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/** Coupe le temps réel. */
|
|
416
|
+
close(): void {
|
|
417
|
+
this.realtimeInstance?.close();
|
|
418
|
+
this.realtimeInstance = null;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function query(input: object): string {
|
|
423
|
+
const params = new URLSearchParams();
|
|
424
|
+
for (const [k, v] of Object.entries(input)) {
|
|
425
|
+
if (v !== undefined && v !== null) params.set(k, String(v));
|
|
426
|
+
}
|
|
427
|
+
return params.toString();
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** « @alice » ou « alice » : l'API attend l'identifiant sans arobase. */
|
|
431
|
+
function bare(handle: string): string {
|
|
432
|
+
return handle.replace(/^@/, "");
|
|
433
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { Phono, PhonoError, type AudioAssets, type PhonoOptions, type TokenSource } from "./client";
|
|
2
|
+
export { Phone, type PhoneCall, type PhoneOptions, type PhoneState } from "./phone";
|
|
3
|
+
export { Realtime, type RealtimeStatus } from "./realtime";
|
|
4
|
+
export { Talkie, type TalkieSnapshot, type TalkieState } from "./talkie";
|
|
5
|
+
export { CallAudio, type CallAudioSnapshot, type TalkMode } from "./audio";
|
|
6
|
+
export { ringback, ringtone } from "./tones";
|
|
7
|
+
export {
|
|
8
|
+
announcementForEnd,
|
|
9
|
+
announcementForError,
|
|
10
|
+
playAnnouncement,
|
|
11
|
+
type AnnouncementFiles,
|
|
12
|
+
} from "./announcements";
|
|
13
|
+
export type * from "./types";
|