@@ -2,8 +2,10 @@ import { getFullName } from '@clerk/shared/internal/clerk-js/user';
22import type {
33 BackupCodeJSON ,
44 BackupCodeResource ,
5+ ClerkPaginatedResponse ,
56 CreateEmailAddressParams ,
67 CreateExternalAccountParams ,
8+ CreateMeEnterpriseConnectionParams ,
79 CreatePhoneNumberParams ,
810 CreateWeb3WalletParams ,
911 DeletedObjectJSON ,
@@ -12,9 +14,15 @@ import type {
1214 EnterpriseAccountResource ,
1315 EnterpriseConnectionJSON ,
1416 EnterpriseConnectionResource ,
17+ EnterpriseConnectionTestRunInitJSON ,
18+ EnterpriseConnectionTestRunInitResource ,
19+ EnterpriseConnectionTestRunJSON ,
20+ EnterpriseConnectionTestRunResource ,
21+ EnterpriseConnectionTestRunsPaginatedJSON ,
1522 ExternalAccountJSON ,
1623 ExternalAccountResource ,
1724 GetEnterpriseConnectionsParams ,
25+ GetEnterpriseConnectionTestRunsParams ,
1826 GetOrganizationMemberships ,
1927 GetUserOrganizationInvitationsParams ,
2028 GetUserOrganizationSuggestionsParams ,
@@ -26,6 +34,7 @@ import type {
2634 SetProfileImageParams ,
2735 TOTPJSON ,
2836 TOTPResource ,
37+ UpdateMeEnterpriseConnectionParams ,
2938 UpdateUserParams ,
3039 UpdateUserPasswordParams ,
3140 UserJSON ,
@@ -34,7 +43,9 @@ import type {
3443 VerifyTOTPParams ,
3544 Web3WalletResource ,
3645} from '@clerk/shared/types' ;
46+ import { deepCamelToSnake } from '@clerk/shared/underscore' ;
3747
48+ import { convertPageToOffsetSearchParams } from '../../utils/convertPageToOffsetSearchParams' ;
3849import { unixEpochToDate } from '../../utils/date' ;
3950import { normalizeUnsafeMetadata } from '../../utils/resourceParams' ;
4051import { eventBus , events } from '../events' ;
@@ -46,6 +57,7 @@ import {
4657 EmailAddress ,
4758 EnterpriseAccount ,
4859 EnterpriseConnection ,
60+ EnterpriseConnectionTestRun ,
4961 ExternalAccount ,
5062 Image ,
5163 OrganizationMembership ,
@@ -316,6 +328,85 @@ export class User extends BaseResource implements UserResource {
316328 return ( json || [ ] ) . map ( connection => new EnterpriseConnection ( connection ) ) ;
317329 } ;
318330
331+ createEnterpriseConnection = async (
332+ params : CreateMeEnterpriseConnectionParams ,
333+ ) : Promise < EnterpriseConnectionResource > => {
334+ const json = (
335+ await BaseResource . _fetch < EnterpriseConnectionJSON > ( {
336+ path : `${ this . path ( ) } /enterprise_connections` ,
337+ method : 'POST' ,
338+ body : toMeEnterpriseConnectionBody ( params ) as any ,
339+ } )
340+ ) ?. response as unknown as EnterpriseConnectionJSON ;
341+
342+ return new EnterpriseConnection ( json ) ;
343+ } ;
344+
345+ updateEnterpriseConnection = async (
346+ enterpriseConnectionId : string ,
347+ params : UpdateMeEnterpriseConnectionParams ,
348+ ) : Promise < EnterpriseConnectionResource > => {
349+ const json = (
350+ await BaseResource . _fetch < EnterpriseConnectionJSON > ( {
351+ path : `${ this . path ( ) } /enterprise_connections/${ enterpriseConnectionId } ` ,
352+ method : 'PATCH' ,
353+ body : toMeEnterpriseConnectionBody ( params ) as any ,
354+ } )
355+ ) ?. response as unknown as EnterpriseConnectionJSON ;
356+
357+ return new EnterpriseConnection ( json ) ;
358+ } ;
359+
360+ deleteEnterpriseConnection = async ( enterpriseConnectionId : string ) : Promise < DeletedObjectResource > => {
361+ const json = (
362+ await BaseResource . _fetch < DeletedObjectJSON > ( {
363+ path : `${ this . path ( ) } /enterprise_connections/${ enterpriseConnectionId } ` ,
364+ method : 'DELETE' ,
365+ } )
366+ ) ?. response as unknown as DeletedObjectJSON ;
367+
368+ return new DeletedObject ( json ) ;
369+ } ;
370+
371+ createEnterpriseConnectionTestRun = async (
372+ enterpriseConnectionId : string ,
373+ ) : Promise < EnterpriseConnectionTestRunInitResource > => {
374+ const json = (
375+ await BaseResource . _fetch ( {
376+ path : `${ this . path ( ) } /enterprise_connections/${ enterpriseConnectionId } /test_runs` ,
377+ method : 'POST' ,
378+ } )
379+ ) ?. response as unknown as EnterpriseConnectionTestRunInitJSON ;
380+
381+ return { url : json . url } ;
382+ } ;
383+
384+ getEnterpriseConnectionTestRuns = async (
385+ enterpriseConnectionId : string ,
386+ params ?: GetEnterpriseConnectionTestRunsParams ,
387+ ) : Promise < ClerkPaginatedResponse < EnterpriseConnectionTestRunResource > > => {
388+ const { status, ...rest } = params || { } ;
389+ const search = convertPageToOffsetSearchParams ( rest ) ;
390+ if ( status ?. length ) {
391+ for ( const s of status ) {
392+ search . append ( 'status' , s ) ;
393+ }
394+ }
395+
396+ const res = await BaseResource . _fetch ( {
397+ path : `${ this . path ( ) } /enterprise_connections/${ enterpriseConnectionId } /test_runs` ,
398+ method : 'GET' ,
399+ search,
400+ } ) ;
401+
402+ const payload = res ?. response as unknown as EnterpriseConnectionTestRunsPaginatedJSON | undefined ;
403+
404+ return {
405+ total_count : payload ?. total_count ?? 0 ,
406+ data : ( payload ?. data ?? [ ] ) . map ( ( row : EnterpriseConnectionTestRunJSON ) => new EnterpriseConnectionTestRun ( row ) ) ,
407+ } ;
408+ } ;
409+
319410 initializePaymentMethod : typeof initializePaymentMethod = params => {
320411 return initializePaymentMethod ( params ) ;
321412 } ;
@@ -455,3 +546,30 @@ export class User extends BaseResource implements UserResource {
455546 } ;
456547 }
457548}
549+
550+ /**
551+ * Serializes `CreateMeEnterpriseConnectionParams` / `UpdateMeEnterpriseConnectionParams`
552+ * for the `/me/enterprise_connections` FAPI endpoints.
553+ *
554+ * Uses `deepCamelToSnake` but preserves `saml.attributeMapping` and `customAttributes` as-is. Their keys are
555+ * user-supplied data and must not be camel→snake transformed.
556+ */
557+ function toMeEnterpriseConnectionBody (
558+ params : CreateMeEnterpriseConnectionParams | UpdateMeEnterpriseConnectionParams ,
559+ ) : Record < string , unknown > {
560+ const originalAttributeMapping =
561+ params . saml && typeof params . saml === 'object' ? params . saml . attributeMapping : undefined ;
562+ const originalCustomAttributes = 'customAttributes' in params ? params . customAttributes : undefined ;
563+
564+ const body = deepCamelToSnake ( params ) as Record < string , any > ;
565+
566+ if ( originalAttributeMapping !== undefined && body . saml && typeof body . saml === 'object' ) {
567+ body . saml . attribute_mapping = originalAttributeMapping ;
568+ }
569+
570+ if ( originalCustomAttributes !== undefined ) {
571+ body . custom_attributes = originalCustomAttributes ;
572+ }
573+
574+ return body ;
575+ }
0 commit comments