@@ -10,6 +10,35 @@ const fapiClient = createFapiClient({
1010 } ,
1111} as Clerk ) ;
1212
13+ const proxyUrl = 'example.com/__clerk' ;
14+
15+ const fapiClientWithProxy = createFapiClient ( {
16+ frontendApi : 'clerk.example.com' ,
17+ proxyUrl : `example.com` ,
18+ version : '42.0.0' ,
19+ session : {
20+ id : 'deadbeef' ,
21+ } ,
22+ } as Clerk ) ;
23+
24+ const fapiClientWithProxyPath = createFapiClient ( {
25+ frontendApi : 'clerk.example.com' ,
26+ proxyUrl,
27+ version : '42.0.0' ,
28+ session : {
29+ id : 'deadbeef' ,
30+ } ,
31+ } as Clerk ) ;
32+
33+ const fapiClientWithProxyPath2 = createFapiClient ( {
34+ frontendApi : 'clerk.example.com' ,
35+ proxyUrl : 'example.com/api/__clerk' ,
36+ version : '42.0.0' ,
37+ session : {
38+ id : 'deadbeef' ,
39+ } ,
40+ } as Clerk ) ;
41+
1342type RecursivePartial < T > = {
1443 [ P in keyof T ] ?: RecursivePartial < T [ P ] > ;
1544} ;
@@ -68,6 +97,23 @@ describe('buildUrl(options)', () => {
6897 ) ;
6998 } ) ;
7099
100+ it ( 'returns the full frontend API URL using proxy url' , ( ) => {
101+ expect ( fapiClientWithProxy . buildUrl ( { path : '/foo' } ) . href ) . toBe (
102+ `https://example.com/v1/foo?_clerk_js_version=42.0.0` ,
103+ ) ;
104+ } ) ;
105+
106+ it ( 'returns the full frontend API URL using proxy url path' , ( ) => {
107+ expect ( fapiClientWithProxyPath . buildUrl ( { path : '/foo' } ) . href ) . toBe (
108+ `https://${ proxyUrl } /v1/foo?_clerk_js_version=42.0.0` ,
109+ ) ;
110+ } ) ;
111+ it ( 'returns the full frontend API URL using proxy url path 2' , ( ) => {
112+ expect ( fapiClientWithProxyPath2 . buildUrl ( { path : '/foo' } ) . href ) . toBe (
113+ `https://example.com/api/__clerk/v1/foo?_clerk_js_version=42.0.0` ,
114+ ) ;
115+ } ) ;
116+
71117 it ( 'adds _clerk_session_id as a query parameter if provided and path does not start with client' , ( ) => {
72118 expect ( fapiClient . buildUrl ( { path : '/foo' , sessionId : 'sess_42' } ) . href ) . toBe (
73119 'https://clerk.example.com/v1/foo?_clerk_js_version=42.0.0&_clerk_session_id=sess_42' ,
@@ -100,8 +146,6 @@ describe('buildUrl(options)', () => {
100146
101147describe ( 'request' , ( ) => {
102148 it ( 'invokes global.fetch' , async ( ) => {
103- window . location . href = 'http://clerk.example.com' ;
104-
105149 await fapiClient . request ( {
106150 path : '/foo' ,
107151 } ) ;
@@ -116,6 +160,21 @@ describe('request', () => {
116160 ) ;
117161 } ) ;
118162
163+ it ( 'invokes global.fetch with proxy' , async ( ) => {
164+ await fapiClientWithProxy . request ( {
165+ path : '/foo' ,
166+ } ) ;
167+
168+ expect ( fetch ) . toHaveBeenCalledWith (
169+ 'https://example.com/v1/foo?_clerk_js_version=42.0.0&_clerk_session_id=deadbeef' ,
170+ expect . objectContaining ( {
171+ credentials : 'include' ,
172+ method : 'GET' ,
173+ path : '/foo' ,
174+ } ) ,
175+ ) ;
176+ } ) ;
177+
119178 describe ( 'for production instances' , ( ) => {
120179 it . todo ( 'does not append the __dev_session cookie value to the query string' ) ;
121180 it . todo ( 'does not set the __dev_session cookie from the response Clerk-Cookie header' ) ;
0 commit comments