Skip to content

Commit b373891

Browse files
committed
updating setAccessToken to authenticateHubspotClient
1 parent 06f80bd commit b373891

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/auth.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,30 @@ const getAccessToken = async (customerId: string): Promise => {
195195
);
196196
}
197197
};
198-
async function setAccessToken(): Promise<Client> {
198+
199+
function applyHubSpotAccessToken(accessToken: string): Client {
199200
try {
200-
const accessToken = await getAccessToken(getCustomerId());
201-
if (!accessToken) {
202-
throw new Error('No access token returned');
203-
}
204201
hubspotClient.setAccessToken(accessToken);
205202
return hubspotClient;
206203
} catch (error) {
207-
handleError(error, 'Error setting access token');
208-
throw new Error('Failed to authenticate HubSpot client');
204+
handleError(error, 'Error setting HubSpot access token');
205+
throw new Error(`Failed to apply access token: ${error instanceof Error ? error.message : 'Unknown error'}`);
206+
}
207+
}
208+
209+
async function authenticateHubspotClient(): Promise<Client> {
210+
try {
211+
const customerId = getCustomerId();
212+
const accessToken = await getAccessToken(customerId);
213+
if (!accessToken) {
214+
throw new Error(`No access token returned for customer ID: ${customerId}`);
215+
}
216+
return applyHubSpotAccessToken(accessToken);
217+
} catch (error) {
218+
handleError(error, 'Error retrieving HubSpot access token');
219+
throw error instanceof Error
220+
? new Error(`Failed to authenticate HubSpot client: ${error.message}`)
221+
: new Error('Failed to authenticate HubSpot client due to an unknown error');
209222
}
210223
}
211224

@@ -216,5 +229,5 @@ export {
216229
getAccessToken,
217230
prisma,
218231
hubspotClient,
219-
setAccessToken,
232+
authenticateHubspotClient,
220233
};

src/properties.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const createPropertyGroupForContacts = async (accessToken: string) => {
7676
type: "HubSpot",
7777
logMessage: { message: "Creating contact property group..." },
7878
});
79-
await setAccessToken();
79+
await authenticateHubspotClient();
8080
try {
8181
const propertyGroupCreateResponse =
8282
await hubspotClient.crm.properties.groupsApi.create("contact", {
@@ -98,7 +98,7 @@ export const createPropertyGroupForCompanies = async (accessToken: string) => {
9898
type: "HubSpot",
9999
logMessage: { message: "Creating company property group..." },
100100
});
101-
await setAccessToken();
101+
await authenticateHubspotClient();
102102
try {
103103
const propertyGroupCreateResponse =
104104
await hubspotClient.crm.properties.groupsApi.create("company", {
@@ -123,7 +123,7 @@ export const createRequiredContactProperty = async (accessToken: string) => {
123123
type: "HubSpot",
124124
logMessage: { message: "Creating required contact property..." },
125125
});
126-
await setAccessToken();
126+
await authenticateHubspotClient();
127127
try {
128128
const propertyCreateResponse =
129129
await hubspotClient.crm.properties.coreApi.create("contact", {
@@ -151,7 +151,7 @@ export const createContactIdProperty = async (accessToken: string) => {
151151
type: "HubSpot",
152152
logMessage: { message: "Creating custom contact ID property..." },
153153
});
154-
await setAccessToken();
154+
await authenticateHubspotClient();
155155
try {
156156
const propertyCreateResponse =
157157
await hubspotClient.crm.properties.coreApi.create("contact", {
@@ -181,7 +181,7 @@ export const createCompanyIdProperty = async (accessToken: string) => {
181181
type: "HubSpot",
182182
logMessage: { message: "Creating custom company ID property..." },
183183
});
184-
await setAccessToken();
184+
await authenticateHubspotClient();
185185
try {
186186
const propertyCreateResponse =
187187
await hubspotClient.crm.properties.coreApi.create("company", {
@@ -258,7 +258,7 @@ export const getHubSpotProperties = async (
258258
): Promise<{ contactProperties: any; companyProperties: any } | undefined> => {
259259
// const propertiesCacheIsValid = await checkPropertiesCache(customerId);
260260

261-
await setAccessToken();
261+
await authenticateHubspotClient();
262262

263263
// add DB call to check if we've looked in the last 5 minutes
264264
// https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#updatedat
@@ -347,7 +347,7 @@ export const checkForPropertyOrGroup = async (
347347
type: "HubSpot",
348348
logMessage: { message: `Checking for ${objectType} ${isGroupString} ${propertyName}` }
349349
});
350-
await setAccessToken();
350+
await authenticateHubspotClient();
351351
try{
352352
if ( propertyOrGroup == 'property' ){
353353
getPropertyResponse = await hubspotClient.crm.properties.coreApi.getByNameWithHttpInfo(objectType, propertyName);

0 commit comments

Comments
 (0)