Added getAllPages operation

This commit is contained in:
Luis Alvarez 2021-05-20 17:26:14 -05:00
parent 4412d8e66c
commit 6bcf9f9e75
6 changed files with 69 additions and 49 deletions

View File

@ -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

View 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

View File

@ -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

View 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[] }
}

View File

@ -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']>
} }
} }

View File

@ -0,0 +1,6 @@
// TODO: define this type
export type Page = any
export type GetAllPagesOperation = {
data: { pages: Page[] }
}