@terros-inc/sdk 1.0.7 → 1.0.9
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 +198 -49
- package/dist/index.mjs +204 -67
- package/package.json +4 -2
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,115 @@ type CalendarEventRemoveInput = {
|
|
|
941
960
|
eventId: CalendarEventId;
|
|
942
961
|
};
|
|
943
962
|
//#endregion
|
|
944
|
-
//#region src/
|
|
945
|
-
type
|
|
946
|
-
|
|
947
|
-
|
|
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
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
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
|
+
}>;
|
|
1042
|
+
//#endregion
|
|
1043
|
+
//#region src/models/webhook.d.ts
|
|
1044
|
+
type WebhookAuditProps = {
|
|
1045
|
+
createdAt: string;
|
|
1046
|
+
updatedAt: string;
|
|
1047
|
+
};
|
|
1048
|
+
type WebhookAddUpdatePayload<Entity extends string, Payload> = {
|
|
1049
|
+
entity: Entity;
|
|
1050
|
+
action: 'add' | 'update';
|
|
1051
|
+
data: Payload;
|
|
1052
|
+
};
|
|
1053
|
+
type WebhookRemovePayload<Entity extends string, Payload> = {
|
|
1054
|
+
entity: Entity;
|
|
1055
|
+
action: 'remove';
|
|
1056
|
+
data: Payload;
|
|
1057
|
+
};
|
|
1058
|
+
type WebhookPayload<Entity extends string, DataPayload, Key extends keyof DataPayload> = WebhookAddUpdatePayload<Entity, DataPayload> | WebhookRemovePayload<Entity, DataPayload[Key]>;
|
|
1059
|
+
type UserWebhookData = WebhookAuditProps & Pick<UserProfileData, 'firstName' | 'lastName' | 'email' | 'phone' | 'clientUserId' | 'preferredName' | 'timeZone' | 'closerStatus' | 'isDeleted' | 'customFields'> & {
|
|
1060
|
+
id: UserId;
|
|
1061
|
+
};
|
|
1062
|
+
type UserWebhook = WebhookPayload<'User', UserWebhookData, 'id'>;
|
|
1063
|
+
type CompanyWebhookData = WebhookAuditProps & Omit<CompanyData, 'companyId'> & {
|
|
1064
|
+
id: CompanyId;
|
|
1065
|
+
};
|
|
1066
|
+
type CompanyWebhook = WebhookPayload<'Company', CompanyWebhookData, 'id'>;
|
|
956
1067
|
//#endregion
|
|
957
1068
|
//#region src/clients/account.d.ts
|
|
958
1069
|
declare class AccountStatusClient {
|
|
959
1070
|
private readonly api;
|
|
960
|
-
constructor(api:
|
|
1071
|
+
constructor(api: TerrosApiClient);
|
|
961
1072
|
add(input: AccountStatusAddInput): Promise<AccountStatusAddSuccess>;
|
|
962
1073
|
update(input: AccountStatusUpdateInput): Promise<AccountStatusUpdateSuccess>;
|
|
963
1074
|
list(input: AccountStatusListInput): Promise<AccountStatusListSuccess>;
|
|
@@ -966,7 +1077,7 @@ declare class AccountStatusClient {
|
|
|
966
1077
|
declare class AccountClient {
|
|
967
1078
|
private readonly api;
|
|
968
1079
|
readonly status: AccountStatusClient;
|
|
969
|
-
constructor(api:
|
|
1080
|
+
constructor(api: TerrosApiClient);
|
|
970
1081
|
add(input: AccountAddInput): Promise<AccountAddSuccess>;
|
|
971
1082
|
update(input: AccountUpdateInput): Promise<AccountUpdateSuccess>;
|
|
972
1083
|
updateBatch(input: AccountUpdateBatchInput): Promise<AccountUpdateBatchSuccess>;
|
|
@@ -981,7 +1092,7 @@ declare class AccountClient {
|
|
|
981
1092
|
//#region src/clients/calendar.d.ts
|
|
982
1093
|
declare class CalendarEventClient {
|
|
983
1094
|
private readonly api;
|
|
984
|
-
constructor(api:
|
|
1095
|
+
constructor(api: TerrosApiClient);
|
|
985
1096
|
add(input: CalendarEventAddInput): Promise<CalendarEventAddSuccess>;
|
|
986
1097
|
get(input: CalendarEventGetInput): Promise<CalendarEventGetSuccess>;
|
|
987
1098
|
list(input: CalendarEventListInput): Promise<CalendarEventListSuccess>;
|
|
@@ -993,16 +1104,35 @@ declare class CalendarEventClient {
|
|
|
993
1104
|
declare class CalendarClient {
|
|
994
1105
|
private readonly api;
|
|
995
1106
|
readonly event: CalendarEventClient;
|
|
996
|
-
constructor(api:
|
|
1107
|
+
constructor(api: TerrosApiClient);
|
|
997
1108
|
availability(input: CalendarAvailabilityInput): Promise<CalendarAvailabilitySuccess>;
|
|
998
1109
|
closers(input: CalendarClosersInput): Promise<CalendarClosersSuccess>;
|
|
999
1110
|
timeslots(input: CalendarTimeslotsInput): Promise<CalendarTimeslotsSuccess>;
|
|
1000
1111
|
}
|
|
1001
1112
|
//#endregion
|
|
1113
|
+
//#region src/clients/connect.d.ts
|
|
1114
|
+
declare class ConnectAppVersionClient {
|
|
1115
|
+
private readonly api;
|
|
1116
|
+
constructor(api: TerrosApiClient);
|
|
1117
|
+
add(input: VersionAddInput): Promise<VersionAddSuccess>;
|
|
1118
|
+
update(input: VersionUpdateInput): Promise<VersionUpdateSuccess>;
|
|
1119
|
+
}
|
|
1120
|
+
declare class ConnectScriptClient {
|
|
1121
|
+
private readonly api;
|
|
1122
|
+
constructor(api: TerrosApiClient);
|
|
1123
|
+
add(input: ScriptAddInput): Promise<ScriptAddSuccess>;
|
|
1124
|
+
}
|
|
1125
|
+
declare class ConnectClient {
|
|
1126
|
+
private readonly api;
|
|
1127
|
+
readonly version: ConnectAppVersionClient;
|
|
1128
|
+
readonly script: ConnectScriptClient;
|
|
1129
|
+
constructor(api: TerrosApiClient);
|
|
1130
|
+
}
|
|
1131
|
+
//#endregion
|
|
1002
1132
|
//#region src/clients/user.d.ts
|
|
1003
1133
|
declare class UserClient {
|
|
1004
1134
|
private readonly api;
|
|
1005
|
-
constructor(api:
|
|
1135
|
+
constructor(api: TerrosApiClient);
|
|
1006
1136
|
list(input: UserListInput): Promise<UserListSuccess>;
|
|
1007
1137
|
add(input: UserAddInput): Promise<UserAddSuccess>;
|
|
1008
1138
|
update(input: UserUpdateInput): Promise<UserUpdateSuccess>;
|
|
@@ -1019,10 +1149,29 @@ declare class UserClient {
|
|
|
1019
1149
|
//#endregion
|
|
1020
1150
|
//#region src/client.d.ts
|
|
1021
1151
|
declare class TerrosClient {
|
|
1022
|
-
readonly user: UserClient;
|
|
1023
1152
|
readonly account: AccountClient;
|
|
1024
1153
|
readonly calendar: CalendarClient;
|
|
1025
|
-
|
|
1154
|
+
readonly connect: ConnectClient;
|
|
1155
|
+
readonly user: UserClient;
|
|
1156
|
+
constructor(config?: TerrosClientConfig);
|
|
1026
1157
|
}
|
|
1027
1158
|
//#endregion
|
|
1028
|
-
|
|
1159
|
+
//#region src/connect/handler.d.ts
|
|
1160
|
+
type ConnectHandlerFunction<Input, Result> = (payload: ConnectExecutionInput<Input>, client: TerrosApiClient) => Promise<Result>;
|
|
1161
|
+
type ConnectExecutionConfig = {
|
|
1162
|
+
scriptConfig: Record<string, string>;
|
|
1163
|
+
secrets: Record<string, string>;
|
|
1164
|
+
authorization?: string;
|
|
1165
|
+
};
|
|
1166
|
+
type ConnectExecutionContext<Payload> = {
|
|
1167
|
+
payload: Payload;
|
|
1168
|
+
config: ConnectExecutionConfig;
|
|
1169
|
+
};
|
|
1170
|
+
type ConnectExecutionInput<Payload> = {
|
|
1171
|
+
runId: `ConnectRun.${string}`;
|
|
1172
|
+
context: ConnectExecutionContext<Payload>;
|
|
1173
|
+
};
|
|
1174
|
+
type WrappedHandler<Input, Result> = (input: ConnectExecutionInput<Input>) => Promise<Result>;
|
|
1175
|
+
declare function wrapConnectHandler<Input, Result = void>(handler: ConnectHandlerFunction<Input, Result>): WrappedHandler<Input, Result>;
|
|
1176
|
+
//#endregion
|
|
1177
|
+
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, CompanyWebhook, CompanyWebhookData, ConfigSchemaField, ConnectHandlerFunction, 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, UserWebhook, UserWebhookData, VersionAddInput, VersionAddSuccess, VersionUpdateInput, VersionUpdateSuccess, WebhookAddUpdatePayload, WebhookAuditProps, WebhookPayload, WebhookRemovePayload, Weekday, WorkflowActionId, WorkflowHistoryItem, WorkflowId, WorkflowStageId, wrapConnectHandler };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,156 @@
|
|
|
1
|
-
import {
|
|
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.9";
|
|
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,31 @@ 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
|
|
216
|
-
|
|
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
|
-
|
|
350
|
+
//#region src/connect/handler.ts
|
|
351
|
+
function wrapConnectHandler(handler) {
|
|
352
|
+
return async (input) => {
|
|
353
|
+
const apiKey = input.context.config.authorization;
|
|
354
|
+
delete input.context.config.authorization;
|
|
355
|
+
return await handler(input, new TerrosApiClient({ apiKey }));
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
export { TerrosClient, wrapConnectHandler };
|
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.
|
|
42
|
+
"version": "1.0.9",
|
|
41
43
|
"scripts": {
|
|
42
44
|
"build": "tsdown",
|
|
43
45
|
"test": "vitest run"
|