conductor-node 0.0.18 → 0.1.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.
@@ -1,11 +1,13 @@
1
+ import type { Environment } from "./environment";
1
2
  export interface BaseClientOptions {
2
3
  apiKey: string;
3
4
  verbose?: boolean;
4
- environment?: "development" | "staging";
5
+ environment?: Environment;
5
6
  }
6
7
  export default class BaseClient {
7
8
  protected readonly apiKey: string;
8
9
  protected readonly verbose: boolean;
9
10
  protected readonly serverURL: string;
10
11
  constructor({ apiKey, verbose, environment, }: BaseClientOptions);
12
+ protected sendAPIRequest<T>(username: string, requestObj: T, apiPath: string): Promise<T>;
11
13
  }
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const environment_1 = require("./environment");
3
4
  // Store properties via superclass `BaseClient` instead of on `Client` passed to
4
5
  // the integration clients (e.g., `ClientQBD`) to hide these properties from the
5
6
  // dev user while still allowing the integration clients to access them.
@@ -10,15 +11,33 @@ class BaseClient {
10
11
  apiKey;
11
12
  verbose;
12
13
  serverURL;
13
- constructor({ apiKey, verbose = false, environment = "development", }) {
14
+ constructor({ apiKey, verbose = false, environment = "staging", }) {
14
15
  this.apiKey = apiKey;
15
16
  this.verbose = verbose;
16
- if (environment === "development") {
17
- this.serverURL = "https://conductor.ngrok.io";
17
+ this.serverURL = (0, environment_1.envToBaseServerURL)(environment);
18
+ }
19
+ async sendAPIRequest(username, requestObj, apiPath) {
20
+ const apiServerURL = `${this.serverURL}/${apiPath}`;
21
+ if (this.verbose) {
22
+ console.log(`Client sent request to ${apiServerURL} for user ${username}:`, JSON.stringify(requestObj, null, 2));
23
+ }
24
+ const response = await fetch(apiServerURL, {
25
+ body: JSON.stringify({ username, requestObj }),
26
+ headers: {
27
+ "Content-Type": "application/json",
28
+ Authorization: `Bearer ${this.apiKey}`,
29
+ },
30
+ method: "POST",
31
+ });
32
+ if (response.status >= 400) {
33
+ const errorMessage = (await response.text()) || response.statusText;
34
+ throw new Error(`Request to ${apiServerURL} failed with status ${response.status}: ${errorMessage}`);
18
35
  }
19
- else {
20
- throw new Error(`Unsupported environment: ${environment}`);
36
+ const responseObj = (await response.json());
37
+ if (this.verbose) {
38
+ console.log(`Client received response for user ${username}:`, JSON.stringify(responseObj, null, 2));
21
39
  }
40
+ return responseObj;
22
41
  }
23
42
  }
24
43
  exports.default = BaseClient;
@@ -0,0 +1,2 @@
1
+ export declare type Environment = "development" | "staging";
2
+ export declare function envToBaseServerURL(environment: Environment): string;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.envToBaseServerURL = void 0;
4
+ const STAGING_BASE_URL = "https://conductor-vgag.onrender.com";
5
+ const DEVELOPMENT_BASE_URL = "https://conductor.ngrok.io";
6
+ function envToBaseServerURL(environment) {
7
+ switch (environment) {
8
+ case "staging":
9
+ return STAGING_BASE_URL;
10
+ case "development":
11
+ return DEVELOPMENT_BASE_URL;
12
+ default:
13
+ throw new Error("Invalid environment");
14
+ }
15
+ }
16
+ exports.envToBaseServerURL = envToBaseServerURL;
@@ -11,22 +11,7 @@ class ClientQBD extends BaseClient_1.default {
11
11
  * Available APIs: https://developer.intuit.com/app/developer/qbdesktop/docs/api-reference/qbdesktop
12
12
  */
13
13
  async sendRequest(qbwcUsername, requestObj) {
14
- if (this.verbose) {
15
- console.log(`Client sent request for user ${qbwcUsername}:`, JSON.stringify(requestObj, null, 2));
16
- }
17
- const response = await fetch(this.serverURL, {
18
- body: JSON.stringify({ qbwcUsername, requestObj }),
19
- headers: {
20
- "Content-Type": "application/json",
21
- Authorization: `Bearer ${this.apiKey}`,
22
- },
23
- method: "POST",
24
- });
25
- const responseObj = (await response.json());
26
- if (this.verbose) {
27
- console.log(`Client received response for user ${qbwcUsername}:`, JSON.stringify(responseObj, null, 2));
28
- }
29
- return responseObj;
14
+ return this.sendAPIRequest(qbwcUsername, requestObj, "qbd");
30
15
  }
31
16
  /**
32
17
  * Perform the same activities as a user does in the QB New Account form,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "conductor-node",
3
- "version": "0.0.18",
3
+ "version": "0.1.2",
4
4
  "description": "Conductor API wrapper",
5
5
  "author": "Danny Nemer <hi@DannyNemer.com>",
6
6
  "license": "MIT",