Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/pink-dolls-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': minor
'@clerk/shared': minor
---

Add internal API methods to manage enterprise connections
172 changes: 172 additions & 0 deletions packages/clerk-js/src/core/resources/EnterpriseConnectionTestRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import type {
ClerkResourceReloadParams,
EnterpriseConnectionTestRunJSON,
EnterpriseConnectionTestRunJSONSnapshot,
EnterpriseConnectionTestRunLogResource,
EnterpriseConnectionTestRunOauthPayloadJSON,
EnterpriseConnectionTestRunOauthPayloadResource,
EnterpriseConnectionTestRunParsedUserInfoJSON,
EnterpriseConnectionTestRunParsedUserInfoResource,
EnterpriseConnectionTestRunResource,
EnterpriseConnectionTestRunSamlPayloadJSON,
EnterpriseConnectionTestRunSamlPayloadResource,
} from '@clerk/shared/types';

import { unixEpochToDate } from '../../utils/date';
import { clerkUnsupportedReloadMethod } from '../errors';
import { BaseResource } from './Base';

export class EnterpriseConnectionTestRun extends BaseResource implements EnterpriseConnectionTestRunResource {
pathRoot = '/me';
private enterpriseConnectionId = '';

id!: string;
status!: string;
connectionType!: 'saml' | 'oauth';
parsedUserInfo: EnterpriseConnectionTestRunParsedUserInfoResource | null = null;
logs: EnterpriseConnectionTestRunLogResource[] = [];
saml: EnterpriseConnectionTestRunSamlPayloadResource | null = null;
oauth: EnterpriseConnectionTestRunOauthPayloadResource | null = null;
createdAt: Date | null = null;

constructor(data: EnterpriseConnectionTestRunJSON, enterpriseConnectionId: string) {
super();
this.enterpriseConnectionId = enterpriseConnectionId;
this.fromJSON(data);
}

protected path(): string {
return `${this.pathRoot}/enterprise_connections/${this.enterpriseConnectionId}/test_runs/${this.id}`;
}

reload(_?: ClerkResourceReloadParams): Promise<this> {
clerkUnsupportedReloadMethod('EnterpriseConnectionTestRun');
}

protected fromJSON(data: EnterpriseConnectionTestRunJSON | null): this {
if (!data) {
return this;
}

this.id = data.id;
this.status = data.status;
this.connectionType = data.connection_type;
this.parsedUserInfo = parsedUserInfoFromJSON(data.parsed_user_info ?? null);
this.saml = samlPayloadFromJSON(data.saml ?? null);
this.oauth = oauthPayloadFromJSON(data.oauth ?? null);
this.createdAt = unixEpochToDate(data.created_at);
this.logs = (data.logs ?? []).map(log => ({
level: log.level,
code: log.code,
shortMessage: log.short_message,
message: log.message,
}));

return this;
}

public __internal_toSnapshot(): EnterpriseConnectionTestRunJSONSnapshot {
return {
object: 'enterprise_connection_test_run',
id: this.id,
status: this.status,
connection_type: this.connectionType,
parsed_user_info: parsedUserInfoToJSON(this.parsedUserInfo),
saml: samlPayloadToJSON(this.saml),
oauth: oauthPayloadToJSON(this.oauth),
logs: this.logs.map(log => ({
level: log.level,
code: log.code,
short_message: log.shortMessage,
message: log.message,
})),
created_at: this.createdAt?.getTime() ?? 0,
};
}
}

function parsedUserInfoFromJSON(
data: EnterpriseConnectionTestRunParsedUserInfoJSON | null | undefined,
): EnterpriseConnectionTestRunParsedUserInfoResource | null {
if (!data) {
return null;
}

return {
emailAddress: data.email_address,
firstName: data.first_name,
lastName: data.last_name,
userId: data.user_id,
};
}

function parsedUserInfoToJSON(
data: EnterpriseConnectionTestRunParsedUserInfoResource | null,
): EnterpriseConnectionTestRunParsedUserInfoJSON | null {
if (!data) {
return null;
}

return {
email_address: data.emailAddress,
first_name: data.firstName,
last_name: data.lastName,
user_id: data.userId,
};
}

function samlPayloadFromJSON(
data: EnterpriseConnectionTestRunSamlPayloadJSON | null | undefined,
): EnterpriseConnectionTestRunSamlPayloadResource | null {
if (!data) {
return null;
}

return {
samlRequest: data.saml_request,
samlResponse: data.saml_response,
relayState: data.relay_state,
};
}

function samlPayloadToJSON(
data: EnterpriseConnectionTestRunSamlPayloadResource | null,
): EnterpriseConnectionTestRunSamlPayloadJSON | null {
if (!data) {
return null;
}

return {
saml_request: data.samlRequest,
saml_response: data.samlResponse,
relay_state: data.relayState,
};
}

function oauthPayloadFromJSON(
data: EnterpriseConnectionTestRunOauthPayloadJSON | null | undefined,
): EnterpriseConnectionTestRunOauthPayloadResource | null {
if (!data) {
return null;
}

return {
idToken: data.id_token,
accessToken: data.access_token,
userInfo: data.user_info,
};
}

function oauthPayloadToJSON(
data: EnterpriseConnectionTestRunOauthPayloadResource | null,
): EnterpriseConnectionTestRunOauthPayloadJSON | null {
if (!data) {
return null;
}

return {
id_token: data.idToken,
access_token: data.accessToken,
user_info: data.userInfo,
};
}
93 changes: 93 additions & 0 deletions packages/clerk-js/src/core/resources/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getFullName } from '@clerk/shared/internal/clerk-js/user';
import { deepCamelToSnake } from '@clerk/shared/underscore';
import type {
BackupCodeJSON,
BackupCodeResource,
Expand All @@ -14,7 +15,16 @@ import type {
EnterpriseConnectionResource,
ExternalAccountJSON,
ExternalAccountResource,
ClerkPaginatedResponse,
CreateMeEnterpriseConnectionParams,
EnterpriseConnectionTestRunInitJSON,
EnterpriseConnectionTestRunJSON,
EnterpriseConnectionTestRunsPaginatedJSON,
EnterpriseConnectionTestRunInitResource,
EnterpriseConnectionTestRunResource,
GetEnterpriseConnectionTestRunsParams,
GetEnterpriseConnectionsParams,
UpdateMeEnterpriseConnectionParams,
GetOrganizationMemberships,
GetUserOrganizationInvitationsParams,
GetUserOrganizationSuggestionsParams,
Expand All @@ -36,6 +46,7 @@ import type {
} from '@clerk/shared/types';

import { unixEpochToDate } from '../../utils/date';
import { convertPageToOffsetSearchParams } from '../../utils/convertPageToOffsetSearchParams';
import { normalizeUnsafeMetadata } from '../../utils/resourceParams';
import { eventBus, events } from '../events';
import { addPaymentMethod, getPaymentMethods, initializePaymentMethod } from '../modules/billing';
Expand All @@ -46,6 +57,7 @@ import {
EmailAddress,
EnterpriseAccount,
EnterpriseConnection,
EnterpriseConnectionTestRun,
ExternalAccount,
Image,
OrganizationMembership,
Expand Down Expand Up @@ -316,6 +328,87 @@ export class User extends BaseResource implements UserResource {
return (json || []).map(connection => new EnterpriseConnection(connection));
};

createEnterpriseConnection = async (
params: CreateMeEnterpriseConnectionParams,
): Promise<EnterpriseConnectionResource> => {
const json = (
await BaseResource._fetch<EnterpriseConnectionJSON>({
path: `${this.path()}/enterprise_connections`,
method: 'POST',
body: deepCamelToSnake(params) as any,
})
)?.response as unknown as EnterpriseConnectionJSON;

return new EnterpriseConnection(json);
};

updateEnterpriseConnection = async (
enterpriseConnectionId: string,
params: UpdateMeEnterpriseConnectionParams,
): Promise<EnterpriseConnectionResource> => {
const json = (
await BaseResource._fetch<EnterpriseConnectionJSON>({
path: `${this.path()}/enterprise_connections/${enterpriseConnectionId}`,
method: 'PATCH',
body: deepCamelToSnake(params) as any,
})
)?.response as unknown as EnterpriseConnectionJSON;

return new EnterpriseConnection(json);
};

deleteEnterpriseConnection = async (enterpriseConnectionId: string): Promise<DeletedObjectResource> => {
const json = (
await BaseResource._fetch<DeletedObjectJSON>({
path: `${this.path()}/enterprise_connections/${enterpriseConnectionId}`,
method: 'DELETE',
})
)?.response as unknown as DeletedObjectJSON;

return new DeletedObject(json);
};

createEnterpriseConnectionTestRun = async (
enterpriseConnectionId: string,
): Promise<EnterpriseConnectionTestRunInitResource> => {
const json = (
await BaseResource._fetch({
path: `${this.path()}/enterprise_connections/${enterpriseConnectionId}/test_runs`,
method: 'POST',
})
)?.response as unknown as EnterpriseConnectionTestRunInitJSON;

return { url: json.url };
};

getEnterpriseConnectionTestRuns = async (
enterpriseConnectionId: string,
params?: GetEnterpriseConnectionTestRunsParams,
): Promise<ClerkPaginatedResponse<EnterpriseConnectionTestRunResource>> => {
const { status, ...rest } = params || {};
const search = convertPageToOffsetSearchParams({ ...rest, paginated: true });
if (status?.length) {
for (const s of status) {
search.append('status', s);
}
}

const res = await BaseResource._fetch({
path: `${this.path()}/enterprise_connections/${enterpriseConnectionId}/test_runs`,
method: 'GET',
search,
});

const payload = res?.response as unknown as EnterpriseConnectionTestRunsPaginatedJSON | undefined;

return {
total_count: payload?.total_count ?? 0,
data: (payload?.data ?? []).map(
(row: EnterpriseConnectionTestRunJSON) => new EnterpriseConnectionTestRun(row, enterpriseConnectionId),
),
};
};

initializePaymentMethod: typeof initializePaymentMethod = params => {
return initializePaymentMethod(params);
};
Expand Down
Loading
Loading