@hubspot/local-dev-lib 0.2.3 → 0.2.4

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/lib/oauth.js CHANGED
@@ -32,7 +32,7 @@ function addOauthToAccountConfig(oauth, logCallbacks) {
32
32
  logger('init', `${i18nKey}.addOauthToAccountConfig.init`);
33
33
  try {
34
34
  (0, config_1.updateAccountConfig)({
35
- ...oauth.toObj(),
35
+ ...oauth.account,
36
36
  authType: auth_1.AUTH_METHODS.oauth.value,
37
37
  });
38
38
  (0, config_1.writeConfig)();
@@ -1,4 +1,16 @@
1
- import { FlatAccountFields, OAuthAccount, TokenInfo } from '../types/Accounts';
1
+ import { FlatAccountFields, TokenInfo } from '../types/Accounts';
2
+ import { Environment } from '../types/Config';
3
+ type OAuth2ManagerAccountConfig = {
4
+ name?: string;
5
+ accountId?: number;
6
+ clientId?: string;
7
+ clientSecret?: string;
8
+ scopes?: Array<string>;
9
+ env?: Environment;
10
+ environment?: Environment;
11
+ tokenInfo?: TokenInfo;
12
+ authType?: 'oauth2';
13
+ };
2
14
  type WriteTokenInfoFunction = (tokenInfo: TokenInfo) => void;
3
15
  type RefreshTokenResponse = {
4
16
  refresh_token: string;
@@ -12,23 +24,14 @@ type ExchangeProof = {
12
24
  refresh_token?: string;
13
25
  };
14
26
  declare class OAuth2Manager {
15
- account: OAuthAccount;
16
- writeTokenInfo: WriteTokenInfoFunction;
27
+ account: OAuth2ManagerAccountConfig;
28
+ writeTokenInfo?: WriteTokenInfoFunction;
17
29
  refreshTokenRequest: Promise<RefreshTokenResponse> | null;
18
- constructor(account: OAuthAccount, writeTokenInfo: WriteTokenInfoFunction);
30
+ constructor(account: OAuth2ManagerAccountConfig, writeTokenInfo?: WriteTokenInfoFunction);
19
31
  accessToken(): Promise<string | undefined>;
20
32
  fetchAccessToken(exchangeProof: ExchangeProof): Promise<void>;
21
33
  exchangeForTokens(exchangeProof: ExchangeProof): Promise<void>;
22
34
  refreshAccessToken(): Promise<void>;
23
- toObj(): {
24
- env: import("../types/Config").Environment;
25
- clientSecret: string | undefined;
26
- clientId: string | undefined;
27
- scopes: string[] | undefined;
28
- tokenInfo: TokenInfo | undefined;
29
- name: string | undefined;
30
- accountId: number;
31
- };
32
35
  static fromConfig(accountConfig: FlatAccountFields, writeTokenInfo: WriteTokenInfoFunction): OAuth2Manager;
33
36
  }
34
37
  export default OAuth2Manager;
@@ -25,45 +25,47 @@ class OAuth2Manager {
25
25
  }
26
26
  }
27
27
  async accessToken() {
28
- if (!this.account.auth.tokenInfo?.refreshToken) {
28
+ if (!this.account.tokenInfo?.refreshToken) {
29
29
  (0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.missingRefreshToken`, {
30
30
  accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
31
31
  });
32
32
  }
33
- if (!this.account.auth.tokenInfo?.accessToken ||
33
+ if (!this.account.tokenInfo?.accessToken ||
34
34
  (0, moment_1.default)()
35
35
  .add(5, 'minutes')
36
- .isAfter((0, moment_1.default)(this.account.auth?.tokenInfo.expiresAt))) {
36
+ .isAfter((0, moment_1.default)(new Date(this.account.tokenInfo.expiresAt || '')))) {
37
37
  await this.refreshAccessToken();
38
38
  }
39
- return this.account.auth?.tokenInfo.accessToken;
39
+ return this.account.tokenInfo.accessToken;
40
40
  }
41
41
  async fetchAccessToken(exchangeProof) {
42
42
  (0, logger_1.debug)(`${i18nKey}.fetchingAccessToken`, {
43
43
  accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
44
- clientId: this.account.auth.clientId || '',
44
+ clientId: this.account.clientId || '',
45
45
  });
46
46
  try {
47
- const { data } = await axios_1.default.post(`${(0, urls_1.getHubSpotApiOrigin)((0, environment_1.getValidEnv)(this.account.env))}/oauth/v1/token`, {
48
- form: exchangeProof,
49
- json: true,
47
+ const { data } = await (0, axios_1.default)({
48
+ url: `${(0, urls_1.getHubSpotApiOrigin)((0, environment_1.getValidEnv)(this.account.env))}/oauth/v1/token`,
49
+ method: 'post',
50
+ data: exchangeProof,
51
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
50
52
  });
51
53
  this.refreshTokenRequest = data;
52
54
  const { refresh_token: refreshToken, access_token: accessToken, expires_in: expiresIn, } = data;
53
- if (!this.account.auth.tokenInfo) {
54
- this.account.auth.tokenInfo = {};
55
+ if (!this.account.tokenInfo) {
56
+ this.account.tokenInfo = {};
55
57
  }
56
- this.account.auth.tokenInfo.refreshToken = refreshToken;
57
- this.account.auth.tokenInfo.accessToken = accessToken;
58
- this.account.auth.tokenInfo.expiresAt = (0, moment_1.default)()
58
+ this.account.tokenInfo.refreshToken = refreshToken;
59
+ this.account.tokenInfo.accessToken = accessToken;
60
+ this.account.tokenInfo.expiresAt = (0, moment_1.default)()
59
61
  .add(Math.round(parseInt(expiresIn) * 0.75), 'seconds')
60
62
  .toString();
61
63
  if (this.writeTokenInfo) {
62
64
  (0, logger_1.debug)(`${i18nKey}.updatingTokenInfo`, {
63
65
  accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
64
- clientId: this.account.auth.clientId || '',
66
+ clientId: this.account.clientId || '',
65
67
  });
66
- this.writeTokenInfo(this.account.auth.tokenInfo);
68
+ this.writeTokenInfo(this.account.tokenInfo);
67
69
  }
68
70
  this.refreshTokenRequest = null;
69
71
  }
@@ -77,7 +79,7 @@ class OAuth2Manager {
77
79
  if (this.refreshTokenRequest) {
78
80
  (0, logger_1.debug)(`${i18nKey}.refreshingAccessToken`, {
79
81
  accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
80
- clientId: this.account.auth.clientId || '',
82
+ clientId: this.account.clientId || '',
81
83
  });
82
84
  await this.refreshTokenRequest;
83
85
  }
@@ -100,28 +102,17 @@ class OAuth2Manager {
100
102
  async refreshAccessToken() {
101
103
  const refreshTokenProof = {
102
104
  grant_type: 'refresh_token',
103
- client_id: this.account.auth.clientId,
104
- client_secret: this.account.auth.clientSecret,
105
- refresh_token: this.account.auth.tokenInfo?.refreshToken,
105
+ client_id: this.account.clientId,
106
+ client_secret: this.account.clientSecret,
107
+ refresh_token: this.account.tokenInfo?.refreshToken,
106
108
  };
107
109
  await this.exchangeForTokens(refreshTokenProof);
108
110
  }
109
- toObj() {
110
- return {
111
- env: this.account.env,
112
- clientSecret: this.account.auth.clientSecret,
113
- clientId: this.account.auth.clientId,
114
- scopes: this.account.auth.scopes,
115
- tokenInfo: this.account.auth.tokenInfo,
116
- name: this.account.name,
117
- accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
118
- };
119
- }
120
111
  static fromConfig(accountConfig, writeTokenInfo) {
121
112
  return new OAuth2Manager({
122
113
  ...accountConfig,
123
114
  authType: auth_1.AUTH_METHODS.oauth.value,
124
- auth: accountConfig.auth || {},
115
+ ...(accountConfig.auth || {}),
125
116
  }, writeTokenInfo);
126
117
  }
127
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hubspot/local-dev-lib",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Provides library functionality for HubSpot local development tooling, including the HubSpot CLI",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -54,7 +54,8 @@
54
54
  "./http": "./http/index.js",
55
55
  "./config": "./config/index.js",
56
56
  "./constants/*": "./constants/*.js",
57
- "./logger": "./lib/logging/logger.js"
57
+ "./logger": "./lib/logging/logger.js",
58
+ "./models/*": "./models/*.js"
58
59
  },
59
60
  "dependencies": {
60
61
  "address": "^2.0.1",