@terros-inc/sdk 1.0.7 → 1.0.8

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/dist/index.d.mts CHANGED
@@ -1,3 +1,35 @@
1
+ //#region ../packages/connect-common/dist/models.d.ts
2
+ /** A Connect API route string, e.g. "user/get" or "account/status/add". */
3
+ type ApiRoute = string;
4
+ type ApiSuccess<T = unknown> = {
5
+ type: 'success';
6
+ } & T;
7
+ //#endregion
8
+ //#region ../packages/connect-common/dist/analytics.d.ts
9
+ type TerrosHeaderMetadata = {
10
+ 'Terros-Platform'?: string;
11
+ 'Terros-Platform-Version'?: string;
12
+ 'Terros-Bundle-Identifier'?: string;
13
+ 'Terros-App-Version'?: string;
14
+ };
15
+ //#endregion
16
+ //#region ../packages/connect-common/dist/terrosApiClient.d.ts
17
+ type TerrosClientConfig = {
18
+ apiKey?: string;
19
+ baseUrl?: string;
20
+ analytics?: TerrosHeaderMetadata;
21
+ };
22
+ declare class TerrosApiClient {
23
+ private readonly baseUrl;
24
+ private readonly authStrategy;
25
+ private readonly analytics;
26
+ constructor(config?: TerrosClientConfig);
27
+ call<Success>(route: ApiRoute, input: object): Promise<Success>;
28
+ private getAuthorizationHeader;
29
+ private determineAuthStrategy;
30
+ private determineBaseUrl;
31
+ }
32
+ //#endregion
1
33
  //#region src/models/team/model.d.ts
2
34
  type TinyTeam = {
3
35
  teamId: TeamId;
@@ -16,6 +48,23 @@ type TinyTeam = {
16
48
  }[];
17
49
  };
18
50
  //#endregion
51
+ //#region src/models/shared.d.ts
52
+ type CustomFieldMap = Record<string, string | number | boolean | null>;
53
+ type AuditLog = {
54
+ userId: UserId;
55
+ timestamp: number;
56
+ updateProcess?: string;
57
+ };
58
+ type AuditProps = {
59
+ modifiedBy?: AuditLog[]; /** @deprecated */
60
+ createdAt?: number; /** @deprecated */
61
+ createdBy?: UserId;
62
+ updatedAt?: number; /** @deprecated */
63
+ updatedBy?: UserId;
64
+ lastUpdateProcess?: string;
65
+ version?: number;
66
+ };
67
+ //#endregion
19
68
  //#region src/models/location/model.d.ts
20
69
  type LatLng = {
21
70
  latitude: number;
@@ -361,36 +410,6 @@ type UserManagerSuccess = ApiSuccess<{
361
410
  primaryManager?: TinyUser;
362
411
  }>;
363
412
  //#endregion
364
- //#region src/models/shared.d.ts
365
- /** A Connect API route string, e.g. "user/get" or "account/status/add". */
366
- type ApiRoute = string;
367
- type ApiSuccess<T = unknown> = {
368
- type: 'success';
369
- } & T;
370
- type ResponseError = {
371
- type: 'error';
372
- error: string;
373
- message?: string;
374
- retry?: boolean;
375
- };
376
- type ResponseOutput = ApiSuccess | ResponseError;
377
- declare function isResponseError(response: unknown): response is ResponseError;
378
- type CustomFieldMap = Record<string, string | number | boolean | null>;
379
- type AuditLog = {
380
- userId: UserId;
381
- timestamp: number;
382
- updateProcess?: string;
383
- };
384
- type AuditProps = {
385
- modifiedBy?: AuditLog[]; /** @deprecated */
386
- createdAt?: number; /** @deprecated */
387
- createdBy?: UserId;
388
- updatedAt?: number; /** @deprecated */
389
- updatedBy?: UserId;
390
- lastUpdateProcess?: string;
391
- version?: number;
392
- };
393
- //#endregion
394
413
  //#region src/models/account/model.d.ts
395
414
  type AccountId = `Account:${string}` | `Account.${string}`;
396
415
  type AccountStatusId = `AS.${string}`;
@@ -941,23 +960,90 @@ type CalendarEventRemoveInput = {
941
960
  eventId: CalendarEventId;
942
961
  };
943
962
  //#endregion
944
- //#region src/apiCaller.d.ts
945
- type ClientConfig = {
946
- apiKey?: string;
947
- baseUrl?: string;
963
+ //#region src/models/connect/app/model.d.ts
964
+ type AppId = `App.${string}`;
965
+ //#endregion
966
+ //#region src/models/connect/script/model.d.ts
967
+ type ConfigSchemaField = {
968
+ name: string;
969
+ type: 'string' | 'number' | 'mapping';
970
+ description?: string;
971
+ dataType?: 'None' | 'Account' | 'User' | 'Event';
948
972
  };
949
- declare class ApiCaller {
950
- private readonly baseUrl;
951
- private readonly auth;
952
- private readonly analytics;
953
- constructor(config: ClientConfig);
954
- call<Success>(route: ApiRoute, input: object): Promise<Success>;
955
- }
973
+ type ScriptBase = {
974
+ appId: AppId;
975
+ appVersion: string;
976
+ name: string;
977
+ description?: string;
978
+ configSchema: ConfigSchemaField[];
979
+ authSchema: Record<string, string>;
980
+ scriptStorage?: 'inline' | 'blob';
981
+ mainScript?: string;
982
+ permissions: string[];
983
+ slug?: string;
984
+ };
985
+ type IncomingScript = ScriptBase & {
986
+ type: 'incoming';
987
+ };
988
+ type OutgoingScript = ScriptBase & {
989
+ type: 'outgoing';
990
+ dataType: 'Account' | 'Company' | 'User' | 'Event';
991
+ };
992
+ type ScheduledScript = ScriptBase & {
993
+ type: 'scheduled';
994
+ cron: string;
995
+ };
996
+ type UnsavedScript = IncomingScript | OutgoingScript | ScheduledScript;
997
+ type ScriptData = UnsavedScript & {
998
+ scriptId: string;
999
+ };
1000
+ //#endregion
1001
+ //#region src/models/connect/script/api.d.ts
1002
+ type ScriptAddInput = {
1003
+ script: UnsavedScript;
1004
+ scriptSize?: number;
1005
+ };
1006
+ type ScriptAddSuccess = ApiSuccess<{
1007
+ script: ScriptData;
1008
+ uploadUrl?: string;
1009
+ }>;
1010
+ //#endregion
1011
+ //#region src/models/connect/version/model.d.ts
1012
+ type AppVersionId = `AppVersion.${string}`;
1013
+ type UnsavedAppVersion = {
1014
+ appId: AppId;
1015
+ appVersion: string;
1016
+ status: 'draft' | 'deployed' | 'deprecated';
1017
+ changelog?: string;
1018
+ permissions?: string[];
1019
+ cache?: {
1020
+ enabled: boolean;
1021
+ ttlSeconds: number;
1022
+ };
1023
+ deployedAt?: number;
1024
+ };
1025
+ type AppVersionData = UnsavedAppVersion & {
1026
+ versionId: AppVersionId;
1027
+ };
1028
+ //#endregion
1029
+ //#region src/models/connect/version/api.d.ts
1030
+ type VersionAddInput = {
1031
+ versionData: UnsavedAppVersion;
1032
+ };
1033
+ type VersionAddSuccess = ApiSuccess<{
1034
+ appVersion: AppVersionData;
1035
+ }>;
1036
+ type VersionUpdateInput = {
1037
+ appVersion: AppVersionData;
1038
+ };
1039
+ type VersionUpdateSuccess = ApiSuccess<{
1040
+ appVersion: AppVersionData;
1041
+ }>;
956
1042
  //#endregion
957
1043
  //#region src/clients/account.d.ts
958
1044
  declare class AccountStatusClient {
959
1045
  private readonly api;
960
- constructor(api: ApiCaller);
1046
+ constructor(api: TerrosApiClient);
961
1047
  add(input: AccountStatusAddInput): Promise<AccountStatusAddSuccess>;
962
1048
  update(input: AccountStatusUpdateInput): Promise<AccountStatusUpdateSuccess>;
963
1049
  list(input: AccountStatusListInput): Promise<AccountStatusListSuccess>;
@@ -966,7 +1052,7 @@ declare class AccountStatusClient {
966
1052
  declare class AccountClient {
967
1053
  private readonly api;
968
1054
  readonly status: AccountStatusClient;
969
- constructor(api: ApiCaller);
1055
+ constructor(api: TerrosApiClient);
970
1056
  add(input: AccountAddInput): Promise<AccountAddSuccess>;
971
1057
  update(input: AccountUpdateInput): Promise<AccountUpdateSuccess>;
972
1058
  updateBatch(input: AccountUpdateBatchInput): Promise<AccountUpdateBatchSuccess>;
@@ -981,7 +1067,7 @@ declare class AccountClient {
981
1067
  //#region src/clients/calendar.d.ts
982
1068
  declare class CalendarEventClient {
983
1069
  private readonly api;
984
- constructor(api: ApiCaller);
1070
+ constructor(api: TerrosApiClient);
985
1071
  add(input: CalendarEventAddInput): Promise<CalendarEventAddSuccess>;
986
1072
  get(input: CalendarEventGetInput): Promise<CalendarEventGetSuccess>;
987
1073
  list(input: CalendarEventListInput): Promise<CalendarEventListSuccess>;
@@ -993,16 +1079,35 @@ declare class CalendarEventClient {
993
1079
  declare class CalendarClient {
994
1080
  private readonly api;
995
1081
  readonly event: CalendarEventClient;
996
- constructor(api: ApiCaller);
1082
+ constructor(api: TerrosApiClient);
997
1083
  availability(input: CalendarAvailabilityInput): Promise<CalendarAvailabilitySuccess>;
998
1084
  closers(input: CalendarClosersInput): Promise<CalendarClosersSuccess>;
999
1085
  timeslots(input: CalendarTimeslotsInput): Promise<CalendarTimeslotsSuccess>;
1000
1086
  }
1001
1087
  //#endregion
1088
+ //#region src/clients/connect.d.ts
1089
+ declare class ConnectAppVersionClient {
1090
+ private readonly api;
1091
+ constructor(api: TerrosApiClient);
1092
+ add(input: VersionAddInput): Promise<VersionAddSuccess>;
1093
+ update(input: VersionUpdateInput): Promise<VersionUpdateSuccess>;
1094
+ }
1095
+ declare class ConnectScriptClient {
1096
+ private readonly api;
1097
+ constructor(api: TerrosApiClient);
1098
+ add(input: ScriptAddInput): Promise<ScriptAddSuccess>;
1099
+ }
1100
+ declare class ConnectClient {
1101
+ private readonly api;
1102
+ readonly version: ConnectAppVersionClient;
1103
+ readonly script: ConnectScriptClient;
1104
+ constructor(api: TerrosApiClient);
1105
+ }
1106
+ //#endregion
1002
1107
  //#region src/clients/user.d.ts
1003
1108
  declare class UserClient {
1004
1109
  private readonly api;
1005
- constructor(api: ApiCaller);
1110
+ constructor(api: TerrosApiClient);
1006
1111
  list(input: UserListInput): Promise<UserListSuccess>;
1007
1112
  add(input: UserAddInput): Promise<UserAddSuccess>;
1008
1113
  update(input: UserUpdateInput): Promise<UserUpdateSuccess>;
@@ -1019,10 +1124,11 @@ declare class UserClient {
1019
1124
  //#endregion
1020
1125
  //#region src/client.d.ts
1021
1126
  declare class TerrosClient {
1022
- readonly user: UserClient;
1023
1127
  readonly account: AccountClient;
1024
1128
  readonly calendar: CalendarClient;
1025
- constructor(config?: ClientConfig);
1129
+ readonly connect: ConnectClient;
1130
+ readonly user: UserClient;
1131
+ constructor(config?: TerrosClientConfig);
1026
1132
  }
1027
1133
  //#endregion
1028
- export { AccessStatus, AccountAddInput, AccountAddSuccess, AccountContact, AccountData, AccountDataResponse, AccountGetInput, AccountGetSuccess, AccountHooksInput, AccountId, AccountListInput, AccountListSuccess, AccountMatchInput, AccountMatchSuccess, AccountNote, AccountPartialUpdateInput, AccountRemoveInput, AccountSortBy, AccountSortOrder, AccountStatusAddInput, AccountStatusAddSuccess, AccountStatusCode, AccountStatusData, AccountStatusId, AccountStatusListInput, AccountStatusListSuccess, AccountStatusRemoveInput, AccountStatusSearchInput, AccountStatusUpdateInput, AccountStatusUpdateSuccess, AccountUpdateBatchInput, AccountUpdateBatchSuccess, AccountUpdateInput, AccountUpdateSuccess, AccountUpsertInput, AccountUpsertSuccess, AccountUserDetails, Address, ApiRoute, ApiSuccess, AttachmentId, AuditLog, AuditProps, AvailableCloser, BillingMethod, BulkModifyAccountsAction, BulkModifyAccountsInput, CalendarAvailabilityInput, CalendarAvailabilitySuccess, CalendarClosersInput, CalendarClosersSuccess, CalendarEventAddInput, CalendarEventAddSuccess, CalendarEventConfirmInput, CalendarEventConfirmSuccess, CalendarEventData, CalendarEventDataWithDetails, CalendarEventGetInput, CalendarEventGetSuccess, CalendarEventId, CalendarEventListInput, CalendarEventListSuccess, CalendarEventLocation, CalendarEventRemoveInput, CalendarEventUpdateInput, CalendarEventUpdateSuccess, CalendarEventUpsertInput, CalendarEventUpsertSuccess, CalendarTimeslotsInput, CalendarTimeslotsSuccess, CloserSettings, CloserStatus, CompanyData, CompanyId, CompanyIndustry, CompanyKind, CompanyState, ContactId, CustomFieldFilter, CustomFieldMap, DispositionId, ElasticFilter, EntityId, EventOutcomeHistory, EventOutcomeId, EventType, InterviewId, LatLng, LocationId, NoteId, NotificationPreference, PartialAddress, PermissionMap, RangeFilter, ResponseError, ResponseOutput, RoleId, SmallAddress, SmallCloser, SmallUser, SortCursor, SpecialtyAttachments, StatusHistoryItem, SyncedCalendarEvent, TagId, TeamId, TerrosClient, TinyAccountStatus, TinyCloser, TinyResidentData, TinyRole, TinyTeam, TinyUser, TinyWorkflowAccount, UnsavedAccount, UnsavedAccountContact, UnsavedAccountNote, UnsavedAccountStatus, UnsavedCalendarEventData, UnsavedUser, UpsertLatLng, UpsertTinyResidentData, UserAddInput, UserAddSuccess, UserBulkUpdateInput, UserBulkUpdateSuccess, UserClosersInput, UserClosersSuccess, UserData, UserGetInput, UserGetSuccess, UserHistory, UserId, UserIdentifier, UserListInput, UserListSuccess, UserManagerInput, UserManagerSuccess, UserPreferences, UserProfileData, UserProfileInput, UserProfileSuccess, UserRemoveInput, UserReportInput, UserReportSuccess, UserSearchInput, UserSearchSuccess, UserTeamData, UserTeamsByLevel, UserTimezoneInput, UserUpdateInput, UserUpdateSuccess, Weekday, WorkflowActionId, WorkflowHistoryItem, WorkflowId, WorkflowStageId, isResponseError };
1134
+ export { AccessStatus, AccountAddInput, AccountAddSuccess, AccountContact, AccountData, AccountDataResponse, AccountGetInput, AccountGetSuccess, AccountHooksInput, AccountId, AccountListInput, AccountListSuccess, AccountMatchInput, AccountMatchSuccess, AccountNote, AccountPartialUpdateInput, AccountRemoveInput, AccountSortBy, AccountSortOrder, AccountStatusAddInput, AccountStatusAddSuccess, AccountStatusCode, AccountStatusData, AccountStatusId, AccountStatusListInput, AccountStatusListSuccess, AccountStatusRemoveInput, AccountStatusSearchInput, AccountStatusUpdateInput, AccountStatusUpdateSuccess, AccountUpdateBatchInput, AccountUpdateBatchSuccess, AccountUpdateInput, AccountUpdateSuccess, AccountUpsertInput, AccountUpsertSuccess, AccountUserDetails, Address, AppId, AppVersionData, AppVersionId, AttachmentId, AuditLog, AuditProps, AvailableCloser, BillingMethod, BulkModifyAccountsAction, BulkModifyAccountsInput, CalendarAvailabilityInput, CalendarAvailabilitySuccess, CalendarClosersInput, CalendarClosersSuccess, CalendarEventAddInput, CalendarEventAddSuccess, CalendarEventConfirmInput, CalendarEventConfirmSuccess, CalendarEventData, CalendarEventDataWithDetails, CalendarEventGetInput, CalendarEventGetSuccess, CalendarEventId, CalendarEventListInput, CalendarEventListSuccess, CalendarEventLocation, CalendarEventRemoveInput, CalendarEventUpdateInput, CalendarEventUpdateSuccess, CalendarEventUpsertInput, CalendarEventUpsertSuccess, CalendarTimeslotsInput, CalendarTimeslotsSuccess, CloserSettings, CloserStatus, CompanyData, CompanyId, CompanyIndustry, CompanyKind, CompanyState, ConfigSchemaField, ContactId, CustomFieldFilter, CustomFieldMap, DispositionId, ElasticFilter, EntityId, EventOutcomeHistory, EventOutcomeId, EventType, IncomingScript, InterviewId, LatLng, LocationId, NoteId, NotificationPreference, OutgoingScript, PartialAddress, PermissionMap, RangeFilter, RoleId, ScheduledScript, ScriptAddInput, ScriptAddSuccess, ScriptBase, ScriptData, SmallAddress, SmallCloser, SmallUser, SortCursor, SpecialtyAttachments, StatusHistoryItem, SyncedCalendarEvent, TagId, TeamId, TerrosClient, TinyAccountStatus, TinyCloser, TinyResidentData, TinyRole, TinyTeam, TinyUser, TinyWorkflowAccount, UnsavedAccount, UnsavedAccountContact, UnsavedAccountNote, UnsavedAccountStatus, UnsavedAppVersion, UnsavedCalendarEventData, UnsavedScript, UnsavedUser, UpsertLatLng, UpsertTinyResidentData, UserAddInput, UserAddSuccess, UserBulkUpdateInput, UserBulkUpdateSuccess, UserClosersInput, UserClosersSuccess, UserData, UserGetInput, UserGetSuccess, UserHistory, UserId, UserIdentifier, UserListInput, UserListSuccess, UserManagerInput, UserManagerSuccess, UserPreferences, UserProfileData, UserProfileInput, UserProfileSuccess, UserRemoveInput, UserReportInput, UserReportSuccess, UserSearchInput, UserSearchSuccess, UserTeamData, UserTeamsByLevel, UserTimezoneInput, UserUpdateInput, UserUpdateSuccess, VersionAddInput, VersionAddSuccess, VersionUpdateInput, VersionUpdateSuccess, Weekday, WorkflowActionId, WorkflowHistoryItem, WorkflowId, WorkflowStageId };
package/dist/index.mjs CHANGED
@@ -1,4 +1,156 @@
1
- import { platform, release } from "node:os";
1
+ import { join } from "node:path";
2
+ import { homedir, platform, release } from "node:os";
3
+ import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
4
+ //#region ../packages/connect-common/dist/models.js
5
+ function isResponseError(response) {
6
+ return typeof response === "object" && response !== null && response.type === "error" && Boolean(response.error);
7
+ }
8
+ //#endregion
9
+ //#region ../packages/connect-common/dist/oauth/constants.js
10
+ const AUTH0_CLIENT_ID = "Aj2tDQZgcN8de0k4jvsndHILsLvyDh5o";
11
+ const AUTH0_DOMAIN = "https://auth.terros.com";
12
+ //#endregion
13
+ //#region ../packages/connect-common/dist/oauth/auth0.js
14
+ async function refreshTokens(refreshToken) {
15
+ const res = await fetch(`${AUTH0_DOMAIN}/oauth/token`, {
16
+ method: "POST",
17
+ headers: { "Content-Type": "application/json" },
18
+ body: JSON.stringify({
19
+ client_id: AUTH0_CLIENT_ID,
20
+ grant_type: "refresh_token",
21
+ refresh_token: refreshToken
22
+ })
23
+ });
24
+ if (res.ok) return await res.json();
25
+ throw new Error("Unable to refresh token, it may be expired. Run `terros auth login` to sign in again");
26
+ }
27
+ //#endregion
28
+ //#region ../packages/connect-common/dist/oauth/tokens.js
29
+ const AUTH_DIR_MODE = 448;
30
+ const AUTH_FILE_MODE = 384;
31
+ const SECOND = 1e3;
32
+ const MINUTE = 60 * SECOND;
33
+ async function getTokens() {
34
+ const tokens = await readTokens();
35
+ if (tokens === null) return null;
36
+ if (areTokensValid(tokens)) return tokens;
37
+ return await saveTokens(await refreshTokens(tokens.refresh_token));
38
+ }
39
+ function areTokensValid(tokens) {
40
+ return tokens.expires_at > Date.now() + MINUTE * 5;
41
+ }
42
+ async function readTokens() {
43
+ try {
44
+ const file = await readFile(await getAuthFilePath(), "utf-8");
45
+ return JSON.parse(file);
46
+ } catch (e) {
47
+ return null;
48
+ }
49
+ }
50
+ async function saveTokens(tokenResponse) {
51
+ const expiresAt = Date.now() + tokenResponse.expires_in * SECOND;
52
+ const savedTokens = {
53
+ access_token: tokenResponse.access_token,
54
+ refresh_token: tokenResponse.refresh_token,
55
+ id_token: tokenResponse.id_token,
56
+ token_type: tokenResponse.token_type,
57
+ expires_at: expiresAt
58
+ };
59
+ const authFile = await getAuthFilePath();
60
+ await writeFile(authFile, JSON.stringify(savedTokens), { mode: AUTH_FILE_MODE });
61
+ await chmod(authFile, AUTH_FILE_MODE);
62
+ return savedTokens;
63
+ }
64
+ async function getAuthFilePath() {
65
+ const configPath = join(homedir(), ".config", "terros");
66
+ await mkdir(configPath, {
67
+ recursive: true,
68
+ mode: AUTH_DIR_MODE
69
+ });
70
+ await chmod(configPath, AUTH_DIR_MODE);
71
+ return join(configPath, "auth.json");
72
+ }
73
+ //#endregion
74
+ //#region ../packages/connect-common/dist/analytics.js
75
+ function getAnalyticsHeaders(override = {}) {
76
+ if (typeof globalThis.process === "undefined") return {};
77
+ try {
78
+ return {
79
+ "Terros-Platform": platform(),
80
+ "Terros-Platform-Version": release(),
81
+ "Terros-Bundle-Identifier": "com.terros.sdk",
82
+ ...override
83
+ };
84
+ } catch {
85
+ return {};
86
+ }
87
+ }
88
+ //#endregion
89
+ //#region ../packages/connect-common/dist/terrosApiClient.js
90
+ const PROD_BASE_URL = "https://api.terros.com";
91
+ var TerrosApiClient = class {
92
+ baseUrl;
93
+ authStrategy;
94
+ analytics;
95
+ constructor(config = {}) {
96
+ this.baseUrl = this.determineBaseUrl(config);
97
+ this.analytics = getAnalyticsHeaders(config.analytics);
98
+ this.authStrategy = this.determineAuthStrategy(config);
99
+ }
100
+ async call(route, input) {
101
+ let response;
102
+ try {
103
+ const authorization = await this.getAuthorizationHeader();
104
+ response = await fetch(`${this.baseUrl}/${route}`, {
105
+ method: "POST",
106
+ headers: {
107
+ ...this.analytics,
108
+ "Content-Type": "application/json",
109
+ authorization
110
+ },
111
+ body: JSON.stringify(input)
112
+ });
113
+ } catch (cause) {
114
+ throw new Error(`Request to ${route} failed`, { cause });
115
+ }
116
+ if (!response.ok) throw new Error(response.statusText);
117
+ const output = await response.json();
118
+ if (isResponseError(output)) throw new Error(output.message ?? output.error);
119
+ return output;
120
+ }
121
+ async getAuthorizationHeader() {
122
+ if (this.authStrategy.type === "apikey") return `ApiKey ${this.authStrategy.apiKey}`;
123
+ const cliTokens = await getTokens();
124
+ if (cliTokens === null) throw new Error("No authorization found. Log in with Terros CLI or provide an API key via environment variables or client config.");
125
+ return `Bearer ${cliTokens.access_token}`;
126
+ }
127
+ determineAuthStrategy(config) {
128
+ if (config.apiKey) return {
129
+ type: "apikey",
130
+ apiKey: config.apiKey
131
+ };
132
+ const apiKeyFromEnv = readProcessEnv("TERROS_API_KEY");
133
+ if (apiKeyFromEnv) return {
134
+ type: "apikey",
135
+ apiKey: apiKeyFromEnv
136
+ };
137
+ return { type: "oauth" };
138
+ }
139
+ determineBaseUrl(config) {
140
+ if (config.baseUrl) return config.baseUrl;
141
+ const envEndpoint = readProcessEnv("TERROS_API_ENDPOINT");
142
+ if (envEndpoint) return envEndpoint;
143
+ return PROD_BASE_URL;
144
+ }
145
+ };
146
+ function readProcessEnv(key) {
147
+ if (typeof globalThis.process === "undefined") return void 0;
148
+ return globalThis.process.env[key];
149
+ }
150
+ //#endregion
151
+ //#region package.json
152
+ var version = "1.0.8";
153
+ //#endregion
2
154
  //#region src/clients/account.ts
3
155
  var AccountStatusClient = class {
4
156
  api;
@@ -100,6 +252,39 @@ var CalendarClient = class {
100
252
  }
101
253
  };
102
254
  //#endregion
255
+ //#region src/clients/connect.ts
256
+ var ConnectAppVersionClient = class {
257
+ api;
258
+ constructor(api) {
259
+ this.api = api;
260
+ }
261
+ add(input) {
262
+ return this.api.call("connect/app/version/add", input);
263
+ }
264
+ update(input) {
265
+ return this.api.call("connect/app/version/update", input);
266
+ }
267
+ };
268
+ var ConnectScriptClient = class {
269
+ api;
270
+ constructor(api) {
271
+ this.api = api;
272
+ }
273
+ add(input) {
274
+ return this.api.call("connect/script/add", input);
275
+ }
276
+ };
277
+ var ConnectClient = class {
278
+ api;
279
+ version;
280
+ script;
281
+ constructor(api) {
282
+ this.api = api;
283
+ this.version = new ConnectAppVersionClient(api);
284
+ this.script = new ConnectScriptClient(api);
285
+ }
286
+ };
287
+ //#endregion
103
288
  //#region src/clients/user.ts
104
289
  var UserClient = class {
105
290
  api;
@@ -144,79 +329,22 @@ var UserClient = class {
144
329
  }
145
330
  };
146
331
  //#endregion
147
- //#region src/models/shared.ts
148
- function isResponseError(response) {
149
- return typeof response === "object" && response !== null && response.type === "error" && Boolean(response.error);
150
- }
151
- //#endregion
152
- //#region package.json
153
- var version = "1.0.7";
154
- //#endregion
155
- //#region src/analytics.ts
156
- function getAnalyticsHeaders() {
157
- if (typeof globalThis.process === "undefined") return {};
158
- try {
159
- return {
160
- "Terros-Platform": platform(),
161
- "Terros-Platform-Version": release(),
162
- "Terros-Bundle-Identifier": "com.terros.sdk",
163
- "Terros-App-Version": version
164
- };
165
- } catch {
166
- return {};
167
- }
168
- }
169
- //#endregion
170
- //#region src/apiCaller.ts
171
- const PROD_BASE_URL = "https://api.terros.com";
172
- var ApiCaller = class {
173
- baseUrl;
174
- auth;
175
- analytics;
176
- constructor(config) {
177
- this.baseUrl = config.baseUrl ?? readProcessEnv("TERROS_SDK_BASE_URL") ?? PROD_BASE_URL;
178
- this.analytics = getAnalyticsHeaders();
179
- const auth = config.apiKey ?? readProcessEnv("TERROS_API_KEY");
180
- if (!auth) throw new Error("No auth provided: set TERROS_API_KEY or pass config.auth");
181
- this.auth = auth;
182
- }
183
- async call(route, input) {
184
- let response;
185
- try {
186
- response = await fetch(`${this.baseUrl}/${route}`, {
187
- method: "POST",
188
- headers: {
189
- ...this.analytics,
190
- "Content-Type": "application/json",
191
- authorization: `ApiKey ${this.auth}`
192
- },
193
- body: JSON.stringify(input)
194
- });
195
- } catch (cause) {
196
- throw new Error(`Request to ${route} failed`, { cause });
197
- }
198
- if (!response.ok) throw new Error(response.statusText);
199
- const output = await response.json();
200
- if (isResponseError(output)) throw new Error(output.message ?? output.error);
201
- return output;
202
- }
203
- };
204
- function readProcessEnv(key) {
205
- if (typeof globalThis.process === "undefined") return void 0;
206
- return globalThis.process.env[key];
207
- }
208
- //#endregion
209
332
  //#region src/client.ts
210
333
  var TerrosClient = class {
211
- user;
212
334
  account;
213
335
  calendar;
336
+ connect;
337
+ user;
214
338
  constructor(config = {}) {
215
- const api = new ApiCaller(config);
216
- this.user = new UserClient(api);
339
+ const api = new TerrosApiClient({
340
+ ...config,
341
+ analytics: { "Terros-App-Version": version }
342
+ });
217
343
  this.account = new AccountClient(api);
218
344
  this.calendar = new CalendarClient(api);
345
+ this.connect = new ConnectClient(api);
346
+ this.user = new UserClient(api);
219
347
  }
220
348
  };
221
349
  //#endregion
222
- export { TerrosClient, isResponseError };
350
+ export { TerrosClient };
package/package.json CHANGED
@@ -8,7 +8,8 @@
8
8
  "@types/node": "^24.13.2",
9
9
  "tsdown": "^0.22.3",
10
10
  "typescript": "^6.0.3",
11
- "vitest": "^4.1.10"
11
+ "vitest": "^4.1.10",
12
+ "@terros-inc/connect-common": "^1.0.0"
12
13
  },
13
14
  "engines": {
14
15
  "node": "^24"
@@ -35,9 +36,10 @@
35
36
  "type": "git",
36
37
  "url": "git+https://github.com/terros-inc/connect.git"
37
38
  },
39
+ "sideEffects": false,
38
40
  "type": "module",
39
41
  "types": "./dist/index.d.mts",
40
- "version": "1.0.7",
42
+ "version": "1.0.8",
41
43
  "scripts": {
42
44
  "build": "tsdown",
43
45
  "test": "vitest run"