Skip to content

Commit 4385093

Browse files
committed
AppId -> AppNameSlug
Correcting external user ID method. Correcting API request base method to handle authType
1 parent ad1804d commit 4385093

File tree

3 files changed

+64
-75
lines changed

3 files changed

+64
-75
lines changed

packages/sdk/src/browser/index.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,10 @@ type CreateBrowserClientOpts = {
2020
};
2121

2222
/**
23-
* A unique identifier for an app.
23+
* The name slug for an app, a unique, human-readable identifier like "github" or "google_sheets".
24+
* Find this in the Authentication section for any app's page at https://pipedream.com/apps
2425
*/
25-
type AppId = string;
26-
27-
/**
28-
* Object representing an app to start connecting with.
29-
*/
30-
type StartConnectApp = {
31-
/**
32-
* The unique identifier of the app.
33-
*/
34-
id: AppId;
35-
};
26+
type AppNameSlug = string;
3627

3728
/**
3829
* The result of a successful connection.
@@ -61,7 +52,7 @@ type StartConnectOpts = {
6152
/**
6253
* The app to connect to, either as an ID or an object containing the ID.
6354
*/
64-
app: AppId | StartConnectApp;
55+
app: AppNameSlug;
6556

6657
/**
6758
* The OAuth app ID to connect to.

packages/sdk/src/server/__tests__/server.test.ts

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ describe("ServerClient", () => {
2121
const client = createClient(params);
2222
expect(client).toBeInstanceOf(ServerClient);
2323
});
24-
25-
it("should mock the createClient method with a project object and return a ServerClient instance", () => {
26-
const params = {
27-
project: {
28-
publicKey: "test-public-key",
29-
secretKey: "test-secret",
30-
},
31-
};
32-
33-
const client = createClient(params);
34-
expect(client).toBeInstanceOf(ServerClient);
35-
});
3624
});
3725

3826
describe("makeRequest", () => {
@@ -576,7 +564,7 @@ describe("ServerClient", () => {
576564
});
577565

578566
describe("invokeWorkflow", () => {
579-
it("should invoke a workflow with provided URL and body", async () => {
567+
it("should invoke a workflow with provided URL and body, with no auth type", async () => {
580568
// Create a mock oauthClient
581569
const getTokenMock = jest.fn().mockResolvedValue({
582570
token: {
@@ -592,10 +580,8 @@ describe("ServerClient", () => {
592580
// Inject the mock oauthClient into the ServerClient instance
593581
const client = new ServerClient(
594582
{
595-
project: {
596-
publicKey: "test-public-key",
597-
secretKey: "test",
598-
},
583+
publicKey: "test-public-key",
584+
secretKey: "test",
599585
oauth: {
600586
clientId: "test-client-id",
601587
clientSecret: "test-client-secret",
@@ -632,7 +618,8 @@ describe("ServerClient", () => {
632618
foo: "bar",
633619
}),
634620
headers: expect.objectContaining({
635-
Authorization: "Bearer mocked-oauth-token",
621+
"Content-Type": "application/json",
622+
"X-PD-Environment": "production",
636623
}),
637624
}),
638625
);
@@ -667,10 +654,8 @@ describe("ServerClient", () => {
667654

668655
const client = new ServerClient(
669656
{
670-
project: {
671-
publicKey: "test-public-key",
672-
secretKey: "test",
673-
},
657+
publicKey: "test-public-key",
658+
secretKey: "test",
674659
oauth: {
675660
clientId: "test-client-id",
676661
clientSecret: "test-client-secret",
@@ -738,10 +723,8 @@ describe("ServerClient", () => {
738723
} as unknown as ClientCredentials;
739724
client = new ServerClient(
740725
{
741-
project: {
742-
publicKey: "test-public-key",
743-
secretKey: "test",
744-
},
726+
publicKey: "test-public-key",
727+
secretKey: "test",
745728
oauth: {
746729
clientId: "test-client-id",
747730
clientSecret: "test-client-secret",
@@ -751,7 +734,7 @@ describe("ServerClient", () => {
751734
);
752735
});
753736

754-
it("should include externalUserId and publicKey headers", async () => {
737+
it("should include externalUserId and environment headers", async () => {
755738
fetchMock.mockResponseOnce(
756739
JSON.stringify({
757740
result: "workflow-response",
@@ -778,7 +761,7 @@ describe("ServerClient", () => {
778761
expect.objectContaining({
779762
headers: expect.objectContaining({
780763
"X-PD-External-User-ID": "external-user-id",
781-
"X-PD-Project-Public-Key": "test-public-key",
764+
"X-PD-Environment": "production",
782765
}),
783766
}),
784767
);
@@ -791,18 +774,6 @@ describe("ServerClient", () => {
791774
},
792775
})).rejects.toThrow("External user ID is required");
793776
});
794-
795-
it("should throw error when publicKey is missing", async () => {
796-
const clientWithoutPublicKey = new ServerClient({
797-
secretKey: "test-secret-key",
798-
});
799-
800-
await expect(clientWithoutPublicKey.invokeWorkflowForExternalUser("https://example.com/workflow", "external-user-id", {
801-
body: {
802-
foo: "bar",
803-
},
804-
})).rejects.toThrow("Project public key is required to map the external user ID to the correct project");
805-
});
806777
});
807778

808779
});

packages/sdk/src/server/index.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import {
77
ClientCredentials,
88
} from "simple-oauth2";
99

10-
export type ProjectKeys = {
11-
publicKey: string;
12-
secretKey: string;
10+
export type Project = {
11+
id: string;
12+
};
13+
14+
export type ProjectEnvironment = {
15+
project: Project;
16+
environment: string;
1317
};
1418

1519
export type PipedreamOAuthClient = {
@@ -34,6 +38,11 @@ export type CreateServerClientOpts = {
3438
*/
3539
secretKey?: string;
3640

41+
/**
42+
* The environment in which the server client is running (e.g., "production", "development").
43+
*/
44+
environment?: string;
45+
3746
/**
3847
* @deprecated Use the `oauth` object instead.
3948
* The client ID of your workspace's OAuth application.
@@ -46,11 +55,6 @@ export type CreateServerClientOpts = {
4655
*/
4756
oauthClientSecret?: string;
4857

49-
/**
50-
* The project object, containing publicKey and secretKey.
51-
*/
52-
project?: ProjectKeys;
53-
5458
/**
5559
* The OAuth object, containing client ID and client secret.
5660
*/
@@ -62,6 +66,15 @@ export type CreateServerClientOpts = {
6266
apiHost?: string;
6367
};
6468

69+
/**
70+
* Different types of ways customers can authorize requests to HTTP endpoints
71+
*/
72+
export enum HTTPAuthType {
73+
None = "none",
74+
StaticBearer = "static-bearer",
75+
OAuth = "oauth"
76+
}
77+
6578
/**
6679
* Options for creating a Connect token.
6780
*/
@@ -136,7 +149,7 @@ export type ConnectParams = {
136149
/**
137150
* The authentication type for the app.
138151
*/
139-
export enum AuthType {
152+
export enum AppAuthType {
140153
OAuth = "oauth",
141154
Keys = "keys",
142155
None = "none",
@@ -164,7 +177,7 @@ export type AppResponse = {
164177
/**
165178
* The authentication type used by the app.
166179
*/
167-
auth_type: AuthType;
180+
auth_type: AppAuthType;
168181

169182
/**
170183
* The URL to the app's logo.
@@ -322,6 +335,7 @@ export function createClient(opts: CreateServerClientOpts) {
322335
export class ServerClient {
323336
private secretKey?: string;
324337
private publicKey?: string;
338+
private environment: string;
325339
private oauthClient?: ClientCredentials;
326340
private oauthToken?: AccessToken;
327341
private readonly baseURL: string;
@@ -336,12 +350,9 @@ export class ServerClient {
336350
opts: CreateServerClientOpts,
337351
oauthClient?: ClientCredentials,
338352
) {
353+
this.environment = opts.environment ?? "production";
339354
this.secretKey = opts.secretKey;
340355
this.publicKey = opts.publicKey;
341-
if (opts.project) {
342-
this.publicKey = opts.project.publicKey;
343-
this.secretKey = opts.project.secretKey;
344-
}
345356

346357
const { apiHost = "api.pipedream.com" } = opts;
347358
this.baseURL = `https://${apiHost}/v1`;
@@ -460,8 +471,9 @@ export class ServerClient {
460471
}
461472
}
462473

463-
const headers = {
474+
const headers: Record<string, string> = {
464475
...customHeaders,
476+
"X-PD-Environment": this.environment,
465477
};
466478

467479
let processedBody: string | Buffer | URLSearchParams | FormData | null = null;
@@ -571,7 +583,8 @@ export class ServerClient {
571583
}
572584

573585
/**
574-
* Creates a new connect token.
586+
* Creates a new Pipedream Connect token.
587+
* See https://pipedream.com/docs/connect/quickstart#connect-to-the-pipedream-api-from-your-server-and-create-a-token
575588
*
576589
* @param opts - The options for creating the connect token.
577590
* @returns A promise resolving to the connect token response.
@@ -782,20 +795,35 @@ export class ServerClient {
782795
* console.log(response);
783796
* ```
784797
*/
785-
public async invokeWorkflow(url: string, opts: RequestOptions = {}): Promise<unknown> {
798+
public async invokeWorkflow(url: string, opts: RequestOptions = {}, authType: HTTPAuthType = HTTPAuthType.None): Promise<unknown> {
786799
const {
787800
body,
788801
headers = {},
789802
} = opts;
790803

804+
let authHeader: string | undefined;
805+
switch (authType) {
806+
// It's expected that users will pass their own Authorization header in the static bearer case
807+
case HTTPAuthType.StaticBearer:
808+
authHeader = headers["Authorization"];
809+
break;
810+
case HTTPAuthType.OAuth:
811+
authHeader = await this.oauthAuthorizationHeader();
812+
break;
813+
default:
814+
break;
815+
}
816+
791817
return this.makeRequest("", {
792818
...opts,
793819
baseURL: url,
794820
method: opts.method || "POST", // Default to POST if not specified
795-
headers: {
796-
...headers,
797-
"Authorization": await this.oauthAuthorizationHeader(),
798-
},
821+
headers: authHeader
822+
? {
823+
...headers,
824+
"Authorization": authHeader,
825+
}
826+
: headers,
799827
body,
800828
});
801829
}
@@ -850,7 +878,6 @@ export class ServerClient {
850878
headers: {
851879
...headers,
852880
"X-PD-External-User-ID": externalUserId,
853-
"X-PD-Project-Public-Key": this.publicKey,
854881
},
855882
});
856883
}

0 commit comments

Comments
 (0)