diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md index 453f666b98c9e..3bc01d2347fab 100644 --- a/packages/sdk/CHANGELOG.md +++ b/packages/sdk/CHANGELOG.md @@ -2,6 +2,12 @@ # Changelog +## [1.3.2] - 2025-02-3 + +### Changed + +- Add getEnvironment function to BaseClient + ## [1.3.1] - 2025-01-30 ### Changed diff --git a/packages/sdk/jest.config.js b/packages/sdk/jest.config.js index 3285aae08c7e6..67e93f088a952 100644 --- a/packages/sdk/jest.config.js +++ b/packages/sdk/jest.config.js @@ -1,5 +1,4 @@ export default { - preset: "ts-jest", testEnvironment: "node", roots: [ "/src", @@ -15,6 +14,9 @@ export default { moduleNameMapper: { "^(.+)\\.js$": "$1", }, + extensionsToTreatAsEsm: [ + ".ts", + ], transform: { // '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest` // '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest` @@ -22,6 +24,7 @@ export default { "ts-jest", { tsconfig: "tsconfig.node.json", + useESM: true, }, ], }, diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 8a0657e7989f6..13ad64555d8b7 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,7 +1,7 @@ { "name": "@pipedream/sdk", "type": "module", - "version": "1.3.1", + "version": "1.3.2", "description": "Pipedream SDK", "main": "./dist/server.js", "module": "./dist/server.js", @@ -40,7 +40,7 @@ "prepublish": "pnpm run build", "prebuild": "node scripts/updateVersion.js", "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", - "test": "node --experimental-vm-modules node_modules/.bin/jest", + "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest", "watch": "nodemon --watch src --exec 'pnpm run build'", "cli": "node dist/server/cli.js" }, diff --git a/packages/sdk/src/server/__tests__/server.test.ts b/packages/sdk/src/server/__tests__/server.test.ts index 2b471f7f13292..18efed23bcc54 100644 --- a/packages/sdk/src/server/__tests__/server.test.ts +++ b/packages/sdk/src/server/__tests__/server.test.ts @@ -1,3 +1,4 @@ +import { jest } from "@jest/globals" import { BackendClient, BackendClientOpts, @@ -50,7 +51,9 @@ describe("BackendClient", () => { url: "https://api.pipedream.com/v1/test-path", }, response: { - json: { data: "test-response" }, + json: { + data: "test-response", + }, }, }) @@ -72,7 +75,9 @@ describe("BackendClient", () => { }, }, response: { - json: { success: true }, + json: { + success: true, + }, }, }) @@ -116,7 +121,9 @@ describe("BackendClient", () => { }, }, response: { - json: { success: true }, + json: { + success: true, + }, }, }) @@ -144,7 +151,9 @@ describe("BackendClient", () => { }, }, response: { - json: { success: true }, + json: { + success: true, + }, }, }) @@ -311,10 +320,12 @@ describe("BackendClient", () => { url: `https://api.pipedream.com/v1/connect/${projectId}/accounts?app=app-1`, }, response: { - json: [{ - id: "account-1", - name: "Test Account", - }], + json: [ + { + id: "account-1", + name: "Test Account", + }, + ], }, }); @@ -339,10 +350,12 @@ describe("BackendClient", () => { url: `https://api.pipedream.com/v1/connect/${projectId}/accounts?external_user_id=external-id-1`, }, response: { - json: [{ - id: "account-1", - name: "Test Account", - }], + json: [ + { + id: "account-1", + name: "Test Account", + }, + ], }, }); @@ -464,7 +477,9 @@ describe("BackendClient", () => { }, }, response: { - json: { result: "workflow-response" }, + json: { + result: "workflow-response", + }, }, }) @@ -480,8 +495,10 @@ describe("BackendClient", () => { }); it("should invoke a workflow with OAuth auth type", async () => { - const token = ""+Math.random() - fetchMock.expectAccessTokenSuccess({ accessToken: token }); + const token = "" + Math.random() + fetchMock.expectAccessTokenSuccess({ + accessToken: token, + }); fetchMock.expect({ request: { url: "https://example.com/workflow", @@ -490,7 +507,9 @@ describe("BackendClient", () => { }, }, response: { - json: { result: "workflow-response" }, + json: { + result: "workflow-response", + }, }, }) @@ -502,7 +521,7 @@ describe("BackendClient", () => { }); it("should invoke a workflow with static bearer auth type", async () => { - const token = ""+Math.random() + const token = "" + Math.random() fetchMock.expect({ request: { url: "https://example.com/workflow", @@ -511,7 +530,9 @@ describe("BackendClient", () => { }, }, response: { - json: { result: "workflow-response" }, + json: { + result: "workflow-response", + }, }, }) @@ -537,7 +558,9 @@ describe("BackendClient", () => { url: "https://api.pipedream.com/v1/test-path", }, response: { - json: { success: true }, + json: { + success: true, + }, }, }) @@ -570,7 +593,9 @@ describe("BackendClient", () => { }, }, response: { - json: { result: "workflow-response" }, + json: { + result: "workflow-response", + }, }, }) @@ -696,17 +721,17 @@ describe("BackendClient", () => { type ExpectRequest = { method?: string url?: string | RegExp - json?: Record + json?: Record headersContaining?: Record } type MockResponse = | Response - | { status?: number; json?: any } + | { status?: number; json?: unknown } type IfOpts = { method: string url: string headers: Record // NonNullable - json?: any // body json + json?: unknown // body json // XXX etc. } function setupFetchMock() { @@ -715,7 +740,7 @@ function setupFetchMock() { response: () => Response }[] = [] - const jsonResponse = (o: any, opts?: { status?: number }) => { + const jsonResponse = (o: unknown, opts?: { status?: number }) => { return new Response(JSON.stringify(o), { status: opts?.status, headers: { @@ -726,17 +751,27 @@ function setupFetchMock() { beforeEach(() => { intercepts = []; - jest.spyOn(global, "fetch").mockImplementation(jest.fn((url: string, init: RequestInit) => { - let json: any - if (init.body && typeof init.body === "string") { + // without these generics this fails typecheck and can't figure out why + jest.spyOn(global, "fetch").mockImplementation(jest.fn(async (...args: Parameters) => { // eslint-disable-line @typescript-eslint/no-explicit-any + const [ + url, + init, + ] = args + let json: unknown + if (init?.body && typeof init.body === "string") { try { json = JSON.parse(init.body) - } catch {} + } catch { + // pass + } + } + if (url instanceof Request) { + throw new Error("not supported") } const ifOpts: IfOpts = { - method: init.method || "GET", - url, - headers: init.headers as Record || {}, + method: init?.method || "GET", + url: url.toString(), + headers: init?.headers as Record || {}, json, } for (let i = 0; i < intercepts.length; i++) { @@ -758,7 +793,9 @@ function setupFetchMock() { // const _expect = (opts: { if: (opts: IfOpts) => boolean, jsonResponse?: any, response?: Response }) => { const _expect = (opts: { request: ExpectRequest, response: MockResponse }) => { - const { method, url, headersContaining, json } = opts.request + const { + method, url, headersContaining, json, + } = opts.request intercepts.push({ if: (ifOpts) => { if (method && ifOpts.method !== method) return false @@ -800,7 +837,7 @@ function setupFetchMock() { } const expectAccessTokenSuccess = (opts?: { accessToken?: string; expiresIn?: number }) => { - const accessToken = opts?.accessToken || ""+Math.random() + const accessToken = opts?.accessToken || "" + Math.random() _expect({ request: { url: /\/v1\/oauth\/token$/, diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index d9c43e42f9a4e..c61003fff8b58 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -143,7 +143,7 @@ export class BackendClient extends BaseClient { token: string expiresAt: number }; - protected override projectId: string; + protected override projectId: string = ""; /** * Constructs a new ServerClient instance. @@ -199,7 +199,11 @@ export class BackendClient extends BaseClient { } private async ensureValidOauthAccessToken(): Promise { - const { client, clientAuth, as } = this.oauthClient + const { + client, + clientAuth, + as, + } = this.oauthClient let attempts = 0; const maxAttempts = 2; @@ -221,7 +225,9 @@ export class BackendClient extends BaseClient { token: oauthTokenResponse.access_token, expiresAt: Date.now() + (oauthTokenResponse.expires_in || 0) * 1000, }; - } catch {} + } catch { + // pass + } attempts++; } diff --git a/packages/sdk/src/shared/index.ts b/packages/sdk/src/shared/index.ts index 2ee4ebdf31b87..7fcf185e742d0 100644 --- a/packages/sdk/src/shared/index.ts +++ b/packages/sdk/src/shared/index.ts @@ -871,6 +871,14 @@ export abstract class BaseClient { this.workflowDomain = workflowDomain; } + /** + * Retrieves the current environment the client is configured to use. + * @returns {string} The current environment. + */ + public getEnvironment(): string { + return this.environment; + } + /** * Makes an HTTP request * diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 81b7c4b942d68..a64aa7c24e9c3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5312,8 +5312,7 @@ importers: specifier: ^1.5.1 version: 1.6.6 - components/instant: - specifiers: {} + components/instant: {} components/instantly: dependencies: