Skip to content

Commit 745e3b4

Browse files
committed
add optional scope param to backendClient
1 parent 7c978a8 commit 745e3b4

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
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.7.0] - 2025-07-03
6+
7+
### Added
8+
9+
- Added optional scope parameter to backendClient creation.
10+
511
## [1.6.9] - 2025-06-10
612

713
### Added

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@pipedream/sdk",
33
"type": "module",
4-
"version": "1.6.9",
4+
"version": "1.7.0",
55
"description": "Pipedream SDK",
66
"main": "./dist/server.js",
77
"module": "./dist/server.js",

packages/sdk/src/server/index.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -298,68 +298,68 @@ export class BackendClient extends BaseClient {
298298
as,
299299
} = this.oauthClient
300300

301-
let attempts = 0;
302301
const maxAttempts = 2;
303302

304303
while (!this.oauthAccessToken || this.oauthAccessToken.expiresAt - Date.now() < 1000) {
305-
if (attempts > maxAttempts) {
306-
throw new Error("ran out of attempts trying to retrieve oauth access token");
307-
}
308-
if (attempts > 0) {
309-
// Wait for a short duration before retrying to avoid rapid retries
310-
await new Promise((resolve) => setTimeout(resolve, 100));
311-
}
304+
for (let attempts = 0; attempts <= maxAttempts; attempts++) {
305+
if (attempts > 0) {
306+
// Wait for a short duration before retrying to avoid rapid retries
307+
await new Promise((resolve) => setTimeout(resolve, 100));
308+
}
312309

313-
const parameters = new URLSearchParams();
314-
if (this.scope && this.scope.length > 0) {
315-
parameters.set("scope", this.scope.join(" "));
316-
}
317-
try {
318-
const response = await oauth.clientCredentialsGrantRequest(as, client, clientAuth, parameters);
319-
const oauthTokenResponse = await oauth.processClientCredentialsResponse(as, client, response);
320-
this.oauthAccessToken = {
321-
token: oauthTokenResponse.access_token,
322-
expiresAt: Date.now() + (oauthTokenResponse.expires_in || 0) * 1000,
323-
};
324-
} catch (e) {
325-
// Extract error details from OAuth response
326-
let errorMessage = "OAuth token request failed";
327-
let wwwAuthenticate: string | undefined;
328-
let statusCode: number | undefined;
329-
330-
if (e instanceof Error) {
331-
errorMessage = e.message;
310+
const parameters = new URLSearchParams();
311+
if (this.scope && this.scope.length > 0) {
312+
parameters.set("scope", this.scope.join(" "));
332313
}
314+
parameters.set("project_id", this.projectId);
315+
parameters.set("environment", this.environment);
333316

334-
// Check if the error contains response information
335-
if (e && typeof e === 'object' && 'response' in e) {
336-
const errorResponse = (e as any).response;
337-
if (errorResponse) {
338-
statusCode = errorResponse.status;
339-
wwwAuthenticate = errorResponse.headers?.get?.('www-authenticate') ||
340-
errorResponse.headers?.['www-authenticate'];
341-
342-
// Create more specific error message based on status code
343-
if (statusCode === 401) {
344-
errorMessage = `OAuth authentication failed (401 Unauthorized)${wwwAuthenticate ? `: ${wwwAuthenticate}` : ''}`;
345-
} else if (statusCode === 400) {
346-
errorMessage = "OAuth request invalid (400 Bad Request) - check client credentials";
347-
} else if (statusCode) {
348-
errorMessage = `OAuth request failed with status ${statusCode}`;
317+
try {
318+
const response = await oauth.clientCredentialsGrantRequest(as, client, clientAuth, parameters);
319+
const oauthTokenResponse = await oauth.processClientCredentialsResponse(as, client, response);
320+
this.oauthAccessToken = {
321+
token: oauthTokenResponse.access_token,
322+
expiresAt: Date.now() + (oauthTokenResponse.expires_in || 0) * 1000,
323+
};
324+
break; // Successfully got token, exit retry loop
325+
} catch (e) {
326+
// Extract error details from OAuth response
327+
let errorMessage = "OAuth token request failed";
328+
let wwwAuthenticate: string | undefined;
329+
let statusCode: number | undefined;
330+
331+
if (e instanceof Error) {
332+
errorMessage = e.message;
333+
}
334+
335+
// Check if the error contains response information
336+
if (e && typeof e === 'object' && 'response' in e) {
337+
const errorResponse = (e as any).response;
338+
if (errorResponse) {
339+
statusCode = errorResponse.status;
340+
wwwAuthenticate = errorResponse.headers?.get?.('www-authenticate') ||
341+
errorResponse.headers?.['www-authenticate'];
342+
343+
// Create more specific error message based on status code
344+
if (statusCode === 401) {
345+
errorMessage = `OAuth authentication failed (401 Unauthorized)${wwwAuthenticate ? `: ${wwwAuthenticate}` : ''}`;
346+
} else if (statusCode === 400) {
347+
errorMessage = "OAuth request invalid (400 Bad Request) - check client credentials";
348+
} else if (statusCode) {
349+
errorMessage = `OAuth request failed with status ${statusCode}`;
350+
}
349351
}
350352
}
351-
}
352-
353-
// If this is the last attempt, throw a detailed error
354-
if (attempts >= maxAttempts) {
355-
const error = new Error(errorMessage);
356-
(error as any).statusCode = statusCode;
357-
(error as any).wwwAuthenticate = wwwAuthenticate;
358-
throw error;
353+
354+
// If this is the last attempt, throw a detailed error
355+
if (attempts >= maxAttempts) {
356+
const error = new Error(errorMessage);
357+
(error as any).statusCode = statusCode;
358+
(error as any).wwwAuthenticate = wwwAuthenticate;
359+
throw error;
360+
}
359361
}
360362
}
361-
362-
attempts++;
363363
}
364364

365365
return this.oauthAccessToken.token;

0 commit comments

Comments
 (0)