Skip to content

Commit

Permalink
Fix Storefront and i18n types (#496)
Browse files Browse the repository at this point in the history
* Fix default Storefront type. Closes #486

* Add extra i18n properties to Storefront type in user app

* Changeset

* Simplify type in hello-world template
  • Loading branch information
frandiox authored Feb 13, 2023
1 parent 4bb7572 commit a1f3056
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-bags-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Fix default Storefront type in LoaderArgs.
2 changes: 1 addition & 1 deletion packages/hydrogen/src/storefront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type StorefrontClient<TI18n extends I18nBase> = {
storefront: Storefront<TI18n>;
};

export type Storefront<TI18n extends I18nBase> = {
export type Storefront<TI18n extends I18nBase = I18nBase> = {
query: <T>(
query: string,
payload?: StorefrontCommonOptions & {
Expand Down
11 changes: 2 additions & 9 deletions templates/demo-store/app/lib/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {LoaderArgs} from '@shopify/remix-oxygen';
import {Storefront as HydrogenStorefront} from '@shopify/hydrogen';
import {
import type {Storefront as HydrogenStorefront} from '@shopify/hydrogen';
import type {
CountryCode,
CurrencyCode,
LanguageCode,
Expand All @@ -20,12 +19,6 @@ export type I18nLocale = Locale & {
};

export type Storefront = HydrogenStorefront<I18nLocale>;
export type StorefrontContext = {
storefront: Storefront;
};
export type StorefrontLoaderArgs = {
context: StorefrontContext;
};

export enum CartAction {
ADD_TO_CART = 'ADD_TO_CART',
Expand Down
3 changes: 1 addition & 2 deletions templates/demo-store/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {DEFAULT_LOCALE, parseMenu, type EnhancedMenu} from './lib/utils';
import invariant from 'tiny-invariant';
import {Shop, Cart} from '@shopify/hydrogen/storefront-api-types';
import {useAnalytics} from './hooks/useAnalytics';
import type {StorefrontContext} from './lib/type';

const seo: SeoHandleFunction<typeof loader> = ({data, pathname}) => ({
title: data?.layout?.shop?.name,
Expand Down Expand Up @@ -359,7 +358,7 @@ const CART_QUERY = `#graphql
}
`;

export async function getCart({storefront}: StorefrontContext, cartId: string) {
export async function getCart({storefront}: AppLoadContext, cartId: string) {
invariant(storefront, 'missing storefront client in cart query');

const {cart} = await storefront.query<{cart?: Cart}>(CART_QUERY, {
Expand Down
3 changes: 1 addition & 2 deletions templates/demo-store/app/routes/($lang)/journal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {flattenConnection, Image} from '@shopify/hydrogen';
import type {Article, Blog} from '@shopify/hydrogen/storefront-api-types';
import {Grid, PageHeader, Section, Link} from '~/components';
import {getImageLoadingPriority, PAGINATION_SIZE} from '~/lib/const';
import {StorefrontLoaderArgs} from '~/lib/type';

const BLOG_HANDLE = 'Journal';

Expand All @@ -14,7 +13,7 @@ export const handle = {
},
};

export const loader = async ({context: {storefront}}: StorefrontLoaderArgs) => {
export const loader = async ({context: {storefront}}: LoaderArgs) => {
const {language, country} = storefront.i18n;
const {blog} = await storefront.query<{
blog: Blog;
Expand Down
5 changes: 2 additions & 3 deletions templates/demo-store/app/routes/($lang)/policies/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import {json} from '@shopify/remix-oxygen';
import {json, type LoaderArgs} from '@shopify/remix-oxygen';
import {useLoaderData} from '@remix-run/react';
import type {ShopPolicy} from '@shopify/hydrogen/storefront-api-types';
import invariant from 'tiny-invariant';

import {PageHeader, Section, Heading, Link} from '~/components';
import {StorefrontLoaderArgs} from '~/lib/type';

export const handle = {
seo: {
title: 'Policies',
},
};

export async function loader({context: {storefront}}: StorefrontLoaderArgs) {
export async function loader({context: {storefront}}: LoaderArgs) {
const data = await storefront.query<{
shop: Record<string, ShopPolicy>;
}>(POLICIES_QUERY);
Expand Down
2 changes: 1 addition & 1 deletion templates/demo-store/remix.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// <reference types="@shopify/remix-oxygen" />
/// <reference types="@shopify/oxygen-workers-types" />

import type {Storefront} from '@shopify/hydrogen';
import type {Storefront} from '~/lib/type';
import type {HydrogenSession} from '~/lib/session.server';

declare global {
Expand Down
4 changes: 2 additions & 2 deletions templates/hello-world/remix.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// <reference types="@shopify/remix-oxygen" />
/// <reference types="@shopify/oxygen-workers-types" />

import type {StorefrontClient} from '@shopify/hydrogen';
import type {Storefront} from '@shopify/hydrogen';
import type {HydrogenSession} from '../server';

declare global {
Expand Down Expand Up @@ -30,7 +30,7 @@ declare global {
declare module '@shopify/remix-oxygen' {
export interface AppLoadContext {
session: HydrogenSession;
storefront: StorefrontClient['storefront'];
storefront: Storefront;
env: Env;
}
}
4 changes: 2 additions & 2 deletions templates/skeleton/remix.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// <reference types="@shopify/remix-oxygen" />
/// <reference types="@shopify/oxygen-workers-types" />

import type {StorefrontClient} from '@shopify/hydrogen';
import type {Storefront} from '@shopify/hydrogen';
import type {HydrogenSession} from '../server';

declare global {
Expand Down Expand Up @@ -30,7 +30,7 @@ declare global {
declare module '@shopify/remix-oxygen' {
export interface AppLoadContext {
session: HydrogenSession;
storefront: StorefrontClient['storefront'];
storefront: Storefront;
env: Env;
}
}

0 comments on commit a1f3056

Please sign in to comment.