Skip to content

Commit 790449f

Browse files
committed
Fixes (to a certain point)
* Switch to the REST API to create a Connect token * Rename "client user ID" to "external ID" * Moved some env vars to the server * Use public key instead of project ID * Switch from app's name slug to ID
1 parent 4a27b5f commit 790449f

File tree

5 files changed

+67
-76
lines changed

5 files changed

+67
-76
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Config for the front-end
2+
NEXT_PUBLIC_PIPEDREAM_APP_ID=oa_
3+
NEXT_PUBLIC_PIPEDREAM_FRONTEND_HOST=frontend.gkes.pipedream.net
4+
5+
# API host (dev or prod)
6+
PIPEDREAM_API_HOST=api.<username>.gkes.pipedream.net
7+
8+
# Project credentials
9+
PIPEDREAM_PROJECT_PUBLIC_KEY=pub_
10+
PIPEDREAM_PROJECT_SECRET_KEY=sec_

packages/sdk/examples/next-app/app/page.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ import { createRef, useEffect, useState } from "react";
44
import { serverConnectTokenCreate, getAppsData } from "./server"
55
import { createClient } from "../../../src/browser"
66

7-
const publicKey = process.env.NEXT_PUBLIC_PIPEDREAM_PROJECT_PUBLIC_KEY
87
const frontendHost = process.env.NEXT_PUBLIC_PIPEDREAM_FRONTEND_HOST
9-
const oauthAppId = process.env.NEXT_PUBLIC_PIPEDREAM_TEST_APP_ID
10-
const appSlug = process.env.NEXT_PUBLIC_PIPEDREAM_APP_SLUG
8+
const oauthAppId = process.env.NEXT_PUBLIC_PIPEDREAM_APP_ID
119

1210
export default function Home() {
13-
if (!publicKey) throw new Error("Missing NEXT_PUBLIC_PIPEDREAM_PROJECT_PUBLIC_KEY env var")
14-
if (!oauthAppId) throw new Error("Missing NEXT_PUBLIC_PIPEDREAM_TEST_APP_ID env var")
15-
if (!appSlug) throw new Error("Missing NEXT_PUBLIC_PIPEDREAM_APP_SLUG env var")
11+
if (!oauthAppId) throw new Error("Missing NEXT_PUBLIC_PIPEDREAM_APP_ID env var")
1612

17-
const pd = createClient({ publicKey, frontendHost })
13+
const pd = createClient({ frontendHost })
1814
const [externalUserId, setExternalUserId] = useState<string | null>(null)
1915
const [githubData, setGithubData] = useState<{ login: string } | null>(null)
2016
const [token, setToken] = useState<string | null>(null)
@@ -35,8 +31,7 @@ export default function Home() {
3531
}
3632

3733
const connectAccount = async () => {
38-
connectApp(oauthAppId as string)
39-
34+
await connectApp(oauthAppId as string)
4035
}
4136

4237
const signIn = () => {
@@ -55,7 +50,10 @@ export default function Home() {
5550
setOauthAppId(null)
5651
setAuthProvisionId(null)
5752
} else {
58-
serverConnectTokenCreate(externalUserId).then((t) => setToken(t))
53+
serverConnectTokenCreate({
54+
app_id: oauthAppId,
55+
external_id: externalUserId
56+
}).then((t) => setToken(t))
5957
getAppsData(externalUserId).then((d) => {
6058
setGithubData(d.github)
6159
})

packages/sdk/examples/next-app/app/server.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11
"use server";
22

3-
import { createClient } from "../../../src";
3+
import {
4+
createClient,
5+
type ConnectTokenCreateOpts,
6+
} from "../../../src";
47

58
const {
9+
NEXT_PUBLIC_PIPEDREAM_APP_ID,
10+
PIPEDREAM_API_HOST,
11+
PIPEDREAM_PROJECT_PUBLIC_KEY,
612
PIPEDREAM_PROJECT_SECRET_KEY,
7-
NEXT_PUBLIC_PIPEDREAM_APP_SLUG,
8-
PIPEDREAM_PROJECT_ID,
9-
NEXT_PUBLIC_PIPEDREAM_PROJECT_PUBLIC_KEY,
1013
} = process.env;
1114

12-
if (!PIPEDREAM_PROJECT_SECRET_KEY) throw new Error("PIPEDREAM_PROJECT_SECRET_KEY not set in environment");
13-
if (!NEXT_PUBLIC_PIPEDREAM_APP_SLUG) throw new Error("NEXT_PUBLIC_PIPEDREAM_APP_SLUG not set in environment");
15+
if (!NEXT_PUBLIC_PIPEDREAM_APP_ID)
16+
throw new Error("NEXT_PUBLIC_PIPEDREAM_APP_ID not set in environment");
17+
if (!PIPEDREAM_PROJECT_PUBLIC_KEY)
18+
throw new Error("PIPEDREAM_PROJECT_SECRET_KEY not set in environment");
19+
if (!PIPEDREAM_PROJECT_SECRET_KEY)
20+
throw new Error("PIPEDREAM_PROJECT_SECRET_KEY not set in environment");
1421

1522
const pd = createClient({
1623
secretKey: PIPEDREAM_PROJECT_SECRET_KEY,
17-
apiHost: process.env.PIPEDREAM_API_HOST,
18-
//projectId: PIPEDREAM_PROJECT_ID,
19-
publicKey: process.env.NEXT_PUBLIC_PIPEDREAM_PROJECT_PUBLIC_KEY,
24+
apiHost: PIPEDREAM_API_HOST,
25+
publicKey: PIPEDREAM_PROJECT_PUBLIC_KEY,
2026
});
2127

22-
export async function serverConnectTokenCreate(clientUserId: string) {
23-
return pd.connectTokenCreate({
24-
clientUserId,
25-
});
28+
export async function serverConnectTokenCreate(opts: ConnectTokenCreateOpts) {
29+
return pd.connectTokenCreate(opts);
2630
}
2731

28-
export async function getAppsData(clientUserId: string) {
32+
export async function getAppsData(externalId: string) {
2933
const [
3034
github,
3135
] = await Promise.all([
32-
getGithubData(clientUserId),
36+
getGithubData(externalId),
3337
]);
3438
return {
3539
github,
3640
};
3741
}
3842

39-
export async function getGithubData(clientUserId: string) {
40-
if (!NEXT_PUBLIC_PIPEDREAM_APP_SLUG) {
41-
throw new Error("NEXT_PUBLIC_PIPEDREAM_APP_SLUG not set in environment");
42-
}
43+
export async function getGithubData(externalId: string) {
44+
if (!NEXT_PUBLIC_PIPEDREAM_APP_ID)
45+
throw new Error("NEXT_PUBLIC_PIPEDREAM_APP_ID not set in environment");
46+
4347
const data = await pd.getAccount({
44-
app: NEXT_PUBLIC_PIPEDREAM_APP_SLUG,
45-
clientUserId,
48+
appId: NEXT_PUBLIC_PIPEDREAM_APP_ID,
49+
externalId,
4650
}, {
4751
includeCredentials: true,
4852
});
4953
if (!data?.accounts.length) {
5054
return null;
5155
}
52-
const account = data.accounts[data.accounts.length - 1]
56+
const account = data.accounts[data.accounts.length - 1];
5357
const resp = await fetch("https://api.github.com/user", {
5458
headers: {
5559
Authorization: `Bearer ${account.credentials.oauth_access_token}`,

packages/sdk/src/browser/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
type CreateBrowserClientOpts = {
22
environment?: string;
3-
publicKey: string;
43
frontendHost?: string;
54
};
65

@@ -29,15 +28,13 @@ export function createClient(opts: CreateBrowserClientOpts) {
2928

3029
class BrowserClient {
3130
environment?: string;
32-
publicKey: string;
3331
baseURL: string;
3432
iframeURL: string;
3533
iframe?: HTMLIFrameElement;
3634
iframeId = 0;
3735

3836
constructor(opts: CreateBrowserClientOpts) {
3937
this.environment = opts.environment;
40-
this.publicKey = opts.publicKey;
4138
this.baseURL = `https://${opts.frontendHost || "pipedream.com"}`;
4239
this.iframeURL = `${this.baseURL}/_static/connect.html`;
4340
}
@@ -59,7 +56,6 @@ class BrowserClient {
5956

6057
const qp = new URLSearchParams();
6158
qp.set("token", opts.token);
62-
qp.set("publicKey", this.publicKey);
6359
if (this.environment) {
6460
qp.set("environment", this.environment);
6561
}

packages/sdk/src/index.ts

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
type CreateServerClientOpts = {
2-
environment?: string;
3-
secretKey: string;
42
apiHost?: string;
5-
//projectId?: string;
3+
environment?: string;
64
publicKey: string;
5+
secretKey: string;
76
};
87

9-
type ConnectTokenCreateOpts = {
10-
clientUserId: string;
8+
export type ConnectTokenCreateOpts = {
9+
app_id: string;
10+
client_name?: string;
11+
external_id: string;
1112
};
1213

1314
type AccountId = string;
1415
type AccountKeyFields = {
15-
clientUserId: string;
16-
app: string;
16+
externalId: string;
17+
appId: string;
1718
};
1819
type AccountKey = AccountId | AccountKeyFields;
1920

@@ -25,57 +26,39 @@ class ServerClient {
2526
environment?: string;
2627
secretKey: string;
2728
publicKey: string;
28-
//projectId: string;
2929
baseURL: string;
3030

3131
constructor(opts: CreateServerClientOpts) {
3232
this.environment = opts.environment;
3333
this.secretKey = opts.secretKey;
3434
this.publicKey = opts.publicKey;
35-
//this.projectId = opts.projectId;
36-
this.baseURL = `https://${opts.apiHost || "pipedream.com"}`;
35+
36+
const { apiHost = "pipedream.com" } = opts;
37+
this.baseURL = `https://${apiHost}`;
3738
}
3839

3940
private _authorizonHeader(): string {
40-
return "Basic " + Buffer.from(`${this.publicKey}:${this.secretKey}`).toString("base64");
41+
const encoded = Buffer
42+
.from(`${this.publicKey}:${this.secretKey}`)
43+
.toString("base64");
44+
return `Basic ${encoded}`;
4145
}
4246

4347
// XXX move to REST API endpoint
4448
async connectTokenCreate(opts: ConnectTokenCreateOpts): Promise<string> {
45-
const resp = await fetch(`${this.baseURL}/graphql`, {
49+
const resp = await fetch(`${this.baseURL}/v1/connect/tokens`, {
4650
method: "POST",
4751
headers: {
52+
"authorization": this._authorizonHeader(),
4853
"content-type": "application/json",
4954
},
50-
body: JSON.stringify({
51-
query: `mutation sdkConnectTokenCreate(
52-
$publicKey: String!
53-
$secretKey: String!
54-
$externalId: String!
55-
$oauthAppId: String!
56-
) {
57-
connectTokenCreate(
58-
publicKey: $publicKey
59-
secretKey: $secretKey
60-
externalId: $externalId
61-
oauthAppId: $oauthAppId
62-
) {
63-
token
64-
}
65-
}`,
66-
variables: {
67-
publicKey: this.publicKey,
68-
secretKey: this.secretKey,
69-
externalId: opts.clientUserId,
70-
oauthAppId: "oa_aLZiMJ",
71-
},
72-
}),
55+
body: JSON.stringify(opts),
7356
});
7457
const res = await resp.json();
7558
// XXX expose error here
76-
console.log("connect token create result")
77-
console.log(res)
78-
return res.data?.connectTokenCreate?.token;
59+
console.log("connect token create result");
60+
console.log(res);
61+
return res?.token;
7962
}
8063

8164
async getAccount(key: AccountKey, opts?: { includeCredentials?: boolean; }) {
@@ -86,7 +69,7 @@ class ServerClient {
8669
id = key;
8770
url = `${baseAccountURL}/${id}`;
8871
} else {
89-
url = `${baseAccountURL}?app=${key.app}&limit=100&external_id=${key.clientUserId}`;
72+
url = `${baseAccountURL}?app=${key.appId}&limit=100&external_id=${key.externalId}`;
9073
}
9174
if (opts?.includeCredentials) {
9275
url += `${id
@@ -99,7 +82,7 @@ class ServerClient {
9982
},
10083
});
10184
const res = await resp.json();
102-
console.log('res :>> ', res);
85+
console.log("res :>> ", res);
10386
const {
10487
data, error,
10588
} = res;

0 commit comments

Comments
 (0)