Skip to content

Commit d7c9c58

Browse files
Centralize code snippets in dedicated file to reduce duplication
1 parent 11ea367 commit d7c9c58

File tree

2 files changed

+67
-41
lines changed

2 files changed

+67
-41
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* This file contains all the code snippets used in the Connect SDK demo.
3+
* Centralizing them here helps to maintain consistency and makes updates easier.
4+
*/
5+
6+
/**
7+
* Server-side code for generating a Connect token
8+
* @param {string} externalUserId - The user's external ID
9+
* @returns {string} The server code snippet
10+
*/
11+
export function getServerCodeSnippet(externalUserId) {
12+
return `import { createBackendClient } from "@pipedream/sdk/server";
13+
14+
// This code runs on your server
15+
const pd = createBackendClient({
16+
environment: "development",
17+
credentials: {
18+
clientId: process.env.PIPEDREAM_CLIENT_ID,
19+
clientSecret: process.env.PIPEDREAM_CLIENT_SECRET,
20+
},
21+
projectId: process.env.PIPEDREAM_PROJECT_ID
22+
});
23+
24+
// Create a token for a specific user
25+
const { token, expires_at, connect_link_url } = await pd.createConnectToken({
26+
external_user_id: "${externalUserId || "YOUR_USER_ID"}",
27+
});`;
28+
}
29+
30+
/**
31+
* Client-side code for connecting an account
32+
* @param {string} appSlug - The app to connect to (slack, github, etc)
33+
* @param {object} tokenData - The token data from the server
34+
* @returns {string} The client code snippet
35+
*/
36+
export function getClientCodeSnippet(appSlug, tokenData) {
37+
return `import { createFrontendClient } from "@pipedream/sdk/browser"
38+
39+
// This code runs in the browser
40+
const pd = createFrontendClient()
41+
42+
// Connect an account using the token from your server
43+
pd.connectAccount({
44+
app: "${appSlug}",
45+
token: "${tokenData?.token
46+
? tokenData.token.substring(0, 10) + "..."
47+
: "YOUR_TOKEN"}",
48+
onSuccess: (account) => {
49+
// Handle successful connection
50+
console.log(\`Account successfully connected: \${account.name}\`)
51+
},
52+
onError: (err) => {
53+
// Handle connection error
54+
console.error(\`Connection error: \${err.message}\`)
55+
},
56+
onClose: () => {
57+
// Handle dialog closed by user
58+
}
59+
})`;
60+
}

docs-v2/components/GlobalConnectProvider.jsx

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { createContext, useContext, useState, useEffect } from "react";
44
import { createFrontendClient } from "@pipedream/sdk/browser";
5+
import { getServerCodeSnippet, getClientCodeSnippet } from "./ConnectCodeSnippets";
56

67
// Generate a UUID v4 for use as external_user_id
78
function generateUUID() {
@@ -34,46 +35,11 @@ export function GlobalConnectProvider({ children }) {
3435
setExternalUserId(generateUUID());
3536
}, []);
3637

37-
// Get server code snippet
38-
const getServerCodeSnippet = () => `import { createBackendClient } from "@pipedream/sdk/server";
39-
40-
// This code runs on your server
41-
const pd = createBackendClient({
42-
environment: "development",
43-
credentials: {
44-
clientId: process.env.PIPEDREAM_CLIENT_ID,
45-
clientSecret: process.env.PIPEDREAM_CLIENT_SECRET,
46-
},
47-
projectId: process.env.PIPEDREAM_PROJECT_ID
48-
});
49-
50-
// Create a token for a specific user
51-
const { token, expires_at, connect_link_url } = await pd.createConnectToken({
52-
external_user_id: "${externalUserId || "YOUR_USER_ID"}",
53-
});`;
38+
// Get server code snippet wrapper function
39+
const getServerSnippet = () => getServerCodeSnippet(externalUserId);
5440

55-
// Get client code snippet
56-
const getClientCodeSnippet = () => `import { createFrontendClient } from "@pipedream/sdk/browser"
57-
58-
// This code runs in the browser
59-
const pd = createFrontendClient()
60-
61-
// Connect an account using the token from your server
62-
pd.connectAccount({
63-
app: "${appSlug}",
64-
token: "${tokenData?.token ? tokenData.token.substring(0, 10) + "..." : "YOUR_TOKEN"}",
65-
onSuccess: (account) => {
66-
// Handle successful connection
67-
console.log(\`Account successfully connected: \${account.name}\`)
68-
},
69-
onError: (err) => {
70-
// Handle connection error
71-
console.error(\`Connection error: \${err.message}\`)
72-
},
73-
onClose: () => {
74-
// Handle dialog closed by user
75-
}
76-
})`;
41+
// Get client code snippet wrapper function
42+
const getClientSnippet = () => getClientCodeSnippet(appSlug, tokenData);
7743

7844
// Generate token async function
7945
async function generateToken() {
@@ -150,8 +116,8 @@ pd.connectAccount({
150116
connectAccount,
151117

152118
// Code snippets
153-
getServerCodeSnippet,
154-
getClientCodeSnippet,
119+
getServerCodeSnippet: getServerSnippet,
120+
getClientCodeSnippet: getClientSnippet,
155121
};
156122

157123
return (

0 commit comments

Comments
 (0)