Skip to content

Commit 7791200

Browse files
authored
Merge pull request #96 from nestjs-shopify/migrate-to-express-package
feat(): migrate to nestjs-shopify/express
2 parents bf4e96d + b3aecf6 commit 7791200

File tree

8 files changed

+833
-431
lines changed

8 files changed

+833
-431
lines changed

apps/api/src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MikroOrmModule } from '@mikro-orm/nestjs';
22
import { ShopifyAuthModule } from '@nestjs-shopify/auth';
3-
import { ShopifyCoreModule } from '@nestjs-shopify/core';
3+
import { ShopifyExpressModule } from '@nestjs-shopify/express';
44
import { ShopifyGraphqlProxyModule } from '@nestjs-shopify/graphql';
55
import { Module } from '@nestjs/common';
66
import { ConfigModule } from '@nestjs/config';
@@ -25,7 +25,7 @@ import { WebhooksModule } from './shopify/webhooks/webhooks.module';
2525
isGlobal: true,
2626
}),
2727
MikroOrmModule.forRootAsync(databaseConfig.asProvider()),
28-
ShopifyCoreModule.forRootAsync({
28+
ShopifyExpressModule.forRootAsync({
2929
imports: [ConfigModule.forFeature(shopifyCoreConfig), SessionModule],
3030
useClass: ShopifyCoreConfigService,
3131
}),

apps/api/src/app/products/products.controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { CurrentSession, UseShopifyAuth } from '@nestjs-shopify/auth';
2-
import { SHOPIFY_API_CONTEXT } from '@nestjs-shopify/core';
3-
import { Controller, Get, Inject } from '@nestjs/common';
4-
import { Shopify } from '@shopify/shopify-api';
2+
import { InjectShopify } from '@nestjs-shopify/core';
3+
import { Controller, Get } from '@nestjs/common';
4+
import { ConfigParams, Shopify } from '@shopify/shopify-api';
55
import { restResources } from '@shopify/shopify-api/rest/admin/2023-07';
66
import { SessionEntity } from '../session/session.entity';
77

88
@UseShopifyAuth()
99
@Controller('products')
1010
export class ProductsController {
1111
constructor(
12-
@Inject(SHOPIFY_API_CONTEXT)
13-
private readonly shopifyApi: Shopify<typeof restResources>
12+
@InjectShopify()
13+
private readonly shopifyApi: Shopify<ConfigParams<typeof restResources>>
1414
) {}
1515

1616
@Get('count')

apps/web/components/ProductsCard.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useState, useCallback } from 'react';
22
import { VerticalStack, Button, LegacyCard, Text } from '@shopify/polaris';
3-
import { Toast, useAppBridge } from '@shopify/app-bridge-react';
3+
import { useAppBridge } from '@shopify/app-bridge-react';
44
import { gql, useMutation } from '@apollo/client';
55
import { userLoggedInFetch } from '../utils/userLoggedInFetch';
66

@@ -32,16 +32,19 @@ export function ProductsCard() {
3232
updateProductCount();
3333
}, [updateProductCount]);
3434

35-
const toastMarkup = hasResults && (
36-
<Toast
37-
content="1 product created!"
38-
onDismiss={() => setHasResults(false)}
39-
/>
40-
);
35+
const showToast = useCallback(() => {
36+
if (hasResults) {
37+
app.toast.show('1 product created!', {
38+
onDismiss() {
39+
setHasResults(false);
40+
},
41+
});
42+
}
43+
}, [app, hasResults, setHasResults]);
4144

4245
return (
4346
<>
44-
{toastMarkup}
47+
{showToast()}
4548
<LegacyCard title="Product Counter" sectioned>
4649
<VerticalStack gap="4">
4750
<p>

apps/web/pages/_app.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import {
44
HttpLink,
55
InMemoryCache,
66
} from '@apollo/client';
7-
import {
8-
Provider as AppBridgeProvider,
9-
useAppBridge,
10-
} from '@shopify/app-bridge-react';
7+
import { useAppBridge } from '@shopify/app-bridge-react';
118
import { AppProvider as PolarisProvider } from '@shopify/polaris';
129
import translations from '@shopify/polaris/locales/en.json';
1310
import '@shopify/polaris/build/esm/styles.css';
@@ -42,15 +39,7 @@ class MyApp extends App {
4239

4340
return (
4441
<PolarisProvider i18n={translations}>
45-
<AppBridgeProvider
46-
config={{
47-
apiKey: process.env.SHOPIFY_API_KEY,
48-
host,
49-
forceRedirect: true,
50-
}}
51-
>
52-
<MyProvider Component={Component} host={host} {...pageProps} />
53-
</AppBridgeProvider>
42+
<MyProvider Component={Component} host={host} {...pageProps} />
5443
</PolarisProvider>
5544
);
5645
}

apps/web/pages/_document.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Html, Head, Main, NextScript } from 'next/document';
2+
3+
export default function Document() {
4+
return (
5+
<Html>
6+
<Head>
7+
<meta name="shopify-api-key" content={process.env.SHOPIFY_API_KEY} />
8+
<script src="https://cdn.shopify.com/shopifycloud/app-bridge.js" />
9+
</Head>
10+
<body>
11+
<Main />
12+
<NextScript />
13+
</body>
14+
</Html>
15+
);
16+
}

apps/web/utils/userLoggedInFetch.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { authenticatedFetch } from '@shopify/app-bridge-utils';
21
import { Redirect } from '@shopify/app-bridge/actions';
32

43
export function userLoggedInFetch(app) {
5-
const fetchFunction = authenticatedFetch(app);
6-
74
return async (uri, options?: RequestInit) => {
8-
const response = await fetchFunction(uri, options);
5+
const response = await fetch(uri, options);
96

107
if (
118
response.headers.get('X-Shopify-API-Request-Failure-Reauthorize') === '1'

0 commit comments

Comments
 (0)