@cosmoops/connect-sdk 0.20.0 → 0.23.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 +281 -0
- package/README.md +2 -0
- package/dist/cjs/client/components/identity.js +24 -0
- package/dist/cjs/client/components/one-tap-state.js +96 -0
- package/dist/cjs/client/components/one-tap.js +106 -0
- package/dist/cjs/client/components/plan-pricing.js +28 -0
- package/dist/cjs/client/components/plans.js +8 -3
- package/dist/cjs/client/components/user-menu.js +2 -6
- package/dist/cjs/client/index.js +11 -3
- package/dist/cjs/client/provider.js +102 -7
- package/dist/cjs/server/client.js +12 -1
- package/dist/cjs/server/handler.js +126 -2
- package/dist/cjs/server/resources/calls.js +57 -0
- package/dist/cjs/server/resources/channels.js +115 -0
- package/dist/cjs/server/resources/enrichment.js +56 -0
- package/dist/cjs/server/resources/identity.js +40 -0
- package/dist/cjs/server/resources/workspaces.js +73 -2
- package/dist/esm/client/components/identity.js +21 -0
- package/dist/esm/client/components/one-tap-state.js +90 -0
- package/dist/esm/client/components/one-tap.js +103 -0
- package/dist/esm/client/components/plan-pricing.js +27 -0
- package/dist/esm/client/components/plans.js +9 -4
- package/dist/esm/client/components/user-menu.js +1 -5
- package/dist/esm/client/index.js +8 -1
- package/dist/esm/client/provider.js +102 -7
- package/dist/esm/server/client.js +12 -1
- package/dist/esm/server/handler.js +127 -3
- package/dist/esm/server/resources/calls.js +53 -0
- package/dist/esm/server/resources/channels.js +111 -0
- package/dist/esm/server/resources/enrichment.js +52 -0
- package/dist/esm/server/resources/identity.js +39 -0
- package/dist/esm/server/resources/workspaces.js +73 -2
- package/dist/types/client/components/identity.d.ts +17 -0
- package/dist/types/client/components/one-tap-state.d.ts +39 -0
- package/dist/types/client/components/one-tap.d.ts +50 -0
- package/dist/types/client/components/plan-pricing.d.ts +23 -0
- package/dist/types/client/index.d.ts +4 -2
- package/dist/types/client/provider.d.ts +101 -3
- package/dist/types/client/use-checkout.d.ts +9 -0
- package/dist/types/server/client.d.ts +9 -1
- package/dist/types/server/index.d.ts +3 -0
- package/dist/types/server/resources/calls.d.ts +45 -0
- package/dist/types/server/resources/channels.d.ts +91 -0
- package/dist/types/server/resources/enrichment.d.ts +44 -0
- package/dist/types/server/resources/identity.d.ts +35 -0
- package/dist/types/server/resources/workspaces.d.ts +63 -3
- package/dist/types/types.d.ts +344 -1
- package/package.json +1 -1
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { AccessResource } from "./resources/access.js";
|
|
2
2
|
import { AppsResource } from "./resources/apps.js";
|
|
3
3
|
import { AiResource } from "./resources/ai.js";
|
|
4
|
+
import { EnrichmentResource } from "./resources/enrichment.js";
|
|
4
5
|
import { DataResource, storePassthrough } from "./resources/data.js";
|
|
5
6
|
import { WorkspacesResource } from "./resources/workspaces.js";
|
|
6
7
|
import { SchedulingResource } from "./resources/scheduling.js";
|
|
7
8
|
import { ContactsResource } from "./resources/contacts.js";
|
|
9
|
+
import { ChannelsResource } from "./resources/channels.js";
|
|
10
|
+
import { CallsResource } from "./resources/calls.js";
|
|
8
11
|
import { KvResource } from "./resources/kv.js";
|
|
9
12
|
import { BillingResource } from "./resources/billing.js";
|
|
10
13
|
import { DriveResource } from "./resources/drive.js";
|
|
@@ -82,8 +85,13 @@ export class ConnectClient {
|
|
|
82
85
|
scheduling;
|
|
83
86
|
/** Who a workspace already knows how to reach, by phone or by id. */
|
|
84
87
|
contacts;
|
|
88
|
+
/** The phone numbers Connect has assigned to your application, and where each one routes. */
|
|
89
|
+
channels;
|
|
90
|
+
/** The calls that arrived on those channels, with their transcripts. */
|
|
91
|
+
calls;
|
|
85
92
|
/** RAG chat and structured extraction. */
|
|
86
93
|
ai;
|
|
94
|
+
enrichment;
|
|
87
95
|
/** Your application's own document store - see `DataResource`. */
|
|
88
96
|
data;
|
|
89
97
|
/** Your application's key/value store - see `KvResource`. */
|
|
@@ -132,7 +140,10 @@ export class ConnectClient {
|
|
|
132
140
|
this.drive = new DriveResource(transport);
|
|
133
141
|
this.scheduling = new SchedulingResource(transport);
|
|
134
142
|
this.contacts = new ContactsResource(transport);
|
|
143
|
+
this.channels = new ChannelsResource(transport);
|
|
144
|
+
this.calls = new CallsResource(transport);
|
|
135
145
|
this.ai = new AiResource(transport);
|
|
146
|
+
this.enrichment = new EnrichmentResource(transport);
|
|
136
147
|
this.data = new DataResource(transport);
|
|
137
148
|
this.kv = new KvResource(transport);
|
|
138
149
|
this.otp = new OtpResource(transport);
|
|
@@ -146,4 +157,4 @@ export class ConnectClient {
|
|
|
146
157
|
}
|
|
147
158
|
}
|
|
148
159
|
/** Kept in step with `package.json` by `sdk.test.ts`. */
|
|
149
|
-
export const SDK_VERSION = "0.
|
|
160
|
+
export const SDK_VERSION = "0.23.0";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ConnectError } from "../errors.js";
|
|
2
|
-
import { createState } from "./resources/identity.js";
|
|
2
|
+
import { createPkcePair, createState } from "./resources/identity.js";
|
|
3
3
|
/**
|
|
4
4
|
* A sentinel origin to resolve a candidate destination against. Never reached -
|
|
5
5
|
* `.invalid` is reserved by RFC 2606 precisely so it cannot resolve.
|
|
@@ -102,6 +102,7 @@ export function createConnectHandler(options) {
|
|
|
102
102
|
const defaultRedirect = options.defaultRedirect ?? "/";
|
|
103
103
|
const stateCookieName = options.stateCookieName ?? "connect_oauth_state";
|
|
104
104
|
const silentCookieName = `${stateCookieName}_silent`;
|
|
105
|
+
const pkceCookieName = `${stateCookieName}_pkce`;
|
|
105
106
|
const callbackPath = `${basePath}/callback`;
|
|
106
107
|
/**
|
|
107
108
|
* Short-lived, httpOnly, and `SameSite=Lax` - `Strict` would not be sent on
|
|
@@ -148,6 +149,35 @@ export function createConnectHandler(options) {
|
|
|
148
149
|
.filter(Boolean)
|
|
149
150
|
.join("; ");
|
|
150
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Holds the One Tap PKCE **verifier** for the few seconds between the tap and
|
|
154
|
+
* the exchange.
|
|
155
|
+
*
|
|
156
|
+
* This is the cookie the whole no-navigation flow rests on. Connect hands the
|
|
157
|
+
* authorization code to the *page*, so the code alone must not be enough to
|
|
158
|
+
* mint a session - the verifier is what completes it, and it never leaves
|
|
159
|
+
* this origin's cookie jar. `SameSite=Lax` is sufficient and correct: unlike
|
|
160
|
+
* the state cookie there is no cross-site redirect that has to carry it, and
|
|
161
|
+
* `Lax` additionally means a cross-site `POST` to `/one-tap/complete` arrives
|
|
162
|
+
* without it and simply fails.
|
|
163
|
+
*
|
|
164
|
+
* Five minutes, not the state cookie's fifteen: it is minted on the tap and
|
|
165
|
+
* spent on the next request, so a long window is only a longer chance for a
|
|
166
|
+
* stale one to be lying around.
|
|
167
|
+
*/
|
|
168
|
+
function pkceCookie(value, secure) {
|
|
169
|
+
const cleared = value === "";
|
|
170
|
+
return [
|
|
171
|
+
`${pkceCookieName}=${encodeURIComponent(value)}`,
|
|
172
|
+
`Path=${basePath}`,
|
|
173
|
+
"HttpOnly",
|
|
174
|
+
"SameSite=Lax",
|
|
175
|
+
secure ? "Secure" : "",
|
|
176
|
+
cleared ? "Max-Age=0" : "Max-Age=300",
|
|
177
|
+
]
|
|
178
|
+
.filter(Boolean)
|
|
179
|
+
.join("; ");
|
|
180
|
+
}
|
|
151
181
|
async function requireToken(request) {
|
|
152
182
|
const token = await session.read(request);
|
|
153
183
|
if (!token) {
|
|
@@ -255,6 +285,14 @@ export function createConnectHandler(options) {
|
|
|
255
285
|
},
|
|
256
286
|
});
|
|
257
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Where the browser asks Connect about the visitor, and where it asks for a
|
|
290
|
+
* code once they tap - or `null` when this deployment cannot run One Tap at
|
|
291
|
+
* all (no `clientId`, or a session store that cannot `write`, in which case
|
|
292
|
+
* `/one-tap/complete` does not exist and offering the prompt would strand
|
|
293
|
+
* anyone who used it).
|
|
294
|
+
*/
|
|
295
|
+
const oneTapEndpoints = session.write ? client.identity.oneTapEndpoints() : null;
|
|
258
296
|
const routes = {
|
|
259
297
|
"GET /session": async (request) => {
|
|
260
298
|
const token = await session.read(request);
|
|
@@ -264,9 +302,21 @@ export function createConnectHandler(options) {
|
|
|
264
302
|
// the client never has to tell "signed out" from "signed in with no
|
|
265
303
|
// role" by which keys happen to be present.
|
|
266
304
|
if (!token) {
|
|
267
|
-
return json({
|
|
305
|
+
return json({
|
|
306
|
+
user: null,
|
|
307
|
+
subscription: null,
|
|
308
|
+
role: null,
|
|
309
|
+
permissions: [],
|
|
310
|
+
staff: false,
|
|
311
|
+
// Carried on the read the provider already makes on mount, so One Tap
|
|
312
|
+
// costs no extra round trip to discover where to ask. Present on both
|
|
313
|
+
// branches even though only the signed-out one can use it: a response
|
|
314
|
+
// whose shape depends on the answer is a response consumers start
|
|
315
|
+
// testing for key presence instead of value.
|
|
316
|
+
oneTap: oneTapEndpoints,
|
|
317
|
+
});
|
|
268
318
|
}
|
|
269
|
-
return json(await client.identity.getUser(token));
|
|
319
|
+
return json({ ...(await client.identity.getUser(token)), oneTap: oneTapEndpoints });
|
|
270
320
|
},
|
|
271
321
|
"GET /entitlements": async (request) => {
|
|
272
322
|
const { workspaceId } = await requireWorkspace(request);
|
|
@@ -701,6 +751,74 @@ export function createConnectHandler(options) {
|
|
|
701
751
|
await session.write(request, response, result.sessionToken);
|
|
702
752
|
return response;
|
|
703
753
|
},
|
|
754
|
+
/**
|
|
755
|
+
* One Tap, step one: mint the PKCE pair, keep the verifier here, hand the
|
|
756
|
+
* challenge to the page.
|
|
757
|
+
*
|
|
758
|
+
* Called on the *tap*, not on mount, so the five-minute verifier is minted
|
|
759
|
+
* seconds before it is spent rather than sitting in a cookie for as long as
|
|
760
|
+
* someone leaves the prompt on screen.
|
|
761
|
+
*
|
|
762
|
+
* A `POST` despite reading nothing: it sets a cookie, and a `GET` that
|
|
763
|
+
* changes state is both wrong and prefetchable - a link prefetcher or a
|
|
764
|
+
* scanner would silently rotate the verifier out from under an open prompt.
|
|
765
|
+
*/
|
|
766
|
+
"POST /one-tap/start": async (request) => {
|
|
767
|
+
const url = new URL(request.url);
|
|
768
|
+
const { verifier, challenge } = await createPkcePair();
|
|
769
|
+
const response = json({
|
|
770
|
+
codeChallenge: challenge,
|
|
771
|
+
// The already-registered callback URI. One Tap never navigates to it -
|
|
772
|
+
// it is the value the code is *bound* to, and it must be byte-identical
|
|
773
|
+
// at `/one-tap/complete`, so both ends derive it the same way the
|
|
774
|
+
// redirect flow does rather than accepting it from the page.
|
|
775
|
+
redirectUri: `${url.origin}${callbackPath}`,
|
|
776
|
+
});
|
|
777
|
+
response.headers.append("set-cookie", pkceCookie(verifier, url.protocol === "https:"));
|
|
778
|
+
return response;
|
|
779
|
+
},
|
|
780
|
+
/**
|
|
781
|
+
* One Tap, step two: trade the page's code for a session, using the
|
|
782
|
+
* verifier the page never saw.
|
|
783
|
+
*
|
|
784
|
+
* This is `GET /callback`'s body without the navigation - same exchange,
|
|
785
|
+
* same verify-before-store ordering, same session store. The verifier is
|
|
786
|
+
* what makes the difference: a code lifted out of the browser cannot be
|
|
787
|
+
* redeemed anywhere else, because the exchange is refused without it.
|
|
788
|
+
*/
|
|
789
|
+
"POST /one-tap/complete": async (request) => {
|
|
790
|
+
const url = new URL(request.url);
|
|
791
|
+
const secure = url.protocol === "https:";
|
|
792
|
+
const clearPkce = pkceCookie("", secure);
|
|
793
|
+
const verifier = readCookie(request, pkceCookieName);
|
|
794
|
+
if (!verifier) {
|
|
795
|
+
// The prompt sat unanswered past the cookie's life, or a cross-site
|
|
796
|
+
// `POST` arrived without it. Either way there is nothing to complete,
|
|
797
|
+
// and the client falls back to the interactive flow.
|
|
798
|
+
const expired = json({ error: "expired" }, 400);
|
|
799
|
+
expired.headers.append("set-cookie", clearPkce);
|
|
800
|
+
return expired;
|
|
801
|
+
}
|
|
802
|
+
const body = (await request.json().catch(() => null));
|
|
803
|
+
const code = typeof body?.code === "string" ? body.code : null;
|
|
804
|
+
if (!code) {
|
|
805
|
+
const invalid = json({ error: "invalid_request" }, 400);
|
|
806
|
+
invalid.headers.append("set-cookie", clearPkce);
|
|
807
|
+
return invalid;
|
|
808
|
+
}
|
|
809
|
+
const result = await client.identity.exchangeCode({
|
|
810
|
+
code,
|
|
811
|
+
redirectUri: `${url.origin}${callbackPath}`,
|
|
812
|
+
codeVerifier: verifier,
|
|
813
|
+
});
|
|
814
|
+
// Verified before the session is stored, exactly as the callback does -
|
|
815
|
+
// an unverifiable token must never become a signed-in user.
|
|
816
|
+
await client.identity.verifyIdToken(result.idToken);
|
|
817
|
+
const response = json({ ok: true });
|
|
818
|
+
response.headers.append("set-cookie", clearPkce);
|
|
819
|
+
await session.write(request, response, result.sessionToken);
|
|
820
|
+
return response;
|
|
821
|
+
},
|
|
704
822
|
"POST /logout": async (request) => {
|
|
705
823
|
const token = await session.read(request);
|
|
706
824
|
const response = json({ ok: true });
|
|
@@ -719,6 +837,12 @@ export function createConnectHandler(options) {
|
|
|
719
837
|
if (!session.write) {
|
|
720
838
|
delete routes["GET /signin"];
|
|
721
839
|
delete routes["GET /callback"];
|
|
840
|
+
// Same reason, and `oneTapEndpoints` is already `null` above so the client
|
|
841
|
+
// never offers the prompt in the first place - this is the second half of
|
|
842
|
+
// that, so a hand-written call cannot reach a route that would throw on
|
|
843
|
+
// `session.write!`.
|
|
844
|
+
delete routes["POST /one-tap/start"];
|
|
845
|
+
delete routes["POST /one-tap/complete"];
|
|
722
846
|
// A switch rotates the session token. Without somewhere to persist the new
|
|
723
847
|
// one, the old one would be revoked and the user silently signed out - so
|
|
724
848
|
// the route does not exist rather than working once and breaking.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { request } from "../http.js";
|
|
2
|
+
/**
|
|
3
|
+
* Every call that arrived on a channel assigned to your application.
|
|
4
|
+
*
|
|
5
|
+
* The application is resolved from your API key and is never a parameter, so
|
|
6
|
+
* there is no request that reads another partner's calls.
|
|
7
|
+
*
|
|
8
|
+
* `channelId` is the only filter, and that is a budget decision rather than an
|
|
9
|
+
* oversight: each filter costs a Firestore composite index against a project
|
|
10
|
+
* already near its ceiling. Filter the rest client-side, or read the
|
|
11
|
+
* aggregates from `analytics`.
|
|
12
|
+
*/
|
|
13
|
+
export class CallsResource {
|
|
14
|
+
transport;
|
|
15
|
+
constructor(transport) {
|
|
16
|
+
this.transport = transport;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Your calls, newest first, across all of your subscribing workspaces.
|
|
20
|
+
*
|
|
21
|
+
* **Page until `hasMore` is `false`; do not stop on an empty `items`.** A
|
|
22
|
+
* page can come back empty with more behind it.
|
|
23
|
+
*/
|
|
24
|
+
list(input = {}) {
|
|
25
|
+
return request(this.transport, {
|
|
26
|
+
method: "GET",
|
|
27
|
+
path: "/calls",
|
|
28
|
+
query: {
|
|
29
|
+
channelId: input.channelId,
|
|
30
|
+
cursor: input.cursor,
|
|
31
|
+
limit: input.limit === undefined ? undefined : String(input.limit),
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* One call and every transcript turn, ordered by `sequence` - which is the
|
|
37
|
+
* sort to trust, since two turns can share a millisecond.
|
|
38
|
+
*
|
|
39
|
+
* `recordingFileId` and `transcriptFileId` are Drive file ids, readable with
|
|
40
|
+
* a `drive:read` key. Both are `null` until the carrier posts the audio,
|
|
41
|
+
* which is the normal state for a call that has only just ended - branch on
|
|
42
|
+
* `recordingStatus` rather than treating `null` as a failure.
|
|
43
|
+
*
|
|
44
|
+
* Throws `ConnectError` with `code: "not_found"` for a call that does not
|
|
45
|
+
* exist *or* belongs to another application.
|
|
46
|
+
*/
|
|
47
|
+
get(callId) {
|
|
48
|
+
return request(this.transport, {
|
|
49
|
+
method: "GET",
|
|
50
|
+
path: `/calls/${encodeURIComponent(callId)}`,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { request } from "../http.js";
|
|
2
|
+
/**
|
|
3
|
+
* The phone numbers Connect has assigned to your application.
|
|
4
|
+
*
|
|
5
|
+
* **Inventory is platform-owned.** A number is a regulated asset bought under
|
|
6
|
+
* Connect's carrier account and reassignable between workspaces, so your
|
|
7
|
+
* application never provisions, prices or releases one - it claims from a pool
|
|
8
|
+
* it cannot see, and reads back what it was given. That is why `claim` takes a
|
|
9
|
+
* workspace and a kind rather than a number, and why nothing here exposes a
|
|
10
|
+
* carrier, a provider reference or a monthly rate.
|
|
11
|
+
*
|
|
12
|
+
* The application is resolved from your API key and is never a parameter, so
|
|
13
|
+
* no request shape here can reach another partner's channels. A channel
|
|
14
|
+
* belonging to someone else answers `404`, not `403` - confirming it exists
|
|
15
|
+
* would make these reads an oracle for enumerating their channel ids.
|
|
16
|
+
*/
|
|
17
|
+
export class ChannelsResource {
|
|
18
|
+
transport;
|
|
19
|
+
constructor(transport) {
|
|
20
|
+
this.transport = transport;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Every channel assigned to your application, across all of your subscribing
|
|
24
|
+
* workspaces.
|
|
25
|
+
*
|
|
26
|
+
* Page until `hasMore` is `false`.
|
|
27
|
+
*/
|
|
28
|
+
list(input = {}) {
|
|
29
|
+
return request(this.transport, {
|
|
30
|
+
method: "GET",
|
|
31
|
+
path: "/channels",
|
|
32
|
+
query: {
|
|
33
|
+
workspaceId: input.workspaceId,
|
|
34
|
+
cursor: input.cursor,
|
|
35
|
+
limit: input.limit === undefined ? undefined : String(input.limit),
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The channel behind an id you already hold - a webhook's `channelId`, or a
|
|
41
|
+
* call record's.
|
|
42
|
+
*
|
|
43
|
+
* Throws `ConnectError` with `code: "not_found"` for an id that does not
|
|
44
|
+
* exist *or* belongs to another application.
|
|
45
|
+
*/
|
|
46
|
+
get(channelId) {
|
|
47
|
+
return request(this.transport, {
|
|
48
|
+
method: "GET",
|
|
49
|
+
path: `/channels/${encodeURIComponent(channelId)}`,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Takes a number out of Connect's inventory and binds it to one of your
|
|
54
|
+
* subscribing workspaces.
|
|
55
|
+
*
|
|
56
|
+
* **You do not pick the number.** A workspace may hold exactly one, across
|
|
57
|
+
* every application it uses, and it is then fixed for the life of the
|
|
58
|
+
* engagement - there is no re-claim and no swap. Only a Connect operator can
|
|
59
|
+
* unassign one.
|
|
60
|
+
*
|
|
61
|
+
* Two different `409`s, and they need different handling: the workspace
|
|
62
|
+
* already holds a channel of this kind (fix the call), or the pool is empty
|
|
63
|
+
* right now (retry - Connect is notified, and this is not permanent).
|
|
64
|
+
*
|
|
65
|
+
* The claimed channel comes back without routing, because it has none yet.
|
|
66
|
+
* Call `setRouting` next, or the number refuses every call that reaches it.
|
|
67
|
+
*/
|
|
68
|
+
claim(input) {
|
|
69
|
+
return request(this.transport, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
path: "/channels/claim",
|
|
72
|
+
json: { workspaceId: input.workspaceId, kind: input.kind ?? "voice" },
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Points a channel at wherever inbound conversations should go.
|
|
77
|
+
*
|
|
78
|
+
* `destination` is yours: Connect stores it verbatim and never reads into
|
|
79
|
+
* it. It **replaces** the stored record rather than merging, so send the
|
|
80
|
+
* whole destination each time; it must serialize to 16 KB or fewer.
|
|
81
|
+
*
|
|
82
|
+
* Throws `code: "conflict"` when the channel is suspended or released - use
|
|
83
|
+
* `clearRouting` to stop a suspended number answering, which is deliberately
|
|
84
|
+
* still allowed.
|
|
85
|
+
*/
|
|
86
|
+
setRouting(channelId, destination) {
|
|
87
|
+
return request(this.transport, {
|
|
88
|
+
method: "PUT",
|
|
89
|
+
path: `/channels/${encodeURIComponent(channelId)}/routing`,
|
|
90
|
+
json: { destination },
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Takes a number out of service without giving it up - the channel stays
|
|
95
|
+
* assigned to you and stays billable, and inbound calls are refused until
|
|
96
|
+
* you write routing again.
|
|
97
|
+
*
|
|
98
|
+
* This is the only way to say "stop answering": overwriting `destination`
|
|
99
|
+
* can only ever point the number somewhere *else*, and unassigning it is a
|
|
100
|
+
* Console action that returns the number to Connect's inventory.
|
|
101
|
+
*
|
|
102
|
+
* Idempotent - clearing already-clear routing answers `200` with
|
|
103
|
+
* `routing: null`, so a retry after a lost response is safe.
|
|
104
|
+
*/
|
|
105
|
+
clearRouting(channelId) {
|
|
106
|
+
return request(this.transport, {
|
|
107
|
+
method: "DELETE",
|
|
108
|
+
path: `/channels/${encodeURIComponent(channelId)}/routing`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { actingAs } from "./acting-as.js";
|
|
2
|
+
import { request } from "../http.js";
|
|
3
|
+
/**
|
|
4
|
+
* What Connect knows about the company at a domain.
|
|
5
|
+
*
|
|
6
|
+
* The thing behind "the account filled itself in": a record holding nothing
|
|
7
|
+
* but `priya@acme.com` can offer a name, an industry, a size band and an
|
|
8
|
+
* address instead of an empty form.
|
|
9
|
+
*
|
|
10
|
+
* Two properties are worth knowing before you wire it in, because they decide
|
|
11
|
+
* how you should render the result:
|
|
12
|
+
*
|
|
13
|
+
* - **Every value is cited, and nothing is recalled.** Connect reads the
|
|
14
|
+
* company's own website and returns the URL each claim came from. A field
|
|
15
|
+
* the pages never stated comes back `null` rather than as a plausible guess.
|
|
16
|
+
* - **It is a suggestion, not a save.** Nothing is written to your store, and
|
|
17
|
+
* nothing should be written to it without the person in front of it
|
|
18
|
+
* agreeing - which is also how HubSpot and Zoho present the same feature.
|
|
19
|
+
*/
|
|
20
|
+
export class EnrichmentResource {
|
|
21
|
+
transport;
|
|
22
|
+
constructor(transport) {
|
|
23
|
+
this.transport = transport;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Look a company up from a work email address or a domain.
|
|
27
|
+
*
|
|
28
|
+
* Send whichever you hold; `domain` wins when both are given. A consumer
|
|
29
|
+
* address (`founder@gmail.com`) is answered with
|
|
30
|
+
* `{ status: "skipped", reason: "consumer_domain" }` rather than an error -
|
|
31
|
+
* it names a person, not an employer.
|
|
32
|
+
*
|
|
33
|
+
* `asUser` attributes the generation to the signed-in caller's workspace,
|
|
34
|
+
* resolved from their session token. Without it the call is attributed to
|
|
35
|
+
* your application.
|
|
36
|
+
*/
|
|
37
|
+
company(input, options = {}) {
|
|
38
|
+
return request(this.transport, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
path: "/enrichment/company",
|
|
41
|
+
// `asUser` never reaches the body - it is a credential, and Connect
|
|
42
|
+
// reads it from the header.
|
|
43
|
+
json: {
|
|
44
|
+
...(input.email ? { email: input.email } : {}),
|
|
45
|
+
...(input.domain ? { domain: input.domain } : {}),
|
|
46
|
+
...(input.refresh ? { refresh: true } : {}),
|
|
47
|
+
},
|
|
48
|
+
headers: actingAs(input.asUser),
|
|
49
|
+
signal: options.signal,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -12,6 +12,23 @@ function base64UrlEncode(bytes) {
|
|
|
12
12
|
binary += String.fromCharCode(byte);
|
|
13
13
|
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Mints a PKCE pair for the One Tap flow.
|
|
17
|
+
*
|
|
18
|
+
* The redirect flow can take PKCE or leave it, because the code is delivered
|
|
19
|
+
* to the application's server on a redirect the browser only forwards. One Tap
|
|
20
|
+
* hands the code *to the page*, so the verifier is the thing that makes a
|
|
21
|
+
* stolen code worthless - it stays in an `HttpOnly` cookie on the application's
|
|
22
|
+
* own origin and is never sent to the browser.
|
|
23
|
+
*
|
|
24
|
+
* `crypto.subtle` rather than a Node hash, so this stays isomorphic and the
|
|
25
|
+
* `./next` entry keeps loading on the edge runtime as well as Node.
|
|
26
|
+
*/
|
|
27
|
+
export async function createPkcePair() {
|
|
28
|
+
const verifier = createState(32);
|
|
29
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
|
|
30
|
+
return { verifier, challenge: base64UrlEncode(new Uint8Array(digest)) };
|
|
31
|
+
}
|
|
15
32
|
function base64UrlDecode(value) {
|
|
16
33
|
const padded = value.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(value.length / 4) * 4, "=");
|
|
17
34
|
const binary = atob(padded);
|
|
@@ -50,6 +67,28 @@ export class IdentityResource {
|
|
|
50
67
|
}
|
|
51
68
|
return url.toString();
|
|
52
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* The two Connect endpoints One Tap talks to **from the browser**, or `null`
|
|
72
|
+
* when this client has no `clientId` configured and the flow cannot run.
|
|
73
|
+
*
|
|
74
|
+
* Handed out by the bridge rather than built in the page, so One Tap needs no
|
|
75
|
+
* client-side configuration at all - a `./next` application sets
|
|
76
|
+
* `CONNECT_CLIENT_ID` server-side and nothing else. Both URLs carry only the
|
|
77
|
+
* public `client_id`; neither is a credential.
|
|
78
|
+
*
|
|
79
|
+
* Returning `null` rather than throwing (as `authorizeUrl` does) because this
|
|
80
|
+
* is read on the `/session` route, which must keep answering for an
|
|
81
|
+
* application that has no OAuth client configured at all.
|
|
82
|
+
*/
|
|
83
|
+
oneTapEndpoints() {
|
|
84
|
+
if (!this.clientId)
|
|
85
|
+
return null;
|
|
86
|
+
const hint = new URL("/api/sso/session-hint", this.appBaseUrl);
|
|
87
|
+
hint.searchParams.set("client_id", this.clientId);
|
|
88
|
+
const authorize = new URL("/api/sso/authorize/one-tap", this.appBaseUrl);
|
|
89
|
+
authorize.searchParams.set("client_id", this.clientId);
|
|
90
|
+
return { hintUrl: hint.toString(), authorizeUrl: authorize.toString() };
|
|
91
|
+
}
|
|
53
92
|
/** Step 2 - trade the one-time `code` for a session. Server-side only. */
|
|
54
93
|
async exchangeCode(input) {
|
|
55
94
|
const wire = await this.post("/sso/token", {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { actingAs } from "./acting-as.js";
|
|
2
2
|
import { request } from "../http.js";
|
|
3
3
|
/**
|
|
4
|
-
* Which workspaces the signed-in user belongs to, moving between them,
|
|
5
|
-
* else is on the team.
|
|
4
|
+
* Which workspaces the signed-in user belongs to, moving between them, who
|
|
5
|
+
* else is on the team, and inviting or granting access to your application.
|
|
6
6
|
*
|
|
7
7
|
* **Every call here needs `asUser`** - the signed-in user's opaque
|
|
8
8
|
* `sessionToken`. There is no application-wide form of any of these: they take
|
|
@@ -67,4 +67,75 @@ export class WorkspacesResource {
|
|
|
67
67
|
});
|
|
68
68
|
return result.members;
|
|
69
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Seats, what the caller may do with them, and the invitations already
|
|
72
|
+
* holding one - everything a team screen needs to decide what to draw.
|
|
73
|
+
*
|
|
74
|
+
* Behind `members:manage` rather than `workspaces:read`, and gated on the
|
|
75
|
+
* caller's own `member:invite`: it names people who are not members yet.
|
|
76
|
+
*/
|
|
77
|
+
access(options) {
|
|
78
|
+
return request(this.transport, {
|
|
79
|
+
method: "GET",
|
|
80
|
+
path: "/workspaces/members/access",
|
|
81
|
+
headers: actingAs(options.asUser),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Invite someone into the workspace, with access to your application.
|
|
86
|
+
*
|
|
87
|
+
* `roleKey` names a role from your own catalog (or the reserved `"admin"`);
|
|
88
|
+
* omit it to add someone to the workspace with no access to your
|
|
89
|
+
* application. You cannot send a Connect role - the grant is built server
|
|
90
|
+
* side as `[member, app:<yourKey>:<roleKey>]`, so nothing this method can
|
|
91
|
+
* express reaches outside your application.
|
|
92
|
+
*
|
|
93
|
+
* Spends a member seat when the invitation is created, not when it is
|
|
94
|
+
* accepted. Throws `ConnectError` with status 409 and a message written to
|
|
95
|
+
* be shown when there is no seat, and 403 when the caller is inviting
|
|
96
|
+
* someone with access they do not hold themselves.
|
|
97
|
+
*/
|
|
98
|
+
invite(input, options) {
|
|
99
|
+
return request(this.transport, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
path: "/workspaces/members/invite",
|
|
102
|
+
headers: actingAs(options.asUser),
|
|
103
|
+
json: input,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Grant or revoke **your application's** role for an existing member.
|
|
108
|
+
* `roleKey: null` revokes it.
|
|
109
|
+
*
|
|
110
|
+
* Moves only your own grant: Connect recomputes the member's roles as what
|
|
111
|
+
* they hold now, minus any grant naming your application, plus the one you
|
|
112
|
+
* asked for. Their workspace role and every other application they use are
|
|
113
|
+
* untouched, and there is no form of this that removes them from the
|
|
114
|
+
* workspace.
|
|
115
|
+
*
|
|
116
|
+
* Re-sending the role they already have is a no-op rather than an error, so
|
|
117
|
+
* a form that submits whatever is on screen is safe.
|
|
118
|
+
*/
|
|
119
|
+
setAccess(input, options) {
|
|
120
|
+
return request(this.transport, {
|
|
121
|
+
method: "POST",
|
|
122
|
+
path: "/workspaces/members/access",
|
|
123
|
+
headers: actingAs(options.asUser),
|
|
124
|
+
json: input,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Withdraw a pending invitation your application sent, freeing its seat.
|
|
129
|
+
*
|
|
130
|
+
* Only yours - an invitation carrying another application's grant is a 404,
|
|
131
|
+
* deliberately indistinguishable from one that does not exist.
|
|
132
|
+
*/
|
|
133
|
+
async revokeInvitation(input, options) {
|
|
134
|
+
await request(this.transport, {
|
|
135
|
+
method: "POST",
|
|
136
|
+
path: "/workspaces/invitations/revoke",
|
|
137
|
+
headers: actingAs(options.asUser),
|
|
138
|
+
json: input,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
70
141
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a person is rendered when there is no photo to render.
|
|
3
|
+
*
|
|
4
|
+
* Shared rather than copied because two surfaces showing *different* initials
|
|
5
|
+
* for the same account reads as a bug in the account, not in the component -
|
|
6
|
+
* and the One Tap prompt and the user menu are frequently on screen within
|
|
7
|
+
* seconds of each other.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Up to two letters standing for a person.
|
|
11
|
+
*
|
|
12
|
+
* Splits on the punctuation an email address uses as well as whitespace, so
|
|
13
|
+
* `priya.nair@acme.test` yields `PN` rather than `PR` - an account with no
|
|
14
|
+
* display name is exactly the case the fallback exists for, and taking the
|
|
15
|
+
* first two characters of a local part would make it useless there.
|
|
16
|
+
*/
|
|
17
|
+
export declare function initials(name: string | null, email?: string | null): string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How long One Tap stays quiet, and why.
|
|
3
|
+
*
|
|
4
|
+
* A prompt that reappears on every page load after being closed is not a
|
|
5
|
+
* convenience, it is an interstitial. Two things silence it, both stored in the
|
|
6
|
+
* application's own `localStorage` - nothing here leaves the browser, and
|
|
7
|
+
* nothing here is a security control:
|
|
8
|
+
*
|
|
9
|
+
* - **Dismissal, escalating.** Closing it once is "not now"; closing it three
|
|
10
|
+
* times is "stop asking". The backoff says that back.
|
|
11
|
+
* - **Signing out.** Someone who has just signed out of this application does
|
|
12
|
+
* not want to be offered the same account back on the next render. That is a
|
|
13
|
+
* different fact from a dismissal, so it does not consume a dismissal, but it
|
|
14
|
+
* buys a day of quiet.
|
|
15
|
+
*
|
|
16
|
+
* Every access is wrapped: `localStorage` throws outright in some embedded and
|
|
17
|
+
* privacy-restricted contexts, and a prompt is never worth breaking a page for.
|
|
18
|
+
* A read that fails reads as "not suppressed", which is the same state a fresh
|
|
19
|
+
* browser is in.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Whether the prompt may be shown at all.
|
|
23
|
+
*
|
|
24
|
+
* Read once when the component mounts, never during render: `localStorage` is
|
|
25
|
+
* external mutable state, and reading it in a render body is impure - with the
|
|
26
|
+
* React Compiler on, exactly the kind of thing that can be hoisted into the
|
|
27
|
+
* wrong answer. Same reasoning as `ConnectProvider`'s `silentAttemptSpent`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function oneTapSuppressed(): boolean;
|
|
30
|
+
/** Records a close, and escalates how long the next quiet period lasts. */
|
|
31
|
+
export declare function dismissOneTap(): void;
|
|
32
|
+
/**
|
|
33
|
+
* Silences the prompt after an explicit sign-out, without spending a dismissal.
|
|
34
|
+
* Signing out is not a complaint about the prompt, so it must not push someone
|
|
35
|
+
* closer to the seven-day backoff.
|
|
36
|
+
*/
|
|
37
|
+
export declare function suppressOneTapAfterSignOut(): void;
|
|
38
|
+
/** Clears the record once a sign-in actually happens - the prompt did its job. */
|
|
39
|
+
export declare function clearOneTapSuppression(): void;
|