Skip to content

Commit

Permalink
Update types (vercel#831)
Browse files Browse the repository at this point in the history
* Update product types

* Cart types progress, add zod & initial schema validator

* Update normalize.ts

* Update with-schema-parser.ts

* Updated types, schemas & providers

* Fix providers after schema parse errors

* Fix paths

* More provider fixes

* Fix kibocommerce & commercejs

* Add customer updated types & fixes

* Add checkout & customer types

* Import core types only from commerce

* Update tsconfig.json

* Convert hooks interfaces to types

* Requested changes

* Change to relative paths

* Move Zod dependency
  • Loading branch information
cond0r authored Oct 5, 2022
1 parent 8398a96 commit 6c26105
Show file tree
Hide file tree
Showing 291 changed files with 1,972 additions and 1,788 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Demo live at: [demo.vercel.store](https://demo.vercel.store/)
> To run a minimal version of Next.js Commerce you can start with the default local provider `@vercel/commerce-local` that has disabled all features (cart, auth) and use static files for the backend
```bash
pnpm install # run this command in root folder of the mono repo
pnpm dev
pnpm install & pnpm build # run this commands in root folder of the mono repo
pnpm dev # run this commands in the site folder
```

> If you encounter any problems while installing and running for the first time, please see the Troubleshoot section
Expand Down Expand Up @@ -111,10 +111,11 @@ Our commitment to Open Source can be found [here](https://vercel.com/oss).
1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device.
2. Create a new branch `git checkout -b MY_BRANCH_NAME`
3. Install the dependencies: `pnpm install`
4. Duplicate `site/.env.template` and rename it to `site/.env.local`
5. Add proper store values to `site/.env.local`
6. Run `pnpm dev` to build the packages and watch for code changes
7. Run `pnpm turbo run build` to check the build after your changes
4. Build the packages: `pnpm build`
5. Duplicate `site/.env.template` and rename it to `site/.env.local`
6. Add proper store values to `site/.env.local`
7. Run `cd site` & `pnpm dev` to watch for code changes
8. Run `pnpm turbo run build` to check the build after your changes

## Work in progress

Expand Down Expand Up @@ -191,7 +192,7 @@ info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this comm

The error usually occurs when running `pnpm dev` inside of the `/site/` folder after installing a fresh repository.

In order to fix this, run `pnpm dev` in the monorepo root folder first.
In order to fix this, run `pnpm build` in the monorepo root folder first.

> Using `pnpm dev` from the root is recommended for developing, which will run watch mode on all packages.
Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/definitions/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ export interface definitions {
*/
search_keywords?: string
/**
* Image URL used for this category on the storefront. Images can be uploaded via form file post to `/brands/{brandId}/image`, or by providing a publicly accessible URL in this field.
* Image URL used for this category on the storefront. Images can be uploaded via form file post to `/{brandId}/image`, or by providing a publicly accessible URL in this field.
*/
image_url?: string
custom_url?: definitions['customUrl_Full']
Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/endpoints/cart/get-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { normalizeCart } from '../../../lib/normalize'
import { BigcommerceApiError } from '../../utils/errors'
import getCartCookie from '../../utils/get-cart-cookie'
import type { BigcommerceCart } from '../../../types/cart'
import type { BigcommerceCart } from '../../../types'
import type { CartEndpoint } from '.'

// Return current cart info
Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/endpoints/cart/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import cartEndpoint from '@vercel/commerce/api/endpoints/cart'
import type { CartSchema } from '../../../types/cart'
import type { CartSchema } from '@vercel/commerce/types/cart'
import type { BigcommerceAPI } from '../..'
import getCart from './get-cart'
import addItem from './add-item'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import productsEndpoint from '@vercel/commerce/api/endpoints/catalog/products'
import type { ProductsSchema } from '../../../../types/product'
import type { ProductsSchema } from '@vercel/commerce/types/product'
import type { BigcommerceAPI } from '../../..'
import getProducts from './get-products'

Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/endpoints/checkout/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import checkoutEndpoint from '@vercel/commerce/api/endpoints/checkout'
import type { CheckoutSchema } from '../../../types/checkout'
import type { CheckoutSchema } from '@vercel/commerce/types/checkout'
import type { BigcommerceAPI } from '../..'
import getCheckout from './get-checkout'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,19 @@ const getLoggedInCustomer: CustomerEndpoint['handlers']['getLoggedInCustomer'] =
})
}

return res.status(200).json({ data: { customer } })
return res.status(200).json({
data: {
customer: {
id: String(customer.entityId),
firstName: customer.firstName,
lastName: customer.lastName,
email: customer.email,
company: customer.company,
phone: customer.phone,
notes: customer.notes,
},
},
})
}

res.status(200).json({ data: null })
Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/endpoints/customer/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import customerEndpoint from '@vercel/commerce/api/endpoints/customer'
import type { CustomerSchema } from '../../../types/customer'
import type { CustomerSchema } from '@vercel/commerce/types/customer'
import type { BigcommerceAPI } from '../..'
import getLoggedInCustomer from './get-logged-in-customer'

Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/endpoints/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import loginEndpoint from '@vercel/commerce/api/endpoints/login'
import type { LoginSchema } from '../../../types/login'
import type { LoginSchema } from '@vercel/commerce/types/login'
import type { BigcommerceAPI } from '../..'
import login from './login'

Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/endpoints/logout/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import logoutEndpoint from '@vercel/commerce/api/endpoints/logout'
import type { LogoutSchema } from '../../../types/logout'
import type { LogoutSchema } from '@vercel/commerce/types/logout'
import type { BigcommerceAPI } from '../..'
import logout from './logout'

Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/endpoints/signup/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import signupEndpoint from '@vercel/commerce/api/endpoints/signup'
import type { SignupSchema } from '../../../types/signup'
import type { SignupSchema } from '@vercel/commerce/types/signup'
import type { BigcommerceAPI } from '../..'
import signup from './signup'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Wishlist } from '../../../types/wishlist'
import type { Wishlist } from '@vercel/commerce/types/wishlist'
import type { WishlistEndpoint } from '.'
import getCustomerId from '../../utils/get-customer-id'
import getCustomerWishlist from '../../operations/get-customer-wishlist'

// Return wishlist info
const getWishlist: WishlistEndpoint['handlers']['getWishlist'] = async ({
Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/endpoints/wishlist/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import wishlistEndpoint from '@vercel/commerce/api/endpoints/wishlist'
import type { WishlistSchema } from '../../../types/wishlist'
import type { WishlistSchema } from '@vercel/commerce/types/wishlist'
import type { BigcommerceAPI } from '../..'
import getWishlist from './get-wishlist'
import addItem from './add-item'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Wishlist } from '../../../types/wishlist'
import getCustomerWishlist from '../../operations/get-customer-wishlist'
import type { Wishlist } from '@vercel/commerce/types/wishlist'
import getCustomerId from '../../utils/get-customer-id'
import type { WishlistEndpoint } from '.'

Expand Down
11 changes: 8 additions & 3 deletions packages/bigcommerce/src/api/operations/get-all-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import type {
OperationContext,
OperationOptions,
} from '@vercel/commerce/api/operations'
import type { Page, GetAllPagesOperation } from '../../types/page'
import type { GetAllPagesOperation } from '@vercel/commerce/types/page'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import { BigcommerceConfig, Provider } from '..'

import { definitions } from '../definitions/store-content'
import { normalizePage } from '../../lib/normalize'

export default function getAllPagesOperation({
commerce,
}: OperationContext<Provider>) {
Expand Down Expand Up @@ -33,12 +36,14 @@ export default function getAllPagesOperation({
// RecursivePartial forces the method to check for every prop in the data, which is
// required in case there's a custom `url`
const { data } = await cfg.storeApiFetch<
RecursivePartial<{ data: Page[] }>
RecursivePartial<{ data: definitions['page_Full'][] }>
>('/v3/content/pages')
const pages = (data as RecursiveRequired<typeof data>) ?? []

return {
pages: preview ? pages : pages.filter((p) => p.is_visible),
pages: preview
? pages.map(normalizePage)
: pages.filter((p) => p.is_visible).map(normalizePage),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
OperationOptions,
} from '@vercel/commerce/api/operations'
import type { GetAllProductPathsQuery } from '../../../schema'
import type { GetAllProductPathsOperation } from '../../types/product'
import type { GetAllProductPathsOperation } from '@vercel/commerce/types/product'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import filterEdges from '../utils/filter-edges'
import { BigcommerceConfig, Provider } from '..'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
GetAllProductsQuery,
GetAllProductsQueryVariables,
} from '../../../schema'
import type { GetAllProductsOperation } from '../../types/product'
import type { GetAllProductsOperation } from '@vercel/commerce/types/product'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import filterEdges from '../utils/filter-edges'
import setProductLocaleMeta from '../utils/set-product-locale-meta'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
import type {
GetCustomerWishlistOperation,
Wishlist,
} from '../../types/wishlist'
} from '@vercel/commerce/types/wishlist'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import { BigcommerceConfig, Provider } from '..'
import getAllProducts, { ProductEdge } from './get-all-products'
Expand Down Expand Up @@ -49,7 +49,7 @@ export default function getCustomerWishlistOperation({

if (includeProducts && wishlist?.items?.length) {
const ids = wishlist.items
?.map((item) => (item?.product_id ? String(item?.product_id) : null))
?.map((item) => (item?.productId ? String(item?.productId) : null))
.filter((id): id is string => !!id)

if (ids?.length) {
Expand All @@ -66,7 +66,7 @@ export default function getCustomerWishlistOperation({
}, {})
// Populate the wishlist items with the graphql products
wishlist.items.forEach((item) => {
const product = item && productsById[item.product_id!]
const product = item && productsById[Number(item.productId)]
if (item && product) {
// @ts-ignore Fix this type when the wishlist type is properly defined
item.product = product
Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/operations/get-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
OperationContext,
OperationOptions,
} from '@vercel/commerce/api/operations'
import type { GetPageOperation, Page } from '../../types/page'
import type { GetPageOperation, Page } from '@vercel/commerce/types/page'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import type { BigcommerceConfig, Provider } from '..'
import { normalizePage } from '../../lib/normalize'
Expand Down
4 changes: 2 additions & 2 deletions packages/bigcommerce/src/api/operations/get-product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
OperationContext,
OperationOptions,
} from '@vercel/commerce/api/operations'
import type { GetProductOperation } from '../../types/product'
import type { GetProductOperation } from '@vercel/commerce/types/product'
import type { GetProductQuery, GetProductQueryVariables } from '../../../schema'
import setProductLocaleMeta from '../utils/set-product-locale-meta'
import { productInfoFragment } from '../fragments/product'
Expand Down Expand Up @@ -100,7 +100,7 @@ export default function getAllProductPathsOperation({
const variables: GetProductQueryVariables = {
locale,
hasLocale: !!locale,
path: slug ? `/${slug}/` : vars.path!,
path: slug ? `/${slug}` : vars.path!,
}
const { data } = await config.fetch<GetProductQuery>(query, { variables })
const product = data.site?.route?.node
Expand Down
6 changes: 3 additions & 3 deletions packages/bigcommerce/src/api/operations/get-site-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import type {
OperationContext,
OperationOptions,
} from '@vercel/commerce/api/operations'
import type { GetSiteInfoOperation } from '../../types/site'
import type { GetSiteInfoOperation } from '@vercel/commerce/types/site'
import type { GetSiteInfoQuery } from '../../../schema'
import filterEdges from '../utils/filter-edges'
import type { BigcommerceConfig, Provider } from '..'
import { categoryTreeItemFragment } from '../fragments/category-tree'
import { normalizeCategory } from '../../lib/normalize'
import { normalizeBrand, normalizeCategory } from '../../lib/normalize'

// Get 3 levels of categories
export const getSiteInfoQuery = /* GraphQL */ `
Expand Down Expand Up @@ -79,7 +79,7 @@ export default function getSiteInfoOperation({

return {
categories: categories ?? [],
brands: filterEdges(brands),
brands: filterEdges(brands).map(normalizeBrand),
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/api/operations/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
OperationContext,
OperationOptions,
} from '@vercel/commerce/api/operations'
import type { LoginOperation } from '../../types/login'
import type { LoginOperation } from '@vercel/commerce/types/login'
import type { LoginMutation } from '../../../schema'
import type { RecursivePartial } from '../utils/types'
import concatHeader from '../utils/concat-cookie'
Expand Down
8 changes: 4 additions & 4 deletions packages/bigcommerce/src/api/utils/parse-item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WishlistItemBody } from '../../types/wishlist'
import type { CartItemBody, OptionSelections } from '../../types/cart'
import type { WishlistItemBody } from '@vercel/commerce/types/wishlist'
import type { CartItemBody, SelectedOption } from '@vercel/commerce/types/cart'

type BCWishlistItemBody = {
product_id: number
Expand All @@ -10,7 +10,7 @@ type BCCartItemBody = {
product_id: number
variant_id: number
quantity?: number
option_selections?: OptionSelections[]
option_selections?: SelectedOption[]
}

export const parseWishlistItem = (
Expand All @@ -24,5 +24,5 @@ export const parseCartItem = (item: CartItemBody): BCCartItemBody => ({
quantity: item.quantity,
product_id: Number(item.productId),
variant_id: Number(item.variantId),
option_selections: item.optionSelections,
option_selections: item.optionsSelected,
})
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/auth/use-login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback } from 'react'
import type { MutationHook } from '@vercel/commerce/utils/types'
import { CommerceError } from '@vercel/commerce/utils/errors'
import useLogin, { UseLogin } from '@vercel/commerce/auth/use-login'
import type { LoginHook } from '../types/login'
import type { LoginHook } from '@vercel/commerce/types/login'
import useCustomer from '../customer/use-customer'

export default useLogin as UseLogin<typeof handler>
Expand Down
2 changes: 1 addition & 1 deletion packages/bigcommerce/src/auth/use-logout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback } from 'react'
import type { MutationHook } from '@vercel/commerce/utils/types'
import useLogout, { UseLogout } from '@vercel/commerce/auth/use-logout'
import type { LogoutHook } from '../types/logout'
import type { LogoutHook } from '@vercel/commerce/types/logout'
import useCustomer from '../customer/use-customer'

export default useLogout as UseLogout<typeof handler>
Expand Down
4 changes: 2 additions & 2 deletions packages/bigcommerce/src/auth/use-signup.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback } from 'react'
import type { MutationHook } from '@vercel/commerce/utils/types'
import { CommerceError } from '@vercel/commerce/utils/errors'
import useSignup, { UseSignup } from '@vercel/commerce/auth/use-signup'
import type { SignupHook } from '../types/signup'
import useSignup, { type UseSignup } from '@vercel/commerce/auth/use-signup'
import type { SignupHook } from '@vercel/commerce/types/signup'
import useCustomer from '../customer/use-customer'

export default useSignup as UseSignup<typeof handler>
Expand Down
10 changes: 8 additions & 2 deletions packages/bigcommerce/src/cart/use-remove-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import type {
HookFetcherContext,
} from '@vercel/commerce/utils/types'
import { ValidationError } from '@vercel/commerce/utils/errors'
import useRemoveItem, { UseRemoveItem } from '@vercel/commerce/cart/use-remove-item'
import type { Cart, LineItem, RemoveItemHook } from '@vercel/commerce/types/cart'
import useRemoveItem, {
UseRemoveItem,
} from '@vercel/commerce/cart/use-remove-item'
import type {
Cart,
LineItem,
RemoveItemHook,
} from '@vercel/commerce/types/cart'
import useCart from './use-cart'

export type RemoveItemFn<T = any> = T extends LineItem
Expand Down
8 changes: 5 additions & 3 deletions packages/bigcommerce/src/customer/use-customer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SWRHook } from '@vercel/commerce/utils/types'
import useCustomer, { UseCustomer } from '@vercel/commerce/customer/use-customer'
import type { CustomerHook } from '../types/customer'
import type { SWRHook } from '@vercel/commerce/utils/types'
import useCustomer, {
type UseCustomer,
} from '@vercel/commerce/customer/use-customer'
import type { CustomerHook } from '@vercel/commerce/types/customer'

export default useCustomer as UseCustomer<typeof handler>

Expand Down
Loading

0 comments on commit 6c26105

Please sign in to comment.