@terros-inc/sdk 1.0.0 → 1.0.2

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.
@@ -0,0 +1,8 @@
1
+ type TerrosHeaderMetadata = {
2
+ 'Terros-Platform'?: string;
3
+ 'Terros-Platform-Version'?: string;
4
+ 'Terros-Bundle-Identifier'?: string;
5
+ 'Terros-App-Version'?: string;
6
+ };
7
+ export declare function getAnalyticsHeaders(): TerrosHeaderMetadata;
8
+ export {};
@@ -0,0 +1,22 @@
1
+ import { fileURLToPath } from 'node:url';
2
+ import { dirname, join } from 'node:path';
3
+ import { platform, release } from 'node:os';
4
+ import { readFileSync } from 'node:fs';
5
+ export function getAnalyticsHeaders() {
6
+ if (typeof globalThis.process === 'undefined')
7
+ return {};
8
+ try {
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = dirname(__filename);
11
+ const packageJson = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf8'));
12
+ return {
13
+ 'Terros-Platform': platform(),
14
+ 'Terros-Platform-Version': release(),
15
+ 'Terros-Bundle-Identifier': 'com.terros.sdk',
16
+ 'Terros-App-Version': packageJson.version,
17
+ };
18
+ }
19
+ catch {
20
+ return {};
21
+ }
22
+ }
@@ -0,0 +1,19 @@
1
+ import { type ApiRoute } from './models/index.ts';
2
+ import { AccountClient, CalendarClient, UserClient } from './clients/index.ts';
3
+ export type ClientConfig = {
4
+ apiKey?: string;
5
+ baseUrl?: string;
6
+ };
7
+ export declare class ApiCaller {
8
+ private readonly baseUrl;
9
+ private readonly auth;
10
+ private readonly analytics;
11
+ constructor(config: ClientConfig);
12
+ call<Success>(route: ApiRoute, input: object): Promise<Success>;
13
+ }
14
+ export declare class TerrosClient {
15
+ readonly user: UserClient;
16
+ readonly account: AccountClient;
17
+ readonly calendar: CalendarClient;
18
+ constructor(config: ClientConfig);
19
+ }
package/dist/client.js ADDED
@@ -0,0 +1,56 @@
1
+ import { isResponseError } from "./models/index.js";
2
+ import { AccountClient, CalendarClient, UserClient } from "./clients/index.js";
3
+ import { getAnalyticsHeaders } from "./analytics.js";
4
+ const PROD_BASE_URL = 'https://api.terros.com';
5
+ export class ApiCaller {
6
+ baseUrl;
7
+ auth;
8
+ analytics;
9
+ constructor(config) {
10
+ this.baseUrl = config.baseUrl ?? readProcessEnv('TERROS_SDK_BASE_URL') ?? PROD_BASE_URL;
11
+ this.analytics = getAnalyticsHeaders();
12
+ const auth = config.apiKey ?? readProcessEnv('TERROS_API_KEY');
13
+ if (!auth)
14
+ throw new Error('No auth provided: set TERROS_API_KEY or pass config.auth');
15
+ this.auth = auth;
16
+ }
17
+ async call(route, input) {
18
+ let response;
19
+ try {
20
+ response = await fetch(`${this.baseUrl}/${route}`, {
21
+ method: 'POST',
22
+ headers: {
23
+ ...this.analytics,
24
+ 'Content-Type': 'application/json',
25
+ authorization: `ApiKey ${this.auth}`,
26
+ },
27
+ body: JSON.stringify(input),
28
+ });
29
+ }
30
+ catch (cause) {
31
+ throw new Error(`Request to ${route} failed`, { cause });
32
+ }
33
+ if (!response.ok)
34
+ throw new Error(response.statusText);
35
+ const output = await response.json();
36
+ if (isResponseError(output))
37
+ throw new Error(output.message ?? output.error);
38
+ return output;
39
+ }
40
+ }
41
+ function readProcessEnv(key) {
42
+ if (typeof globalThis.process === 'undefined')
43
+ return undefined;
44
+ return globalThis.process.env[key];
45
+ }
46
+ export class TerrosClient {
47
+ user;
48
+ account;
49
+ calendar;
50
+ constructor(config) {
51
+ const api = new ApiCaller(config);
52
+ this.user = new UserClient(api);
53
+ this.account = new AccountClient(api);
54
+ this.calendar = new CalendarClient(api);
55
+ }
56
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,81 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { ApiCaller } from "./client.js";
3
+ describe('ApiCaller', () => {
4
+ const originalFetch = globalThis.fetch;
5
+ afterEach(() => {
6
+ globalThis.fetch = originalFetch;
7
+ delete process.env.TERROS_SDK_BASE_URL;
8
+ });
9
+ function mockFetch(body, ok = true) {
10
+ globalThis.fetch = vi.fn().mockResolvedValue({
11
+ ok,
12
+ statusText: 'Bad Request',
13
+ json: () => Promise.resolve(body),
14
+ });
15
+ }
16
+ it('sends a POST with the route, headers, and JSON body, returning the parsed success', async () => {
17
+ mockFetch({ type: 'success', value: 42 });
18
+ const caller = new ApiCaller({ apiKey: 'test-key', baseUrl: 'https://example.com' });
19
+ const result = await caller.call('user/get', { userId: 'U:1' });
20
+ expect(result).toEqual({ type: 'success', value: 42 });
21
+ expect(globalThis.fetch).toHaveBeenCalledWith('https://example.com/user/get', {
22
+ method: 'POST',
23
+ headers: expect.objectContaining({
24
+ 'Content-Type': 'application/json',
25
+ authorization: 'ApiKey test-key',
26
+ }),
27
+ body: JSON.stringify({ userId: 'U:1' }),
28
+ });
29
+ });
30
+ it('includes analytics headers describing the platform and SDK version', async () => {
31
+ mockFetch({ type: 'success', value: 42 });
32
+ const caller = new ApiCaller({ apiKey: 'test-key', baseUrl: 'https://example.com' });
33
+ await caller.call('user/get', { userId: 'U:1' });
34
+ const [, options] = vi.mocked(globalThis.fetch).mock.calls[0];
35
+ expect(options.headers).toMatchObject({
36
+ 'Terros-Bundle-Identifier': 'com.terros.sdk',
37
+ 'Terros-Platform': expect.any(String),
38
+ 'Terros-Platform-Version': expect.any(String),
39
+ 'Terros-App-Version': expect.any(String),
40
+ });
41
+ });
42
+ it('wraps a network failure in an Error with the underlying cause', async () => {
43
+ const networkError = new TypeError('fetch failed');
44
+ globalThis.fetch = vi.fn().mockRejectedValue(networkError);
45
+ const caller = new ApiCaller({ apiKey: 'test-key', baseUrl: 'https://example.com' });
46
+ await expect(caller.call('user/get', {})).rejects.toThrow('Request to user/get failed');
47
+ await expect(caller.call('user/get', {})).rejects.toMatchObject({ cause: networkError });
48
+ });
49
+ it('throws with the status text on a non-ok response', async () => {
50
+ mockFetch({}, false);
51
+ const caller = new ApiCaller({ apiKey: 'test-key', baseUrl: 'https://example.com' });
52
+ await expect(caller.call('user/get', {})).rejects.toThrow('Bad Request');
53
+ });
54
+ it('throws with the error message on a ResponseError body', async () => {
55
+ mockFetch({ type: 'error', error: 'not_found', message: 'User not found' });
56
+ const caller = new ApiCaller({ apiKey: 'test-key', baseUrl: 'https://example.com' });
57
+ await expect(caller.call('user/get', {})).rejects.toThrow('User not found');
58
+ });
59
+ describe('resolveBaseUrl precedence', () => {
60
+ beforeEach(() => {
61
+ mockFetch({ type: 'success' });
62
+ });
63
+ it('prefers the explicit config baseUrl over the env var and default', async () => {
64
+ process.env.TERROS_SDK_BASE_URL = 'https://env.example.com';
65
+ const caller = new ApiCaller({ apiKey: 'test-key', baseUrl: 'https://config.example.com' });
66
+ await caller.call('user/get', {});
67
+ expect(globalThis.fetch).toHaveBeenCalledWith('https://config.example.com/user/get', expect.anything());
68
+ });
69
+ it('falls back to the env var when no baseUrl is configured', async () => {
70
+ process.env.TERROS_SDK_BASE_URL = 'https://env.example.com';
71
+ const caller = new ApiCaller({ apiKey: 'test-key' });
72
+ await caller.call('user/get', {});
73
+ expect(globalThis.fetch).toHaveBeenCalledWith('https://env.example.com/user/get', expect.anything());
74
+ });
75
+ it('falls back to the prod default when neither baseUrl nor env var is set', async () => {
76
+ const caller = new ApiCaller({ apiKey: 'test-key' });
77
+ await caller.call('user/get', {});
78
+ expect(globalThis.fetch).toHaveBeenCalledWith('https://api.terros.com/user/get', expect.anything());
79
+ });
80
+ });
81
+ });
@@ -0,0 +1,25 @@
1
+ import type { ApiSuccess } from '../models/shared.ts';
2
+ import type { AccountAddInput, AccountAddSuccess, AccountGetInput, AccountGetSuccess, AccountListInput, AccountListSuccess, AccountMatchInput, AccountMatchSuccess, AccountRemoveInput, AccountStatusAddInput, AccountStatusAddSuccess, AccountStatusListInput, AccountStatusListSuccess, AccountStatusRemoveInput, AccountStatusUpdateInput, AccountStatusUpdateSuccess, AccountUpdateBatchInput, AccountUpdateBatchSuccess, AccountUpdateInput, AccountUpdateSuccess, AccountUpsertInput, AccountUpsertSuccess, BulkModifyAccountsInput } from '../models/account/api.ts';
3
+ import type { ApiCaller } from '../client.ts';
4
+ export declare class AccountStatusClient {
5
+ private readonly api;
6
+ constructor(api: ApiCaller);
7
+ add(input: AccountStatusAddInput): Promise<AccountStatusAddSuccess>;
8
+ update(input: AccountStatusUpdateInput): Promise<AccountStatusUpdateSuccess>;
9
+ list(input: AccountStatusListInput): Promise<AccountStatusListSuccess>;
10
+ remove(input: AccountStatusRemoveInput): Promise<ApiSuccess>;
11
+ }
12
+ export declare class AccountClient {
13
+ private readonly api;
14
+ readonly status: AccountStatusClient;
15
+ constructor(api: ApiCaller);
16
+ add(input: AccountAddInput): Promise<AccountAddSuccess>;
17
+ update(input: AccountUpdateInput): Promise<AccountUpdateSuccess>;
18
+ updateBatch(input: AccountUpdateBatchInput): Promise<AccountUpdateBatchSuccess>;
19
+ list(input: AccountListInput): Promise<AccountListSuccess>;
20
+ bulk(input: BulkModifyAccountsInput): Promise<ApiSuccess>;
21
+ get(input: AccountGetInput): Promise<AccountGetSuccess>;
22
+ match(input: AccountMatchInput): Promise<AccountMatchSuccess>;
23
+ remove(input: AccountRemoveInput): Promise<ApiSuccess>;
24
+ upsert(input: AccountUpsertInput): Promise<AccountUpsertSuccess>;
25
+ }
@@ -0,0 +1,53 @@
1
+ export class AccountStatusClient {
2
+ api;
3
+ constructor(api) {
4
+ this.api = api;
5
+ }
6
+ add(input) {
7
+ return this.api.call('account/status/add', input);
8
+ }
9
+ update(input) {
10
+ return this.api.call('account/status/update', input);
11
+ }
12
+ list(input) {
13
+ return this.api.call('account/status/list', input);
14
+ }
15
+ remove(input) {
16
+ return this.api.call('account/status/remove', input);
17
+ }
18
+ }
19
+ export class AccountClient {
20
+ api;
21
+ status;
22
+ constructor(api) {
23
+ this.api = api;
24
+ this.status = new AccountStatusClient(api);
25
+ }
26
+ add(input) {
27
+ return this.api.call('account/add', input);
28
+ }
29
+ update(input) {
30
+ return this.api.call('account/update', input);
31
+ }
32
+ updateBatch(input) {
33
+ return this.api.call('account/update/batch', input);
34
+ }
35
+ list(input) {
36
+ return this.api.call('account/list', input);
37
+ }
38
+ bulk(input) {
39
+ return this.api.call('account/bulk', input);
40
+ }
41
+ get(input) {
42
+ return this.api.call('account/get', input);
43
+ }
44
+ match(input) {
45
+ return this.api.call('account/match', input);
46
+ }
47
+ remove(input) {
48
+ return this.api.call('account/remove', input);
49
+ }
50
+ upsert(input) {
51
+ return this.api.call('account/upsert', input);
52
+ }
53
+ }
@@ -0,0 +1,22 @@
1
+ import type { ApiSuccess } from '../models/shared.ts';
2
+ import type { CalendarAvailabilityInput, CalendarAvailabilitySuccess, CalendarClosersInput, CalendarClosersSuccess, CalendarEventAddInput, CalendarEventAddSuccess, CalendarEventConfirmInput, CalendarEventConfirmSuccess, CalendarEventGetInput, CalendarEventGetSuccess, CalendarEventListInput, CalendarEventListSuccess, CalendarEventRemoveInput, CalendarEventUpdateInput, CalendarEventUpdateSuccess, CalendarEventUpsertInput, CalendarEventUpsertSuccess, CalendarTimeslotsInput, CalendarTimeslotsSuccess } from '../models/calendar/api.ts';
3
+ import type { ApiCaller } from '../client.ts';
4
+ export declare class CalendarEventClient {
5
+ private readonly api;
6
+ constructor(api: ApiCaller);
7
+ add(input: CalendarEventAddInput): Promise<CalendarEventAddSuccess>;
8
+ get(input: CalendarEventGetInput): Promise<CalendarEventGetSuccess>;
9
+ list(input: CalendarEventListInput): Promise<CalendarEventListSuccess>;
10
+ update(input: CalendarEventUpdateInput): Promise<CalendarEventUpdateSuccess>;
11
+ upsert(input: CalendarEventUpsertInput): Promise<CalendarEventUpsertSuccess>;
12
+ confirm(input: CalendarEventConfirmInput): Promise<CalendarEventConfirmSuccess>;
13
+ remove(input: CalendarEventRemoveInput): Promise<ApiSuccess>;
14
+ }
15
+ export declare class CalendarClient {
16
+ private readonly api;
17
+ readonly event: CalendarEventClient;
18
+ constructor(api: ApiCaller);
19
+ availability(input: CalendarAvailabilityInput): Promise<CalendarAvailabilitySuccess>;
20
+ closers(input: CalendarClosersInput): Promise<CalendarClosersSuccess>;
21
+ timeslots(input: CalendarTimeslotsInput): Promise<CalendarTimeslotsSuccess>;
22
+ }
@@ -0,0 +1,44 @@
1
+ export class CalendarEventClient {
2
+ api;
3
+ constructor(api) {
4
+ this.api = api;
5
+ }
6
+ add(input) {
7
+ return this.api.call('calendar/event/add', input);
8
+ }
9
+ get(input) {
10
+ return this.api.call('calendar/event/get', input);
11
+ }
12
+ list(input) {
13
+ return this.api.call('calendar/event/list', input);
14
+ }
15
+ update(input) {
16
+ return this.api.call('calendar/event/update', input);
17
+ }
18
+ upsert(input) {
19
+ return this.api.call('calendar/event/upsert', input);
20
+ }
21
+ confirm(input) {
22
+ return this.api.call('calendar/event/confirm', input);
23
+ }
24
+ remove(input) {
25
+ return this.api.call('calendar/event/remove', input);
26
+ }
27
+ }
28
+ export class CalendarClient {
29
+ api;
30
+ event;
31
+ constructor(api) {
32
+ this.api = api;
33
+ this.event = new CalendarEventClient(api);
34
+ }
35
+ availability(input) {
36
+ return this.api.call('calendar/availability', input);
37
+ }
38
+ closers(input) {
39
+ return this.api.call('calendar/closers', input);
40
+ }
41
+ timeslots(input) {
42
+ return this.api.call('calendar/timeslots', input);
43
+ }
44
+ }
@@ -0,0 +1,3 @@
1
+ export * from './account.ts';
2
+ export * from './calendar.ts';
3
+ export * from './user.ts';
@@ -0,0 +1,3 @@
1
+ export * from "./account.js";
2
+ export * from "./calendar.js";
3
+ export * from "./user.js";
@@ -0,0 +1,19 @@
1
+ import type { UserAddInput, UserAddSuccess, UserBulkUpdateInput, UserBulkUpdateSuccess, UserClosersInput, UserClosersSuccess, UserGetInput, UserGetSuccess, UserListInput, UserListSuccess, UserManagerInput, UserManagerSuccess, UserProfileInput, UserProfileSuccess, UserRemoveInput, UserReportInput, UserReportSuccess, UserSearchInput, UserSearchSuccess, UserTimezoneInput, UserUpdateInput, UserUpdateSuccess } from '../models/user/api.ts';
2
+ import type { ApiSuccess } from '../models/shared.ts';
3
+ import type { ApiCaller } from '../client.ts';
4
+ export declare class UserClient {
5
+ private readonly api;
6
+ constructor(api: ApiCaller);
7
+ list(input: UserListInput): Promise<UserListSuccess>;
8
+ add(input: UserAddInput): Promise<UserAddSuccess>;
9
+ update(input: UserUpdateInput): Promise<UserUpdateSuccess>;
10
+ updateBulk(input: UserBulkUpdateInput): Promise<UserBulkUpdateSuccess>;
11
+ remove(input: UserRemoveInput): Promise<ApiSuccess>;
12
+ get(input?: UserGetInput): Promise<UserGetSuccess>;
13
+ profile(input?: UserProfileInput): Promise<UserProfileSuccess>;
14
+ search(input: UserSearchInput): Promise<UserSearchSuccess>;
15
+ timezone(input: UserTimezoneInput): Promise<ApiSuccess>;
16
+ report(input: UserReportInput): Promise<UserReportSuccess>;
17
+ closers(input: UserClosersInput): Promise<UserClosersSuccess>;
18
+ manager(input: UserManagerInput): Promise<UserManagerSuccess>;
19
+ }
@@ -0,0 +1,42 @@
1
+ export class UserClient {
2
+ api;
3
+ constructor(api) {
4
+ this.api = api;
5
+ }
6
+ list(input) {
7
+ return this.api.call('user/list', input);
8
+ }
9
+ add(input) {
10
+ return this.api.call('user/add', input);
11
+ }
12
+ update(input) {
13
+ return this.api.call('user/update', input);
14
+ }
15
+ updateBulk(input) {
16
+ return this.api.call('user/update/bulk', input);
17
+ }
18
+ remove(input) {
19
+ return this.api.call('user/remove', input);
20
+ }
21
+ get(input = {}) {
22
+ return this.api.call('user/get', input);
23
+ }
24
+ profile(input = {}) {
25
+ return this.api.call('user/profile', input);
26
+ }
27
+ search(input) {
28
+ return this.api.call('user/search', input);
29
+ }
30
+ timezone(input) {
31
+ return this.api.call('user/timezone', input);
32
+ }
33
+ report(input) {
34
+ return this.api.call('user/report', input);
35
+ }
36
+ closers(input) {
37
+ return this.api.call('user/closers', input);
38
+ }
39
+ manager(input) {
40
+ return this.api.call('user/manager', input);
41
+ }
42
+ }
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ export * from './models/index.ts';
2
+ export * from './clients/index.ts';
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ export * from "./models/index.js";
2
+ export * from "./clients/index.js";
package/package.json CHANGED
@@ -34,7 +34,7 @@
34
34
  "url": "git+https://github.com/terros-inc/sdk.git"
35
35
  },
36
36
  "type": "module",
37
- "version": "1.0.0",
37
+ "version": "1.0.2",
38
38
  "scripts": {
39
39
  "build": "tsc",
40
40
  "format": "oxfmt --write .",