@whereby.com/core 1.1.4 → 1.1.6

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.
@@ -2,7 +2,9 @@
2
2
 
3
3
  var toolkit = require('@reduxjs/toolkit');
4
4
  var media = require('@whereby.com/media');
5
- var events = require('events');
5
+ var events$1 = require('events');
6
+ var nodeBtoa = require('btoa');
7
+ var axios = require('axios');
6
8
 
7
9
  function createAppAsyncThunk(typePrefix, payloadCreator) {
8
10
  return toolkit.createAsyncThunk(typePrefix, payloadCreator);
@@ -73,7 +75,7 @@ const createReactor = (selectors, callback) => {
73
75
  });
74
76
  };
75
77
 
76
- const coreVersion = "1.1.4";
78
+ const coreVersion = "1.1.6";
77
79
 
78
80
  const initialState = {
79
81
  isNodeSdk: false,
@@ -2305,7 +2307,7 @@ createReactor([selectBreakoutAssignments, selectDeviceId, selectLocalParticipant
2305
2307
  dispatch(setBreakoutGroupAssigned({ breakoutGroupAssigned }));
2306
2308
  });
2307
2309
 
2308
- const emitter = new events.EventEmitter();
2310
+ const emitter = new events$1.EventEmitter();
2309
2311
  function createNotificationEvent(payload) {
2310
2312
  const notificationEvent = Object.assign(Object.assign({}, payload), { timestamp: Date.now() });
2311
2313
  return notificationEvent;
@@ -3019,6 +3021,726 @@ const observeStore = (store, select, onChange) => {
3019
3021
  return unsubscribe;
3020
3022
  };
3021
3023
 
3024
+ class Response {
3025
+ constructor(initialValues = {}) {
3026
+ this.data = initialValues.data === undefined ? {} : initialValues.data;
3027
+ this.headers = initialValues.headers || {};
3028
+ this.status = initialValues.status || 200;
3029
+ this.statusText = initialValues.statusText || "OK";
3030
+ this.url = initialValues.url || null;
3031
+ }
3032
+ }
3033
+
3034
+ function assertTruthy(value, parameterName) {
3035
+ media.assert.ok(value, `${parameterName} is required`);
3036
+ return value;
3037
+ }
3038
+ function assertBoolean(value, parameterName) {
3039
+ media.assert.ok(typeof value === "boolean", `${parameterName}<boolean> is required`);
3040
+ return value;
3041
+ }
3042
+ function assertNumber(value, parameterName) {
3043
+ media.assert.ok(typeof value === "number", `${parameterName}<number> is required`);
3044
+ return value;
3045
+ }
3046
+ function assertString(value, parameterName) {
3047
+ media.assert.ok(typeof value === "string", `${parameterName}<string> is required`);
3048
+ return value;
3049
+ }
3050
+ function assertInstanceOf(value, type, parameterName) {
3051
+ const resolvedParameterName = parameterName || type.name[0].toLowerCase() + type.name.substring(1);
3052
+ media.assert.ok(value instanceof type, `${resolvedParameterName}<${type.name}> is required`);
3053
+ return value;
3054
+ }
3055
+ function assertArray(array, parameterName) {
3056
+ media.assert.ok(Array.isArray(array), `${parameterName}<array> is required`);
3057
+ return array;
3058
+ }
3059
+ function assertRecord(ref, name) {
3060
+ if (ref === null || ref === undefined || typeof ref !== "object" || Array.isArray(ref)) {
3061
+ throw new Error(`${name} must be a record. ${JSON.stringify(ref)}`);
3062
+ }
3063
+ return ref;
3064
+ }
3065
+ function assertNullOrType(ref, type, name, message) {
3066
+ assertString(name, "name");
3067
+ const errorMessage = `${name} must be null or of type ${type}`;
3068
+ media.assert.ok(ref === null || typeof ref === type, errorMessage);
3069
+ }
3070
+ function assertNullOrString(ref, name, message) {
3071
+ assertString(name, "name");
3072
+ assertNullOrType(ref, "string", name);
3073
+ }
3074
+
3075
+ function _getAbsoluteUrl({ baseUrl, url }) {
3076
+ return baseUrl ? baseUrl + url : url;
3077
+ }
3078
+ class HttpClient {
3079
+ constructor({ baseUrl }) {
3080
+ assertString(baseUrl, "baseUrl");
3081
+ this._baseUrl = baseUrl;
3082
+ }
3083
+ _requestAxios(url, options) {
3084
+ const axiosOptions = Object.assign({}, options, {
3085
+ url,
3086
+ baseURL: this._baseUrl,
3087
+ });
3088
+ return axios.request(axiosOptions);
3089
+ }
3090
+ request(url, options) {
3091
+ assertString(url, "url");
3092
+ media.assert.ok(url[0] === "/", 'url<String> only accepts relative URLs beginning with "/".');
3093
+ media.assert.ok(options, "options are required");
3094
+ return this._requestAxios(url, options)
3095
+ .then((response) => {
3096
+ const { data, headers, status, statusText, config } = response;
3097
+ const requestUrl = config && config.url ? _getAbsoluteUrl({ baseUrl: config.baseURL, url: config.url }) : null;
3098
+ return new Response({
3099
+ data,
3100
+ headers,
3101
+ status,
3102
+ statusText,
3103
+ url: requestUrl,
3104
+ });
3105
+ })
3106
+ .catch((error) => {
3107
+ const responseObject = error.response;
3108
+ if (!responseObject) {
3109
+ throw new Error("Could not make the request.");
3110
+ }
3111
+ const { data, headers, status, statusText, config } = responseObject;
3112
+ const requestUrl = config && config.url ? _getAbsoluteUrl({ baseUrl: config.baseURL, url: config.url }) : null;
3113
+ return Promise.reject(new Response({
3114
+ data,
3115
+ headers,
3116
+ status,
3117
+ statusText,
3118
+ url: requestUrl,
3119
+ }));
3120
+ });
3121
+ }
3122
+ }
3123
+
3124
+ class MultipartHttpClient {
3125
+ constructor({ httpClient }) {
3126
+ media.assert.ok(httpClient, "httpClient is required");
3127
+ this._httpClient = httpClient;
3128
+ }
3129
+ static dataToFormData(data) {
3130
+ media.assert.ok(data, "data is required");
3131
+ const fd = new FormData();
3132
+ Object.keys(data).forEach((key) => {
3133
+ const value = data[key];
3134
+ fd.append(key, value);
3135
+ });
3136
+ return fd;
3137
+ }
3138
+ request(url, options = {}) {
3139
+ const headers = Object.assign(options.headers || {}, {
3140
+ "Content-Type": undefined,
3141
+ });
3142
+ return this._httpClient.request(url, Object.assign(options, {
3143
+ headers,
3144
+ transformRequest: MultipartHttpClient.dataToFormData,
3145
+ }));
3146
+ }
3147
+ }
3148
+
3149
+ let btoa;
3150
+ if (typeof window === "object") {
3151
+ btoa = window.btoa || nodeBtoa;
3152
+ }
3153
+ else if (typeof global === "object") {
3154
+ btoa = global.btoa || nodeBtoa;
3155
+ }
3156
+ else {
3157
+ btoa = nodeBtoa;
3158
+ }
3159
+ function _getAuthHeader(credentials) {
3160
+ if (credentials && credentials.credentials) {
3161
+ const btoaStr = `${credentials.credentials.uuid}:${credentials.hmac}`;
3162
+ return { Authorization: `Basic ${btoa(btoaStr)}` };
3163
+ }
3164
+ return {};
3165
+ }
3166
+ const noCredentials = () => Promise.resolve(null);
3167
+ class AuthenticatedHttpClient {
3168
+ constructor({ httpClient, fetchDeviceCredentials }) {
3169
+ this._httpClient = httpClient;
3170
+ this._fetchDeviceCredentials = fetchDeviceCredentials;
3171
+ }
3172
+ request(url, options) {
3173
+ return this._fetchDeviceCredentials().then((credentials) => {
3174
+ const headers = Object.assign({}, options.headers, _getAuthHeader(credentials), {
3175
+ "X-Appearin-Device-Platform": "web",
3176
+ });
3177
+ const httpClientOptions = Object.assign({}, options, { headers });
3178
+ return this._httpClient.request(url, httpClientOptions);
3179
+ });
3180
+ }
3181
+ }
3182
+ class ApiClient {
3183
+ constructor({ baseUrl = "https://api.appearin.net", fetchDeviceCredentials = noCredentials, } = {}) {
3184
+ this.authenticatedHttpClient = new AuthenticatedHttpClient({
3185
+ httpClient: new HttpClient({
3186
+ baseUrl,
3187
+ }),
3188
+ fetchDeviceCredentials,
3189
+ });
3190
+ this.authenticatedFormDataHttpClient = new MultipartHttpClient({ httpClient: this.authenticatedHttpClient });
3191
+ }
3192
+ request(url, options) {
3193
+ assertString(url, "url");
3194
+ media.assert.ok(url[0] === "/", 'url<String> only accepts relative URLs beginning with "/".');
3195
+ media.assert.ok(options, "options are required");
3196
+ return this.authenticatedHttpClient.request(url, options);
3197
+ }
3198
+ requestMultipart(url, options) {
3199
+ assertString(url, "url");
3200
+ media.assert.ok(url[0] === "/", 'url<String> only accepts relative URLs beginning with "/".');
3201
+ media.assert.ok(options, "options are required");
3202
+ return this.authenticatedFormDataHttpClient.request(url, options);
3203
+ }
3204
+ }
3205
+
3206
+ function nullOrExtract(extract) {
3207
+ return (data, propertyName) => {
3208
+ const record = assertRecord(data, "data");
3209
+ const value = record[propertyName];
3210
+ return value === null || value === undefined ? null : extract(data, propertyName);
3211
+ };
3212
+ }
3213
+ function extractString(data, propertyName) {
3214
+ const record = assertRecord(data, "data");
3215
+ return assertString(record[propertyName], propertyName);
3216
+ }
3217
+ const extractNullOrString = nullOrExtract(extractString);
3218
+ function extractArrayOfJson(data, propertyName) {
3219
+ const record = assertRecord(data, "data");
3220
+ return assertArray(record[propertyName], propertyName);
3221
+ }
3222
+ function extractArray(data, propertyName, transformer) {
3223
+ return extractArrayOfJson(data, propertyName).map((value) => transformer(value));
3224
+ }
3225
+ function extractJson(data, propertyName) {
3226
+ const record = assertRecord(data, "data");
3227
+ const value = record[propertyName];
3228
+ return value === undefined ? null : value;
3229
+ }
3230
+
3231
+ class Credentials {
3232
+ constructor(uuid, hmac, userId = undefined) {
3233
+ this.credentials = {
3234
+ uuid,
3235
+ };
3236
+ this.hmac = hmac;
3237
+ this.userId = userId;
3238
+ }
3239
+ toJson() {
3240
+ return Object.assign({ credentials: this.credentials, hmac: this.hmac }, (this.userId && { userId: this.userId }));
3241
+ }
3242
+ static fromJson(json) {
3243
+ return new Credentials(extractString(extractJson(json, "credentials"), "uuid"), extractString(json, "hmac"), extractNullOrString(json, "userId") || undefined);
3244
+ }
3245
+ }
3246
+
3247
+ class DeviceService {
3248
+ constructor({ apiClient }) {
3249
+ this._apiClient = apiClient;
3250
+ }
3251
+ getCredentials() {
3252
+ return this._apiClient
3253
+ .request("/devices", {
3254
+ method: "post",
3255
+ })
3256
+ .then(({ data }) => {
3257
+ return Credentials.fromJson(data);
3258
+ })
3259
+ .catch((error) => {
3260
+ if (error.response) {
3261
+ if (error.response.status === 404) {
3262
+ return null;
3263
+ }
3264
+ }
3265
+ throw error;
3266
+ });
3267
+ }
3268
+ }
3269
+
3270
+ class ChromeStorageStore {
3271
+ constructor(key, chromeStorage) {
3272
+ this._key = key;
3273
+ this._chromeStorage = chromeStorage;
3274
+ }
3275
+ loadOrDefault(defaultValue) {
3276
+ return new Promise((resolve) => {
3277
+ this._chromeStorage.get(this._key, (result) => {
3278
+ const unknownResult = result;
3279
+ resolve(unknownResult[this._key] || defaultValue);
3280
+ });
3281
+ });
3282
+ }
3283
+ save(value) {
3284
+ return new Promise((resolve) => {
3285
+ this._chromeStorage.set({ [this._key]: value }, () => {
3286
+ resolve();
3287
+ });
3288
+ });
3289
+ }
3290
+ }
3291
+
3292
+ class LocalStorageStore {
3293
+ constructor(key, localStorage) {
3294
+ assertTruthy(localStorage, "localStorage");
3295
+ this._key = assertString(key, "key");
3296
+ this._localStorage = localStorage;
3297
+ }
3298
+ loadOrDefault(defaultValue) {
3299
+ try {
3300
+ const value = this._localStorage.getItem(this._key);
3301
+ if (value) {
3302
+ try {
3303
+ return Promise.resolve(JSON.parse(value));
3304
+ }
3305
+ catch (_a) {
3306
+ }
3307
+ }
3308
+ return Promise.resolve(defaultValue);
3309
+ }
3310
+ catch (e) {
3311
+ console.warn("Error getting access to storage. Are cookies blocked?", e);
3312
+ return Promise.resolve(defaultValue);
3313
+ }
3314
+ }
3315
+ save(value) {
3316
+ try {
3317
+ this._localStorage.setItem(this._key, JSON.stringify(value));
3318
+ return Promise.resolve();
3319
+ }
3320
+ catch (e) {
3321
+ console.warn("Error getting access to storage. Are cookies blocked?", e);
3322
+ return Promise.reject(e);
3323
+ }
3324
+ }
3325
+ }
3326
+
3327
+ let localStorage;
3328
+ try {
3329
+ localStorage = self.localStorage;
3330
+ }
3331
+ catch (_a) {
3332
+ localStorage = {
3333
+ getItem: () => undefined,
3334
+ key: () => undefined,
3335
+ setItem: () => undefined,
3336
+ removeItem: () => undefined,
3337
+ hasOwnProperty: () => undefined,
3338
+ length: 0,
3339
+ };
3340
+ }
3341
+ var localStorage$1 = localStorage;
3342
+
3343
+ const events = {
3344
+ CREDENTIALS_SAVED: "credentials_saved",
3345
+ };
3346
+ class CredentialsService extends events$1.EventEmitter {
3347
+ constructor({ deviceService, credentialsStore, }) {
3348
+ super();
3349
+ this._deviceService = deviceService;
3350
+ this._credentialsStore = credentialsStore;
3351
+ }
3352
+ static create({ baseUrl, storeName = "CredentialsStorage", storeType = "localStorage", }) {
3353
+ const deviceService = new DeviceService({
3354
+ apiClient: new ApiClient({ baseUrl }),
3355
+ });
3356
+ let credentialsStore = null;
3357
+ if (storeType === "localStorage") {
3358
+ credentialsStore = new LocalStorageStore(storeName, localStorage$1);
3359
+ }
3360
+ else if (storeType === "chromeStorage") {
3361
+ credentialsStore = new ChromeStorageStore(storeName, window["chrome"].storage.local);
3362
+ }
3363
+ else {
3364
+ throw new Error(`Unknown store type: ${storeType}`);
3365
+ }
3366
+ return new CredentialsService({
3367
+ deviceService,
3368
+ credentialsStore,
3369
+ });
3370
+ }
3371
+ _fetchNewCredentialsFromApi() {
3372
+ const credentialsStore = this._credentialsStore;
3373
+ return new Promise((resolve) => {
3374
+ const fetchCredentials = () => {
3375
+ this._deviceService
3376
+ .getCredentials()
3377
+ .then((credentials) => {
3378
+ return credentialsStore
3379
+ .save(credentials ? credentials.toJson() : null)
3380
+ .then(() => resolve(credentials));
3381
+ })
3382
+ .catch(() => {
3383
+ setTimeout(fetchCredentials, 2000);
3384
+ });
3385
+ };
3386
+ fetchCredentials();
3387
+ });
3388
+ }
3389
+ getCurrentCredentials() {
3390
+ return this._credentialsStore.loadOrDefault(null).then((json) => (json ? Credentials.fromJson(json) : null));
3391
+ }
3392
+ getCredentials() {
3393
+ if (!this.credentialsPromise) {
3394
+ this.credentialsPromise = this.getCurrentCredentials().then((storedCredentials) => {
3395
+ if (storedCredentials) {
3396
+ return storedCredentials;
3397
+ }
3398
+ return this._fetchNewCredentialsFromApi();
3399
+ });
3400
+ }
3401
+ return this.credentialsPromise;
3402
+ }
3403
+ saveCredentials(credentials) {
3404
+ this.credentialsPromise = undefined;
3405
+ return this._credentialsStore.save(credentials.toJson()).then(() => {
3406
+ this.emit(events.CREDENTIALS_SAVED, credentials);
3407
+ return credentials;
3408
+ });
3409
+ }
3410
+ setUserId(userId) {
3411
+ return this.getCurrentCredentials()
3412
+ .then((storedCredentials) => {
3413
+ if (!storedCredentials) {
3414
+ console.error("Illegal state: no credentials to set user id for.");
3415
+ }
3416
+ const userIdChangedFromLocalStorage = storedCredentials === null || storedCredentials.userId !== userId;
3417
+ if (!userIdChangedFromLocalStorage) {
3418
+ return undefined;
3419
+ }
3420
+ return this._credentialsStore.save(Object.assign({}, storedCredentials === null || storedCredentials === void 0 ? void 0 : storedCredentials.toJson(), { userId }));
3421
+ })
3422
+ .then(() => undefined);
3423
+ }
3424
+ }
3425
+
3426
+ class EmbeddedFreeTierStatus {
3427
+ constructor({ isExhausted, renewsAt, totalMinutesLimit, totalMinutesUsed, }) {
3428
+ this.isExhausted = isExhausted;
3429
+ this.renewsAt = renewsAt;
3430
+ this.totalMinutesLimit = totalMinutesLimit;
3431
+ this.totalMinutesUsed = totalMinutesUsed;
3432
+ }
3433
+ static fromJson(data) {
3434
+ return new EmbeddedFreeTierStatus({
3435
+ isExhausted: assertBoolean(data.isExhausted, "isExhausted"),
3436
+ renewsAt: new Date(assertString(data.renewsAt, "renewsAt")),
3437
+ totalMinutesLimit: assertNumber(data.totalMinutesLimit, "totalMinutesLimit"),
3438
+ totalMinutesUsed: assertNumber(data.totalMinutesUsed, "totalMinutesUsed"),
3439
+ });
3440
+ }
3441
+ }
3442
+
3443
+ class Account {
3444
+ constructor({ basePlanId, embeddedFreeTierStatus, isDeactivated, isOnTrial, onTrialUntil, trialStatus, }) {
3445
+ this.basePlanId = basePlanId;
3446
+ this.isDeactivated = isDeactivated;
3447
+ this.isOnTrial = isOnTrial;
3448
+ this.onTrialUntil = onTrialUntil || null;
3449
+ this.trialStatus = trialStatus || null;
3450
+ this.embeddedFreeTierStatus = embeddedFreeTierStatus || null;
3451
+ }
3452
+ static fromJson(data) {
3453
+ return new Account({
3454
+ basePlanId: typeof data.basePlanId === "string" ? data.basePlanId : null,
3455
+ isDeactivated: assertBoolean(data.isDeactivated, "isDeactivated"),
3456
+ isOnTrial: assertBoolean(data.isOnTrial, "isOnTrial"),
3457
+ onTrialUntil: typeof data.onTrialUntil === "string" ? new Date(data.onTrialUntil) : null,
3458
+ trialStatus: typeof data.trialStatus === "string" ? data.trialStatus : null,
3459
+ embeddedFreeTierStatus: data.embeddedFreeTierStatus
3460
+ ? EmbeddedFreeTierStatus.fromJson(data.embeddedFreeTierStatus)
3461
+ : null,
3462
+ });
3463
+ }
3464
+ }
3465
+
3466
+ function hasValue(value) {
3467
+ return value !== null && value !== undefined;
3468
+ }
3469
+ function createOrganizationLimits(limits = {}) {
3470
+ return {
3471
+ maxNumberOfInvitationsAndUsers: hasValue(limits === null || limits === void 0 ? void 0 : limits.maxNumberOfInvitationsAndUsers)
3472
+ ? Number(limits === null || limits === void 0 ? void 0 : limits.maxNumberOfInvitationsAndUsers)
3473
+ : null,
3474
+ maxNumberOfClaimedRooms: hasValue(limits === null || limits === void 0 ? void 0 : limits.maxNumberOfClaimedRooms)
3475
+ ? Number(limits === null || limits === void 0 ? void 0 : limits.maxNumberOfClaimedRooms)
3476
+ : null,
3477
+ maxRoomLimitPerOrganization: hasValue(limits === null || limits === void 0 ? void 0 : limits.maxRoomLimitPerOrganization)
3478
+ ? Number(limits === null || limits === void 0 ? void 0 : limits.maxRoomLimitPerOrganization)
3479
+ : null,
3480
+ trialMinutesLimit: hasValue(limits === null || limits === void 0 ? void 0 : limits.trialMinutesLimit) ? Number(limits === null || limits === void 0 ? void 0 : limits.trialMinutesLimit) : null,
3481
+ includedUnits: hasValue(limits === null || limits === void 0 ? void 0 : limits.includedUnits) ? Number(limits === null || limits === void 0 ? void 0 : limits.includedUnits) : null,
3482
+ };
3483
+ }
3484
+ class Organization {
3485
+ constructor(properties) {
3486
+ this.logoImageUrl = null;
3487
+ this.roomBackgroundImageUrl = null;
3488
+ this.roomBackgroundThumbnailUrl = null;
3489
+ this.roomKnockPageBackgroundImageUrl = null;
3490
+ this.roomKnockPageBackgroundThumbnailUrl = null;
3491
+ this.preferences = null;
3492
+ this.onboardingSurvey = null;
3493
+ this.type = null;
3494
+ assertInstanceOf(properties, Object, "properties");
3495
+ assertString(properties.organizationId, "organizationId");
3496
+ assertString(properties.organizationName, "organizationName");
3497
+ assertString(properties.subdomain, "subdomain");
3498
+ assertInstanceOf(properties.permissions, Object, "permissions");
3499
+ assertInstanceOf(properties.limits, Object, "limits");
3500
+ this.organizationId = properties.organizationId;
3501
+ this.organizationName = properties.organizationName;
3502
+ this.subdomain = properties.subdomain;
3503
+ this.permissions = properties.permissions;
3504
+ this.limits = properties.limits;
3505
+ this.account = properties.account ? new Account(properties.account) : null;
3506
+ this.logoImageUrl = properties.logoImageUrl;
3507
+ this.roomBackgroundImageUrl = properties.roomBackgroundImageUrl;
3508
+ this.roomBackgroundThumbnailUrl = properties.roomBackgroundThumbnailUrl;
3509
+ this.roomKnockPageBackgroundImageUrl = properties.roomKnockPageBackgroundImageUrl;
3510
+ this.roomKnockPageBackgroundThumbnailUrl = properties.roomKnockPageBackgroundThumbnailUrl;
3511
+ this.preferences = properties.preferences;
3512
+ this.onboardingSurvey = properties.onboardingSurvey;
3513
+ this.type = properties.type;
3514
+ }
3515
+ static fromJson(data) {
3516
+ const parsedData = assertInstanceOf(data, Object, "data");
3517
+ const preferences = ((parsedData === null || parsedData === void 0 ? void 0 : parsedData.preferences) || {});
3518
+ const onboardingSurvey = ((parsedData === null || parsedData === void 0 ? void 0 : parsedData.onboardingSurvey) || null);
3519
+ const permissions = assertInstanceOf(parsedData.permissions, Object, "permissions");
3520
+ return new Organization({
3521
+ organizationId: assertString(parsedData.organizationId, "organizationId"),
3522
+ organizationName: assertString(parsedData.organizationName, "organizationName"),
3523
+ subdomain: assertString(parsedData.subdomain, "subdomain"),
3524
+ permissions,
3525
+ limits: createOrganizationLimits(assertInstanceOf(parsedData.limits, Object, "limits")),
3526
+ account: parsedData.account ? Account.fromJson(parsedData.account) : null,
3527
+ logoImageUrl: typeof parsedData.logoImageUrl === "string" ? parsedData.logoImageUrl : null,
3528
+ roomBackgroundImageUrl: typeof parsedData.roomBackgroundImageUrl === "string" ? parsedData.roomBackgroundImageUrl : null,
3529
+ roomBackgroundThumbnailUrl: typeof parsedData.roomBackgroundThumbnailUrl === "string"
3530
+ ? parsedData.roomBackgroundThumbnailUrl
3531
+ : null,
3532
+ roomKnockPageBackgroundImageUrl: typeof parsedData.roomKnockPageBackgroundImageUrl === "string"
3533
+ ? parsedData.roomKnockPageBackgroundImageUrl
3534
+ : null,
3535
+ roomKnockPageBackgroundThumbnailUrl: typeof parsedData.roomKnockPageBackgroundThumbnailUrl === "string"
3536
+ ? parsedData.roomKnockPageBackgroundThumbnailUrl
3537
+ : null,
3538
+ preferences,
3539
+ onboardingSurvey,
3540
+ type: typeof parsedData.type === "string" ? parsedData.type : null,
3541
+ });
3542
+ }
3543
+ }
3544
+ Organization.GLOBAL_ORGANIZATION_ID = "1";
3545
+
3546
+ class OrganizationService {
3547
+ constructor({ apiClient }) {
3548
+ this._apiClient = apiClient;
3549
+ }
3550
+ createOrganization({ organizationName, subdomain, owner, }) {
3551
+ const { displayName, consents } = owner || {};
3552
+ const email = "email" in owner
3553
+ ? {
3554
+ value: owner.email,
3555
+ verificationCode: assertString(owner.verificationCode, "owner.verificationCode"),
3556
+ }
3557
+ : null;
3558
+ const idToken = "idToken" in owner ? owner.idToken : null;
3559
+ assertString(subdomain, "subdomain");
3560
+ assertString(organizationName, "organizationName");
3561
+ assertString(displayName, "owner.displayName");
3562
+ media.assert.ok(email || idToken, "owner.email or owner.idToken is required");
3563
+ if (consents) {
3564
+ assertArray(consents, "consents");
3565
+ for (const { consentRevisionId, action } of consents) {
3566
+ assertString(consentRevisionId, "consentRevisionId");
3567
+ assertNullOrString(action, "action");
3568
+ }
3569
+ }
3570
+ return this._apiClient
3571
+ .request(`/organizations`, {
3572
+ method: "POST",
3573
+ data: {
3574
+ organizationName,
3575
+ type: "private",
3576
+ subdomain,
3577
+ owner: Object.assign(Object.assign(Object.assign(Object.assign({}, (email && { email })), (idToken && { idToken })), (consents && { consents })), { displayName }),
3578
+ },
3579
+ })
3580
+ .then(({ data }) => {
3581
+ return extractString(data, "organizationId");
3582
+ });
3583
+ }
3584
+ getOrganizationBySubdomain(subdomain) {
3585
+ assertString(subdomain, "subdomain");
3586
+ return this._apiClient
3587
+ .request(`/organization-subdomains/${encodeURIComponent(subdomain)}/?fields=permissions,account,onboardingSurvey`, {
3588
+ method: "GET",
3589
+ })
3590
+ .then(({ data }) => {
3591
+ return Organization.fromJson(data);
3592
+ })
3593
+ .catch((res) => {
3594
+ if (res instanceof Response) {
3595
+ if (res.status === 404) {
3596
+ return null;
3597
+ }
3598
+ throw new Error(res.statusText);
3599
+ }
3600
+ throw res;
3601
+ });
3602
+ }
3603
+ getOrganizationByOrganizationId(organizationId) {
3604
+ assertString(organizationId, "organizationId");
3605
+ return this._apiClient
3606
+ .request(`/organizations/${encodeURIComponent(organizationId)}?fields=permissions,account`, {
3607
+ method: "GET",
3608
+ })
3609
+ .then(({ data }) => {
3610
+ return Organization.fromJson(data);
3611
+ })
3612
+ .catch((res) => {
3613
+ if (res instanceof Response) {
3614
+ if (res.status === 404) {
3615
+ return null;
3616
+ }
3617
+ throw new Error(res.statusText);
3618
+ }
3619
+ throw res;
3620
+ });
3621
+ }
3622
+ getOrganizationsByContactPoint(options) {
3623
+ const { code } = options;
3624
+ const email = "email" in options ? options.email : null;
3625
+ const phoneNumber = "phoneNumber" in options ? options.phoneNumber : null;
3626
+ media.assert.ok((email || phoneNumber) && !(email && phoneNumber), "either email or phoneNumber is required");
3627
+ assertString(code, "code");
3628
+ const contactPoint = email ? { type: "email", value: email } : { type: "phoneNumber", value: phoneNumber };
3629
+ return this._apiClient
3630
+ .request("/organization-queries", {
3631
+ method: "POST",
3632
+ data: {
3633
+ contactPoint,
3634
+ code,
3635
+ },
3636
+ })
3637
+ .then(({ data }) => {
3638
+ return extractArray(data, "organizations", (organization) => Organization.fromJson(organization));
3639
+ });
3640
+ }
3641
+ getOrganizationsByIdToken({ idToken }) {
3642
+ assertString(idToken, "idToken");
3643
+ return this._apiClient
3644
+ .request("/organization-queries", {
3645
+ method: "POST",
3646
+ data: {
3647
+ idToken,
3648
+ },
3649
+ })
3650
+ .then(({ data }) => {
3651
+ return extractArray(data, "organizations", (organization) => {
3652
+ return Organization.fromJson(Object.assign({ permissions: {}, limits: {} }, assertRecord(organization, "organization")));
3653
+ });
3654
+ });
3655
+ }
3656
+ getOrganizationsByLoggedInUser() {
3657
+ return this._apiClient
3658
+ .request("/user/organizations", {
3659
+ method: "GET",
3660
+ })
3661
+ .then(({ data }) => {
3662
+ return extractArray(data, "organizations", (o) => {
3663
+ return Organization.fromJson(Object.assign({ permissions: {}, limits: {} }, assertRecord(o, "organization")));
3664
+ });
3665
+ });
3666
+ }
3667
+ getSubdomainAvailability(subdomain) {
3668
+ assertString(subdomain, "subdomain");
3669
+ return this._apiClient
3670
+ .request(`/organization-subdomains/${encodeURIComponent(subdomain)}/availability`, {
3671
+ method: "GET",
3672
+ })
3673
+ .then(({ data }) => {
3674
+ assertInstanceOf(data, Object, "data");
3675
+ return {
3676
+ status: extractString(data, "status"),
3677
+ };
3678
+ });
3679
+ }
3680
+ updatePreferences({ organizationId, preferences, }) {
3681
+ assertTruthy(organizationId, "organizationId");
3682
+ assertTruthy(preferences, "preferences");
3683
+ return this._apiClient
3684
+ .request(`/organizations/${encodeURIComponent(organizationId)}/preferences`, {
3685
+ method: "PATCH",
3686
+ data: preferences,
3687
+ })
3688
+ .then(() => undefined);
3689
+ }
3690
+ deleteOrganization({ organizationId }) {
3691
+ assertTruthy(organizationId, "organizationId");
3692
+ return this._apiClient
3693
+ .request(`/organizations/${encodeURIComponent(organizationId)}`, {
3694
+ method: "DELETE",
3695
+ })
3696
+ .then(() => undefined);
3697
+ }
3698
+ }
3699
+
3700
+ class OrganizationServiceCache {
3701
+ constructor({ organizationService, subdomain }) {
3702
+ this._organizationService = organizationService;
3703
+ this._subdomain = subdomain;
3704
+ this._organizationPromise = null;
3705
+ }
3706
+ initOrganization() {
3707
+ return this.fetchOrganization().then(() => undefined);
3708
+ }
3709
+ fetchOrganization() {
3710
+ if (!this._organizationPromise) {
3711
+ this._organizationPromise = this._organizationService.getOrganizationBySubdomain(this._subdomain);
3712
+ }
3713
+ return this._organizationPromise;
3714
+ }
3715
+ }
3716
+
3717
+ const API_BASE_URL = "https://api.whereby.dev";
3718
+ function createServices() {
3719
+ const credentialsService = CredentialsService.create({
3720
+ baseUrl: API_BASE_URL,
3721
+ });
3722
+ const apiClient = new ApiClient({
3723
+ fetchDeviceCredentials: credentialsService.getCredentials.bind(credentialsService),
3724
+ baseUrl: API_BASE_URL,
3725
+ });
3726
+ const organizationService = new OrganizationService({ apiClient });
3727
+ const fetchOrganizationFromRoomUrl = (roomUrl) => {
3728
+ const roomUrlObj = new URL(roomUrl);
3729
+ const urls = media.fromLocation({ host: roomUrlObj.host });
3730
+ const organizationServiceCache = new OrganizationServiceCache({
3731
+ organizationService,
3732
+ subdomain: urls.subdomain,
3733
+ });
3734
+ return organizationServiceCache.fetchOrganization();
3735
+ };
3736
+ return {
3737
+ credentialsService,
3738
+ apiClient,
3739
+ organizationService,
3740
+ fetchOrganizationFromRoomUrl,
3741
+ };
3742
+ }
3743
+
3022
3744
  exports.addAppListener = addAppListener;
3023
3745
  exports.addSpotlight = addSpotlight;
3024
3746
  exports.appSlice = appSlice;
@@ -3041,6 +3763,7 @@ exports.createAuthorizedRoomConnectedThunk = createAuthorizedRoomConnectedThunk;
3041
3763
  exports.createReactor = createReactor;
3042
3764
  exports.createRemoteParticipant = createRemoteParticipant;
3043
3765
  exports.createRoomConnectedThunk = createRoomConnectedThunk;
3766
+ exports.createServices = createServices;
3044
3767
  exports.createStore = createStore;
3045
3768
  exports.createWebRtcEmitter = createWebRtcEmitter;
3046
3769
  exports.deviceBusy = deviceBusy;