mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Moved getPage
This commit is contained in:
parent
9794d5e626
commit
f4e7cb51fa
@ -17,6 +17,7 @@ import type { SignupAPI } from './endpoints/signup'
|
||||
|
||||
import login from './operations/login'
|
||||
import getAllPages from './operations/get-all-pages'
|
||||
import getPage from './operations/get-page'
|
||||
|
||||
export interface BigcommerceConfig extends CommerceAPIConfig {
|
||||
// Indicates if the returned metadata with translations should be applied to the
|
||||
@ -112,7 +113,7 @@ const config2: BigcommerceConfig = {
|
||||
|
||||
export const provider = {
|
||||
config: config2,
|
||||
operations: { login, getAllPages },
|
||||
operations: { login, getAllPages, getPage },
|
||||
}
|
||||
|
||||
export type Provider = typeof provider
|
||||
|
@ -2,11 +2,13 @@ import type {
|
||||
OperationContext,
|
||||
OperationOptions,
|
||||
} from '@commerce/api/operations'
|
||||
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
|
||||
import type { Page, GetAllPagesOperation } from '../../types/page'
|
||||
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
|
||||
import { BigcommerceConfig, Provider } from '..'
|
||||
|
||||
function getAllPagesOperation({ commerce }: OperationContext<Provider>) {
|
||||
export default function getAllPagesOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function getAllPages(opts?: {
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
@ -42,5 +44,3 @@ function getAllPagesOperation({ commerce }: OperationContext<Provider>) {
|
||||
|
||||
return getAllPages
|
||||
}
|
||||
|
||||
export default getAllPagesOperation
|
||||
|
53
framework/bigcommerce/api/operations/get-page.ts
Normal file
53
framework/bigcommerce/api/operations/get-page.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import type {
|
||||
OperationContext,
|
||||
OperationOptions,
|
||||
} from '@commerce/api/operations'
|
||||
import type { GetPageOperation, Page } from '../../types/page'
|
||||
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
|
||||
import type { BigcommerceConfig, Provider } from '..'
|
||||
|
||||
export default function getPageOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function getPage<T extends GetPageOperation>(opts: {
|
||||
variables: T['variables']
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
}): Promise<T['data']>
|
||||
|
||||
async function getPage<T extends GetPageOperation>(
|
||||
opts: {
|
||||
variables: T['variables']
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
|
||||
async function getPage<T extends GetPageOperation>({
|
||||
url,
|
||||
variables,
|
||||
config,
|
||||
preview,
|
||||
}: {
|
||||
url?: string
|
||||
variables: T['variables']
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
}): Promise<T['data']> {
|
||||
config = commerce.getConfig(config)
|
||||
// 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 config.storeApiFetch<
|
||||
RecursivePartial<{ data: Page[] }>
|
||||
>(url || `/v3/content/pages?id=${variables.id}&include=body`)
|
||||
const firstPage = data?.[0]
|
||||
const page = firstPage as RecursiveRequired<typeof firstPage>
|
||||
|
||||
if (preview || page?.is_visible) {
|
||||
return { page }
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
return getPage
|
||||
}
|
@ -17,7 +17,9 @@ export const loginMutation = /* GraphQL */ `
|
||||
}
|
||||
`
|
||||
|
||||
function loginOperation({ commerce }: OperationContext<Provider>) {
|
||||
export default function loginOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function login(opts: {
|
||||
variables: LoginOperation['variables']
|
||||
config?: BigcommerceConfig
|
||||
@ -75,5 +77,3 @@ function loginOperation({ commerce }: OperationContext<Provider>) {
|
||||
|
||||
return login
|
||||
}
|
||||
|
||||
export default loginOperation
|
||||
|
@ -1,53 +0,0 @@
|
||||
import type { RecursivePartial, RecursiveRequired } from '../api/utils/types'
|
||||
import { BigcommerceConfig, getConfig } from '../api'
|
||||
import { definitions } from '../api/definitions/store-content'
|
||||
|
||||
export type Page = definitions['page_Full']
|
||||
|
||||
export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
|
||||
|
||||
export type PageVariables = {
|
||||
id: number
|
||||
}
|
||||
|
||||
async function getPage(opts: {
|
||||
url?: string
|
||||
variables: PageVariables
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
}): Promise<GetPageResult>
|
||||
|
||||
async function getPage<T extends { page?: any }, V = any>(opts: {
|
||||
url: string
|
||||
variables: V
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
}): Promise<GetPageResult<T>>
|
||||
|
||||
async function getPage({
|
||||
url,
|
||||
variables,
|
||||
config,
|
||||
preview,
|
||||
}: {
|
||||
url?: string
|
||||
variables: PageVariables
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
}): Promise<GetPageResult> {
|
||||
config = getConfig(config)
|
||||
// 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 config.storeApiFetch<
|
||||
RecursivePartial<{ data: Page[] }>
|
||||
>(url || `/v3/content/pages?id=${variables.id}&include=body`)
|
||||
const firstPage = data?.[0]
|
||||
const page = firstPage as RecursiveRequired<typeof firstPage>
|
||||
|
||||
if (preview || page?.is_visible) {
|
||||
return { page }
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
export default getPage
|
@ -1,9 +1,13 @@
|
||||
import * as Core from '@commerce/types/page'
|
||||
import { definitions } from '../api/definitions/store-content'
|
||||
|
||||
export * from '@commerce/types/page'
|
||||
|
||||
export type Page = definitions['page_Full']
|
||||
|
||||
export type GetAllPagesOperation = {
|
||||
data: { pages: Page[] }
|
||||
export type PageTypes = {
|
||||
page: Page
|
||||
}
|
||||
|
||||
export type GetAllPagesOperation = Core.GetAllPagesOperation<PageTypes>
|
||||
export type GetPageOperation = Core.GetPageOperation<PageTypes>
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { LoginOperation } from '../types/login'
|
||||
import type { GetAllPagesOperation } from '../types/page'
|
||||
import type { GetAllPagesOperation, GetPageOperation } from '../types/page'
|
||||
import type { ServerResponse } from 'http'
|
||||
import type { APIProvider, CommerceAPI } from '.'
|
||||
|
||||
@ -45,6 +45,21 @@ export type Operations<P extends APIProvider> = {
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
}
|
||||
getPage: {
|
||||
<T extends GetPageOperation>(opts: {
|
||||
variables: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
}): Promise<T['data']>
|
||||
|
||||
<T extends GetPageOperation>(
|
||||
opts: {
|
||||
variables: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
}
|
||||
}
|
||||
|
||||
export type APIOperations<P extends APIProvider> = {
|
||||
|
@ -1,6 +1,15 @@
|
||||
// TODO: define this type
|
||||
export type Page = any
|
||||
|
||||
export type GetAllPagesOperation = {
|
||||
data: { pages: Page[] }
|
||||
export type PageTypes = {
|
||||
page: Page
|
||||
}
|
||||
|
||||
export type GetAllPagesOperation<T extends PageTypes = PageTypes> = {
|
||||
data: { pages: T['page'][] }
|
||||
}
|
||||
|
||||
export type GetPageOperation<T extends PageTypes = PageTypes> = {
|
||||
data: { page?: T['page'] }
|
||||
variables: { id: string }
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import commerce from '@lib/api/commerce'
|
||||
import getSlug from '@lib/get-slug'
|
||||
import { missingLocaleInPages } from '@lib/usage-warns'
|
||||
import { getConfig } from '@framework/api'
|
||||
import getPage from '@framework/common/get-page'
|
||||
import getPage from '@framework/api/operations/get-page'
|
||||
import { defaultPageProps } from '@lib/defaults'
|
||||
|
||||
export async function getStaticProps({
|
||||
|
Loading…
x
Reference in New Issue
Block a user