@dma-sdk/hubspot 1.0.1 → 1.0.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.
@@ -0,0 +1,6 @@
1
+ export declare const apiMethods: {
2
+ getUserInfoByToken: string;
3
+ refreshToken: string;
4
+ createObjectProperties: (objectType: string) => string;
5
+ subscriptions: (appId: string) => string;
6
+ };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.apiMethods = void 0;
4
+ exports.apiMethods = {
5
+ getUserInfoByToken: '/oauth/v1/access-tokens',
6
+ refreshToken: '/oauth/v1/token',
7
+ createObjectProperties: (objectType) => '/crm/v3/properties/{objectType}/batch/create'.replace('{objectType}', objectType),
8
+ subscriptions: (appId) => '/webhooks/v3/{appId}/subscriptions'.replace('{appId}', appId),
9
+ };
@@ -0,0 +1,23 @@
1
+ export declare const hsDataTypes: {
2
+ string: string;
3
+ number: string;
4
+ boolean: string;
5
+ date: string;
6
+ datetime: string;
7
+ single_select: string;
8
+ multiple_select: string;
9
+ checkbox: string;
10
+ calculation: string;
11
+ phone_number: string;
12
+ user: string;
13
+ file: string;
14
+ score: string;
15
+ currency: string;
16
+ rich_text: string;
17
+ owner: string;
18
+ location: string;
19
+ json: string;
20
+ percentage: string;
21
+ ip_address: string;
22
+ video: string;
23
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hsDataTypes = void 0;
4
+ exports.hsDataTypes = {
5
+ string: "string",
6
+ number: "number",
7
+ boolean: "bool",
8
+ date: "date",
9
+ datetime: "datetime",
10
+ single_select: "enumeration",
11
+ multiple_select: "enumeration",
12
+ checkbox: "bool",
13
+ calculation: "calculation_equation",
14
+ phone_number: "phone_number",
15
+ user: "hubspot_user",
16
+ file: "file",
17
+ score: "score",
18
+ currency: "currency",
19
+ rich_text: "rich_text",
20
+ owner: "owner",
21
+ location: "location",
22
+ json: "json",
23
+ percentage: "number",
24
+ ip_address: "ip_address",
25
+ video: "video"
26
+ };
@@ -0,0 +1 @@
1
+ export declare const hsDataTypes: {};
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hsDataTypes = void 0;
4
+ exports.hsDataTypes = {};
@@ -7,6 +7,8 @@ import { TokenResponseIF } from '@hubspot/api-client/lib/codegen/oauth';
7
7
  import { TokenResponse } from './interfaces/token';
8
8
  import { CollectionResponsePropertyNoPaging, PropertyCreate, PropertyGroup, PropertyGroupCreate } from '@hubspot/api-client/lib/codegen/crm/properties';
9
9
  import { HubspotSignatureData } from './types/auth/hs-signature-data';
10
+ import { CreateSubscriptionInput } from './interfaces/input/subscriptions';
11
+ import { CreateSubscriptionOutput } from './interfaces/output/subscriptions';
10
12
  export declare class HubspotService {
11
13
  static HS_BASE_URL: string;
12
14
  static readonly hubspotObjectType: {
@@ -41,7 +43,6 @@ export declare class HubspotService {
41
43
  private hsClientId;
42
44
  private hsClientSecret;
43
45
  private typeConvert;
44
- private apiMethods;
45
46
  constructor(hsRedirectUriOauth: string, hsClientId: string, hsClientSecret: string);
46
47
  exchangeCodeForTokens(params: {
47
48
  code: string;
@@ -84,5 +85,9 @@ export declare class HubspotService {
84
85
  }, convertionMap: {
85
86
  [propertyName: string]: PropertyCreate;
86
87
  }): Record<string, any>;
88
+ createEventSubscription(properties: {
89
+ commandInput: CreateSubscriptionInput;
90
+ devApiKey: string;
91
+ }): Promise<CreateSubscriptionOutput>;
87
92
  private fetchApi;
88
93
  }
@@ -3,19 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HubspotService = void 0;
4
4
  const api_client_1 = require("@hubspot/api-client");
5
5
  const type_converter_1 = require("./utils/type-converter");
6
- const types_1 = require("./constants/types");
6
+ const data_types_1 = require("./constants/data-types");
7
7
  const api_client_2 = require("@hubspot/api-client");
8
8
  const fetch_1 = require("@dma-sdk/utils/src/fetch");
9
+ const api_path_1 = require("./constants/api-path");
9
10
  // Per ogni metodo che viene creato, è fondamentale che
10
- // il client venga inizializzato con il token di accesso
11
+ // il client venga inizializzato con il token di accesso o l'api key
11
12
  // altrimenti non funzionerà correttamente.
12
13
  class HubspotService {
13
14
  constructor(hsRedirectUriOauth, hsClientId, hsClientSecret) {
14
- this.apiMethods = {
15
- getUserInfoByToken: '/oauth/v1/access-tokens',
16
- refreshToken: '/oauth/v1/token',
17
- createObjectProperties: '/crm/v3/properties/{objectType}/batch/create',
18
- };
19
15
  ;
20
16
  (this.hsRedirectUriOauth = hsRedirectUriOauth),
21
17
  (this.hsClientId = hsClientId),
@@ -29,7 +25,7 @@ class HubspotService {
29
25
  async getUserInfoByToken(accessToken) {
30
26
  const response = await this.client.apiRequest({
31
27
  method: 'GET',
32
- path: `${this.apiMethods.getUserInfoByToken}/${accessToken}`,
28
+ path: `${api_path_1.apiMethods.getUserInfoByToken}/${accessToken}`,
33
29
  });
34
30
  const jsonBody = await response.json();
35
31
  const userAttributes = {
@@ -147,7 +143,7 @@ class HubspotService {
147
143
  inputs: properties
148
144
  })
149
145
  };
150
- const response = await this.fetchApi(this.apiMethods.createObjectProperties.replace('{objectType}', objectType), options);
146
+ const response = await this.fetchApi(api_path_1.apiMethods.createObjectProperties(objectType), options);
151
147
  return await response.json();
152
148
  }
153
149
  async createPropertyGroup(objectType, group, accessToken) {
@@ -169,6 +165,19 @@ class HubspotService {
169
165
  });
170
166
  return convertedProps;
171
167
  }
168
+ async createEventSubscription(properties) {
169
+ const { appId, event } = properties.commandInput;
170
+ const devApiKey = properties.devApiKey;
171
+ const options = {
172
+ method: 'POST',
173
+ headers: {
174
+ 'Content-Type': 'application/json'
175
+ },
176
+ body: JSON.stringify(event)
177
+ };
178
+ const response = await this.fetchApi(api_path_1.apiMethods.subscriptions(appId), options, { hapikey: devApiKey });
179
+ return await response.json();
180
+ }
172
181
  async fetchApi(endpoint, options, queryParams) {
173
182
  return await (0, fetch_1.fetchApi)(HubspotService.HS_BASE_URL, endpoint, options, queryParams);
174
183
  }
@@ -179,4 +188,4 @@ HubspotService.hubspotObjectType = {
179
188
  contacts: 'contacts',
180
189
  notes: 'notes',
181
190
  };
182
- HubspotService.hubspotPropertyTypes = types_1.hsDataTypes;
191
+ HubspotService.hubspotPropertyTypes = data_types_1.hsDataTypes;
@@ -0,0 +1,9 @@
1
+ export interface CreateSubscriptionInput {
2
+ appId: string;
3
+ event: {
4
+ objectType: string;
5
+ eventType: string;
6
+ propertyName?: string;
7
+ activate: boolean;
8
+ };
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ export interface CreateSubscriptionOutput {
2
+ active: boolean;
3
+ createdAt: number;
4
+ eventType: string;
5
+ id: string;
6
+ propertyName: string;
7
+ updatedAt: string;
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.typeConverter = void 0;
4
- const types_1 = require("../constants/types");
4
+ const data_types_1 = require("../constants/data-types");
5
5
  exports.typeConverter = {
6
- [types_1.hsDataTypes.date]: (data) => {
6
+ [data_types_1.hsDataTypes.date]: (data) => {
7
7
  return (new Date(data)).setUTCHours(0, 0, 0, 0);
8
8
  },
9
- [types_1.hsDataTypes.datetime]: (data) => {
9
+ [data_types_1.hsDataTypes.datetime]: (data) => {
10
10
  return new Date(data).getTime();
11
11
  },
12
- [types_1.hsDataTypes.number]: (data) => {
12
+ [data_types_1.hsDataTypes.number]: (data) => {
13
13
  return (typeof data === "number") ? data : parseFloat(data);
14
14
  }
15
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dma-sdk/hubspot",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "DMA SDK – Hubspot service",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,4 +16,4 @@
16
16
  "devDependencies": {
17
17
  "typescript": "^5.2.0"
18
18
  }
19
- }
19
+ }
@@ -0,0 +1,6 @@
1
+ export const apiMethods = {
2
+ getUserInfoByToken: '/oauth/v1/access-tokens',
3
+ refreshToken: '/oauth/v1/token',
4
+ createObjectProperties: (objectType: string) => '/crm/v3/properties/{objectType}/batch/create'.replace('{objectType}', objectType),
5
+ subscriptions: (appId: string) => '/webhooks/v3/{appId}/subscriptions'.replace('{appId}', appId),
6
+ }
@@ -0,0 +1,23 @@
1
+ export const hsDataTypes = {
2
+ string: "string",
3
+ number: "number",
4
+ boolean: "bool",
5
+ date: "date",
6
+ datetime: "datetime",
7
+ single_select: "enumeration",
8
+ multiple_select: "enumeration",
9
+ checkbox: "bool",
10
+ calculation: "calculation_equation",
11
+ phone_number: "phone_number",
12
+ user: "hubspot_user",
13
+ file: "file",
14
+ score: "score",
15
+ currency: "currency",
16
+ rich_text: "rich_text",
17
+ owner: "owner",
18
+ location: "location",
19
+ json: "json",
20
+ percentage: "number",
21
+ ip_address: "ip_address",
22
+ video: "video"
23
+ };
@@ -0,0 +1 @@
1
+ export const hsDataTypes = {}
@@ -7,15 +7,18 @@ import { LabelsBetweenObjectPair } from '@hubspot/api-client/lib/codegen/crm/ass
7
7
  import { TokenResponseIF } from '@hubspot/api-client/lib/codegen/oauth'
8
8
  import { typeConverter } from './utils/type-converter'
9
9
  import { HubspotTypeConverter } from './interfaces/hubspot-type-converter'
10
- import { hsDataTypes } from './constants/types'
10
+ import { hsDataTypes } from './constants/data-types'
11
11
  import { Signature } from '@hubspot/api-client'
12
12
  import { TokenResponse } from './interfaces/token'
13
13
  import { CollectionResponsePropertyNoPaging, PropertyCreate, PropertyGroup, PropertyGroupCreate } from '@hubspot/api-client/lib/codegen/crm/properties'
14
14
  import { HubspotSignatureData } from './types/auth/hs-signature-data'
15
15
  import { fetchApi } from '@dma-sdk/utils/src/fetch'
16
+ import { apiMethods } from './constants/api-path'
17
+ import { CreateSubscriptionInput } from './interfaces/input/subscriptions'
18
+ import { CreateSubscriptionOutput } from './interfaces/output/subscriptions'
16
19
 
17
20
  // Per ogni metodo che viene creato, è fondamentale che
18
- // il client venga inizializzato con il token di accesso
21
+ // il client venga inizializzato con il token di accesso o l'api key
19
22
  // altrimenti non funzionerà correttamente.
20
23
 
21
24
  export class HubspotService {
@@ -32,12 +35,6 @@ export class HubspotService {
32
35
  private hsClientSecret: string
33
36
  private typeConvert: HubspotTypeConverter
34
37
 
35
- private apiMethods = {
36
- getUserInfoByToken: '/oauth/v1/access-tokens',
37
- refreshToken: '/oauth/v1/token',
38
- createObjectProperties: '/crm/v3/properties/{objectType}/batch/create',
39
- }
40
-
41
38
 
42
39
  constructor(hsRedirectUriOauth: string, hsClientId: string, hsClientSecret: string) {
43
40
  ;(this.hsRedirectUriOauth = hsRedirectUriOauth),
@@ -61,7 +58,7 @@ export class HubspotService {
61
58
  async getUserInfoByToken(accessToken: string): Promise<HubspotUser> {
62
59
  const response = await this.client.apiRequest({
63
60
  method: 'GET',
64
- path: `${this.apiMethods.getUserInfoByToken}/${accessToken}`,
61
+ path: `${apiMethods.getUserInfoByToken}/${accessToken}`,
65
62
  })
66
63
 
67
64
  const jsonBody = await response.json()
@@ -272,7 +269,7 @@ export class HubspotService {
272
269
  })
273
270
  };
274
271
 
275
- const response = await this.fetchApi(this.apiMethods.createObjectProperties.replace('{objectType}', objectType), options)
272
+ const response = await this.fetchApi(apiMethods.createObjectProperties(objectType), options)
276
273
  return await response.json()
277
274
  }
278
275
 
@@ -300,6 +297,30 @@ export class HubspotService {
300
297
 
301
298
  return convertedProps
302
299
  }
300
+
301
+ public async createEventSubscription(properties: {
302
+ commandInput: CreateSubscriptionInput,
303
+ devApiKey: string,
304
+ }): Promise<CreateSubscriptionOutput> {
305
+ const { appId, event } = properties.commandInput
306
+ const devApiKey = properties.devApiKey
307
+
308
+ const options = {
309
+ method: 'POST',
310
+ headers: {
311
+ 'Content-Type': 'application/json'
312
+ },
313
+ body: JSON.stringify(event)
314
+ };
315
+
316
+ const response = await this.fetchApi(
317
+ apiMethods.subscriptions(appId),
318
+ options,
319
+ { hapikey: devApiKey }
320
+ )
321
+
322
+ return await response.json()
323
+ }
303
324
 
304
325
  private async fetchApi(endpoint: string, options: {}, queryParams?: {}): Promise<any> {
305
326
  return await fetchApi(HubspotService.HS_BASE_URL, endpoint, options, queryParams)
@@ -0,0 +1,9 @@
1
+ export interface CreateSubscriptionInput {
2
+ appId: string,
3
+ event: {
4
+ objectType: string,
5
+ eventType: string,
6
+ propertyName?: string,
7
+ activate: boolean
8
+ }
9
+ }
@@ -0,0 +1,8 @@
1
+ export interface CreateSubscriptionOutput {
2
+ active: boolean,
3
+ createdAt: number,
4
+ eventType: string,
5
+ id: string,
6
+ propertyName: string,
7
+ updatedAt: string
8
+ }
@@ -1,4 +1,4 @@
1
- import { hsDataTypes } from "../constants/types";
1
+ import { hsDataTypes } from "../constants/data-types";
2
2
  import { HubspotTypeConverter } from "../interfaces/hubspot-type-converter";
3
3
  import { string } from "zod";
4
4
 
@@ -1,23 +0,0 @@
1
- export const hsDataTypes = {
2
- string: "string",
3
- number: "number",
4
- boolean: "bool",
5
- date: "date",
6
- datetime: "datetime",
7
- single_select: "enumeration",
8
- multiple_select: "enumeration",
9
- checkbox: "bool",
10
- calculation: "calculation_equation",
11
- phone_number: "phone_number",
12
- user: "hubspot_user",
13
- file: "file",
14
- score: "score",
15
- currency: "currency",
16
- rich_text: "rich_text",
17
- owner: "owner",
18
- location: "location",
19
- json: "json",
20
- percentage: "number",
21
- ip_address: "ip_address",
22
- video: "video"
23
- };