shopify-nuxt 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -6
- package/dist/module.json +1 -1
- package/dist/runtime/admin-api-client.d.ts +1 -0
- package/dist/runtime/admin-api-client.js +1 -0
- package/dist/runtime/composables/useShopifyFetch.d.ts +6 -9
- package/dist/runtime/composables/useShopifyFetch.js +40 -43
- package/dist/runtime/shopify-api.d.ts +1 -0
- package/dist/runtime/shopify-api.js +1 -0
- package/dist/runtime/shopify-app-session-storage-memory.d.ts +1 -0
- package/dist/runtime/shopify-app-session-storage-memory.js +1 -0
- package/dist/runtime/shopify-app-session-storage.d.ts +1 -0
- package/dist/runtime/shopify-app-session-storage.js +1 -0
- package/dist/runtime/storefront-api-client.d.ts +1 -0
- package/dist/runtime/storefront-api-client.js +1 -0
- package/package.json +37 -2
package/README.md
CHANGED
|
@@ -283,21 +283,36 @@ const shop = shopify.config.shop
|
|
|
283
283
|
|
|
284
284
|
### Authenticated fetch
|
|
285
285
|
|
|
286
|
-
Use `useShopifyFetch()` for
|
|
286
|
+
Use `useShopifyFetch()` for API calls that automatically include the Shopify session token. It works on both client and server — on the client it fetches a session token via App Bridge, on the server it forwards the incoming `Authorization` header.
|
|
287
287
|
|
|
288
288
|
```vue
|
|
289
289
|
<script setup>
|
|
290
|
-
const
|
|
290
|
+
const { data: shop } = await useAsyncData(
|
|
291
|
+
'shop',
|
|
292
|
+
() => useShopifyFetch('/api/shop'),
|
|
293
|
+
{ server: false }
|
|
294
|
+
)
|
|
295
|
+
</script>
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
With generic types for full type safety:
|
|
299
|
+
|
|
300
|
+
```vue
|
|
301
|
+
<script setup lang="ts">
|
|
302
|
+
interface ShopData {
|
|
303
|
+
shop: { name: string; currencyCode: string }
|
|
304
|
+
}
|
|
291
305
|
|
|
292
|
-
const { data
|
|
293
|
-
'
|
|
294
|
-
() =>
|
|
306
|
+
const { data } = await useAsyncData(
|
|
307
|
+
'shop',
|
|
308
|
+
() => useShopifyFetch<ShopData>('/api/shop'),
|
|
295
309
|
{ server: false }
|
|
296
310
|
)
|
|
311
|
+
// data.value.data.shop.name is fully typed
|
|
297
312
|
</script>
|
|
298
313
|
```
|
|
299
314
|
|
|
300
|
-
> **Important**: Always use `server: false` with `useShopifyFetch()` — session tokens are only available on the client side within the Shopify admin iframe.
|
|
315
|
+
> **Important**: Always use `server: false` with `useShopifyFetch()` in `useAsyncData` — session tokens are only available on the client side within the Shopify admin iframe.
|
|
301
316
|
|
|
302
317
|
## Using Polaris components
|
|
303
318
|
|
package/dist/module.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@shopify/admin-api-client';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@shopify/admin-api-client";
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import type { NitroFetchRequest, TypedInternalResponse, AvailableRouterMethod } from 'nitropack/types';
|
|
2
2
|
import type { RouterMethod } from 'h3';
|
|
3
3
|
type ShopifyFetchData<T, R extends NitroFetchRequest, M extends RouterMethod> = [T] extends [undefined] ? TypedInternalResponse<R, unknown, M> : T;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}>;
|
|
11
|
-
}
|
|
12
|
-
export declare function useShopifyFetch(): ShopifyFetchFunction;
|
|
4
|
+
export declare function useShopifyFetch<T = undefined, R extends NitroFetchRequest = NitroFetchRequest, M extends AvailableRouterMethod<R> = 'get' extends AvailableRouterMethod<R> ? 'get' : AvailableRouterMethod<R>>(url: R, options?: Omit<RequestInit, 'method'> & {
|
|
5
|
+
method?: Uppercase<M> | M;
|
|
6
|
+
}): Promise<{
|
|
7
|
+
data: ShopifyFetchData<T, R, Extract<Lowercase<M>, RouterMethod>>;
|
|
8
|
+
response: Response;
|
|
9
|
+
}>;
|
|
13
10
|
export {};
|
|
@@ -1,52 +1,49 @@
|
|
|
1
1
|
import { useNuxtApp, useRequestEvent } from "#app";
|
|
2
|
-
export function useShopifyFetch() {
|
|
2
|
+
export async function useShopifyFetch(url, options) {
|
|
3
|
+
const opts = options ?? {};
|
|
3
4
|
if (import.meta.server) {
|
|
4
5
|
const event = useRequestEvent();
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
headers[key] = value;
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
const { method, ...rest } = options;
|
|
18
|
-
const response = await globalThis.$fetch.raw(url, {
|
|
19
|
-
...rest,
|
|
20
|
-
method,
|
|
21
|
-
headers
|
|
6
|
+
const headers2 = {};
|
|
7
|
+
const authHeader = event?.headers.get("authorization");
|
|
8
|
+
if (authHeader) {
|
|
9
|
+
headers2["Authorization"] = authHeader;
|
|
10
|
+
}
|
|
11
|
+
if (opts.headers) {
|
|
12
|
+
const incoming = new Headers(opts.headers);
|
|
13
|
+
incoming.forEach((value, key) => {
|
|
14
|
+
headers2[key] = value;
|
|
22
15
|
});
|
|
23
|
-
|
|
16
|
+
}
|
|
17
|
+
const { method, ...rest } = opts;
|
|
18
|
+
const fetchResponse2 = await globalThis.$fetch.raw(url, {
|
|
19
|
+
...rest,
|
|
20
|
+
method,
|
|
21
|
+
headers: headers2
|
|
24
22
|
});
|
|
23
|
+
return { data: fetchResponse2._data, response: fetchResponse2 };
|
|
25
24
|
}
|
|
26
25
|
const nuxtApp = useNuxtApp();
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
headers
|
|
40
|
-
});
|
|
41
|
-
if (!response.ok) {
|
|
42
|
-
throw new Error(
|
|
43
|
-
`Shopify fetch failed: ${response.status} ${response.statusText}`
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
const contentType = response.headers.get("content-type");
|
|
47
|
-
if (contentType?.includes("application/json")) {
|
|
48
|
-
return { data: await response.json(), response };
|
|
49
|
-
}
|
|
50
|
-
return { data: await response.text(), response };
|
|
26
|
+
const shopify = nuxtApp.$shopify;
|
|
27
|
+
if (!shopify) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
"Shopify App Bridge is not available. Make sure the app is loaded within the Shopify Admin."
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
const token = await shopify.idToken();
|
|
33
|
+
const headers = new Headers(opts.headers || {});
|
|
34
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
35
|
+
const fetchResponse = await fetch(url, {
|
|
36
|
+
...opts,
|
|
37
|
+
headers
|
|
51
38
|
});
|
|
39
|
+
if (!fetchResponse.ok) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`Shopify fetch failed: ${fetchResponse.status} ${fetchResponse.statusText}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
const contentType = fetchResponse.headers.get("content-type");
|
|
45
|
+
if (contentType?.includes("application/json")) {
|
|
46
|
+
return { data: await fetchResponse.json(), response: fetchResponse };
|
|
47
|
+
}
|
|
48
|
+
return { data: await fetchResponse.text(), response: fetchResponse };
|
|
52
49
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@shopify/shopify-api';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@shopify/shopify-api";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@shopify/shopify-app-session-storage-memory';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@shopify/shopify-app-session-storage-memory";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@shopify/shopify-app-session-storage';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@shopify/shopify-app-session-storage";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@shopify/storefront-api-client';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@shopify/storefront-api-client";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shopify-nuxt",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "Shopify app integration for Nuxt - authentication, webhooks, billing, and App Bridge",
|
|
5
5
|
"repository": "kiriminaja/shopify-nuxt",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,6 +15,26 @@
|
|
|
15
15
|
"./test-helpers": {
|
|
16
16
|
"types": "./dist/runtime/server/test-helpers/index.d.ts",
|
|
17
17
|
"import": "./dist/runtime/server/test-helpers/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./shopify-api": {
|
|
20
|
+
"types": "./dist/runtime/shopify-api.d.ts",
|
|
21
|
+
"import": "./dist/runtime/shopify-api.js"
|
|
22
|
+
},
|
|
23
|
+
"./shopify-app-session-storage": {
|
|
24
|
+
"types": "./dist/runtime/shopify-app-session-storage.d.ts",
|
|
25
|
+
"import": "./dist/runtime/shopify-app-session-storage.js"
|
|
26
|
+
},
|
|
27
|
+
"./shopify-app-session-storage-memory": {
|
|
28
|
+
"types": "./dist/runtime/shopify-app-session-storage-memory.d.ts",
|
|
29
|
+
"import": "./dist/runtime/shopify-app-session-storage-memory.js"
|
|
30
|
+
},
|
|
31
|
+
"./admin-api-client": {
|
|
32
|
+
"types": "./dist/runtime/admin-api-client.d.ts",
|
|
33
|
+
"import": "./dist/runtime/admin-api-client.js"
|
|
34
|
+
},
|
|
35
|
+
"./storefront-api-client": {
|
|
36
|
+
"types": "./dist/runtime/storefront-api-client.d.ts",
|
|
37
|
+
"import": "./dist/runtime/storefront-api-client.js"
|
|
18
38
|
}
|
|
19
39
|
},
|
|
20
40
|
"main": "./dist/module.mjs",
|
|
@@ -23,6 +43,21 @@
|
|
|
23
43
|
"*": {
|
|
24
44
|
".": [
|
|
25
45
|
"./dist/types.d.mts"
|
|
46
|
+
],
|
|
47
|
+
"shopify-api": [
|
|
48
|
+
"./dist/runtime/shopify-api.d.ts"
|
|
49
|
+
],
|
|
50
|
+
"shopify-app-session-storage": [
|
|
51
|
+
"./dist/runtime/shopify-app-session-storage.d.ts"
|
|
52
|
+
],
|
|
53
|
+
"shopify-app-session-storage-memory": [
|
|
54
|
+
"./dist/runtime/shopify-app-session-storage-memory.d.ts"
|
|
55
|
+
],
|
|
56
|
+
"admin-api-client": [
|
|
57
|
+
"./dist/runtime/admin-api-client.d.ts"
|
|
58
|
+
],
|
|
59
|
+
"storefront-api-client": [
|
|
60
|
+
"./dist/runtime/storefront-api-client.d.ts"
|
|
26
61
|
]
|
|
27
62
|
}
|
|
28
63
|
},
|
|
@@ -53,7 +88,7 @@
|
|
|
53
88
|
"@shopify/shopify-app-session-storage": "^5.0.0",
|
|
54
89
|
"@shopify/shopify-app-session-storage-memory": "^6.0.0",
|
|
55
90
|
"@shopify/storefront-api-client": "^1.0.10",
|
|
56
|
-
"h3": "^
|
|
91
|
+
"h3": "^2.0.0",
|
|
57
92
|
"isbot": "^5.1.36"
|
|
58
93
|
},
|
|
59
94
|
"devDependencies": {
|