@tryprojectblue/dialer 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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +176 -0
- package/dist/index.cjs +568 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +328 -0
- package/dist/index.d.ts +328 -0
- package/dist/index.js +535 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Project Blue
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# @tryprojectblue/dialer
|
|
2
|
+
|
|
3
|
+
Browser SDK for placing and receiving calls on your Project Blue lines.
|
|
4
|
+
|
|
5
|
+
Your workspace API key never reaches the browser. Your server exchanges it for
|
|
6
|
+
a short-lived token scoped to one line; the SDK takes a `tokenProvider` that
|
|
7
|
+
calls your server, and handles refresh, call lifecycle and usage limits.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install @tryprojectblue/dialer
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 1. Your server: mint a token
|
|
14
|
+
|
|
15
|
+
`POST https://app.tryprojectblue.com/api/v1/dialer/token` with the workspace
|
|
16
|
+
API key from **Settings → API Keys** and the `lineId` of the line to call from
|
|
17
|
+
(`GET /get-lines` on the messaging API returns your line ids).
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// Node / any server runtime. Never ship PROJECT_BLUE_API_KEY to a browser.
|
|
21
|
+
app.post('/dialer-token', requireLogin, async (req, res) => {
|
|
22
|
+
const upstream = await fetch('https://app.tryprojectblue.com/api/v1/dialer/token', {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: {
|
|
25
|
+
Authorization: `Bearer ${process.env.PROJECT_BLUE_API_KEY}`,
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
},
|
|
28
|
+
body: JSON.stringify({ lineId: req.user.projectBlueLineId }),
|
|
29
|
+
});
|
|
30
|
+
// Forward status and body as-is; the SDK understands both.
|
|
31
|
+
res.status(upstream.status).json(await upstream.json());
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Success body:
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"token": "eyJ…",
|
|
40
|
+
"expiresAt": "2026-09-01T12:15:00.000Z",
|
|
41
|
+
"ttlSeconds": 900,
|
|
42
|
+
"lineId": "0b3e0c59-ff0b-4110-faf6-6f976339a8c8",
|
|
43
|
+
"inboundEnabled": true,
|
|
44
|
+
"maxCallsPerToken": 10,
|
|
45
|
+
"allowance": {
|
|
46
|
+
"state": "ok",
|
|
47
|
+
"used": 30,
|
|
48
|
+
"included": 180,
|
|
49
|
+
"cap": 540,
|
|
50
|
+
"pct": 16,
|
|
51
|
+
"periodEnd": "2026-09-02T00:00:00.000Z",
|
|
52
|
+
"line": { "used": 30, "cap": 540, "capped": false }
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Refusals use `{ error, code }` with these codes: `invalid_api_key` (401),
|
|
58
|
+
`rate_limited` (429, `retryAfterSeconds`), `invalid_line_id` / `line_not_found`
|
|
59
|
+
(400/404), `line_inbound_only` / `line_not_eligible` (403),
|
|
60
|
+
`dialer_subscription_required` (403), `account_paywalled` (402),
|
|
61
|
+
`dialer_cap_reached` (402, with `used`, `included`, `cap`, `periodEnd`),
|
|
62
|
+
`dialer_not_provisioned` (404).
|
|
63
|
+
|
|
64
|
+
## 2. Your browser code
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { ProjectBlueDialer, mintErrorFromResponse } from '@tryprojectblue/dialer';
|
|
68
|
+
|
|
69
|
+
const dialer = new ProjectBlueDialer({
|
|
70
|
+
async tokenProvider() {
|
|
71
|
+
const res = await fetch('/dialer-token', { method: 'POST' });
|
|
72
|
+
const body = await res.json();
|
|
73
|
+
const error = mintErrorFromResponse(res.status, body);
|
|
74
|
+
if (error) throw error;
|
|
75
|
+
return body;
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
dialer.on('allowance', (allowance) => {
|
|
80
|
+
if (allowance.state === 'soft') showBanner(`${allowance.pct}% of today's minutes used`);
|
|
81
|
+
});
|
|
82
|
+
dialer.on('capReached', (error) => showBanner(error.message));
|
|
83
|
+
dialer.on('incoming', (call) => {
|
|
84
|
+
showIncomingUi(call.remoteNumber, call.callerName, {
|
|
85
|
+
answer: () => call.accept(),
|
|
86
|
+
decline: () => call.reject(),
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
await dialer.start();
|
|
91
|
+
|
|
92
|
+
const call = await dialer.call('+15551234567');
|
|
93
|
+
call.on('accepted', () => setStatus('Connected'));
|
|
94
|
+
call.on('disconnected', () => setStatus('Ended'));
|
|
95
|
+
muteButton.onclick = () => call.mute(!call.isMuted());
|
|
96
|
+
hangupButton.onclick = () => call.hangup();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## API
|
|
100
|
+
|
|
101
|
+
### `new ProjectBlueDialer(options)`
|
|
102
|
+
|
|
103
|
+
| option | |
|
|
104
|
+
| --- | --- |
|
|
105
|
+
| `tokenProvider` | **required.** Returns the mint response body (or a token string). |
|
|
106
|
+
| `refreshLeadSeconds` | Refresh this long before expiry. Default `60`. |
|
|
107
|
+
| `logger` | `console`-like object, or `null` to silence. |
|
|
108
|
+
| `edge` | Media region hint. Leave unset unless measured. |
|
|
109
|
+
| `playIncomingRingtone` | Default `true`. |
|
|
110
|
+
|
|
111
|
+
Methods: `start()`, `call(to)` (`to` must be E.164 **with** the leading `+`; the SDK never guesses a country code), `refreshToken()`, `destroy()`, `isReady()`.
|
|
112
|
+
Getters: `allowance`, `inboundEnabled`, `activeCall`.
|
|
113
|
+
Events: `ready`, `allowance`, `capReached`, `incoming`, `tokenRefreshFailed`,
|
|
114
|
+
`error`, `destroyed`.
|
|
115
|
+
|
|
116
|
+
### `DialerCall`
|
|
117
|
+
|
|
118
|
+
Fields: `direction`, `remoteNumber`, `callerName`, `lineName`.
|
|
119
|
+
Methods: `accept()`, `reject()`, `hangup()`, `mute(bool)`, `isMuted()`,
|
|
120
|
+
`sendDigits(digits)`, `status()`, `isActive()`.
|
|
121
|
+
Events: `ringing`, `accepted`, `disconnected`, `cancelled`, `rejected`,
|
|
122
|
+
`error`, `reconnecting`, `reconnected`, `mute`, `warning`, `warningCleared`.
|
|
123
|
+
|
|
124
|
+
### Errors
|
|
125
|
+
|
|
126
|
+
Prefer throwing `mintErrorFromResponse(res.status, body)` from your `tokenProvider`. If you return the error body instead, include `status` in it (`{ ...body, status: res.status }`) or the SDK will report 402 for a cap refusal and 400 for everything else.
|
|
127
|
+
|
|
128
|
+
Every rejection is a `DialerError` with a `code`:
|
|
129
|
+
`cap_reached` (`DialerCapReachedError`: `used`, `cap`, `periodEnd`),
|
|
130
|
+
`subscription_required`, `account_paywalled`, `rate_limited`
|
|
131
|
+
(`DialerRateLimitedError`: `retryAfterSeconds`), `line_unavailable`,
|
|
132
|
+
`not_provisioned`, `token_unavailable`, `invalid_state`,
|
|
133
|
+
`microphone_unavailable`, `connection_failed`, `call_failed`.
|
|
134
|
+
|
|
135
|
+
## Limits
|
|
136
|
+
|
|
137
|
+
- Tokens live 15 minutes and are scoped to one line. The SDK refreshes them
|
|
138
|
+
automatically, including before a token's per-token call limit is reached.
|
|
139
|
+
- Each call is metered against your dialer plan. When the workspace or the line
|
|
140
|
+
hits its daily cap the mint returns `dialer_cap_reached`; calls already in
|
|
141
|
+
progress are never cut off.
|
|
142
|
+
- Inbound calls ring the SDK only while the line is provisioned for inbound and
|
|
143
|
+
someone in your workspace is in its ring set.
|
|
144
|
+
|
|
145
|
+
## Developing (Project Blue engineers)
|
|
146
|
+
|
|
147
|
+
This package lives in the `project-blue` repo at `sdk/dialer` and is a
|
|
148
|
+
standalone project (own lockfile, `pnpm install --ignore-workspace`).
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
pnpm sdk:install # deps for sdk/dialer (tsup, typescript)
|
|
152
|
+
pnpm sdk:build # dist/ (ESM + CJS + .d.ts) via tsup
|
|
153
|
+
pnpm sdk:verify # typecheck + build + dist smoke — what prepublishOnly runs
|
|
154
|
+
pnpm sdk:publish # bump version in sdk/dialer/package.json first
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Tests
|
|
158
|
+
|
|
159
|
+
Unit tests run with the app's Vitest (`pnpm test:unit` at the repo root):
|
|
160
|
+
|
|
161
|
+
| file | proves |
|
|
162
|
+
| --- | --- |
|
|
163
|
+
| `src/dialer.test.ts` | happy paths of a session against a fake engine |
|
|
164
|
+
| `src/dialer.lifecycle.test.ts` | token parsing edge cases, refresh de-dup / backoff / cap hold, inbound registration toggling, engine error routing, every invalid-state guard, option passthrough, teardown |
|
|
165
|
+
| `src/call.test.ts` | `DialerCall` status mapping, control passthrough, event translation, error-code mapping, end-once semantics |
|
|
166
|
+
| `src/errors.test.ts` | every mint status/code → `DialerError` code, cap/rate-limit error shapes, engine error mapping |
|
|
167
|
+
| `src/emitter.test.ts` | typed emitter semantics incl. throwing listeners |
|
|
168
|
+
| `src/engine.test.ts` | the adapter constructs the engine `Device` with our options and memoizes the import |
|
|
169
|
+
| `src/engine.real.test.ts` | the **real** `@twilio/voice-sdk` `Device` accepts our option object and event names under jsdom; a session starts and tears down on it without opening signaling |
|
|
170
|
+
| `src/app/api/v1/dialer/token/sdk-contract.test.ts` (app) | the mint route's **actual** success and refusal bodies drive the SDK end to end |
|
|
171
|
+
| `scripts/smoke-dist.mjs` | the built ESM and CJS bundles import, expose the public surface, run a session, ship engine-free types, keep the engine external — runs in `pnpm sdk:verify` and `prepublishOnly` |
|
|
172
|
+
|
|
173
|
+
What is not covered: a real call. jsdom has no WebRTC and the local stack has
|
|
174
|
+
no Twilio emulator, so placing a call is a manual check on a preview deploy.
|
|
175
|
+
|
|
176
|
+
Publishing needs an npm account on the `@tryprojectblue` org (`npm login`).
|