@hubspot/local-dev-lib 0.2.1 → 0.2.2-experimental.0

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.
Files changed (189) hide show
  1. package/LICENSE +12 -0
  2. package/README.md +17 -0
  3. package/api/appsDev.d.ts +6 -0
  4. package/api/appsDev.js +29 -0
  5. package/api/customObjects.d.ts +8 -7
  6. package/api/customObjects.js +13 -16
  7. package/api/designManager.d.ts +4 -13
  8. package/api/designManager.js +6 -9
  9. package/api/developerTestAccounts.d.ts +7 -0
  10. package/api/developerTestAccounts.js +48 -0
  11. package/api/fileManager.d.ts +5 -4
  12. package/api/fileManager.js +11 -11
  13. package/api/fileMapper.d.ts +8 -7
  14. package/api/fileMapper.js +17 -17
  15. package/api/fileTransport.d.ts +4 -0
  16. package/api/fileTransport.js +39 -0
  17. package/api/functions.d.ts +7 -4
  18. package/api/functions.js +22 -11
  19. package/api/github.d.ts +7 -9
  20. package/api/github.js +31 -13
  21. package/api/hubdb.d.ts +11 -10
  22. package/api/hubdb.js +28 -22
  23. package/api/lighthouseScore.d.ts +4 -4
  24. package/api/lighthouseScore.js +9 -12
  25. package/api/localDevAuth.d.ts +8 -16
  26. package/api/localDevAuth.js +34 -13
  27. package/api/marketplaceValidation.d.ts +4 -4
  28. package/api/marketplaceValidation.js +8 -11
  29. package/api/projects.d.ts +31 -24
  30. package/api/projects.js +164 -44
  31. package/api/sandboxHubs.d.ts +6 -4
  32. package/api/sandboxHubs.js +10 -11
  33. package/api/sandboxSync.d.ts +4 -4
  34. package/api/sandboxSync.js +4 -13
  35. package/api/secrets.d.ts +6 -8
  36. package/api/secrets.js +9 -12
  37. package/api/validateHubl.d.ts +2 -1
  38. package/api/validateHubl.js +3 -6
  39. package/config/CLIConfiguration.d.ts +15 -11
  40. package/config/CLIConfiguration.js +175 -90
  41. package/config/configFile.js +13 -21
  42. package/config/configUtils.d.ts +2 -21
  43. package/config/configUtils.js +5 -4
  44. package/config/config_DEPRECATED.d.ts +6 -8
  45. package/config/config_DEPRECATED.js +79 -25
  46. package/config/environment.js +5 -4
  47. package/config/getAccountIdentifier.d.ts +2 -0
  48. package/config/getAccountIdentifier.js +15 -0
  49. package/config/index.d.ts +13 -8
  50. package/config/index.js +97 -57
  51. package/config/migrate.d.ts +20 -0
  52. package/config/migrate.js +150 -0
  53. package/constants/config.d.ts +25 -1
  54. package/constants/config.js +32 -3
  55. package/constants/environments.d.ts +1 -0
  56. package/constants/environments.js +1 -0
  57. package/constants/extensions.d.ts +2 -0
  58. package/constants/extensions.js +3 -1
  59. package/constants/files.d.ts +2 -2
  60. package/constants/files.js +3 -3
  61. package/errors/errors_DEPRECATED.d.ts +1 -5
  62. package/errors/errors_DEPRECATED.js +2 -2
  63. package/errors/index.d.ts +18 -0
  64. package/errors/index.js +63 -0
  65. package/http/addQueryParams.d.ts +2 -0
  66. package/http/addQueryParams.js +14 -0
  67. package/http/getAxiosConfig.d.ts +6 -3
  68. package/http/getAxiosConfig.js +51 -7
  69. package/http/index.d.ts +11 -12
  70. package/http/index.js +35 -41
  71. package/http/unauthed.d.ts +14 -0
  72. package/http/unauthed.js +38 -0
  73. package/lang/en.json +384 -386
  74. package/lib/archive.d.ts +2 -8
  75. package/lib/archive.js +34 -30
  76. package/lib/cms/functions.d.ts +7 -12
  77. package/lib/cms/functions.js +49 -51
  78. package/lib/cms/handleFieldsJS.js +16 -14
  79. package/lib/cms/modules.d.ts +4 -15
  80. package/lib/cms/modules.js +81 -78
  81. package/lib/cms/processFieldsJs.js +10 -33
  82. package/lib/cms/templates.d.ts +43 -3
  83. package/lib/cms/templates.js +53 -11
  84. package/lib/cms/uploadFolder.d.ts +3 -14
  85. package/lib/cms/uploadFolder.js +58 -42
  86. package/lib/cms/validate.js +1 -1
  87. package/lib/cms/watch.d.ts +2 -18
  88. package/lib/cms/watch.js +63 -68
  89. package/lib/customObjects.js +4 -15
  90. package/lib/fileManager.d.ts +2 -6
  91. package/lib/fileManager.js +54 -65
  92. package/lib/fileMapper.d.ts +4 -15
  93. package/lib/fileMapper.js +67 -88
  94. package/lib/fs.d.ts +1 -1
  95. package/lib/fs.js +10 -6
  96. package/lib/github.d.ts +12 -14
  97. package/lib/github.js +70 -35
  98. package/lib/gitignore.d.ts +2 -0
  99. package/lib/gitignore.js +21 -4
  100. package/lib/hubdb.d.ts +3 -2
  101. package/lib/hubdb.js +11 -9
  102. package/{utils → lib}/notify.js +2 -2
  103. package/lib/oauth.d.ts +2 -5
  104. package/lib/oauth.js +14 -25
  105. package/lib/path.d.ts +3 -0
  106. package/lib/path.js +46 -1
  107. package/lib/personalAccessKey.d.ts +8 -11
  108. package/lib/personalAccessKey.js +76 -38
  109. package/lib/portManager.d.ts +3 -5
  110. package/lib/portManager.js +18 -6
  111. package/lib/trackUsage.js +18 -11
  112. package/lib/urls.js +5 -1
  113. package/models/FileSystemError.d.ts +6 -0
  114. package/models/FileSystemError.js +47 -0
  115. package/models/HubSpotHttpError.d.ts +24 -0
  116. package/models/HubSpotHttpError.js +197 -0
  117. package/models/OAuth2Manager.d.ts +5 -27
  118. package/models/OAuth2Manager.js +41 -64
  119. package/package.json +35 -35
  120. package/types/Accounts.d.ts +103 -3
  121. package/types/Apps.d.ts +77 -0
  122. package/types/Archive.d.ts +9 -0
  123. package/types/Archive.js +2 -0
  124. package/types/Build.d.ts +4 -2
  125. package/types/ComponentStructure.d.ts +30 -10
  126. package/types/Config.d.ts +19 -2
  127. package/types/Deploy.d.ts +3 -2
  128. package/types/DesignManager.d.ts +10 -0
  129. package/types/DesignManager.js +2 -0
  130. package/types/Error.d.ts +9 -5
  131. package/types/FieldsJS.d.ts +1 -0
  132. package/types/FieldsJS.js +2 -0
  133. package/types/FileManager.d.ts +1 -1
  134. package/types/Files.d.ts +43 -4
  135. package/types/Functions.d.ts +52 -0
  136. package/types/Github.d.ts +13 -0
  137. package/types/Http.d.ts +12 -11
  138. package/types/Hubdb.d.ts +9 -0
  139. package/types/MarketplaceValidation.d.ts +7 -3
  140. package/types/Migration.d.ts +28 -0
  141. package/types/Migration.js +10 -0
  142. package/types/Modules.d.ts +11 -0
  143. package/types/PortManager.d.ts +7 -0
  144. package/types/Project.d.ts +30 -0
  145. package/types/ProjectLog.d.ts +9 -0
  146. package/types/ProjectLog.js +2 -0
  147. package/types/Sandbox.d.ts +37 -47
  148. package/types/Schemas.d.ts +56 -0
  149. package/types/Secrets.d.ts +3 -0
  150. package/types/Secrets.js +2 -0
  151. package/types/developerTestAccounts.d.ts +13 -0
  152. package/types/developerTestAccounts.js +2 -0
  153. package/utils/PortManagerServer.d.ts +6 -7
  154. package/utils/PortManagerServer.js +22 -16
  155. package/utils/{getAccountIdentifier.d.ts → accounts.d.ts} +0 -6
  156. package/utils/{getAccountIdentifier.js → accounts.js} +1 -13
  157. package/utils/cms/fieldsJS.d.ts +1 -2
  158. package/utils/cms/modules.js +2 -2
  159. package/utils/detectPort.js +3 -3
  160. package/utils/git.d.ts +1 -7
  161. package/utils/git.js +2 -18
  162. package/errors/apiErrors.d.ts +0 -19
  163. package/errors/apiErrors.js +0 -179
  164. package/errors/fileSystemErrors.d.ts +0 -5
  165. package/errors/fileSystemErrors.js +0 -31
  166. package/errors/standardErrors.d.ts +0 -27
  167. package/errors/standardErrors.js +0 -59
  168. package/lang/lang/en.json +0 -401
  169. package/lib/logging/git.d.ts +0 -2
  170. package/lib/logging/git.js +0 -54
  171. package/lib/logging/logs.d.ts +0 -22
  172. package/lib/logging/logs.js +0 -86
  173. package/lib/logging/table.d.ts +0 -3
  174. package/lib/logging/table.js +0 -47
  175. package/lib/sandboxes.d.ts +0 -14
  176. package/lib/sandboxes.js +0 -71
  177. package/models/HubSpotAuthError.d.ts +0 -12
  178. package/models/HubSpotAuthError.js +0 -20
  179. package/types/LogCallbacks.d.ts +0 -7
  180. package/utils/logger.d.ts +0 -5
  181. package/utils/logger.js +0 -23
  182. package/utils/objectUtils.d.ts +0 -8
  183. package/utils/objectUtils.js +0 -33
  184. /package/{utils → lib}/escapeRegExp.d.ts +0 -0
  185. /package/{utils → lib}/escapeRegExp.js +0 -0
  186. /package/lib/{logging/logger.d.ts → logger.d.ts} +0 -0
  187. /package/lib/{logging/logger.js → logger.js} +0 -0
  188. /package/{utils → lib}/notify.d.ts +0 -0
  189. /package/types/{LogCallbacks.js → Apps.js} +0 -0
@@ -0,0 +1,24 @@
1
+ import { HubSpotHttpErrorContext } from '../types/Error';
2
+ export declare class HubSpotHttpError<T = any> extends Error {
3
+ status?: number;
4
+ code?: string;
5
+ statusText?: string;
6
+ data?: T;
7
+ headers?: {
8
+ [key: string]: unknown;
9
+ };
10
+ method: string | undefined;
11
+ context: HubSpotHttpErrorContext | undefined;
12
+ derivedContext: HubSpotHttpErrorContext | undefined;
13
+ validationErrors: string[] | undefined;
14
+ detailedMessage?: string;
15
+ private divider;
16
+ cause: ErrorOptions['cause'];
17
+ constructor(message?: string, options?: ErrorOptions, context?: HubSpotHttpErrorContext);
18
+ updateContext(context: Partial<HubSpotHttpErrorContext>): void;
19
+ toString(): string;
20
+ formattedValidationErrors(): string;
21
+ private extractDerivedContext;
22
+ private parseValidationErrors;
23
+ private joinErrorMessages;
24
+ }
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HubSpotHttpError = void 0;
4
+ const axios_1 = require("axios");
5
+ const api_1 = require("../constants/api");
6
+ const lang_1 = require("../utils/lang");
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ class HubSpotHttpError extends Error {
9
+ status;
10
+ code;
11
+ statusText;
12
+ data;
13
+ headers;
14
+ method;
15
+ context;
16
+ derivedContext;
17
+ validationErrors;
18
+ detailedMessage;
19
+ divider = `\n- `;
20
+ cause;
21
+ constructor(message, options, context) {
22
+ super(message, options);
23
+ this.name = 'HubSpotHttpError';
24
+ this.context = context;
25
+ this.cause = options?.cause;
26
+ if (options && (0, axios_1.isAxiosError)(options.cause)) {
27
+ this.extractDerivedContext(options.cause);
28
+ const { response, config, code } = options.cause;
29
+ this.message = this.joinErrorMessages(options.cause, {
30
+ accountId: this.context?.accountId || this.derivedContext?.accountId,
31
+ });
32
+ this.detailedMessage = this.joinErrorMessages(options.cause, this.context);
33
+ this.code = code;
34
+ this.method = config?.method;
35
+ // Pull the request fields to the top level
36
+ if (response) {
37
+ this.status = response.status;
38
+ this.statusText = response.statusText;
39
+ this.data = response.data;
40
+ this.headers = response.headers;
41
+ this.parseValidationErrors(response.data);
42
+ }
43
+ }
44
+ else if (options && options.cause instanceof Error) {
45
+ const messages = options.cause.name !== 'Error' ? [`${options.cause.name}:`] : [];
46
+ if (options.cause.message) {
47
+ messages.push(options.cause.message);
48
+ }
49
+ if ('reason' in options.cause && options.cause.reason) {
50
+ messages.push(`${options.cause.reason}`);
51
+ }
52
+ this.message = messages.join(' ');
53
+ }
54
+ }
55
+ updateContext(context) {
56
+ this.context = { ...this.context, ...context };
57
+ // Update the error messages when the context is updated
58
+ if ((0, axios_1.isAxiosError)(this.cause)) {
59
+ this.message = this.joinErrorMessages(this.cause, this.context);
60
+ }
61
+ }
62
+ toString() {
63
+ const messages = [
64
+ `${this.name}: ${this.divider}message: ${this.detailedMessage}`,
65
+ ];
66
+ ['status', 'statusText', 'method', 'code'].forEach(field => {
67
+ if (Object.hasOwn(this, field)) {
68
+ // @ts-expect-error this[field] exists, so we know it is a property of this
69
+ messages.push(`${field}: ${this[field]}`);
70
+ }
71
+ });
72
+ if (this.validationErrors && this.validationErrors.length > 0) {
73
+ messages.push(`errors: ${this.formattedValidationErrors()}`);
74
+ }
75
+ if (this.context) {
76
+ messages.push(`context: ${JSON.stringify(this.context, undefined, 2)}`);
77
+ }
78
+ if (this.derivedContext) {
79
+ messages.push(`derivedContext: ${JSON.stringify(this.derivedContext, undefined, 2)}`);
80
+ }
81
+ return messages.join(this.divider);
82
+ }
83
+ formattedValidationErrors() {
84
+ if (!this.validationErrors || this.validationErrors?.length === 0) {
85
+ return '';
86
+ }
87
+ return this.validationErrors?.join(this.divider);
88
+ }
89
+ extractDerivedContext(cause) {
90
+ const generatedContext = {};
91
+ if (!cause) {
92
+ return;
93
+ }
94
+ generatedContext.accountId = cause.config?.params?.portalId;
95
+ generatedContext.payload = JSON.stringify(cause.config?.data);
96
+ // This will just be the url path
97
+ generatedContext.request = cause.config?.url;
98
+ // Allow the provided context to override the generated context
99
+ this.derivedContext = { ...this.derivedContext, ...generatedContext };
100
+ }
101
+ parseValidationErrors(responseData = { errors: [], message: '' }) {
102
+ if (!responseData) {
103
+ return;
104
+ }
105
+ const errorMessages = [];
106
+ const { errors, message } = responseData;
107
+ if (message) {
108
+ errorMessages.push(message);
109
+ }
110
+ if (errors) {
111
+ const specificErrors = errors.map(error => {
112
+ let errorMessage = error.message;
113
+ if (error.context?.requiredScopes) {
114
+ // Sometimes the scopes come back with duplicates
115
+ const scopes = new Set(error.context.requiredScopes);
116
+ scopes.forEach(item => {
117
+ errorMessage = `${errorMessage}\n - ${item}`;
118
+ });
119
+ }
120
+ else if (error.errorTokens && error.errorTokens.line) {
121
+ errorMessage = `line ${error.errorTokens.line}: ${errorMessage}`;
122
+ }
123
+ return errorMessage;
124
+ });
125
+ errorMessages.push(...specificErrors);
126
+ }
127
+ this.validationErrors = errorMessages;
128
+ }
129
+ joinErrorMessages(error, context = {}) {
130
+ const i18nKey = 'errors.apiErrors';
131
+ const status = error.response?.status;
132
+ const method = error.config?.method;
133
+ let messageDetail;
134
+ if (context.accountId) {
135
+ const action = (method && api_1.HTTP_METHOD_VERBS[method]) || api_1.HTTP_METHOD_VERBS.get;
136
+ const preposition = (method && api_1.HTTP_METHOD_PREPOSITIONS[method]) ||
137
+ api_1.HTTP_METHOD_PREPOSITIONS.get;
138
+ const requestName = context.request
139
+ ? `${action} ${preposition} '${context.request}'`
140
+ : action;
141
+ messageDetail = (0, lang_1.i18n)(`${i18nKey}.messageDetail`, {
142
+ accountId: context.accountId,
143
+ requestName,
144
+ });
145
+ }
146
+ else {
147
+ messageDetail = (0, lang_1.i18n)(`${i18nKey}.genericMessageDetail`);
148
+ }
149
+ const errorMessage = [];
150
+ if ((method === 'put' || method === 'post') && context.payload) {
151
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.unableToUpload`, { payload: context.payload }));
152
+ }
153
+ switch (status) {
154
+ case 400:
155
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.codes.400`, { messageDetail }));
156
+ break;
157
+ case 401:
158
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.codes.401`, { messageDetail }));
159
+ break;
160
+ case 403:
161
+ break;
162
+ case 404:
163
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.codes.404`, { messageDetail }));
164
+ break;
165
+ case 429:
166
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.codes.429`, { messageDetail }));
167
+ break;
168
+ case 503:
169
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.codes.503`, { messageDetail }));
170
+ break;
171
+ default:
172
+ if (status && status >= 500 && status < 600) {
173
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.codes.500Generic`, { messageDetail }));
174
+ }
175
+ else if (status && status >= 400 && status < 500) {
176
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.codes.400Generic`, { messageDetail }));
177
+ }
178
+ else {
179
+ errorMessage.push((0, lang_1.i18n)(`${i18nKey}.codes.generic`, { messageDetail }));
180
+ }
181
+ break;
182
+ }
183
+ if (error?.response?.data) {
184
+ const { message, errors } = error.response.data;
185
+ if (message) {
186
+ errorMessage.push(message);
187
+ }
188
+ (errors || []).forEach(err => {
189
+ if (err.message) {
190
+ errorMessage.push(`${this.divider}${err.message}`);
191
+ }
192
+ });
193
+ }
194
+ return errorMessage.join(' ');
195
+ }
196
+ }
197
+ exports.HubSpotHttpError = HubSpotHttpError;
@@ -1,34 +1,12 @@
1
- import { FlatAccountFields, OAuthAccount, TokenInfo } from '../types/Accounts';
2
- type WriteTokenInfoFunction = (tokenInfo: TokenInfo) => void;
3
- type RefreshTokenResponse = {
4
- refresh_token: string;
5
- access_token: string;
6
- expires_in: string;
7
- };
8
- type ExchangeProof = {
9
- grant_type: string;
10
- client_id?: string;
11
- client_secret?: string;
12
- refresh_token?: string;
13
- };
14
- declare class OAuth2Manager {
15
- account: OAuthAccount;
16
- writeTokenInfo: WriteTokenInfoFunction;
1
+ import { FlatAccountFields, OAuth2ManagerAccountConfig, WriteTokenInfoFunction, RefreshTokenResponse, ExchangeProof } from '../types/Accounts';
2
+ export declare class OAuth2Manager {
3
+ account: OAuth2ManagerAccountConfig;
4
+ writeTokenInfo?: WriteTokenInfoFunction;
17
5
  refreshTokenRequest: Promise<RefreshTokenResponse> | null;
18
- constructor(account: OAuthAccount, writeTokenInfo: WriteTokenInfoFunction);
6
+ constructor(account: OAuth2ManagerAccountConfig, writeTokenInfo?: WriteTokenInfoFunction);
19
7
  accessToken(): Promise<string | undefined>;
20
8
  fetchAccessToken(exchangeProof: ExchangeProof): Promise<void>;
21
9
  exchangeForTokens(exchangeProof: ExchangeProof): Promise<void>;
22
10
  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
11
  static fromConfig(accountConfig: FlatAccountFields, writeTokenInfo: WriteTokenInfoFunction): OAuth2Manager;
33
12
  }
34
- export default OAuth2Manager;
@@ -3,14 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.OAuth2Manager = void 0;
6
7
  const axios_1 = __importDefault(require("axios"));
7
8
  const moment_1 = __importDefault(require("moment"));
8
9
  const urls_1 = require("../lib/urls");
9
10
  const environment_1 = require("../lib/environment");
10
- const logger_1 = require("../utils/logger");
11
- const getAccountIdentifier_1 = require("../utils/getAccountIdentifier");
11
+ const logger_1 = require("../lib/logger");
12
+ const getAccountIdentifier_1 = require("../config/getAccountIdentifier");
12
13
  const auth_1 = require("../constants/auth");
13
- const standardErrors_1 = require("../errors/standardErrors");
14
+ const lang_1 = require("../utils/lang");
14
15
  const i18nKey = 'models.OAuth2Manager';
15
16
  class OAuth2Manager {
16
17
  account;
@@ -25,104 +26,80 @@ class OAuth2Manager {
25
26
  }
26
27
  }
27
28
  async accessToken() {
28
- if (!this.account.auth.tokenInfo?.refreshToken) {
29
- (0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.missingRefreshToken`, {
29
+ if (!this.account.tokenInfo?.refreshToken) {
30
+ throw new Error((0, lang_1.i18n)(`${i18nKey}.errors.missingRefreshToken`, {
30
31
  accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
31
- });
32
+ }));
32
33
  }
33
- if (!this.account.auth.tokenInfo?.accessToken ||
34
+ if (!this.account.tokenInfo?.accessToken ||
34
35
  (0, moment_1.default)()
35
36
  .add(5, 'minutes')
36
- .isAfter((0, moment_1.default)(this.account.auth?.tokenInfo.expiresAt))) {
37
+ .isAfter((0, moment_1.default)(new Date(this.account.tokenInfo.expiresAt || '')))) {
37
38
  await this.refreshAccessToken();
38
39
  }
39
- return this.account.auth?.tokenInfo.accessToken;
40
+ return this.account.tokenInfo.accessToken;
40
41
  }
41
42
  async fetchAccessToken(exchangeProof) {
42
- (0, logger_1.debug)(`${i18nKey}.fetchingAccessToken`, {
43
+ logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.fetchingAccessToken`, {
43
44
  accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
44
- clientId: this.account.auth.clientId || '',
45
- });
45
+ clientId: this.account.clientId || '',
46
+ }));
46
47
  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,
48
+ const { data } = await (0, axios_1.default)({
49
+ url: `${(0, urls_1.getHubSpotApiOrigin)((0, environment_1.getValidEnv)(this.account.env))}/oauth/v1/token`,
50
+ method: 'post',
51
+ data: exchangeProof,
52
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
50
53
  });
51
54
  this.refreshTokenRequest = data;
52
55
  const { refresh_token: refreshToken, access_token: accessToken, expires_in: expiresIn, } = data;
53
- if (!this.account.auth.tokenInfo) {
54
- this.account.auth.tokenInfo = {};
56
+ if (!this.account.tokenInfo) {
57
+ this.account.tokenInfo = {};
55
58
  }
56
- this.account.auth.tokenInfo.refreshToken = refreshToken;
57
- this.account.auth.tokenInfo.accessToken = accessToken;
58
- this.account.auth.tokenInfo.expiresAt = (0, moment_1.default)()
59
+ this.account.tokenInfo.refreshToken = refreshToken;
60
+ this.account.tokenInfo.accessToken = accessToken;
61
+ this.account.tokenInfo.expiresAt = (0, moment_1.default)()
59
62
  .add(Math.round(parseInt(expiresIn) * 0.75), 'seconds')
60
63
  .toString();
61
64
  if (this.writeTokenInfo) {
62
- (0, logger_1.debug)(`${i18nKey}.updatingTokenInfo`, {
65
+ logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.updatingTokenInfo`, {
63
66
  accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
64
- clientId: this.account.auth.clientId || '',
65
- });
66
- this.writeTokenInfo(this.account.auth.tokenInfo);
67
+ clientId: this.account.clientId || '',
68
+ }));
69
+ this.writeTokenInfo(this.account.tokenInfo);
67
70
  }
68
- this.refreshTokenRequest = null;
69
71
  }
70
- catch (e) {
72
+ finally {
71
73
  this.refreshTokenRequest = null;
72
- (0, standardErrors_1.throwError)(e);
73
74
  }
74
75
  }
75
76
  async exchangeForTokens(exchangeProof) {
76
- try {
77
- if (this.refreshTokenRequest) {
78
- (0, logger_1.debug)(`${i18nKey}.refreshingAccessToken`, {
79
- accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
80
- clientId: this.account.auth.clientId || '',
81
- });
82
- await this.refreshTokenRequest;
83
- }
84
- else {
85
- await this.fetchAccessToken(exchangeProof);
86
- }
77
+ if (this.refreshTokenRequest) {
78
+ logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.refreshingAccessToken`, {
79
+ accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
80
+ clientId: this.account.clientId || '',
81
+ }));
82
+ await this.refreshTokenRequest;
87
83
  }
88
- catch (e) {
89
- const error = e;
90
- if (error.response) {
91
- (0, standardErrors_1.throwAuthErrorWithMessage)(`${i18nKey}.errors.auth`, {
92
- token: error.response.data.message || '',
93
- }, error);
94
- }
95
- else {
96
- (0, standardErrors_1.throwError)(error);
97
- }
84
+ else {
85
+ await this.fetchAccessToken(exchangeProof);
98
86
  }
99
87
  }
100
88
  async refreshAccessToken() {
101
89
  const refreshTokenProof = {
102
90
  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,
91
+ client_id: this.account.clientId,
92
+ client_secret: this.account.clientSecret,
93
+ refresh_token: this.account.tokenInfo?.refreshToken,
106
94
  };
107
95
  await this.exchangeForTokens(refreshTokenProof);
108
96
  }
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
97
  static fromConfig(accountConfig, writeTokenInfo) {
121
98
  return new OAuth2Manager({
122
99
  ...accountConfig,
123
100
  authType: auth_1.AUTH_METHODS.oauth.value,
124
- auth: accountConfig.auth || {},
101
+ ...(accountConfig.auth || {}),
125
102
  }, writeTokenInfo);
126
103
  }
127
104
  }
128
- exports.default = OAuth2Manager;
105
+ exports.OAuth2Manager = OAuth2Manager;
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@hubspot/local-dev-lib",
3
- "version": "0.2.1",
3
+ "version": "0.2.2-experimental.0",
4
4
  "description": "Provides library functionality for HubSpot local development tooling, including the HubSpot CLI",
5
- "main": "lib/index.js",
6
5
  "repository": {
7
6
  "type": "git",
8
7
  "url": "https://github.com/HubSpot/hubspot-local-dev-lib"
@@ -12,22 +11,16 @@
12
11
  "access": "public"
13
12
  },
14
13
  "scripts": {
15
- "build": "rm -rf ./dist/ && tsc --rootDir . --outdir dist && yarn copy-files && yarn clear-postinstall",
16
- "check-main": "branch=$(git rev-parse --abbrev-ref HEAD) && [ $branch = main ] || (echo 'Error: New release can only be published on main branch' && exit 1)",
17
- "clear-postinstall": "cd dist && npm pkg delete scripts.postinstall",
18
- "copy-files": "cp -r lang dist/lang",
19
- "lint": "eslint --max-warnings=0 . && prettier --check ./**/*.ts",
14
+ "build": "ts-node ./scripts/build.ts",
15
+ "lint": "eslint --max-warnings=0 . && prettier . --check",
20
16
  "local-dev": "yarn build && cd dist && yarn link && cd .. && tsc --watch --rootDir . --outdir dist",
21
- "prettier:write": "prettier --write ./**/*.{ts,js,json}",
22
- "pub": "cd dist && npm publish --tag latest && cd ..",
23
- "push": "git push --atomic origin main v$npm_package_version",
24
- "release:major": "yarn check-main && yarn version --major && yarn build && yarn pub && yarn push",
25
- "release:minor": "yarn check-main && yarn version --minor && yarn build && yarn pub && yarn push",
26
- "release:patch": "yarn check-main && yarn version --patch && yarn build && yarn pub && yarn push",
27
- "test": "node --experimental-vm-modules ./node_modules/.bin/jest"
17
+ "prettier:write": "prettier . --write",
18
+ "release": "ts-node ./scripts/release.ts release",
19
+ "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ./node_modules/.bin/jest"
28
20
  },
29
21
  "license": "Apache-2.0",
30
22
  "devDependencies": {
23
+ "@inquirer/prompts": "^7.0.1",
31
24
  "@types/content-disposition": "^0.5.5",
32
25
  "@types/cors": "^2.8.15",
33
26
  "@types/debounce": "^1.2.1",
@@ -37,44 +30,51 @@
37
30
  "@types/jest": "^29.5.0",
38
31
  "@types/js-yaml": "^4.0.5",
39
32
  "@types/node": "^18.14.2",
40
- "@types/prettier": "^3.0.0",
41
33
  "@types/unixify": "^1.0.0",
42
34
  "@typescript-eslint/eslint-plugin": "^5.54.0",
43
35
  "@typescript-eslint/parser": "^5.59.7",
44
36
  "eslint": "^8.35.0",
37
+ "eslint-plugin-import": "^2.29.1",
45
38
  "husky": "^8.0.0",
46
39
  "jest": "^29.5.0",
40
+ "open": "^8.4.2",
47
41
  "ts-jest": "^29.0.5",
48
- "typescript": "^4.9.5"
42
+ "ts-node": "^10.9.2",
43
+ "typescript": "^4.9.5",
44
+ "yargs": "^17.7.2"
49
45
  },
50
46
  "exports": {
51
47
  "./*": "./lib/*.js",
52
48
  "./api/*": "./api/*.js",
53
49
  "./errors/*": "./errors/*.js",
54
50
  "./http": "./http/index.js",
51
+ "./http/*": "./http/*.js",
52
+ "./config/getAccountIdentifier": "./config/getAccountIdentifier.js",
53
+ "./config/migrate": "./config/migrate.js",
55
54
  "./config": "./config/index.js",
56
55
  "./constants/*": "./constants/*.js",
57
- "./logger": "./lib/logging/logger.js"
56
+ "./models/*": "./models/*.js",
57
+ "./types/*": "./types/*.d.ts"
58
58
  },
59
59
  "dependencies": {
60
- "address": "^2.0.1",
61
- "axios": "^1.3.5",
62
- "chalk": "^2.4.2",
63
- "chokidar": "^3.5.3",
64
- "content-disposition": "^0.5.4",
65
- "cors": "^2.8.5",
66
- "express": "^4.18.2",
67
- "extract-zip": "^2.0.1",
68
- "findup-sync": "^5.0.0",
69
- "fs-extra": "^11.1.0",
70
- "ignore": "^5.1.4",
71
- "js-yaml": "^4.1.0",
72
- "moment": "^2.29.4",
73
- "p-queue": "^6.0.2",
74
- "prettier": "^3.0.3",
75
- "semver": "^6.3.0",
76
- "table": "^6.8.1",
77
- "unixify": "^1.0.0"
60
+ "address": "2.0.2",
61
+ "axios": "1.7.2",
62
+ "chalk": "2.4.2",
63
+ "chokidar": "3.6.0",
64
+ "content-disposition": "0.5.4",
65
+ "cors": "2.8.5",
66
+ "debounce": "1.2.1",
67
+ "express": "4.19.2",
68
+ "extract-zip": "2.0.1",
69
+ "findup-sync": "5.0.0",
70
+ "fs-extra": "11.2.0",
71
+ "ignore": "5.3.1",
72
+ "js-yaml": "4.1.0",
73
+ "moment": "2.30.1",
74
+ "p-queue": "6.6.2",
75
+ "prettier": "3.3.1",
76
+ "semver": "6.3.1",
77
+ "unixify": "1.0.0"
78
78
  },
79
79
  "engines": {
80
80
  "node": ">=16.20.0"