mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
Added getAllPages operation
This commit is contained in:
parent
4412d8e66c
commit
6bcf9f9e75
@ -14,7 +14,9 @@ import type { CustomerAPI } from './endpoints/customer'
|
|||||||
import type { LoginAPI } from './endpoints/login'
|
import type { LoginAPI } from './endpoints/login'
|
||||||
import type { LogoutAPI } from './endpoints/logout'
|
import type { LogoutAPI } from './endpoints/logout'
|
||||||
import type { SignupAPI } from './endpoints/signup'
|
import type { SignupAPI } from './endpoints/signup'
|
||||||
|
|
||||||
import login from './operations/login'
|
import login from './operations/login'
|
||||||
|
import getAllPages from './operations/get-all-pages'
|
||||||
|
|
||||||
export interface BigcommerceConfig extends CommerceAPIConfig {
|
export interface BigcommerceConfig extends CommerceAPIConfig {
|
||||||
// Indicates if the returned metadata with translations should be applied to the
|
// Indicates if the returned metadata with translations should be applied to the
|
||||||
@ -110,7 +112,7 @@ const config2: BigcommerceConfig = {
|
|||||||
|
|
||||||
export const provider = {
|
export const provider = {
|
||||||
config: config2,
|
config: config2,
|
||||||
operations: { login },
|
operations: { login, getAllPages },
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Provider = typeof provider
|
export type Provider = typeof provider
|
||||||
|
46
framework/bigcommerce/api/operations/get-all-pages.ts
Normal file
46
framework/bigcommerce/api/operations/get-all-pages.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import type {
|
||||||
|
OperationContext,
|
||||||
|
OperationOptions,
|
||||||
|
} from '@commerce/api/operations'
|
||||||
|
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
|
||||||
|
import type { Page, GetAllPagesOperation } from '../../types/page'
|
||||||
|
import { BigcommerceConfig, Provider } from '..'
|
||||||
|
|
||||||
|
function getAllPagesOperation({ commerce }: OperationContext<Provider>) {
|
||||||
|
async function getAllPages(opts?: {
|
||||||
|
config?: BigcommerceConfig
|
||||||
|
preview?: boolean
|
||||||
|
}): Promise<GetAllPagesOperation['data']>
|
||||||
|
|
||||||
|
async function getAllPages<T extends GetAllPagesOperation>(
|
||||||
|
opts: {
|
||||||
|
config?: BigcommerceConfig
|
||||||
|
preview?: boolean
|
||||||
|
} & OperationOptions
|
||||||
|
): Promise<T['data']>
|
||||||
|
|
||||||
|
async function getAllPages({
|
||||||
|
config,
|
||||||
|
preview,
|
||||||
|
}: {
|
||||||
|
url?: string
|
||||||
|
config?: BigcommerceConfig
|
||||||
|
preview?: boolean
|
||||||
|
} = {}): Promise<GetAllPagesOperation['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[] }>
|
||||||
|
>('/v3/content/pages')
|
||||||
|
const pages = (data as RecursiveRequired<typeof data>) ?? []
|
||||||
|
|
||||||
|
return {
|
||||||
|
pages: preview ? pages : pages.filter((p) => p.is_visible),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getAllPages
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getAllPagesOperation
|
@ -1,43 +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 GetAllPagesResult<
|
|
||||||
T extends { pages: any[] } = { pages: Page[] }
|
|
||||||
> = T
|
|
||||||
|
|
||||||
async function getAllPages(opts?: {
|
|
||||||
config?: BigcommerceConfig
|
|
||||||
preview?: boolean
|
|
||||||
}): Promise<GetAllPagesResult>
|
|
||||||
|
|
||||||
async function getAllPages<T extends { pages: any[] }>(opts: {
|
|
||||||
url: string
|
|
||||||
config?: BigcommerceConfig
|
|
||||||
preview?: boolean
|
|
||||||
}): Promise<GetAllPagesResult<T>>
|
|
||||||
|
|
||||||
async function getAllPages({
|
|
||||||
config,
|
|
||||||
preview,
|
|
||||||
}: {
|
|
||||||
url?: string
|
|
||||||
config?: BigcommerceConfig
|
|
||||||
preview?: boolean
|
|
||||||
} = {}): Promise<GetAllPagesResult> {
|
|
||||||
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[] }>
|
|
||||||
>('/v3/content/pages')
|
|
||||||
const pages = (data as RecursiveRequired<typeof data>) ?? []
|
|
||||||
|
|
||||||
return {
|
|
||||||
pages: preview ? pages : pages.filter((p) => p.is_visible),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default getAllPages
|
|
9
framework/bigcommerce/types/page.ts
Normal file
9
framework/bigcommerce/types/page.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { definitions } from '../api/definitions/store-content'
|
||||||
|
|
||||||
|
export * from '@commerce/types/page'
|
||||||
|
|
||||||
|
export type Page = definitions['page_Full']
|
||||||
|
|
||||||
|
export type GetAllPagesOperation = {
|
||||||
|
data: { pages: Page[] }
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import type { LoginOperation } from '../types/login'
|
import type { LoginOperation } from '../types/login'
|
||||||
import type { GetAllPagesResult } from '../types/page'
|
import type { GetAllPagesOperation } from '../types/page'
|
||||||
import type { ServerResponse } from 'http'
|
import type { ServerResponse } from 'http'
|
||||||
import type { APIProvider, CommerceAPI, CommerceAPICore } from '.'
|
import type { APIProvider, CommerceAPI, CommerceAPICore } from '.'
|
||||||
|
|
||||||
@ -33,17 +33,17 @@ export type Operations<P extends APIProvider> = {
|
|||||||
): Promise<T['data']>
|
): Promise<T['data']>
|
||||||
}
|
}
|
||||||
getAllPages: {
|
getAllPages: {
|
||||||
(opts?: {
|
<T extends GetAllPagesOperation>(opts?: {
|
||||||
config?: P['config']
|
config?: P['config']
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
}): Promise<GetAllPagesResult>
|
}): Promise<T['data']>
|
||||||
|
|
||||||
<T extends GetAllPagesResult>(
|
<T extends GetAllPagesOperation>(
|
||||||
opts: {
|
opts: {
|
||||||
config?: P['config']
|
config?: P['config']
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
} & OperationOptions
|
} & OperationOptions
|
||||||
): Promise<T>
|
): Promise<T['data']>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
framework/commerce/types/page.ts
Normal file
6
framework/commerce/types/page.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// TODO: define this type
|
||||||
|
export type Page = any
|
||||||
|
|
||||||
|
export type GetAllPagesOperation = {
|
||||||
|
data: { pages: Page[] }
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user