@@ -13,86 +13,82 @@ type ShopifyFetchData<
1313 M extends RouterMethod
1414> = [ T ] extends [ undefined ] ? TypedInternalResponse < R , unknown , M > : T
1515
16- interface ShopifyFetchFunction {
17- <
18- T = undefined ,
19- R extends NitroFetchRequest = NitroFetchRequest ,
20- M extends AvailableRouterMethod < R > = 'get' extends AvailableRouterMethod < R >
21- ? 'get'
22- : AvailableRouterMethod < R >
23- > (
24- url : R ,
25- options ?: Omit < RequestInit , 'method' > & { method ?: Uppercase < M > | M }
26- ) : Promise < {
27- data : ShopifyFetchData < T , R , Extract < Lowercase < M > , RouterMethod > >
28- response : Response
29- } >
30- }
16+ export async function useShopifyFetch <
17+ T = undefined ,
18+ R extends NitroFetchRequest = NitroFetchRequest ,
19+ M extends AvailableRouterMethod < R > = 'get' extends AvailableRouterMethod < R >
20+ ? 'get'
21+ : AvailableRouterMethod < R >
22+ > (
23+ url : R ,
24+ options ?: Omit < RequestInit , 'method' > & { method ?: Uppercase < M > | M }
25+ ) : Promise < {
26+ data : ShopifyFetchData < T , R , Extract < Lowercase < M > , RouterMethod > >
27+ response : Response
28+ } >
29+ export async function useShopifyFetch (
30+ url : NitroFetchRequest ,
31+ options ?: RequestInit & { method ?: RouterMethod | Uppercase < RouterMethod > }
32+ ) : Promise < { data : unknown ; response : Response } > {
33+ const opts = options ?? { }
3134
32- export function useShopifyFetch ( ) : ShopifyFetchFunction {
3335 if ( import . meta. server ) {
3436 const event = useRequestEvent ( )
37+ const headers : Record < string , string > = { }
3538
36- return ( async ( url : string , options : RequestInit = { } ) => {
37- const headers : Record < string , string > = { }
38-
39- // Forward the Authorization header from the incoming request
40- const authHeader = event ?. headers . get ( 'authorization' )
41- if ( authHeader ) {
42- headers [ 'Authorization' ] = authHeader
43- }
39+ // Forward the Authorization header from the incoming request
40+ const authHeader = event ?. headers . get ( 'authorization' )
41+ if ( authHeader ) {
42+ headers [ 'Authorization' ] = authHeader
43+ }
4444
45- if ( options . headers ) {
46- const incoming = new Headers ( options . headers )
47- incoming . forEach ( ( value , key ) => {
48- headers [ key ] = value
49- } )
50- }
45+ if ( opts . headers ) {
46+ const incoming = new Headers ( opts . headers )
47+ incoming . forEach ( ( value , key ) => {
48+ headers [ key ] = value
49+ } )
50+ }
5151
52- const { method, ...rest } = options as RequestInit & { method ?: string }
52+ const { method, ...rest } = opts
5353
54- const response = await globalThis . $fetch . raw ( url , {
55- ...rest ,
56- method : method as any ,
57- headers
58- } )
54+ const fetchResponse = await globalThis . $fetch . raw ( url , {
55+ ...rest ,
56+ method,
57+ headers
58+ } )
5959
60- return { data : response . _data , response : response as unknown as Response }
61- } ) as unknown as ShopifyFetchFunction
60+ return { data : fetchResponse . _data , response : fetchResponse }
6261 }
6362
6463 const nuxtApp = useNuxtApp ( )
64+ const shopify = nuxtApp . $shopify as ShopifyGlobal | undefined
6565
66- return ( async ( url : string , options : RequestInit = { } ) => {
67- const shopify = nuxtApp . $shopify as ShopifyGlobal | undefined
68-
69- if ( ! shopify ) {
70- throw new Error (
71- 'Shopify App Bridge is not available. Make sure the app is loaded within the Shopify Admin.'
72- )
73- }
66+ if ( ! shopify ) {
67+ throw new Error (
68+ 'Shopify App Bridge is not available. Make sure the app is loaded within the Shopify Admin.'
69+ )
70+ }
7471
75- const token = await shopify . idToken ( )
72+ const token = await shopify . idToken ( )
7673
77- const headers = new Headers ( options . headers || { } )
78- headers . set ( 'Authorization' , `Bearer ${ token } ` )
74+ const headers = new Headers ( opts . headers || { } )
75+ headers . set ( 'Authorization' , `Bearer ${ token } ` )
7976
80- const response = await fetch ( url , {
81- ...options ,
82- headers
83- } )
77+ const fetchResponse = await fetch ( url as RequestInfo , {
78+ ...opts ,
79+ headers
80+ } )
8481
85- if ( ! response . ok ) {
86- throw new Error (
87- `Shopify fetch failed: ${ response . status } ${ response . statusText } `
88- )
89- }
82+ if ( ! fetchResponse . ok ) {
83+ throw new Error (
84+ `Shopify fetch failed: ${ fetchResponse . status } ${ fetchResponse . statusText } `
85+ )
86+ }
9087
91- const contentType = response . headers . get ( 'content-type' )
92- if ( contentType ?. includes ( 'application/json' ) ) {
93- return { data : await response . json ( ) , response }
94- }
88+ const contentType = fetchResponse . headers . get ( 'content-type' )
89+ if ( contentType ?. includes ( 'application/json' ) ) {
90+ return { data : await fetchResponse . json ( ) , response : fetchResponse }
91+ }
9592
96- return { data : await response . text ( ) , response }
97- } ) as unknown as ShopifyFetchFunction
93+ return { data : await fetchResponse . text ( ) , response : fetchResponse }
9894}
0 commit comments