Skip to content

Commit d57c8f6

Browse files
bzwrktjk
andauthored
Add getEnvironment to BaseClient (#15488)
* Add getEnvironment to BaseClient * fix not running tests * try this * jest and type stuff * Fix linter errors * Fix type error * Update pnpm-lock.yaml --------- Co-authored-by: TJ Koblentz <[email protected]>
1 parent 6f08e24 commit d57c8f6

File tree

7 files changed

+100
-41
lines changed

7 files changed

+100
-41
lines changed

packages/sdk/CHANGELOG.md

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

33
# Changelog
44

5+
## [1.3.2] - 2025-02-3
6+
7+
### Changed
8+
9+
- Add getEnvironment function to BaseClient
10+
511
## [1.3.1] - 2025-01-30
612

713
### Changed

packages/sdk/jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export default {
2-
preset: "ts-jest",
32
testEnvironment: "node",
43
roots: [
54
"<rootDir>/src",
@@ -15,13 +14,17 @@ export default {
1514
moduleNameMapper: {
1615
"^(.+)\\.js$": "$1",
1716
},
17+
extensionsToTreatAsEsm: [
18+
".ts",
19+
],
1820
transform: {
1921
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
2022
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
2123
"^.+\\.[jt]sx?$": [
2224
"ts-jest",
2325
{
2426
tsconfig: "tsconfig.node.json",
27+
useESM: true,
2528
},
2629
],
2730
},

packages/sdk/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@pipedream/sdk",
33
"type": "module",
4-
"version": "1.3.1",
4+
"version": "1.3.2",
55
"description": "Pipedream SDK",
66
"main": "./dist/server.js",
77
"module": "./dist/server.js",
@@ -40,7 +40,7 @@
4040
"prepublish": "pnpm run build",
4141
"prebuild": "node scripts/updateVersion.js",
4242
"build": "rm -rf dist && pnpm run prebuild && tsup --config tsup.server.cjs.config.js && tsup --config tsup.server.esm.config.js && tsup --config tsup.browser.config.js",
43-
"test": "node --experimental-vm-modules node_modules/.bin/jest",
43+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest",
4444
"watch": "nodemon --watch src --exec 'pnpm run build'",
4545
"cli": "node dist/server/cli.js"
4646
},

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

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { jest } from "@jest/globals"
12
import {
23
BackendClient,
34
BackendClientOpts,
@@ -50,7 +51,9 @@ describe("BackendClient", () => {
5051
url: "https://api.pipedream.com/v1/test-path",
5152
},
5253
response: {
53-
json: { data: "test-response" },
54+
json: {
55+
data: "test-response",
56+
},
5457
},
5558
})
5659

@@ -72,7 +75,9 @@ describe("BackendClient", () => {
7275
},
7376
},
7477
response: {
75-
json: { success: true },
78+
json: {
79+
success: true,
80+
},
7681
},
7782
})
7883

@@ -116,7 +121,9 @@ describe("BackendClient", () => {
116121
},
117122
},
118123
response: {
119-
json: { success: true },
124+
json: {
125+
success: true,
126+
},
120127
},
121128
})
122129

@@ -144,7 +151,9 @@ describe("BackendClient", () => {
144151
},
145152
},
146153
response: {
147-
json: { success: true },
154+
json: {
155+
success: true,
156+
},
148157
},
149158
})
150159

@@ -311,10 +320,12 @@ describe("BackendClient", () => {
311320
url: `https://api.pipedream.com/v1/connect/${projectId}/accounts?app=app-1`,
312321
},
313322
response: {
314-
json: [{
315-
id: "account-1",
316-
name: "Test Account",
317-
}],
323+
json: [
324+
{
325+
id: "account-1",
326+
name: "Test Account",
327+
},
328+
],
318329
},
319330
});
320331

@@ -339,10 +350,12 @@ describe("BackendClient", () => {
339350
url: `https://api.pipedream.com/v1/connect/${projectId}/accounts?external_user_id=external-id-1`,
340351
},
341352
response: {
342-
json: [{
343-
id: "account-1",
344-
name: "Test Account",
345-
}],
353+
json: [
354+
{
355+
id: "account-1",
356+
name: "Test Account",
357+
},
358+
],
346359
},
347360
});
348361

@@ -464,7 +477,9 @@ describe("BackendClient", () => {
464477
},
465478
},
466479
response: {
467-
json: { result: "workflow-response" },
480+
json: {
481+
result: "workflow-response",
482+
},
468483
},
469484
})
470485

@@ -480,8 +495,10 @@ describe("BackendClient", () => {
480495
});
481496

482497
it("should invoke a workflow with OAuth auth type", async () => {
483-
const token = ""+Math.random()
484-
fetchMock.expectAccessTokenSuccess({ accessToken: token });
498+
const token = "" + Math.random()
499+
fetchMock.expectAccessTokenSuccess({
500+
accessToken: token,
501+
});
485502
fetchMock.expect({
486503
request: {
487504
url: "https://example.com/workflow",
@@ -490,7 +507,9 @@ describe("BackendClient", () => {
490507
},
491508
},
492509
response: {
493-
json: { result: "workflow-response" },
510+
json: {
511+
result: "workflow-response",
512+
},
494513
},
495514
})
496515

@@ -502,7 +521,7 @@ describe("BackendClient", () => {
502521
});
503522

504523
it("should invoke a workflow with static bearer auth type", async () => {
505-
const token = ""+Math.random()
524+
const token = "" + Math.random()
506525
fetchMock.expect({
507526
request: {
508527
url: "https://example.com/workflow",
@@ -511,7 +530,9 @@ describe("BackendClient", () => {
511530
},
512531
},
513532
response: {
514-
json: { result: "workflow-response" },
533+
json: {
534+
result: "workflow-response",
535+
},
515536
},
516537
})
517538

@@ -537,7 +558,9 @@ describe("BackendClient", () => {
537558
url: "https://api.pipedream.com/v1/test-path",
538559
},
539560
response: {
540-
json: { success: true },
561+
json: {
562+
success: true,
563+
},
541564
},
542565
})
543566

@@ -570,7 +593,9 @@ describe("BackendClient", () => {
570593
},
571594
},
572595
response: {
573-
json: { result: "workflow-response" },
596+
json: {
597+
result: "workflow-response",
598+
},
574599
},
575600
})
576601

@@ -696,17 +721,17 @@ describe("BackendClient", () => {
696721
type ExpectRequest = {
697722
method?: string
698723
url?: string | RegExp
699-
json?: Record<string, any>
724+
json?: Record<string, unknown>
700725
headersContaining?: Record<string, string>
701726
}
702727
type MockResponse =
703728
| Response
704-
| { status?: number; json?: any }
729+
| { status?: number; json?: unknown }
705730
type IfOpts = {
706731
method: string
707732
url: string
708733
headers: Record<string, string> // NonNullable<RequestInit["headers"]>
709-
json?: any // body json
734+
json?: unknown // body json
710735
// XXX etc.
711736
}
712737
function setupFetchMock() {
@@ -715,7 +740,7 @@ function setupFetchMock() {
715740
response: () => Response
716741
}[] = []
717742

718-
const jsonResponse = (o: any, opts?: { status?: number }) => {
743+
const jsonResponse = (o: unknown, opts?: { status?: number }) => {
719744
return new Response(JSON.stringify(o), {
720745
status: opts?.status,
721746
headers: {
@@ -726,17 +751,27 @@ function setupFetchMock() {
726751

727752
beforeEach(() => {
728753
intercepts = [];
729-
jest.spyOn(global, "fetch").mockImplementation(jest.fn((url: string, init: RequestInit) => {
730-
let json: any
731-
if (init.body && typeof init.body === "string") {
754+
// without these generics this fails typecheck and can't figure out why
755+
jest.spyOn<any, any, any>(global, "fetch").mockImplementation(jest.fn<typeof fetch>(async (...args: Parameters<typeof fetch>) => { // eslint-disable-line @typescript-eslint/no-explicit-any
756+
const [
757+
url,
758+
init,
759+
] = args
760+
let json: unknown
761+
if (init?.body && typeof init.body === "string") {
732762
try {
733763
json = JSON.parse(init.body)
734-
} catch {}
764+
} catch {
765+
// pass
766+
}
767+
}
768+
if (url instanceof Request) {
769+
throw new Error("not supported")
735770
}
736771
const ifOpts: IfOpts = {
737-
method: init.method || "GET",
738-
url,
739-
headers: init.headers as Record<string, string> || {},
772+
method: init?.method || "GET",
773+
url: url.toString(),
774+
headers: init?.headers as Record<string, string> || {},
740775
json,
741776
}
742777
for (let i = 0; i < intercepts.length; i++) {
@@ -758,7 +793,9 @@ function setupFetchMock() {
758793

759794
// const _expect = (opts: { if: (opts: IfOpts) => boolean, jsonResponse?: any, response?: Response }) => {
760795
const _expect = (opts: { request: ExpectRequest, response: MockResponse }) => {
761-
const { method, url, headersContaining, json } = opts.request
796+
const {
797+
method, url, headersContaining, json,
798+
} = opts.request
762799
intercepts.push({
763800
if: (ifOpts) => {
764801
if (method && ifOpts.method !== method) return false
@@ -800,7 +837,7 @@ function setupFetchMock() {
800837
}
801838

802839
const expectAccessTokenSuccess = (opts?: { accessToken?: string; expiresIn?: number }) => {
803-
const accessToken = opts?.accessToken || ""+Math.random()
840+
const accessToken = opts?.accessToken || "" + Math.random()
804841
_expect({
805842
request: {
806843
url: /\/v1\/oauth\/token$/,

packages/sdk/src/server/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class BackendClient extends BaseClient {
143143
token: string
144144
expiresAt: number
145145
};
146-
protected override projectId: string;
146+
protected override projectId: string = "";
147147

148148
/**
149149
* Constructs a new ServerClient instance.
@@ -199,7 +199,11 @@ export class BackendClient extends BaseClient {
199199
}
200200

201201
private async ensureValidOauthAccessToken(): Promise<string> {
202-
const { client, clientAuth, as } = this.oauthClient
202+
const {
203+
client,
204+
clientAuth,
205+
as,
206+
} = this.oauthClient
203207

204208
let attempts = 0;
205209
const maxAttempts = 2;
@@ -221,7 +225,9 @@ export class BackendClient extends BaseClient {
221225
token: oauthTokenResponse.access_token,
222226
expiresAt: Date.now() + (oauthTokenResponse.expires_in || 0) * 1000,
223227
};
224-
} catch {}
228+
} catch {
229+
// pass
230+
}
225231

226232
attempts++;
227233
}

packages/sdk/src/shared/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,14 @@ export abstract class BaseClient {
871871
this.workflowDomain = workflowDomain;
872872
}
873873

874+
/**
875+
* Retrieves the current environment the client is configured to use.
876+
* @returns {string} The current environment.
877+
*/
878+
public getEnvironment(): string {
879+
return this.environment;
880+
}
881+
874882
/**
875883
* Makes an HTTP request
876884
*

pnpm-lock.yaml

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)