@signalhousellc/sdk 1.0.31 → 1.0.33

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signalhousellc/sdk",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "description": "Signal House SDK for use with the Signal House platform",
5
5
  "type": "module",
6
6
  "main": "src/SignalHouseSDK.js",
@@ -1,154 +1,154 @@
1
- import axios from "axios";
2
-
3
- /**
4
- * @typedef {Object} RequestOptions
5
- * @property {string} [token] - An optional bearer token to include in the request for authentication
6
- * @property {object} [headers] - Additional headers to include in the request
7
- */
8
-
9
- // Main SDK Class (SignalHouseSDK.js)
10
- import { Auth } from "./domains/Auth.js";
11
- import { Billing } from "./domains/Billing.js";
12
- import { Brands } from "./domains/Brands.js";
13
- import { Campaigns } from "./domains/Campaigns.js";
14
- import { Groups } from "./domains/Groups.js";
15
- import { Landings } from "./domains/Landings.js";
16
- import { Messages } from "./domains/Messages.js";
17
- import { Numbers } from "./domains/Numbers.js";
18
- import { Shortlinks } from "./domains/Shortlinks.js";
19
- import { Subgroups } from "./domains/Subgroups.js";
20
- import { Subscriptions } from "./domains/Subscriptions.js";
21
- import { Users } from "./domains/Users.js";
22
- import { Notifications } from "./domains/Notifications.js";
23
- import { Webhooks } from "./domains/Webhooks.js";
24
-
25
- export class SignalHouseSDK {
26
- /**
27
- * Initialize the SignalHouseSDK with the required configuration
28
- * @param {Object} config - The configuration object for initializing the SDK
29
- * @param {string} config.apiKey - The API key for authenticating requests to the SignalHouse API
30
- * @param {string} config.baseUrl - The base URL for the SignalHouse API (e.g., "https://api.signalhouse.com")
31
- * @throws {Error} Throws an error if the API key or base URL is missing from the configuration
32
- * @returns {SignalHouseSDK} An instance of the SignalHouseSDK
33
- */
34
- constructor(config = {}) {
35
- // Validate required config parameters
36
- if (!config.apiKey) {
37
- throw new Error("API key is required to initialize SignalHouseSDK");
38
- }
39
- if (!config.baseUrl) {
40
- throw new Error("Base URL is required to initialize SignalHouseSDK");
41
- }
42
-
43
- // Set instance properties
44
- this.baseUrl = config.baseUrl;
45
- this.apiKey = config.apiKey;
46
- this.enableAdmin = config.enableAdmin || false;
47
-
48
- // Shared internal request helper
49
- const client = this._createClient(false);
50
- const multipartClient = this._createClient(true);
51
-
52
- // API Domains
53
- this.auth = new Auth(client, this.enableAdmin);
54
- this.billing = new Billing(client, this.enableAdmin);
55
- this.brands = new Brands(client, multipartClient, this.enableAdmin);
56
- this.campaigns = new Campaigns(client, this.enableAdmin);
57
- this.groups = new Groups(client, this.enableAdmin);
58
- this.landings = new Landings(client, multipartClient, this.enableAdmin);
59
- this.messages = new Messages(client, multipartClient, this.enableAdmin);
60
- this.numbers = new Numbers(client, this.enableAdmin);
61
- this.shortlinks = new Shortlinks(client, this.enableAdmin);
62
- this.subgroups = new Subgroups(client, this.enableAdmin);
63
- this.subscriptions = new Subscriptions(client, this.enableAdmin);
64
- this.users = new Users(client, this.enableAdmin);
65
- this.notifications = new Notifications(client, this.enableAdmin);
66
- this.webhooks = new Webhooks(client, this.enableAdmin);
67
- }
68
-
69
- /**
70
- * Client for API requests
71
- * @private
72
- * @param {boolean} isMultipart - Whether to set the Content-Type header for multipart requests
73
- * @throws {Error} Throws an error if the API key or base URL is missing
74
- * @returns {Function} A function to make API requests
75
- */
76
- _createClient(isMultipart = false) {
77
- // Create an Axios instance with the base URL and default headers
78
- const instance = axios.create({ baseURL: this.baseUrl, headers: isMultipart ? {} : { "Content-Type": "application/json" } });
79
-
80
- // Add a request interceptor to include the API key in the Authorization header
81
- instance.interceptors.request.use((config) => {
82
- const token = config.token || this.apiKey;
83
- config.headers.Authorization = `Bearer ${token}`;
84
- delete config.token; // Remove token from config to avoid sending it as a header
85
- return config;
86
- });
87
-
88
- // Standardize responses
89
- instance.interceptors.response.use(
90
- // Standard 2xx responses
91
- (response) => {
92
- return { success: true, data: response.data, status: response.status };
93
- },
94
-
95
- // Non-2xx responses
96
- (error) => {
97
- return {
98
- success: false,
99
- error: error.response?.data?.message || error.response?.data || error.message || 500,
100
- status: error.response?.status || error.response?.data || null,
101
- };
102
- },
103
- );
104
-
105
- const client = async (url, options = {}) => {
106
- // Extract the body from options and keep the rest as axios config
107
- const { body, ...axiosConfig } = options;
108
- return instance({ url, data: body, ...axiosConfig });
109
- };
110
-
111
- // Attach helpers to the client
112
- client._getQueryString = this._getQueryString.bind(this);
113
- client._require = this._require.bind(this);
114
-
115
- return client;
116
- }
117
-
118
- /**
119
- * Helper method to convert an object to a query string, excluding undefined values and the 'options' key
120
- * @private
121
- * @param {Object} obj - The object to convert to a query string
122
- * @returns {string} The query string
123
- */
124
- _getQueryString(obj) {
125
- const params = new URLSearchParams();
126
- Object.entries(obj).forEach(([key, value]) => {
127
- if (value !== undefined && key !== "options") {
128
- if (Array.isArray(value)) {
129
- value.forEach((v) => params.append(key, v));
130
- } else {
131
- params.append(key, value);
132
- }
133
- }
134
- });
135
- return params.toString() ? `?${params.toString()}` : "";
136
- }
137
-
138
- /**
139
- * Helper method to validate required fields necessary for a request parameter
140
- * @private
141
- * @param {Object} fields - An object where keys are field names and values are the corresponding values to check
142
- * @throws {Error} Throws an error if any required field is missing or empty
143
- */
144
- _require(fields) {
145
- for (const [name, value] of Object.entries(fields)) {
146
- if (value === undefined || value === null || value === "") {
147
- const error = new Error(`Missing required parameter: ${name}`);
148
- error.name = "SignalHouseValidationError";
149
- error.status = 400; // Match API validation code
150
- throw error;
151
- }
152
- }
153
- }
154
- }
1
+ import axios from "axios";
2
+
3
+ /**
4
+ * @typedef {Object} RequestOptions
5
+ * @property {string} [token] - An optional bearer token to include in the request for authentication
6
+ * @property {object} [headers] - Additional headers to include in the request
7
+ */
8
+
9
+ // Main SDK Class (SignalHouseSDK.js)
10
+ import { Auth } from "./domains/Auth.js";
11
+ import { Billing } from "./domains/Billing.js";
12
+ import { Brands } from "./domains/Brands.js";
13
+ import { Campaigns } from "./domains/Campaigns.js";
14
+ import { Groups } from "./domains/Groups.js";
15
+ import { Landings } from "./domains/Landings.js";
16
+ import { Messages } from "./domains/Messages.js";
17
+ import { Numbers } from "./domains/Numbers.js";
18
+ import { Shortlinks } from "./domains/Shortlinks.js";
19
+ import { Subgroups } from "./domains/Subgroups.js";
20
+ import { Subscriptions } from "./domains/Subscriptions.js";
21
+ import { Users } from "./domains/Users.js";
22
+ import { Notifications } from "./domains/Notifications.js";
23
+ import { Webhooks } from "./domains/Webhooks.js";
24
+
25
+ export class SignalHouseSDK {
26
+ /**
27
+ * Initialize the SignalHouseSDK with the required configuration
28
+ * @param {Object} config - The configuration object for initializing the SDK
29
+ * @param {string} config.apiKey - The API key for authenticating requests to the SignalHouse API
30
+ * @param {string} config.baseUrl - The base URL for the SignalHouse API (e.g., "https://api.signalhouse.com")
31
+ * @throws {Error} Throws an error if the API key or base URL is missing from the configuration
32
+ * @returns {SignalHouseSDK} An instance of the SignalHouseSDK
33
+ */
34
+ constructor(config = {}) {
35
+ // Validate required config parameters
36
+ if (!config.apiKey) {
37
+ throw new Error("API key is required to initialize SignalHouseSDK");
38
+ }
39
+ if (!config.baseUrl) {
40
+ throw new Error("Base URL is required to initialize SignalHouseSDK");
41
+ }
42
+
43
+ // Set instance properties
44
+ this.baseUrl = config.baseUrl;
45
+ this.apiKey = config.apiKey;
46
+ this.enableAdmin = config.enableAdmin || false;
47
+
48
+ // Shared internal request helper
49
+ const client = this._createClient(false);
50
+ const multipartClient = this._createClient(true);
51
+
52
+ // API Domains
53
+ this.auth = new Auth(client, this.enableAdmin);
54
+ this.billing = new Billing(client, this.enableAdmin);
55
+ this.brands = new Brands(client, multipartClient, this.enableAdmin);
56
+ this.campaigns = new Campaigns(client, this.enableAdmin);
57
+ this.groups = new Groups(client, this.enableAdmin);
58
+ this.landings = new Landings(client, multipartClient, this.enableAdmin);
59
+ this.messages = new Messages(client, multipartClient, this.enableAdmin);
60
+ this.numbers = new Numbers(client, this.enableAdmin);
61
+ this.shortlinks = new Shortlinks(client, this.enableAdmin);
62
+ this.subgroups = new Subgroups(client, this.enableAdmin);
63
+ this.subscriptions = new Subscriptions(client, this.enableAdmin);
64
+ this.users = new Users(client, this.enableAdmin);
65
+ this.notifications = new Notifications(client, this.enableAdmin);
66
+ this.webhooks = new Webhooks(client, this.enableAdmin);
67
+ }
68
+
69
+ /**
70
+ * Client for API requests
71
+ * @private
72
+ * @param {boolean} isMultipart - Whether to set the Content-Type header for multipart requests
73
+ * @throws {Error} Throws an error if the API key or base URL is missing
74
+ * @returns {Function} A function to make API requests
75
+ */
76
+ _createClient(isMultipart = false) {
77
+ // Create an Axios instance with the base URL and default headers
78
+ const instance = axios.create({ baseURL: this.baseUrl, headers: isMultipart ? {} : { "Content-Type": "application/json" } });
79
+
80
+ // Add a request interceptor to include the API key in the Authorization header
81
+ instance.interceptors.request.use((config) => {
82
+ const token = config.token || this.apiKey;
83
+ config.headers.Authorization = `Bearer ${token}`;
84
+ delete config.token; // Remove token from config to avoid sending it as a header
85
+ return config;
86
+ });
87
+
88
+ // Standardize responses
89
+ instance.interceptors.response.use(
90
+ // Standard 2xx responses
91
+ (response) => {
92
+ return { success: true, data: response.data, status: response.status };
93
+ },
94
+
95
+ // Non-2xx responses
96
+ (error) => {
97
+ return {
98
+ success: false,
99
+ error: error.response?.data?.message || error.response?.data || error.message || 500,
100
+ status: error.response?.status || error.response?.data || null,
101
+ };
102
+ },
103
+ );
104
+
105
+ const client = async (url, options = {}) => {
106
+ // Extract the body from options and keep the rest as axios config
107
+ const { body, ...axiosConfig } = options;
108
+ return instance({ url, data: body, ...axiosConfig });
109
+ };
110
+
111
+ // Attach helpers to the client
112
+ client._getQueryString = this._getQueryString.bind(this);
113
+ client._require = this._require.bind(this);
114
+
115
+ return client;
116
+ }
117
+
118
+ /**
119
+ * Helper method to convert an object to a query string, excluding undefined values and the 'options' key
120
+ * @private
121
+ * @param {Object} obj - The object to convert to a query string
122
+ * @returns {string} The query string
123
+ */
124
+ _getQueryString(obj) {
125
+ const params = new URLSearchParams();
126
+ Object.entries(obj).forEach(([key, value]) => {
127
+ if (value !== undefined && key !== "options") {
128
+ if (Array.isArray(value)) {
129
+ value.forEach((v) => params.append(key, v));
130
+ } else {
131
+ params.append(key, value);
132
+ }
133
+ }
134
+ });
135
+ return params.toString() ? `?${params.toString()}` : "";
136
+ }
137
+
138
+ /**
139
+ * Helper method to validate required fields necessary for a request parameter
140
+ * @private
141
+ * @param {Object} fields - An object where keys are field names and values are the corresponding values to check
142
+ * @throws {Error} Throws an error if any required field is missing or empty
143
+ */
144
+ _require(fields) {
145
+ for (const [name, value] of Object.entries(fields)) {
146
+ if (value === undefined || value === null || value === "") {
147
+ const error = new Error(`Missing required parameter: ${name}`);
148
+ error.name = "SignalHouseValidationError";
149
+ error.status = 400; // Match API validation code
150
+ throw error;
151
+ }
152
+ }
153
+ }
154
+ }