|
| 1 | +import type { |
| 2 | + ClerkResourceReloadParams, |
| 3 | + EnterpriseConnectionTestRunJSON, |
| 4 | + EnterpriseConnectionTestRunJSONSnapshot, |
| 5 | + EnterpriseConnectionTestRunLogResource, |
| 6 | + EnterpriseConnectionTestRunOauthPayloadJSON, |
| 7 | + EnterpriseConnectionTestRunOauthPayloadResource, |
| 8 | + EnterpriseConnectionTestRunParsedUserInfoJSON, |
| 9 | + EnterpriseConnectionTestRunParsedUserInfoResource, |
| 10 | + EnterpriseConnectionTestRunResource, |
| 11 | + EnterpriseConnectionTestRunSamlPayloadJSON, |
| 12 | + EnterpriseConnectionTestRunSamlPayloadResource, |
| 13 | +} from '@clerk/shared/types'; |
| 14 | + |
| 15 | +import { unixEpochToDate } from '../../utils/date'; |
| 16 | +import { clerkUnsupportedReloadMethod } from '../errors'; |
| 17 | +import { BaseResource } from './Base'; |
| 18 | + |
| 19 | +export class EnterpriseConnectionTestRun extends BaseResource implements EnterpriseConnectionTestRunResource { |
| 20 | + pathRoot = '/me'; |
| 21 | + private enterpriseConnectionId = ''; |
| 22 | + |
| 23 | + id!: string; |
| 24 | + status!: string; |
| 25 | + connectionType!: 'saml' | 'oauth'; |
| 26 | + parsedUserInfo: EnterpriseConnectionTestRunParsedUserInfoResource | null = null; |
| 27 | + logs: EnterpriseConnectionTestRunLogResource[] = []; |
| 28 | + saml: EnterpriseConnectionTestRunSamlPayloadResource | null = null; |
| 29 | + oauth: EnterpriseConnectionTestRunOauthPayloadResource | null = null; |
| 30 | + createdAt: Date | null = null; |
| 31 | + |
| 32 | + constructor( |
| 33 | + data: EnterpriseConnectionTestRunJSON | EnterpriseConnectionTestRunJSONSnapshot, |
| 34 | + enterpriseConnectionId: string, |
| 35 | + ) { |
| 36 | + super(); |
| 37 | + this.enterpriseConnectionId = enterpriseConnectionId; |
| 38 | + this.fromJSON(data); |
| 39 | + } |
| 40 | + |
| 41 | + protected path(): string { |
| 42 | + return `${this.pathRoot}/enterprise_connections/${this.enterpriseConnectionId}/test_runs/${this.id}`; |
| 43 | + } |
| 44 | + |
| 45 | + reload(_?: ClerkResourceReloadParams): Promise<this> { |
| 46 | + clerkUnsupportedReloadMethod('EnterpriseConnectionTestRun'); |
| 47 | + } |
| 48 | + |
| 49 | + protected fromJSON(data: EnterpriseConnectionTestRunJSON | EnterpriseConnectionTestRunJSONSnapshot | null): this { |
| 50 | + if (!data) { |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + this.id = data.id; |
| 55 | + this.status = data.status; |
| 56 | + this.connectionType = data.connection_type; |
| 57 | + this.parsedUserInfo = parsedUserInfoFromJSON(data.parsed_user_info ?? null); |
| 58 | + this.logs = (data.logs ?? []).map(log => ({ |
| 59 | + level: log.level, |
| 60 | + code: log.code, |
| 61 | + shortMessage: log.short_message, |
| 62 | + message: log.message, |
| 63 | + })); |
| 64 | + this.saml = samlPayloadFromJSON(data.saml ?? null); |
| 65 | + this.oauth = oauthPayloadFromJSON(data.oauth ?? null); |
| 66 | + this.createdAt = unixEpochToDate(data.created_at); |
| 67 | + |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + public __internal_toSnapshot(): EnterpriseConnectionTestRunJSONSnapshot { |
| 72 | + return { |
| 73 | + object: 'enterprise_connection_test_run', |
| 74 | + id: this.id, |
| 75 | + status: this.status, |
| 76 | + connection_type: this.connectionType, |
| 77 | + parsed_user_info: parsedUserInfoToJSON(this.parsedUserInfo), |
| 78 | + logs: this.logs.map(log => ({ |
| 79 | + level: log.level, |
| 80 | + code: log.code, |
| 81 | + short_message: log.shortMessage, |
| 82 | + message: log.message, |
| 83 | + })), |
| 84 | + saml: samlPayloadToJSON(this.saml), |
| 85 | + oauth: oauthPayloadToJSON(this.oauth), |
| 86 | + created_at: this.createdAt?.getTime() ?? 0, |
| 87 | + }; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +function parsedUserInfoFromJSON( |
| 92 | + data: EnterpriseConnectionTestRunParsedUserInfoJSON | null | undefined, |
| 93 | +): EnterpriseConnectionTestRunParsedUserInfoResource | null { |
| 94 | + if (!data) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + emailAddress: data.email_address, |
| 100 | + firstName: data.first_name, |
| 101 | + lastName: data.last_name, |
| 102 | + userId: data.user_id, |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +function parsedUserInfoToJSON( |
| 107 | + data: EnterpriseConnectionTestRunParsedUserInfoResource | null, |
| 108 | +): EnterpriseConnectionTestRunParsedUserInfoJSON | undefined { |
| 109 | + if (!data) { |
| 110 | + return undefined; |
| 111 | + } |
| 112 | + |
| 113 | + return { |
| 114 | + email_address: data.emailAddress, |
| 115 | + first_name: data.firstName, |
| 116 | + last_name: data.lastName, |
| 117 | + user_id: data.userId, |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | +function samlPayloadFromJSON( |
| 122 | + data: EnterpriseConnectionTestRunSamlPayloadJSON | null | undefined, |
| 123 | +): EnterpriseConnectionTestRunSamlPayloadResource | null { |
| 124 | + if (!data) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + return { |
| 129 | + samlRequest: data.saml_request, |
| 130 | + samlResponse: data.saml_response, |
| 131 | + relayState: data.relay_state, |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +function samlPayloadToJSON( |
| 136 | + data: EnterpriseConnectionTestRunSamlPayloadResource | null, |
| 137 | +): EnterpriseConnectionTestRunSamlPayloadJSON | undefined { |
| 138 | + if (!data) { |
| 139 | + return undefined; |
| 140 | + } |
| 141 | + return { |
| 142 | + saml_request: data.samlRequest, |
| 143 | + saml_response: data.samlResponse, |
| 144 | + relay_state: data.relayState, |
| 145 | + }; |
| 146 | +} |
| 147 | + |
| 148 | +function oauthPayloadFromJSON( |
| 149 | + data: EnterpriseConnectionTestRunOauthPayloadJSON | null | undefined, |
| 150 | +): EnterpriseConnectionTestRunOauthPayloadResource | null { |
| 151 | + if (!data) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + return { |
| 155 | + idToken: data.id_token, |
| 156 | + accessToken: data.access_token, |
| 157 | + userInfo: data.user_info, |
| 158 | + }; |
| 159 | +} |
| 160 | + |
| 161 | +function oauthPayloadToJSON( |
| 162 | + data: EnterpriseConnectionTestRunOauthPayloadResource | null, |
| 163 | +): EnterpriseConnectionTestRunOauthPayloadJSON | undefined { |
| 164 | + if (!data) { |
| 165 | + return undefined; |
| 166 | + } |
| 167 | + return { |
| 168 | + id_token: data.idToken, |
| 169 | + access_token: data.accessToken, |
| 170 | + user_info: data.userInfo, |
| 171 | + }; |
| 172 | +} |
0 commit comments