Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/small-moose-dig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@shopify/shopify-api': patch
---

Add Web API and CF Worker adapter intialized constants for aggressive tree-shakers

For example with the web-api adapter:

```ts
// Instead of just:
import '@shopify/shopify-api/adapters/web-api';

// You can now also import:
import { webApiAdapterInitialized } from '@shopify/shopify-api/adapters/web-api';
import { shopifyApi } from '@shopify/shopify-api';

// And check the adapter is initialized, which forces bundlers to keep the import
if (!webApiAdapterInitialized) {
throw new Error('Failed to initialize web API adapter');
}
```
32 changes: 27 additions & 5 deletions packages/apps/shopify-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,53 @@ The first thing you need to import is the adapter for your app's runtime. This w
import '@shopify/shopify-api/adapters/node';
```

> **Note** Some bundlers may aggressively tree-shake the adapter imports in production builds. If you encounter a "Missing adapter implementation" error, you can use the exported constant to ensure the adapter is loaded:
>
> ```ts
> import { nodeAdapterInitialized } from '@shopify/shopify-api/adapters/node';
> // Optional: Check the adapter loaded successfully
> if (!nodeAdapterInitialized) {
> throw new Error('Failed to initialize Node.js adapter');
> }
> ```

</div><div>CloudFlare Worker

```ts
import '@shopify/shopify-api/adapters/cf-worker';
```

> **Note** Some bundlers may aggressively tree-shake the adapter imports in production builds. If you encounter a "Missing adapter implementation" error, you can use the exported constant to ensure the adapter is loaded:
>
> ```ts
> import { cfWorkerAdapterInitialized } from '@shopify/shopify-api/adapters/cf-worker';
> // Optional: Check the adapter loaded successfully
> if (!cfWorkerAdapterInitialized) {
> throw new Error('Failed to initialize Cloudflare Worker adapter');
> }
> ```

</div>
</div><div>Generic runtimes that implement the <a href="https://developer.mozilla.org/en-US/docs/Web/API">Web API</a>

```ts
import '@shopify/shopify-api/adapters/web-api';
```

</div>

> **Note** Some bundlers may aggressively tree-shake the adapter imports in production builds. If you encounter a "Missing adapter implementation" error, you can use the exported constant to ensure the adapter is loaded:
>
> ```ts
> import { nodeAdapterInitialized } from '@shopify/shopify-api/adapters/node';
> import { webApiAdapterInitialized } from '@shopify/shopify-api/adapters/web-api';
> // Optional: Check the adapter loaded successfully
> if (!nodeAdapterInitialized) {
> throw new Error('Failed to initialize Node.js adapter');
> if (!webApiAdapterInitialized) {
> throw new Error('Failed to initialize web API adapter');
> }
> ```

</div>



Next, configure the library - you'll need some values in advance:

- Your app's API key from [Partners dashboard](https://www.shopify.com/partners) (also called `Client ID`)
Expand Down
3 changes: 3 additions & 0 deletions packages/apps/shopify-api/adapters/cf-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
setAbstractConvertResponseFunc(webApiConvertResponse);
setAbstractConvertHeadersFunc(webApiConvertHeaders);
setAbstractRuntimeString(workerRuntimeString);

// Export a marker to prevent tree-shaking
export const cfWorkerAdapterInitialized = true;

Check failure on line 23 in packages/apps/shopify-api/adapters/cf-worker/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎`

Check failure on line 23 in packages/apps/shopify-api/adapters/cf-worker/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎`
3 changes: 3 additions & 0 deletions packages/apps/shopify-api/adapters/web-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
setAbstractConvertResponseFunc(webApiConvertResponse);
setAbstractConvertHeadersFunc(webApiConvertHeaders);
setAbstractRuntimeString(webApiRuntimeString);

// Export a marker to prevent tree-shaking
export const webApiAdapterInitialized = true;

Check failure on line 23 in packages/apps/shopify-api/adapters/web-api/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎`

Check failure on line 23 in packages/apps/shopify-api/adapters/web-api/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎`
Loading