@hubspot/local-dev-lib 0.0.1-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 (230) 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 -0
  6. package/api/customObjects.js +45 -0
  7. package/api/designManager.d.ts +5 -0
  8. package/api/designManager.js +18 -0
  9. package/api/developerTestAccounts.d.ts +7 -0
  10. package/api/developerTestAccounts.js +48 -0
  11. package/api/fileManager.d.ts +6 -0
  12. package/api/fileManager.js +63 -0
  13. package/api/fileMapper.d.ts +12 -0
  14. package/api/fileMapper.js +106 -0
  15. package/api/fileTransport.d.ts +4 -0
  16. package/api/fileTransport.js +39 -0
  17. package/api/functions.d.ts +8 -0
  18. package/api/functions.js +43 -0
  19. package/api/github.d.ts +11 -0
  20. package/api/github.js +71 -0
  21. package/api/hubdb.d.ts +12 -0
  22. package/api/hubdb.js +67 -0
  23. package/api/lighthouseScore.d.ts +6 -0
  24. package/api/lighthouseScore.js +26 -0
  25. package/api/localDevAuth.d.ts +8 -0
  26. package/api/localDevAuth.js +53 -0
  27. package/api/marketplaceValidation.d.ts +6 -0
  28. package/api/marketplaceValidation.js +26 -0
  29. package/api/projects.d.ts +40 -0
  30. package/api/projects.js +216 -0
  31. package/api/sandboxHubs.d.ts +7 -0
  32. package/api/sandboxHubs.js +49 -0
  33. package/api/sandboxSync.d.ts +4 -0
  34. package/api/sandboxSync.js +26 -0
  35. package/api/secrets.d.ts +6 -0
  36. package/api/secrets.js +37 -0
  37. package/api/validateHubl.d.ts +3 -0
  38. package/api/validateHubl.js +15 -0
  39. package/config/CLIConfiguration.d.ts +62 -0
  40. package/config/CLIConfiguration.js +467 -0
  41. package/config/configFile.d.ts +21 -0
  42. package/config/configFile.js +102 -0
  43. package/config/configUtils.d.ts +5 -0
  44. package/config/configUtils.js +87 -0
  45. package/config/config_DEPRECATED.d.ts +75 -0
  46. package/config/config_DEPRECATED.js +678 -0
  47. package/config/environment.d.ts +2 -0
  48. package/config/environment.js +60 -0
  49. package/config/getAccountIdentifier.d.ts +2 -0
  50. package/config/getAccountIdentifier.js +15 -0
  51. package/config/index.d.ts +41 -0
  52. package/config/index.js +260 -0
  53. package/constants/api.d.ts +15 -0
  54. package/constants/api.js +18 -0
  55. package/constants/auth.d.ts +37 -0
  56. package/constants/auth.js +38 -0
  57. package/constants/config.d.ts +18 -0
  58. package/constants/config.js +22 -0
  59. package/constants/environments.d.ts +15 -0
  60. package/constants/environments.js +18 -0
  61. package/constants/extensions.d.ts +6 -0
  62. package/constants/extensions.js +28 -0
  63. package/constants/files.d.ts +21 -0
  64. package/constants/files.js +24 -0
  65. package/constants/ports.d.ts +3 -0
  66. package/constants/ports.js +6 -0
  67. package/enums/build.d.ts +36 -0
  68. package/enums/build.js +39 -0
  69. package/enums/deploy.d.ts +11 -0
  70. package/enums/deploy.js +14 -0
  71. package/enums/project.d.ts +6 -0
  72. package/enums/project.js +9 -0
  73. package/errors/errors_DEPRECATED.d.ts +3 -0
  74. package/errors/errors_DEPRECATED.js +60 -0
  75. package/errors/index.d.ts +18 -0
  76. package/errors/index.js +63 -0
  77. package/http/addQueryParams.d.ts +2 -0
  78. package/http/addQueryParams.js +14 -0
  79. package/http/getAxiosConfig.d.ts +9 -0
  80. package/http/getAxiosConfig.js +66 -0
  81. package/http/index.d.ts +17 -0
  82. package/http/index.js +173 -0
  83. package/http/unauthed.d.ts +15 -0
  84. package/http/unauthed.js +38 -0
  85. package/lang/en.json +389 -0
  86. package/lib/archive.d.ts +3 -0
  87. package/lib/archive.js +117 -0
  88. package/lib/cms/functions.d.ts +8 -0
  89. package/lib/cms/functions.js +181 -0
  90. package/lib/cms/handleFieldsJS.d.ts +33 -0
  91. package/lib/cms/handleFieldsJS.js +148 -0
  92. package/lib/cms/modules.d.ts +14 -0
  93. package/lib/cms/modules.js +186 -0
  94. package/lib/cms/processFieldsJs.d.ts +1 -0
  95. package/lib/cms/processFieldsJs.js +97 -0
  96. package/lib/cms/templates.d.ts +65 -0
  97. package/lib/cms/templates.js +107 -0
  98. package/lib/cms/themes.d.ts +2 -0
  99. package/lib/cms/themes.js +34 -0
  100. package/lib/cms/uploadFolder.d.ts +7 -0
  101. package/lib/cms/uploadFolder.js +202 -0
  102. package/lib/cms/validate.d.ts +2 -0
  103. package/lib/cms/validate.js +40 -0
  104. package/lib/cms/watch.d.ts +4 -0
  105. package/lib/cms/watch.js +201 -0
  106. package/lib/customObjects.d.ts +5 -0
  107. package/lib/customObjects.js +42 -0
  108. package/lib/environment.d.ts +2 -0
  109. package/lib/environment.js +16 -0
  110. package/lib/escapeRegExp.d.ts +1 -0
  111. package/lib/escapeRegExp.js +7 -0
  112. package/lib/fileManager.d.ts +2 -0
  113. package/lib/fileManager.js +184 -0
  114. package/lib/fileMapper.d.ts +18 -0
  115. package/lib/fileMapper.js +317 -0
  116. package/lib/fs.d.ts +4 -0
  117. package/lib/fs.js +71 -0
  118. package/lib/github.d.ts +8 -0
  119. package/lib/github.js +167 -0
  120. package/lib/gitignore.d.ts +3 -0
  121. package/lib/gitignore.js +49 -0
  122. package/lib/hubdb.d.ts +17 -0
  123. package/lib/hubdb.js +133 -0
  124. package/lib/ignoreRules.d.ts +3 -0
  125. package/lib/ignoreRules.js +69 -0
  126. package/lib/logger.d.ts +56 -0
  127. package/lib/logger.js +146 -0
  128. package/lib/notify.d.ts +1 -0
  129. package/lib/notify.js +43 -0
  130. package/lib/oauth.d.ts +4 -0
  131. package/lib/oauth.js +34 -0
  132. package/lib/path.d.ts +14 -0
  133. package/lib/path.js +134 -0
  134. package/lib/personalAccessKey.d.ts +10 -0
  135. package/lib/personalAccessKey.js +163 -0
  136. package/lib/portManager.d.ts +10 -0
  137. package/lib/portManager.js +46 -0
  138. package/lib/text.d.ts +2 -0
  139. package/lib/text.js +24 -0
  140. package/lib/trackUsage.d.ts +1 -0
  141. package/lib/trackUsage.js +63 -0
  142. package/lib/urls.d.ts +2 -0
  143. package/lib/urls.js +24 -0
  144. package/models/FileSystemError.d.ts +6 -0
  145. package/models/FileSystemError.js +47 -0
  146. package/models/HubSpotHttpError.d.ts +24 -0
  147. package/models/HubSpotHttpError.js +197 -0
  148. package/models/OAuth2Manager.d.ts +12 -0
  149. package/models/OAuth2Manager.js +105 -0
  150. package/package.json +81 -0
  151. package/types/Accounts.d.ts +178 -0
  152. package/types/Accounts.js +2 -0
  153. package/types/Activity.d.ts +20 -0
  154. package/types/Activity.js +2 -0
  155. package/types/Api.d.ts +2 -0
  156. package/types/Api.js +2 -0
  157. package/types/Apps.d.ts +77 -0
  158. package/types/Apps.js +2 -0
  159. package/types/Archive.d.ts +9 -0
  160. package/types/Archive.js +2 -0
  161. package/types/Build.d.ts +41 -0
  162. package/types/Build.js +2 -0
  163. package/types/CLIOptions.d.ts +8 -0
  164. package/types/CLIOptions.js +2 -0
  165. package/types/ComponentStructure.d.ts +40 -0
  166. package/types/ComponentStructure.js +2 -0
  167. package/types/Config.d.ts +37 -0
  168. package/types/Config.js +2 -0
  169. package/types/Deploy.d.ts +42 -0
  170. package/types/Deploy.js +2 -0
  171. package/types/DesignManager.d.ts +10 -0
  172. package/types/DesignManager.js +2 -0
  173. package/types/Error.d.ts +37 -0
  174. package/types/Error.js +2 -0
  175. package/types/FieldsJS.d.ts +1 -0
  176. package/types/FieldsJS.js +2 -0
  177. package/types/FileManager.d.ts +71 -0
  178. package/types/FileManager.js +2 -0
  179. package/types/Files.d.ts +79 -0
  180. package/types/Files.js +2 -0
  181. package/types/Functions.d.ts +66 -0
  182. package/types/Functions.js +2 -0
  183. package/types/Github.d.ts +76 -0
  184. package/types/Github.js +2 -0
  185. package/types/Http.d.ts +29 -0
  186. package/types/Http.js +2 -0
  187. package/types/Hubdb.d.ts +109 -0
  188. package/types/Hubdb.js +2 -0
  189. package/types/HublValidation.d.ts +59 -0
  190. package/types/HublValidation.js +2 -0
  191. package/types/Lang.d.ts +10 -0
  192. package/types/Lang.js +2 -0
  193. package/types/Lighthouse.d.ts +25 -0
  194. package/types/Lighthouse.js +2 -0
  195. package/types/MarketplaceValidation.d.ts +28 -0
  196. package/types/MarketplaceValidation.js +2 -0
  197. package/types/Migration.d.ts +28 -0
  198. package/types/Migration.js +10 -0
  199. package/types/Modules.d.ts +16 -0
  200. package/types/Modules.js +2 -0
  201. package/types/PortManager.d.ts +11 -0
  202. package/types/PortManager.js +2 -0
  203. package/types/Project.d.ts +42 -0
  204. package/types/Project.js +2 -0
  205. package/types/ProjectLog.d.ts +9 -0
  206. package/types/ProjectLog.js +2 -0
  207. package/types/Sandbox.d.ts +155 -0
  208. package/types/Sandbox.js +2 -0
  209. package/types/Schemas.d.ts +39 -0
  210. package/types/Schemas.js +2 -0
  211. package/types/Secrets.d.ts +3 -0
  212. package/types/Secrets.js +2 -0
  213. package/types/Utils.d.ts +6 -0
  214. package/types/Utils.js +2 -0
  215. package/types/developerTestAccounts.d.ts +12 -0
  216. package/types/developerTestAccounts.js +2 -0
  217. package/utils/PortManagerServer.d.ts +26 -0
  218. package/utils/PortManagerServer.js +158 -0
  219. package/utils/accounts.d.ts +4 -0
  220. package/utils/accounts.js +28 -0
  221. package/utils/cms/fieldsJS.d.ts +2 -0
  222. package/utils/cms/fieldsJS.js +18 -0
  223. package/utils/cms/modules.d.ts +4 -0
  224. package/utils/cms/modules.js +54 -0
  225. package/utils/detectPort.d.ts +1 -0
  226. package/utils/detectPort.js +102 -0
  227. package/utils/git.d.ts +3 -0
  228. package/utils/git.js +71 -0
  229. package/utils/lang.d.ts +6 -0
  230. package/utils/lang.js +88 -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;
@@ -0,0 +1,12 @@
1
+ import { FlatAccountFields, OAuth2ManagerAccountConfig, WriteTokenInfoFunction, RefreshTokenResponse, ExchangeProof } from '../types/Accounts';
2
+ export declare class OAuth2Manager {
3
+ account: OAuth2ManagerAccountConfig;
4
+ writeTokenInfo?: WriteTokenInfoFunction;
5
+ refreshTokenRequest: Promise<RefreshTokenResponse> | null;
6
+ constructor(account: OAuth2ManagerAccountConfig, writeTokenInfo?: WriteTokenInfoFunction);
7
+ accessToken(): Promise<string | undefined>;
8
+ fetchAccessToken(exchangeProof: ExchangeProof): Promise<void>;
9
+ exchangeForTokens(exchangeProof: ExchangeProof): Promise<void>;
10
+ refreshAccessToken(): Promise<void>;
11
+ static fromConfig(accountConfig: FlatAccountFields, writeTokenInfo: WriteTokenInfoFunction): OAuth2Manager;
12
+ }
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.OAuth2Manager = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const moment_1 = __importDefault(require("moment"));
9
+ const urls_1 = require("../lib/urls");
10
+ const environment_1 = require("../lib/environment");
11
+ const logger_1 = require("../lib/logger");
12
+ const getAccountIdentifier_1 = require("../config/getAccountIdentifier");
13
+ const auth_1 = require("../constants/auth");
14
+ const lang_1 = require("../utils/lang");
15
+ const i18nKey = 'models.OAuth2Manager';
16
+ class OAuth2Manager {
17
+ account;
18
+ writeTokenInfo;
19
+ refreshTokenRequest;
20
+ constructor(account, writeTokenInfo) {
21
+ this.writeTokenInfo = writeTokenInfo;
22
+ this.refreshTokenRequest = null;
23
+ this.account = account;
24
+ if (this.account.env) {
25
+ this.account.env = (0, environment_1.getValidEnv)(this.account.env, '');
26
+ }
27
+ }
28
+ async accessToken() {
29
+ if (!this.account.tokenInfo?.refreshToken) {
30
+ throw new Error((0, lang_1.i18n)(`${i18nKey}.errors.missingRefreshToken`, {
31
+ accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
32
+ }));
33
+ }
34
+ if (!this.account.tokenInfo?.accessToken ||
35
+ (0, moment_1.default)()
36
+ .add(5, 'minutes')
37
+ .isAfter((0, moment_1.default)(new Date(this.account.tokenInfo.expiresAt || '')))) {
38
+ await this.refreshAccessToken();
39
+ }
40
+ return this.account.tokenInfo.accessToken;
41
+ }
42
+ async fetchAccessToken(exchangeProof) {
43
+ logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.fetchingAccessToken`, {
44
+ accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
45
+ clientId: this.account.clientId || '',
46
+ }));
47
+ try {
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' },
53
+ });
54
+ this.refreshTokenRequest = data;
55
+ const { refresh_token: refreshToken, access_token: accessToken, expires_in: expiresIn, } = data;
56
+ if (!this.account.tokenInfo) {
57
+ this.account.tokenInfo = {};
58
+ }
59
+ this.account.tokenInfo.refreshToken = refreshToken;
60
+ this.account.tokenInfo.accessToken = accessToken;
61
+ this.account.tokenInfo.expiresAt = (0, moment_1.default)()
62
+ .add(Math.round(parseInt(expiresIn) * 0.75), 'seconds')
63
+ .toString();
64
+ if (this.writeTokenInfo) {
65
+ logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.updatingTokenInfo`, {
66
+ accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
67
+ clientId: this.account.clientId || '',
68
+ }));
69
+ this.writeTokenInfo(this.account.tokenInfo);
70
+ }
71
+ }
72
+ finally {
73
+ this.refreshTokenRequest = null;
74
+ }
75
+ }
76
+ async exchangeForTokens(exchangeProof) {
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;
83
+ }
84
+ else {
85
+ await this.fetchAccessToken(exchangeProof);
86
+ }
87
+ }
88
+ async refreshAccessToken() {
89
+ const refreshTokenProof = {
90
+ grant_type: 'refresh_token',
91
+ client_id: this.account.clientId,
92
+ client_secret: this.account.clientSecret,
93
+ refresh_token: this.account.tokenInfo?.refreshToken,
94
+ };
95
+ await this.exchangeForTokens(refreshTokenProof);
96
+ }
97
+ static fromConfig(accountConfig, writeTokenInfo) {
98
+ return new OAuth2Manager({
99
+ ...accountConfig,
100
+ authType: auth_1.AUTH_METHODS.oauth.value,
101
+ ...(accountConfig.auth || {}),
102
+ }, writeTokenInfo);
103
+ }
104
+ }
105
+ exports.OAuth2Manager = OAuth2Manager;
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "name": "@hubspot/local-dev-lib",
3
+ "version": "0.0.1-experimental.0",
4
+ "description": "Provides library functionality for HubSpot local development tooling, including the HubSpot CLI",
5
+ "main": "lib/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/HubSpot/hubspot-local-dev-lib"
9
+ },
10
+ "publishConfig": {
11
+ "registry": "https://registry.npmjs.org/",
12
+ "access": "public"
13
+ },
14
+ "scripts": {
15
+ "build": "ts-node ./scripts/build.ts",
16
+ "lint": "eslint --max-warnings=0 . && prettier . --check",
17
+ "local-dev": "yarn build && cd dist && yarn link && cd .. && tsc --watch --rootDir . --outdir dist",
18
+ "prettier:write": "prettier . --write",
19
+ "release": "ts-node ./scripts/release.ts release",
20
+ "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ./node_modules/.bin/jest"
21
+ },
22
+ "license": "Apache-2.0",
23
+ "devDependencies": {
24
+ "@inquirer/prompts": "^7.0.1",
25
+ "@types/content-disposition": "^0.5.5",
26
+ "@types/cors": "^2.8.15",
27
+ "@types/debounce": "^1.2.1",
28
+ "@types/express": "^4.17.18",
29
+ "@types/findup-sync": "^4.0.2",
30
+ "@types/fs-extra": "^11.0.1",
31
+ "@types/jest": "^29.5.0",
32
+ "@types/js-yaml": "^4.0.5",
33
+ "@types/node": "^18.14.2",
34
+ "@types/unixify": "^1.0.0",
35
+ "@typescript-eslint/eslint-plugin": "^5.54.0",
36
+ "@typescript-eslint/parser": "^5.59.7",
37
+ "eslint": "^8.35.0",
38
+ "eslint-plugin-import": "^2.29.1",
39
+ "husky": "^8.0.0",
40
+ "jest": "^29.5.0",
41
+ "ts-jest": "^29.0.5",
42
+ "ts-node": "^10.9.2",
43
+ "typescript": "^4.9.5",
44
+ "yargs": "^17.7.2"
45
+ },
46
+ "exports": {
47
+ "./*": "./lib/*.js",
48
+ "./api/*": "./api/*.js",
49
+ "./errors/*": "./errors/*.js",
50
+ "./http": "./http/index.js",
51
+ "./http/*": "./http/*.js",
52
+ "./config/getAccountIdentifier": "./config/getAccountIdentifier.js",
53
+ "./config": "./config/index.js",
54
+ "./constants/*": "./constants/*.js",
55
+ "./models/*": "./models/*.js",
56
+ "./types/*": "./types/*.d.ts"
57
+ },
58
+ "dependencies": {
59
+ "address": "^2.0.1",
60
+ "axios": "^1.3.5",
61
+ "chalk": "^2.4.2",
62
+ "chokidar": "^3.5.3",
63
+ "content-disposition": "^0.5.4",
64
+ "cors": "^2.8.5",
65
+ "debounce": "^1.2.0",
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.3.0",
75
+ "semver": "^6.3.0",
76
+ "unixify": "^1.0.0"
77
+ },
78
+ "engines": {
79
+ "node": ">=16.20.0"
80
+ }
81
+ }
@@ -0,0 +1,178 @@
1
+ import { HUBSPOT_ACCOUNT_TYPES } from '../constants/config';
2
+ import { Mode } from './Files';
3
+ import { Environment } from './Config';
4
+ import { ValueOf } from './Utils';
5
+ export type AuthType = 'personalaccesskey' | 'apikey' | 'oauth2';
6
+ export interface CLIAccount_NEW {
7
+ name?: string;
8
+ accountId: number;
9
+ accountType?: AccountType;
10
+ defaultMode?: Mode;
11
+ env: Environment;
12
+ authType?: AuthType;
13
+ auth?: {
14
+ tokenInfo?: TokenInfo;
15
+ };
16
+ sandboxAccountType?: string | null;
17
+ parentAccountId?: number | null;
18
+ apiKey?: string;
19
+ personalAccessKey?: string;
20
+ }
21
+ export interface CLIAccount_DEPRECATED {
22
+ name?: string;
23
+ portalId?: number;
24
+ defaultMode?: Mode;
25
+ env: Environment;
26
+ accountType?: AccountType;
27
+ authType?: AuthType;
28
+ auth?: {
29
+ tokenInfo?: TokenInfo;
30
+ };
31
+ sandboxAccountType?: string | null;
32
+ parentAccountId?: number | null;
33
+ apiKey?: string;
34
+ personalAccessKey?: string;
35
+ }
36
+ export type CLIAccount = CLIAccount_NEW | CLIAccount_DEPRECATED;
37
+ export type GenericAccount = {
38
+ portalId?: number;
39
+ accountId?: number;
40
+ };
41
+ export type AccountType = ValueOf<typeof HUBSPOT_ACCOUNT_TYPES>;
42
+ export type TokenInfo = {
43
+ accessToken?: string;
44
+ expiresAt?: string;
45
+ refreshToken?: string;
46
+ };
47
+ export interface PersonalAccessKeyAccount_NEW extends CLIAccount_NEW {
48
+ authType: 'personalaccesskey';
49
+ personalAccessKey: string;
50
+ }
51
+ export interface PersonalAccessKeyAccount_DEPRECATED extends CLIAccount_DEPRECATED {
52
+ authType: 'personalaccesskey';
53
+ personalAccessKey: string;
54
+ }
55
+ export type PersonalAccessKeyAccount = PersonalAccessKeyAccount_NEW | PersonalAccessKeyAccount_DEPRECATED;
56
+ export interface OAuthAccount_NEW extends CLIAccount_NEW {
57
+ authType: 'oauth2';
58
+ auth: {
59
+ clientId?: string;
60
+ clientSecret?: string;
61
+ scopes?: Array<string>;
62
+ tokenInfo?: TokenInfo;
63
+ };
64
+ }
65
+ export interface OAuthAccount_DEPRECATED extends CLIAccount_DEPRECATED {
66
+ authType: 'oauth2';
67
+ auth: {
68
+ clientId?: string;
69
+ clientSecret?: string;
70
+ scopes?: Array<string>;
71
+ tokenInfo?: TokenInfo;
72
+ };
73
+ }
74
+ export type OAuthAccount = OAuthAccount_NEW | OAuthAccount_DEPRECATED;
75
+ export interface APIKeyAccount_NEW extends CLIAccount_NEW {
76
+ authType: 'apikey';
77
+ apiKey: string;
78
+ }
79
+ export interface APIKeyAccount_DEPRECATED extends CLIAccount_DEPRECATED {
80
+ authType: 'apikey';
81
+ apiKey: string;
82
+ }
83
+ export type APIKeyAccount = APIKeyAccount_NEW | APIKeyAccount_DEPRECATED;
84
+ export interface FlatAccountFields_NEW extends CLIAccount_NEW {
85
+ tokenInfo?: TokenInfo;
86
+ clientId?: string;
87
+ clientSecret?: string;
88
+ scopes?: Array<string>;
89
+ apiKey?: string;
90
+ personalAccessKey?: string;
91
+ }
92
+ export interface FlatAccountFields_DEPRECATED extends CLIAccount_DEPRECATED {
93
+ tokenInfo?: TokenInfo;
94
+ clientId?: string;
95
+ clientSecret?: string;
96
+ scopes?: Array<string>;
97
+ apiKey?: string;
98
+ personalAccessKey?: string;
99
+ }
100
+ export type FlatAccountFields = FlatAccountFields_NEW | FlatAccountFields_DEPRECATED;
101
+ export type ScopeData = {
102
+ portalScopesInGroup: Array<string>;
103
+ userScopesInGroup: Array<string>;
104
+ };
105
+ export type AccessTokenResponse = {
106
+ hubId: number;
107
+ userId: number;
108
+ oauthAccessToken: string;
109
+ expiresAtMillis: number;
110
+ enabledFeatures?: {
111
+ [key: string]: number;
112
+ };
113
+ scopeGroups: Array<string>;
114
+ encodedOAuthRefreshToken: string;
115
+ hubName: string;
116
+ accountType: ValueOf<typeof HUBSPOT_ACCOUNT_TYPES>;
117
+ };
118
+ export type EnabledFeaturesResponse = {
119
+ enabledFeatures: {
120
+ [key: string]: boolean;
121
+ };
122
+ };
123
+ export type UpdateAccountConfigOptions = Partial<FlatAccountFields_DEPRECATED> & {
124
+ environment?: Environment;
125
+ };
126
+ export type PersonalAccessKeyOptions = {
127
+ accountId: number;
128
+ personalAccessKey: string;
129
+ env: Environment;
130
+ };
131
+ export type OAuthOptions = {
132
+ accountId: number;
133
+ clientId: string;
134
+ clientSecret: string;
135
+ refreshToken: string;
136
+ scopes: Array<string>;
137
+ env: Environment;
138
+ };
139
+ export type APIKeyOptions = {
140
+ accountId: number;
141
+ apiKey: string;
142
+ env: Environment;
143
+ };
144
+ export type AccessToken = {
145
+ portalId: number;
146
+ accessToken: string;
147
+ expiresAt: string;
148
+ scopeGroups: Array<string>;
149
+ enabledFeatures?: {
150
+ [key: string]: number;
151
+ };
152
+ encodedOAuthRefreshToken: string;
153
+ hubName: string;
154
+ accountType: ValueOf<typeof HUBSPOT_ACCOUNT_TYPES>;
155
+ };
156
+ export type OAuth2ManagerAccountConfig = {
157
+ name?: string;
158
+ accountId?: number;
159
+ clientId?: string;
160
+ clientSecret?: string;
161
+ scopes?: Array<string>;
162
+ env?: Environment;
163
+ environment?: Environment;
164
+ tokenInfo?: TokenInfo;
165
+ authType?: 'oauth2';
166
+ };
167
+ export type WriteTokenInfoFunction = (tokenInfo: TokenInfo) => void;
168
+ export type RefreshTokenResponse = {
169
+ refresh_token: string;
170
+ access_token: string;
171
+ expires_in: string;
172
+ };
173
+ export type ExchangeProof = {
174
+ grant_type: string;
175
+ client_id?: string;
176
+ client_secret?: string;
177
+ refresh_token?: string;
178
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,20 @@
1
+ import { ACTIVITY_SOURCE } from '../enums/project';
2
+ import { ValueOf } from './Utils';
3
+ import { GithubSourceData } from './Github';
4
+ export interface GithubActivitySource extends GithubSourceData {
5
+ authorEmail: string;
6
+ authorName: string;
7
+ branchName: string;
8
+ branch: string;
9
+ commitHash: string;
10
+ committersUsername: string;
11
+ ownerName: string;
12
+ owner: string;
13
+ repositoryName: string;
14
+ type: typeof ACTIVITY_SOURCE.GITHUB_USER;
15
+ }
16
+ export type HubspotActivitySource = {
17
+ userId?: number;
18
+ type: ValueOf<typeof ACTIVITY_SOURCE>;
19
+ };
20
+ export type ActivitySource = GithubActivitySource | HubspotActivitySource;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/types/Api.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { HTTP_METHOD_VERBS } from '../constants/api';
2
+ export type HttpMethod = keyof typeof HTTP_METHOD_VERBS;
package/types/Api.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });