1- import {
2- BackendClient , createBackendClient , HTTPAuthType ,
3- } from "../index" ;
41import fetchMock from "jest-fetch-mock" ;
2+
53import { ClientCredentials } from "simple-oauth2" ;
4+ jest . mock ( "simple-oauth2" ) ;
5+
6+ import {
7+ BackendClient ,
8+ createBackendClient ,
9+ HTTPAuthType ,
10+ } from "../index" ;
611
712let client : BackendClient ;
813let customDomainClient : BackendClient ;
9- let oauthClientMock : ClientCredentials ;
1014const projectId = "proj_abc123" ;
1115
1216beforeEach ( ( ) => {
13- const getTokenMock = jest . fn ( ) . mockResolvedValue ( {
14- token : {
15- access_token : "mocked-oauth-token" ,
16- } ,
17- expired : jest . fn ( ) . mockReturnValue ( false ) ,
18- } ) ;
19-
20- oauthClientMock = {
21- getToken : getTokenMock ,
22- } as unknown as ClientCredentials ;
17+ // @ts -ignore
18+ ClientCredentials . mockImplementation ( ( ) => ( {
19+ getToken : jest . fn ( ) . mockResolvedValue ( {
20+ token : {
21+ access_token : "mocked-oauth-token" ,
22+ } ,
23+ expired : jest . fn ( ) . mockReturnValue ( false ) ,
24+ } ) ,
25+ } ) ) ;
2326
2427 client = new BackendClient (
2528 {
26- oauth : {
29+ credentials : {
2730 clientId : "test-client-id" ,
2831 clientSecret : "test-client-secret" ,
2932 } ,
3033 projectId,
3134 } ,
32- oauthClientMock ,
3335 ) ;
3436
3537 customDomainClient = new BackendClient ( {
36- oauth : {
38+ credentials : {
3739 clientId : "test-client-id" ,
3840 clientSecret : "test-client-secret" ,
3941 } ,
4042 projectId,
41- baseWorkflowDomain : "example.com" ,
43+ workflowDomain : "example.com" ,
4244 } ) ;
4345} ) ;
4446
@@ -48,14 +50,10 @@ afterEach(() => {
4850} ) ;
4951
5052describe ( "BackendClient" , ( ) => {
51- beforeEach ( ( ) => {
52- fetchMock . resetMocks ( ) ;
53- } ) ;
54-
5553 describe ( "createBackendClient" , ( ) => {
5654 it ( "should mock the createBackendClient method and return a BackendClient instance" , ( ) => {
5755 const params = {
58- oauth : {
56+ credentials : {
5957 clientId : "test-client-id" ,
6058 clientSecret : "test" ,
6159 } ,
@@ -175,8 +173,25 @@ describe("BackendClient", () => {
175173 } ) ;
176174
177175 it ( "should handle OAuth token retrieval failure" , async ( ) => {
178- oauthClientMock . getToken = jest . fn ( ) . mockRejectedValue ( new Error ( "Invalid credentials" ) ) ;
179- await expect ( client [ "makeAuthorizedRequest" ] ( "/test-path" ) ) . rejects . toThrow ( "Failed to obtain OAuth token: Invalid credentials" ) ;
176+ // @ts -ignore
177+ ClientCredentials . mockImplementation ( ( ) => ( {
178+ getToken : jest . fn ( ) . mockRejectedValue ( new Error ( "Invalid credentials" ) ) ,
179+ } ) ) ;
180+
181+ // Need to create a new client instance to use the new mock implementation
182+ const client = new BackendClient (
183+ {
184+ credentials : {
185+ clientId : "test-client-id" ,
186+ clientSecret : "test-client-secret" ,
187+ } ,
188+ projectId,
189+ } ,
190+ ) ;
191+
192+ await expect ( client . makeAuthorizedRequest ( "/test-path" ) ) . rejects . toThrow (
193+ "Failed to obtain OAuth token: Invalid credentials" ,
194+ ) ;
180195 } ) ;
181196 } ) ;
182197
@@ -209,7 +224,7 @@ describe("BackendClient", () => {
209224 } ) ;
210225 } ) ;
211226
212- describe ( "connectTokenCreate " , ( ) => {
227+ describe ( "createConnectToken " , ( ) => {
213228 it ( "should create a connect token" , async ( ) => {
214229 fetchMock . mockResponseOnce (
215230 JSON . stringify ( {
@@ -223,7 +238,7 @@ describe("BackendClient", () => {
223238 } ,
224239 ) ;
225240
226- const result = await client . connectTokenCreate ( {
241+ const result = await client . createConnectToken ( {
227242 external_user_id : "user-id" ,
228243 } ) ;
229244
@@ -260,7 +275,7 @@ describe("BackendClient", () => {
260275 } ,
261276 ) ;
262277
263- const result = await client . connectTokenCreate ( {
278+ const result = await client . createConnectToken ( {
264279 external_user_id : "user-id" ,
265280 success_redirect_uri : "https://example.com/success" ,
266281 error_redirect_uri : "https://example.com/error" ,
@@ -289,7 +304,7 @@ describe("BackendClient", () => {
289304 } ) ;
290305 } ) ;
291306
292- describe ( "accountsGet " , ( ) => {
307+ describe ( "getAccounts " , ( ) => {
293308 it ( "should retrieve accounts" , async ( ) => {
294309 fetchMock . mockResponseOnce (
295310 JSON . stringify ( [
@@ -305,8 +320,8 @@ describe("BackendClient", () => {
305320 } ,
306321 ) ;
307322
308- const result = await client . accountsGet ( {
309- include_credentials : 1 ,
323+ const result = await client . getAccounts ( {
324+ include_credentials : true ,
310325 } ) ;
311326
312327 expect ( result ) . toEqual ( [
@@ -316,13 +331,13 @@ describe("BackendClient", () => {
316331 } ,
317332 ] ) ;
318333 expect ( fetchMock ) . toHaveBeenCalledWith (
319- `https://api.pipedream.com/v1/connect/${ projectId } /accounts?include_credentials=1 ` ,
334+ `https://api.pipedream.com/v1/connect/${ projectId } /accounts?include_credentials=true ` ,
320335 expect . any ( Object ) ,
321336 ) ;
322337 } ) ;
323338 } ) ;
324339
325- describe ( "accountsGetById " , ( ) => {
340+ describe ( "getAccountById " , ( ) => {
326341 it ( "should retrieve a specific account by ID" , async ( ) => {
327342 fetchMock . mockResponseOnce (
328343 JSON . stringify ( {
@@ -336,7 +351,7 @@ describe("BackendClient", () => {
336351 } ,
337352 ) ;
338353
339- const result = await client . accountsGetById ( "account-1" ) ;
354+ const result = await client . getAccountById ( "account-1" ) ;
340355
341356 expect ( result ) . toEqual ( {
342357 id : "account-1" ,
@@ -365,7 +380,7 @@ describe("BackendClient", () => {
365380 } ,
366381 ) ;
367382
368- const result = await client . accountsGet ( {
383+ const result = await client . getAccounts ( {
369384 app : "app-1" ,
370385 } ) ;
371386
@@ -398,7 +413,7 @@ describe("BackendClient", () => {
398413 } ,
399414 ) ;
400415
401- const result = await client . accountsGet ( {
416+ const result = await client . getAccounts ( {
402417 external_user_id : "external-id-1" ,
403418 } ) ;
404419
@@ -421,7 +436,7 @@ describe("BackendClient", () => {
421436 status : 204 ,
422437 } ) ;
423438
424- await client . accountDelete ( "account-1" ) ;
439+ await client . deleteAccount ( "account-1" ) ;
425440
426441 expect ( fetchMock ) . toHaveBeenCalledWith (
427442 `https://api.pipedream.com/v1/connect/${ projectId } /accounts/account-1` ,
@@ -432,13 +447,13 @@ describe("BackendClient", () => {
432447 } ) ;
433448 } ) ;
434449
435- describe ( "accountsDeleteByApp " , ( ) => {
450+ describe ( "deleteAccountsByApp " , ( ) => {
436451 it ( "should delete all accounts associated with a specific app" , async ( ) => {
437452 fetchMock . mockResponseOnce ( "" , {
438453 status : 204 ,
439454 } ) ;
440455
441- await client . accountsDeleteByApp ( "app-1" ) ;
456+ await client . deleteAccountsByApp ( "app-1" ) ;
442457
443458 expect ( fetchMock ) . toHaveBeenCalledWith (
444459 `https://api.pipedream.com/v1/connect/${ projectId } /accounts/app/app-1` ,
@@ -449,13 +464,13 @@ describe("BackendClient", () => {
449464 } ) ;
450465 } ) ;
451466
452- describe ( "accountsDeleteByExternalUser " , ( ) => {
467+ describe ( "deleteExternalUser " , ( ) => {
453468 it ( "should delete all accounts associated with a specific external ID" , async ( ) => {
454469 fetchMock . mockResponseOnce ( "" , {
455470 status : 204 ,
456471 } ) ;
457472
458- await client . accountsDeleteByExternalUser ( "external-id-1" ) ;
473+ await client . deleteExternalUser ( "external-id-1" ) ;
459474
460475 expect ( fetchMock ) . toHaveBeenCalledWith (
461476 `https://api.pipedream.com/v1/connect/${ projectId } /users/external-id-1` ,
@@ -466,7 +481,7 @@ describe("BackendClient", () => {
466481 } ) ;
467482 } ) ;
468483
469- describe ( "projectGetInfo " , ( ) => {
484+ describe ( "getProjectInfo " , ( ) => {
470485 it ( "should retrieve project info" , async ( ) => {
471486 fetchMock . mockResponseOnce (
472487 JSON . stringify ( {
@@ -484,7 +499,7 @@ describe("BackendClient", () => {
484499 } ,
485500 ) ;
486501
487- const result = await client . projectGetInfo ( ) ;
502+ const result = await client . getProjectInfo ( ) ;
488503
489504 expect ( result ) . toEqual ( {
490505 apps : [
@@ -503,7 +518,7 @@ describe("BackendClient", () => {
503518 } ) ;
504519 } ) ;
505520
506- describe ( "workflowInvoke " , ( ) => {
521+ describe ( "invokeWorkflow " , ( ) => {
507522 it ( "should invoke a workflow with provided URL and body, with no auth type" , async ( ) => {
508523 fetchMock . mockResponseOnce (
509524 JSON . stringify ( {
@@ -516,7 +531,7 @@ describe("BackendClient", () => {
516531 } ,
517532 ) ;
518533
519- const result = await client . workflowInvoke ( "https://example.com/workflow" , {
534+ const result = await client . invokeWorkflow ( "https://example.com/workflow" , {
520535 body : {
521536 foo : "bar" ,
522537 } ,
@@ -552,7 +567,7 @@ describe("BackendClient", () => {
552567 } ,
553568 ) ;
554569
555- const result = await client . workflowInvoke ( "https://example.com/workflow" , { } , HTTPAuthType . OAuth ) ;
570+ const result = await client . invokeWorkflow ( "https://example.com/workflow" , { } , HTTPAuthType . OAuth ) ;
556571
557572 expect ( result ) . toEqual ( {
558573 result : "workflow-response" ,
@@ -580,7 +595,7 @@ describe("BackendClient", () => {
580595 } ,
581596 ) ;
582597
583- const result = await client . workflowInvoke ( "https://example.com/workflow" , {
598+ const result = await client . invokeWorkflow ( "https://example.com/workflow" , {
584599 headers : {
585600 "Authorization" : "Bearer static-token" ,
586601 } ,
@@ -623,19 +638,20 @@ describe("BackendClient", () => {
623638 . mockResolvedValueOnce ( expiredTokenMock )
624639 . mockResolvedValueOnce ( newTokenMock ) ;
625640
626- const oauthClientMock = {
641+ // @ts -ignore
642+ ClientCredentials . mockImplementation ( ( ) => ( {
627643 getToken : getTokenMock ,
628- } as unknown as ClientCredentials ;
644+ } ) ) ;
629645
646+ // Need to create a new client instance to use the new mock implementation
630647 const client = new BackendClient (
631648 {
632- oauth : {
649+ credentials : {
633650 clientId : "test-client-id" ,
634651 clientSecret : "test-client-secret" ,
635652 } ,
636653 projectId : "proj_abc123" ,
637654 } ,
638- oauthClientMock ,
639655 ) ;
640656
641657 fetchMock . mockResponse (
@@ -694,18 +710,14 @@ describe("BackendClient", () => {
694710 . mockResolvedValueOnce ( expiredTokenMock )
695711 . mockResolvedValueOnce ( newTokenMock ) ;
696712
697- const oauthClientMock = {
698- getToken : getTokenMock ,
699- } as unknown as ClientCredentials ;
700713 client = new BackendClient (
701714 {
702- oauth : {
715+ credentials : {
703716 clientId : "test-client-id" ,
704717 clientSecret : "test-client-secret" ,
705718 } ,
706719 projectId,
707720 } ,
708- oauthClientMock ,
709721 ) ;
710722 } ) ;
711723
@@ -726,7 +738,7 @@ describe("BackendClient", () => {
726738 } ,
727739 ) ;
728740
729- const result = await client . workflowInvokeForExternalUser ( "https://example.com/workflow" , "external-user-id" , {
741+ const result = await client . invokeWorkflowForExternalUser ( "https://example.com/workflow" , "external-user-id" , {
730742 body : {
731743 foo : "bar" ,
732744 } ,
@@ -748,7 +760,7 @@ describe("BackendClient", () => {
748760 } ) ;
749761
750762 it ( "should throw error when externalUserId is missing" , async ( ) => {
751- await expect ( client . workflowInvokeForExternalUser ( "https://example.com/workflow" , "" , {
763+ await expect ( client . invokeWorkflowForExternalUser ( "https://example.com/workflow" , "" , {
752764 body : {
753765 foo : "bar" ,
754766 } ,
0 commit comments