Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: oauth for onshape integration

Brad Barbin8841c0ba0a61
diff --git a/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.e.$eid.bom.ts b/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.e.$eid.bom.ts
index 855425c870..75f8c30370 100644
--- a/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.e.$eid.bom.ts
+++ b/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.e.$eid.bom.ts
@@ -1,11 +1,9 @@
import { requirePermissions } from "@carbon/auth/auth.server";
-import { Onshape as OnshapeConfig } from "@carbon/ee";
-import { OnshapeClient } from "@carbon/ee/onshape";
+import { getOnshapeClient } from "@carbon/ee/onshape";
import type {
LoaderFunctionArgs,
ShouldRevalidateFunction
} from "react-router";
-import { getIntegration } from "~/modules/settings/settings.service";
import { getReadableIdWithRevision } from "~/utils/string";
export const shouldRevalidate: ShouldRevalidateFunction = () => {
@@ -13,7 +11,7 @@ export const shouldRevalidate: ShouldRevalidateFunction = () => {
};
export async function loader({ request, params }: LoaderFunctionArgs) {
- const { client, companyId } = await requirePermissions(request, {});
+ const { client, companyId, userId } = await requirePermissions(request, {});
const { did } = params;
if (!did) {
@@ -39,31 +37,16 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
};
}
- const integration = await getIntegration(client, "onshape", companyId);
+ const result = await getOnshapeClient(client, companyId, userId);
- if (integration.error || !integration.data) {
+ if (result.error) {
return {
data: [],
- error: integration.error
+ error: result.error
};
}
- const integrationMetadata = OnshapeConfig.schema.safeParse(
- integration?.data?.metadata
- );
-
- if (!integrationMetadata.success) {
- return {
- data: [],
- error: integrationMetadata.error
- };
- }
-
- const onshapeClient = new OnshapeClient({
- baseUrl: integrationMetadata.data.baseUrl,
- accessKey: integrationMetadata.data.accessKey,
- secretKey: integrationMetadata.data.secretKey
- });
+ const onshapeClient = result.client;
try {
const response = await onshapeClient.getBillOfMaterials(did, vid, eid);
diff --git a/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.elements.ts b/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.elements.ts
index b9d66d250d..2d67692eff 100644
--- a/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.elements.ts
+++ b/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.elements.ts
@@ -1,18 +1,16 @@
import { requirePermissions } from "@carbon/auth/auth.server";
-import { Onshape as OnshapeConfig } from "@carbon/ee";
-import { OnshapeClient } from "@carbon/ee/onshape";
+import { getOnshapeClient } from "@carbon/ee/onshape";
import type {
LoaderFunctionArgs,
ShouldRevalidateFunction
} from "react-router";
-import { getIntegration } from "~/modules/settings/settings.service";
export const shouldRevalidate: ShouldRevalidateFunction = () => {
return false;
};
export async function loader({ request, params }: LoaderFunctionArgs) {
- const { client, companyId } = await requirePermissions(request, {});
+ const { client, companyId, userId } = await requirePermissions(request, {});
const { did } = params;
if (!did) {
@@ -30,31 +28,16 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
};
}
- const integration = await getIntegration(client, "onshape", companyId);
+ const result = await getOnshapeClient(client, companyId, userId);
- if (integration.error || !integration.data) {
+ if (result.error) {
return {
data: [],
- error: integration.error
+ error: result.error
};
}
- const integrationMetadata = OnshapeConfig.schema.safeParse(
- integration?.data?.metadata
- );
-
- if (!integrationMetadata.success) {
- return {
- data: [],
- error: integrationMetadata.error
- };
- }
-
- const onshapeClient = new OnshapeClient({
- baseUrl: integrationMetadata.data.baseUrl,
- accessKey: integrationMetadata.data.accessKey,
- secretKey: integrationMetadata.data.secretKey
- });
+ const onshapeClient = result.client;
try {
let limit = 20;
diff --git a/apps/erp/app/routes/api+/integrations.onshape.d.$did.versions.ts b/apps/erp/app/routes/api+/integrations.onshape.d.$did.versions.ts
index 87afa7ed05..6ee7938139 100644
--- a/apps/erp/app/routes/api+/integrations.onshape.d.$did.versions.ts
+++ b/apps/erp/app/routes/api+/integrations.onshape.d.$did.versions.ts
@@ -1,18 +1,16 @@
import { requirePermissions } from "@carbon/auth/auth.server";
-import { Onshape as OnshapeConfig } from "@carbon/ee";
-import { OnshapeClient } from "@carbon/ee/onshape";
+import { getOnshapeClient } from "@carbon/ee/onshape";
import type {
LoaderFunctionArgs,
ShouldRevalidateFunction
} from "react-router";
-import { getIntegration } from "~/modules/settings/settings.service";
export const shouldRevalidate: ShouldRevalidateFunction = () => {
return false;
};
export async function loader({ request, params }: LoaderFunctionArgs) {
- const { client, companyId } = await requirePermissions(request, {});
+ const { client, companyId, userId } = await requirePermissions(request, {});
const { did } = params;
if (!did) {
@@ -22,31 +20,16 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
};
}
- const integration = await getIntegration(client, "onshape", companyId);
+ const result = await getOnshapeClient(client, companyId, userId);
- if (integration.error || !integration.data) {
+ if (result.error) {
return {
data: [],
- error: integration.error
+ error: result.error
};
}
- const integrationMetadata = OnshapeConfig.schema.safeParse(
- integration?.data?.metadata
- );
-
- if (!integrationMetadata.success) {
- return {
- data: [],
- error: integrationMetadata.error
- };
- }
-
- const onshapeClient = new OnshapeClient({
- baseUrl: integrationMetadata.data.baseUrl,
- accessKey: integrationMetadata.data.accessKey,
- secretKey: integrationMetadata.data.secretKey
- });
+ const onshapeClient = result.client;
try {
let limit = 20;
diff --git a/apps/erp/app/routes/api+/integrations.onshape.documents.ts b/apps/erp/app/routes/api+/integrations.onshape.documents.ts
index a0f9296c68..8176513654 100644
--- a/apps/erp/app/routes/api+/integrations.onshape.documents.ts
+++ b/apps/erp/app/routes/api+/integrations.onshape.documents.ts
@@ -1,44 +1,27 @@
import { requirePermissions } from "@carbon/auth/auth.server";
-import { Onshape as OnshapeConfig } from "@carbon/ee";
-import { OnshapeClient } from "@carbon/ee/onshape";
+import { getOnshapeClient } from "@carbon/ee/onshape";
import type {
LoaderFunctionArgs,
ShouldRevalidateFunction
} from "react-router";
-import { getIntegration } from "~/modules/settings/settings.service";
export const shouldRevalidate: ShouldRevalidateFunction = () => {
return false;
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client, companyId } = await requirePermissions(request, {});
+ const { client, companyId, userId } = await requirePermissions(request, {});
- const integration = await getIntegration(client, "onshape", companyId);
+ const result = await getOnshapeClient(client, companyId, userId);
- if (integration.error || !integration.data) {
+ if (result.error) {
return {
data: [],
- error: integration.error
+ error: result.error
};
}
- const integrationMetadata = OnshapeConfig.schema.safeParse(
- integration?.data?.metadata
- );
-
- if (!integrationMetadata.success) {
- return {
- data: [],
- error: integrationMetadata.error
- };
- }
-
- const onshapeClient = new OnshapeClient({
- baseUrl: integrationMetadata.data.baseUrl,
- accessKey: integrationMetadata.data.accessKey,
- secretKey: integrationMetadata.data.secretKey
- });
+ const onshapeClient = result.client;
try {
let limit = 20;
diff --git a/apps/erp/app/routes/api+/integrations.onshape.install.ts b/apps/erp/app/routes/api+/integrations.onshape.install.ts
new file mode 100644
index 0000000000..9e4a022022
--- /dev/null
+++ b/apps/erp/app/routes/api+/integrations.onshape.install.ts
@@ -0,0 +1,26 @@
+import { ONSHAPE_CLIENT_ID, ONSHAPE_OAUTH_REDIRECT_URL } from "@carbon/auth";
+import { requirePermissions } from "@carbon/auth/auth.server";
+import type { LoaderFunctionArgs } from "react-router";
+import { data } from "react-router";
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ await requirePermissions(request, {});
+
+ if (!ONSHAPE_CLIENT_ID || !ONSHAPE_OAUTH_REDIRECT_URL) {
+ return data({ error: "Onshape OAuth not configured" }, { status: 500 });
+ }
+
+ const state = crypto.randomUUID();
+
+ const params = new URLSearchParams({
+ client_id: ONSHAPE_CLIENT_ID,
+ redirect_uri: ONSHAPE_OAUTH_REDIRECT_URL,
+ response_type: "code",
+ scope: "OAuth2Read",
+ state
+ });
+
+ const url = `https://oauth.onshape.com/oauth/authorize?${params}`;
+
+ return { url };
+}
diff --git a/apps/erp/app/routes/api+/integrations.onshape.oauth.ts b/apps/erp/app/routes/api+/integrations.onshape.oauth.ts
new file mode 100644
index 0000000000..11d31625cd
--- /dev/null
+++ b/apps/erp/app/routes/api+/integrations.onshape.oauth.ts
@@ -0,0 +1,122 @@
+import {
+ ONSHAPE_CLIENT_ID,
+ ONSHAPE_CLIENT_SECRET,
+ ONSHAPE_OAUTH_REDIRECT_URL,
+ VERCEL_URL
+} from "@carbon/auth";
+import { requirePermissions } from "@carbon/auth/auth.server";
+import { Onshape } from "@carbon/ee";
+import type { LoaderFunctionArgs } from "react-router";
+import { data, redirect } from "react-router";
+import { upsertCompanyIntegration } from "~/modules/settings/settings.server";
+import { oAuthCallbackSchema } from "~/modules/shared";
+import { path } from "~/utils/path";
+
+export const config = {
+ runtime: "nodejs"
+};
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const { client, userId, companyId } = await requirePermissions(request, {
+ update: "settings"
+ });
+
+ const url = new URL(request.url);
+ const searchParams = Object.fromEntries(url.searchParams.entries());
+
+ const authResponse = oAuthCallbackSchema.safeParse(searchParams);
+
+ if (!authResponse.success) {
+ return data({ error: "Invalid Onshape auth response" }, { status: 400 });
+ }
+
+ const { data: params } = authResponse;
+
+ if (!params.state) {
+ return data({ error: "Invalid state parameter" }, { status: 400 });
+ }
+
+ if (
+ !ONSHAPE_CLIENT_ID ||
+ !ONSHAPE_CLIENT_SECRET ||
+ !ONSHAPE_OAUTH_REDIRECT_URL
+ ) {
+ return data({ error: "Onshape OAuth not configured" }, { status: 500 });
+ }
+
+ try {
+ const tokenResponse = await fetch("https://oauth.onshape.com/oauth/token", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/x-www-form-urlencoded"
+ },
+ body: new URLSearchParams({
+ grant_type: "authorization_code",
+ code: params.code,
+ client_id: ONSHAPE_CLIENT_ID,
+ client_secret: ONSHAPE_CLIENT_SECRET,
+ redirect_uri: ONSHAPE_OAUTH_REDIRECT_URL
+ })
+ });
+
+ if (!tokenResponse.ok) {
+ console.error(
+ "Onshape token exchange failed:",
+ tokenResponse.status,
+ await tokenResponse.text()
+ );
+ return data(
+ { error: "Failed to exchange code for token" },
+ { status: 500 }
+ );
+ }
+
+ const tokenData = await tokenResponse.json();
+
+ if (!tokenData.access_token) {
+ return data(
+ { error: "No access token in Onshape response" },
+ { status: 500 }
+ );
+ }
+
+ const createdIntegration = await upsertCompanyIntegration(client, {
+ id: Onshape.id,
+ active: true,
+ metadata: {
+ credentials: {
+ type: "oauth2",
+ accessToken: tokenData.access_token,
+ refreshToken: tokenData.refresh_token,
+ expiresAt: new Date(Date.now() + 3600 * 1000).toISOString()
+ },
+ baseUrl: "https://cad.onshape.com"
+ },
+ updatedBy: userId,
+ companyId: companyId
+ });
+
+ if (createdIntegration?.data?.metadata) {
+ const requestUrl = new URL(request.url);
+
+ if (!VERCEL_URL || VERCEL_URL.includes("localhost")) {
+ requestUrl.protocol = "http";
+ }
+
+ const redirectUrl = `${requestUrl.origin}${path.to.integrations}`;
+
+ return redirect(redirectUrl);
+ } else {
+ return data(
+ { error: "Failed to save Onshape integration" },
+ { status: 500 }
+ );
+ }
+ } catch (err) {
+ console.error("Onshape OAuth Error:", err);
+ return data(
+ { error: "Failed to exchange code for token" },
+ { status: 500 }
+ );
+ }
+}
diff --git a/packages/auth/src/config/env.ts b/packages/auth/src/config/env.ts
index 07a39933ac..7ac3734681 100644
--- a/packages/auth/src/config/env.ts
+++ b/packages/auth/src/config/env.ts
@@ -31,6 +31,9 @@ declare global {
ERP_URL: string;
MES_URL: string;
NOVU_SECRET_KEY: string;
+ ONSHAPE_CLIENT_ID: string;
+ ONSHAPE_CLIENT_SECRET: string;
+ ONSHAPE_OAUTH_REDIRECT_URL: string;
POSTHOG_API_HOST: string;
POSTHOG_PROJECT_PUBLIC_KEY: string;
QUICKBOOKS_CLIENT_SECRET: string;
@@ -159,6 +162,17 @@ export const NOVU_SECRET_KEY = getEnv("NOVU_SECRET_KEY", {
isSecret: true
});
+export const ONSHAPE_CLIENT_ID = getEnv("ONSHAPE_CLIENT_ID", {
+ isRequired: false
+});
+export const ONSHAPE_CLIENT_SECRET = getEnv("ONSHAPE_CLIENT_SECRET", {
+ isRequired: false,
+ isSecret: true
+});
+export const ONSHAPE_OAUTH_REDIRECT_URL = getEnv("ONSHAPE_OAUTH_REDIRECT_URL", {
+ isRequired: false
+});
+
export const QUICKBOOKS_CLIENT_ID = getEnv("QUICKBOOKS_CLIENT_ID", {
isRequired: false
});
diff --git a/packages/ee/src/onshape/config.tsx b/packages/ee/src/onshape/config.tsx
index a40da5af4e..467afb3895 100644
--- a/packages/ee/src/onshape/config.tsx
+++ b/packages/ee/src/onshape/config.tsx
@@ -1,4 +1,4 @@
-import { Badge } from "@carbon/react";
+import { ONSHAPE_CLIENT_ID } from "@carbon/auth";
import type { SVGProps } from "react";
import { z } from "zod";
import { defineIntegration } from "../fns";
@@ -6,81 +6,49 @@ import { defineIntegration } from "../fns";
export const Onshape = defineIntegration({
name: "Onshape",
id: "onshape",
- active: true,
+ active: !!ONSHAPE_CLIENT_ID,
category: "CAD",
logo: Logo,
description:
"Onshape is a CAD software that allows you to create 2D and 3D designs. This integration will sync data from Onshape to Carbon.",
shortDescription: "Sync data from Onshape to Carbon.",
- setupInstructions: SetupInstructions,
images: [],
+ settings: [],
+ schema: z.object({}),
+ onClientInstall: async () => {
+ const response = await fetch("/api/integrations/onshape/install").then(
+ (res) => res.json()
+ );
- settings: [
- {
- name: "baseUrl",
- label: "Base URL",
- type: "text",
- required: true,
- value: "https://cad.onshape.com"
- },
- {
- name: "accessKey",
- label: "Access Key/Username",
- type: "text",
- required: true,
- value: ""
- },
- {
- name: "secretKey",
- label: "Secret Key/Password",
- type: "text",
- required: true,
- value: ""
- },
- {
- name: "companyId",
- label: "Company ID",
- type: "text",
- required: true,
- value: ""
+ const { url } = response;
+
+ const width = 600;
+ const height = 800;
+ const left = window.screenX + (window.outerWidth - width) / 2;
+ const top = window.screenY + (window.outerHeight - height) / 2.5;
+
+ const popup = window.open(
+ url,
+ "",
+ `toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${width}, height=${height}, top=${top}, left=${left}`
+ );
+
+ if (!popup) {
+ window.location.href = url;
+ return;
}
- ],
- schema: z.object({
- baseUrl: z
- .string()
- .min(1, { message: "Base URL is required" })
- .regex(/^https:\/\/.*onshape\.com$/, {
- message: "URL must start with https:// and end with onshape.com"
- }),
- accessKey: z.string().min(1, { message: "Access Key is required" }),
- secretKey: z.string().min(1, { message: "Secret Key is required" }),
- companyId: z.string().min(1, { message: "Company ID is required" })
- })
-});
+ const listener = (e: MessageEvent) => {
+ if (e.data === "app_oauth_completed") {
+ window.location.reload();
+ window.removeEventListener("message", listener);
+ popup.close();
+ }
+ };
-function SetupInstructions({ companyId }: { companyId: string }) {
- return (
- <>
- <p className="text-sm text-muted-foreground">
- First, get the base URL of your Onshape account. This is either{" "}
- <Badge className="font-mono lowercase" variant="secondary">
- https://cad.onshape.com
- </Badge>{" "}
- or something like{" "}
- <Badge className="font-mono lowercase" variant="secondary">
- https://your-company.onshape.com/
- </Badge>
- .
- </p>
- <p className="text-sm text-muted-foreground">
- Next, copy the API Key and Secret Key from your Onshape account and
- paste them into the API Key and Secret Key fields below. Finally, enter
- the Company ID of your Onshape account.
- </p>
- </>
- );
-}
+ window.addEventListener("message", listener);
+ }
+});
export function Logo(props: SVGProps<SVGSVGElement>) {
return (
diff --git a/packages/ee/src/onshape/lib/client.ts b/packages/ee/src/onshape/lib/client.ts
index c33243716f..4e20ec0b4a 100644
--- a/packages/ee/src/onshape/lib/client.ts
+++ b/packages/ee/src/onshape/lib/client.ts
@@ -1,9 +1,11 @@
+import { ONSHAPE_CLIENT_ID, ONSHAPE_CLIENT_SECRET } from "@carbon/auth";
+import type { Database } from "@carbon/database";
+import type { SupabaseClient } from "@supabase/supabase-js";
import axios from "axios";
interface OnshapeClientConfig {
baseUrl: string;
- accessKey: string;
- secretKey: string;
+ accessToken: string;
}
export interface OnshapePart {
@@ -17,14 +19,12 @@ export interface OnshapePart {
export class OnshapeClient {
private baseUrl: string;
- private accessKey: string;
- private secretKey: string;
+ private accessToken: string;
private axiosInstance: ReturnType<typeof axios.create>;
constructor(config: OnshapeClientConfig) {
this.baseUrl = config.baseUrl;
- this.accessKey = config.accessKey;
- this.secretKey = config.secretKey;
+ this.accessToken = config.accessToken;
this.axiosInstance = axios.create({
baseURL: this.baseUrl,
@@ -32,24 +32,14 @@ export class OnshapeClient {
});
}
- /**
- * Generate the authorization headers for Onshape API requests
- */
private getAuthHeaders(): Record<string, string> {
- const auth = Buffer.from(`${this.accessKey}:${this.secretKey}`).toString(
- "base64"
- );
-
return {
"Content-Type": "application/json",
Accept: "application/json;charset=UTF-8; qs=0.09",
- Authorization: `Basic ${auth}`
+ Authorization: `Bearer ${this.accessToken}`
};
}
- /**
- * Make an authenticated request to the Onshape API
- */
private async request<T>(
method: string,
path: string,
@@ -64,7 +54,6 @@ export class OnshapeClient {
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
- console.log("Onshape API error headers:", error.config?.headers);
throw new Error(
`Onshape API error (${error.response?.status}): ${error.response?.data}`
);
@@ -73,9 +62,6 @@ export class OnshapeClient {
}
}
- /**
- * Get a list of documents
- */
async getDocuments(limit: number = 20, offset: number = 0): Promise<any> {
return this.request(
"GET",
@@ -94,7 +80,12 @@ export class OnshapeClient {
);
}
- async getElements(documentId: string, versionId: string): Promise<any> {
+ async getElements(
+ documentId: string,
+ versionId: string,
+ limit: number = 20,
+ offset: number = 0
+ ): Promise<any> {
return this.request(
"GET",
`/api/v10/documents/d/${documentId}/v/${versionId}/elements`
@@ -111,4 +102,106 @@ export class OnshapeClient {
`/api/v10/assemblies/d/${documentId}/v/${versionId}/e/${elementId}/bom?indented=true&multiLevel=true&generateIfAbsent=true&onlyVisibleColumns=true&includeItemMicroversions=false&includeTopLevelAssemblyRow=true&thumbnail=false`
);
}
+
+ static async refreshAccessToken(refreshToken: string): Promise<{
+ access_token: string;
+ refresh_token: string;
+ token_type: string;
+ }> {
+ if (!ONSHAPE_CLIENT_ID || !ONSHAPE_CLIENT_SECRET) {
+ throw new Error("Onshape OAuth not configured");
+ }
+
+ const response = await fetch("https://oauth.onshape.com/oauth/token", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/x-www-form-urlencoded"
+ },
+ body: new URLSearchParams({
+ grant_type: "refresh_token",
+ refresh_token: refreshToken,
+ client_id: ONSHAPE_CLIENT_ID,
+ client_secret: ONSHAPE_CLIENT_SECRET
+ })
+ });
+
+ if (!response.ok) {
+ throw new Error(
+ `Onshape token refresh failed (${response.status}): ${await response.text()}`
+ );
+ }
+
+ return response.json();
+ }
+}
+
+export async function getOnshapeClient(
+ client: SupabaseClient<Database>,
+ companyId: string,
+ userId: string
+): Promise<
+ { client: OnshapeClient; error: null } | { client: null; error: string }
+> {
+ const integration = await client
+ .from("companyIntegration")
+ .select("*")
+ .eq("id", "onshape")
+ .eq("companyId", companyId)
+ .maybeSingle();
+
+ if (integration.error || !integration.data) {
+ return { client: null, error: "Onshape integration not found" };
+ }
+
+ const metadata = integration.data.metadata as Record<string, any>;
+ const credentials = metadata?.credentials;
+
+ if (!credentials?.accessToken) {
+ return { client: null, error: "Onshape credentials not found" };
+ }
+
+ let accessToken = credentials.accessToken;
+ const baseUrl = metadata?.baseUrl ?? "https://cad.onshape.com";
+
+ // Refresh token if expired
+ if (
+ credentials.expiresAt &&
+ credentials.refreshToken &&
+ new Date(credentials.expiresAt) <= new Date()
+ ) {
+ try {
+ const refreshed = await OnshapeClient.refreshAccessToken(
+ credentials.refreshToken
+ );
+
+ accessToken = refreshed.access_token;
+
+ // Persist the new tokens
+ await client
+ .from("companyIntegration")
+ .update({
+ metadata: {
+ ...metadata,
+ credentials: {
+ ...credentials,
+ accessToken: refreshed.access_token,
+ refreshToken: refreshed.refresh_token,
+ expiresAt: new Date(Date.now() + 3600 * 1000).toISOString()
+ }
+ },
+ updatedBy: userId,
+ updatedAt: new Date().toISOString()
+ })
+ .eq("id", "onshape")
+ .eq("companyId", companyId);
+ } catch (error) {
+ console.error("Failed to refresh Onshape token:", error);
+ return { client: null, error: "Failed to refresh Onshape token" };
+ }
+ }
+
+ return {
+ client: new OnshapeClient({ baseUrl, accessToken }),
+ error: null
+ };
}