The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor HubSpot integration by consolidating constants and updating token handling
Kiran K2e9cdc226b26
diff --git a/apps/web/app/(ee)/api/hubspot/webhook/route.ts b/apps/web/app/(ee)/api/hubspot/webhook/route.tsindex 1b2888eb86..24b497c649 100644--- a/apps/web/app/(ee)/api/hubspot/webhook/route.ts+++ b/apps/web/app/(ee)/api/hubspot/webhook/route.ts@@ -1,5 +1,4 @@import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";-import { HUBSPOT_CLIENT_SECRET } from "@/lib/integrations/hubspot/constants";import { hubSpotOAuthProvider } from "@/lib/integrations/hubspot/oauth";import {hubSpotSettingsSchema,@@ -12,6 +11,8 @@ import crypto from "crypto";import { AxiomRequest, withAxiom } from "next-axiom";import { NextResponse } from "next/server";+const HUBSPOT_CLIENT_SECRET = process.env.HUBSPOT_CLIENT_SECRET || "";+// POST /api/hubspot/webhook – listen to webhook events from Hubspotexport const POST = withAxiom(async (req: AxiomRequest) => {try {@@ -91,7 +92,8 @@ async function processWebhookEvent(event: any) {const workspace = installation.project;// Refresh the access token if needed- const authToken = await hubSpotOAuthProvider.refreshTokenForInstallation(installation);+ const authToken =+ await hubSpotOAuthProvider.refreshTokenForInstallation(installation);if (!authToken) {console.error(diff --git a/apps/web/lib/integrations/hubspot/api.ts b/apps/web/lib/integrations/hubspot/api.tsindex eafffe7e50..b1329318dc 100644--- a/apps/web/lib/integrations/hubspot/api.ts+++ b/apps/web/lib/integrations/hubspot/api.ts@@ -1,4 +1,3 @@-import { HUBSPOT_API_HOST } from "./constants";import { hubSpotContactSchema, hubSpotDealSchema } from "./schema";type FetchOptions = Omit<RequestInit, "body"> & {@@ -6,7 +5,7 @@ type FetchOptions = Omit<RequestInit, "body"> & {};export class HubSpotApi {- private readonly baseUrl = `${HUBSPOT_API_HOST}/crm/v3`;+ private readonly baseUrl = "https://api.hubapi.com/crm/v3";private readonly token: string;constructor({ token }: { token: string }) {diff --git a/apps/web/lib/integrations/hubspot/constants.ts b/apps/web/lib/integrations/hubspot/constants.tsindex 44a2fc20b2..09aa2728f4 100644--- a/apps/web/lib/integrations/hubspot/constants.ts+++ b/apps/web/lib/integrations/hubspot/constants.ts@@ -1,9 +1,3 @@-export const HUBSPOT_CLIENT_ID = process.env.HUBSPOT_CLIENT_ID || "";--export const HUBSPOT_CLIENT_SECRET = process.env.HUBSPOT_CLIENT_SECRET || "";--export const HUBSPOT_API_HOST = "https://api.hubapi.com";-export const HUBSPOT_OBJECT_TYPE_IDS = ["0-1", // contact"0-3", // dealdiff --git a/apps/web/lib/integrations/hubspot/oauth.ts b/apps/web/lib/integrations/hubspot/oauth.tsindex 4b06542b36..145b1c0572 100644--- a/apps/web/lib/integrations/hubspot/oauth.ts+++ b/apps/web/lib/integrations/hubspot/oauth.ts@@ -1,20 +1,11 @@import { prisma } from "@dub/prisma";import { InstalledIntegration } from "@dub/prisma/client";import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";-import { z } from "zod";import { OAuthProvider, OAuthProviderConfig } from "../oauth-provider";+import { hubSpotAuthTokenSchema } from "./schema";import { HubSpotAuthToken } from "./types";-const hubSpotTokenSchema = z.object({- access_token: z.string(),- refresh_token: z.string(),- scopes: z.array(z.string()),- hub_id: z.number(),- expires_in: z.number().describe("Expires in seconds."),- created_at: z.number().optional(),-});--class HubSpotOAuthProvider extends OAuthProvider<typeof hubSpotTokenSchema> {+class HubSpotOAuthProvider extends OAuthProvider<typeof hubSpotAuthTokenSchema> {constructor(config: OAuthProviderConfig) {super(config);}@@ -22,7 +13,7 @@ class HubSpotOAuthProvider extends OAuthProvider<typeof hubSpotTokenSchema> {async refreshTokenForInstallation(installation: InstalledIntegration,): Promise<HubSpotAuthToken> {- const token = hubSpotTokenSchema.parse(installation.credentials);+ const token = hubSpotAuthTokenSchema.parse(installation.credentials);if (this.isTokenValid(token)) {return token;@@ -74,5 +65,5 @@ export const hubSpotOAuthProvider = new HubSpotOAuthProvider({"crm.objects.deals.read","crm.schemas.contacts.write",],- tokenSchema: hubSpotTokenSchema,+ tokenSchema: hubSpotAuthTokenSchema,});diff --git a/apps/web/lib/integrations/hubspot/schema.ts b/apps/web/lib/integrations/hubspot/schema.tsindex 41a2980f8e..372c2d12a7 100644--- a/apps/web/lib/integrations/hubspot/schema.ts+++ b/apps/web/lib/integrations/hubspot/schema.ts@@ -19,14 +19,6 @@ export const hubSpotSettingsSchema = z.object({.describe("The ID of the deal stage that represents a closed won deal."),});-export const hubSpotRefreshTokenSchema = z.object({- access_token: z.string(),- refresh_token: z.string(),- token_type: z.string(),- expires_in: z.number(),- id_token: z.string().nullable(),-});-// CRMexport const hubSpotContactSchema = z.object({id: z.string(),diff --git a/apps/web/lib/integrations/hubspot/types.ts b/apps/web/lib/integrations/hubspot/types.tsindex e2386a41a8..7a362044e7 100644--- a/apps/web/lib/integrations/hubspot/types.ts+++ b/apps/web/lib/integrations/hubspot/types.ts@@ -1,12 +1,6 @@import { z } from "zod";-import {- hubSpotAuthTokenSchema,- hubSpotContactSchema,- hubSpotRefreshTokenSchema,-} from "./schema";+import { hubSpotAuthTokenSchema, hubSpotContactSchema } from "./schema";export type HubSpotAuthToken = z.infer<typeof hubSpotAuthTokenSchema>;-export type HubSpotRefreshToken = z.infer<typeof hubSpotRefreshTokenSchema>;-export type HubSpotContact = z.infer<typeof hubSpotContactSchema>;