Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

# Changelog

## [1.3.2] - 2025-02-3

### Changed

- Add getEnvironment function to BaseClient

## [1.3.1] - 2025-01-30

### Changed
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export default {
preset: "ts-jest",
testEnvironment: "node",
roots: [
"<rootDir>/src",
Expand All @@ -15,13 +14,17 @@ 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`
"^.+\\.[jt]sx?$": [
"ts-jest",
{
tsconfig: "tsconfig.node.json",
useESM: true,
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
103 changes: 70 additions & 33 deletions packages/sdk/src/server/__tests__/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jest } from "@jest/globals"
import {
BackendClient,
BackendClientOpts,
Expand Down Expand Up @@ -50,7 +51,9 @@
url: "https://api.pipedream.com/v1/test-path",
},
response: {
json: { data: "test-response" },
json: {
data: "test-response",
},
},
})

Expand All @@ -72,7 +75,9 @@
},
},
response: {
json: { success: true },
json: {
success: true,
},
},
})

Expand Down Expand Up @@ -116,7 +121,9 @@
},
},
response: {
json: { success: true },
json: {
success: true,
},
},
})

Expand Down Expand Up @@ -144,7 +151,9 @@
},
},
response: {
json: { success: true },
json: {
success: true,
},
},
})

Expand Down Expand Up @@ -311,10 +320,12 @@
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",
},
],
},
});

Expand All @@ -339,10 +350,12 @@
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",
},
],
},
});

Expand All @@ -360,7 +373,7 @@
});

describe("deleteAccount", () => {
it("should delete a specific account by ID", async () => {

Check warning on line 376 in packages/sdk/src/server/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / Lint Code Base

Test has no assertions
fetchMock.expectAccessTokenSuccess();
fetchMock.expect({
request: {
Expand All @@ -377,7 +390,7 @@
});

describe("deleteAccountsByApp", () => {
it("should delete all accounts associated with a specific app", async () => {

Check warning on line 393 in packages/sdk/src/server/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / Lint Code Base

Test has no assertions
fetchMock.expectAccessTokenSuccess();
fetchMock.expect({
request: {
Expand All @@ -394,7 +407,7 @@
});

describe("deleteExternalUser", () => {
it("should delete all accounts associated with a specific external ID", async () => {

Check warning on line 410 in packages/sdk/src/server/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / Lint Code Base

Test has no assertions
fetchMock.expectAccessTokenSuccess();
fetchMock.expect({
request: {
Expand Down Expand Up @@ -464,7 +477,9 @@
},
},
response: {
json: { result: "workflow-response" },
json: {
result: "workflow-response",
},
},
})

Expand All @@ -480,8 +495,10 @@
});

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",
Expand All @@ -490,7 +507,9 @@
},
},
response: {
json: { result: "workflow-response" },
json: {
result: "workflow-response",
},
},
})

Expand All @@ -502,7 +521,7 @@
});

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",
Expand All @@ -511,7 +530,9 @@
},
},
response: {
json: { result: "workflow-response" },
json: {
result: "workflow-response",
},
},
})

Expand All @@ -537,7 +558,9 @@
url: "https://api.pipedream.com/v1/test-path",
},
response: {
json: { success: true },
json: {
success: true,
},
},
})

Expand Down Expand Up @@ -570,7 +593,9 @@
},
},
response: {
json: { result: "workflow-response" },
json: {
result: "workflow-response",
},
},
})

Expand Down Expand Up @@ -696,17 +721,17 @@
type ExpectRequest = {
method?: string
url?: string | RegExp
json?: Record<string, any>
json?: Record<string, unknown>
headersContaining?: Record<string, string>
}
type MockResponse =
| Response
| { status?: number; json?: any }
| { status?: number; json?: unknown }
type IfOpts = {
method: string
url: string
headers: Record<string, string> // NonNullable<RequestInit["headers"]>
json?: any // body json
json?: unknown // body json
// XXX etc.
}
function setupFetchMock() {
Expand All @@ -715,7 +740,7 @@
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: {
Expand All @@ -726,17 +751,27 @@

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<any, any, any>(global, "fetch").mockImplementation(jest.fn<typeof fetch>(async (...args: Parameters<typeof fetch>) => { // 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<string, string> || {},
method: init?.method || "GET",
url: url.toString(),
headers: init?.headers as Record<string, string> || {},
json,
}
for (let i = 0; i < intercepts.length; i++) {
Expand All @@ -758,7 +793,9 @@

// 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
Expand Down Expand Up @@ -800,7 +837,7 @@
}

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$/,
Expand Down
12 changes: 9 additions & 3 deletions packages/sdk/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class BackendClient extends BaseClient {
token: string
expiresAt: number
};
protected override projectId: string;
protected override projectId: string = "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Reconsider adding a default empty string value for projectId.

The default empty string value for projectId seems unnecessary and potentially problematic because:

  1. projectId is a required field in BackendClientOpts (line 44)
  2. The constructor always sets it from opts.projectId (line 158)
  3. An empty projectId might cause issues in API requests

Remove the default value since it's not needed:

-  protected override projectId: string = "";
+  protected override projectId: string;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
protected override projectId: string = "";
protected override projectId: string;


/**
* Constructs a new ServerClient instance.
Expand Down Expand Up @@ -199,7 +199,11 @@ export class BackendClient extends BaseClient {
}

private async ensureValidOauthAccessToken(): Promise<string> {
const { client, clientAuth, as } = this.oauthClient
const {
client,
clientAuth,
as,
} = this.oauthClient

let attempts = 0;
const maxAttempts = 2;
Expand All @@ -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++;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/sdk/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
3 changes: 1 addition & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading