mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Adding more experimental types
This commit is contained in:
parent
149df82f45
commit
fed3c82c69
@ -1,9 +1,9 @@
|
|||||||
import { normalizeCart } from '@framework/lib/normalize'
|
import { normalizeCart } from '@framework/lib/normalize'
|
||||||
import { parseCartItem } from '../utils/parse-item'
|
import { parseCartItem } from '../utils/parse-item'
|
||||||
import getCartCookie from '../utils/get-cart-cookie'
|
import getCartCookie from '../utils/get-cart-cookie'
|
||||||
import type { CartHandlers } from '.'
|
import type { CartEndpoint } from '.'
|
||||||
|
|
||||||
const addItem: CartHandlers['addItem'] = async ({
|
const addItem: CartEndpoint['operations']['addItem'] = async ({
|
||||||
res,
|
res,
|
||||||
body: { cartId, item },
|
body: { cartId, item },
|
||||||
config,
|
config,
|
||||||
|
@ -2,10 +2,10 @@ import { normalizeCart } from '@framework/lib/normalize'
|
|||||||
import { BigcommerceApiError } from '../utils/errors'
|
import { BigcommerceApiError } from '../utils/errors'
|
||||||
import getCartCookie from '../utils/get-cart-cookie'
|
import getCartCookie from '../utils/get-cart-cookie'
|
||||||
import type { BigcommerceCart } from '../../types'
|
import type { BigcommerceCart } from '../../types'
|
||||||
import type { CartHandlers } from '.'
|
import type { CartEndpoint } from '.'
|
||||||
|
|
||||||
// Return current cart info
|
// Return current cart info
|
||||||
const getCart: CartHandlers['getCart'] = async ({
|
const getCart: CartEndpoint['operations']['getCart'] = async ({
|
||||||
res,
|
res,
|
||||||
body: { cartId },
|
body: { cartId },
|
||||||
config,
|
config,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { EndpointSchema } from '@commerce/api'
|
import { EndpointSchema, GetAPISchema } from '@commerce/api'
|
||||||
import getCart from './get-cart'
|
import getCart from './get-cart'
|
||||||
import addItem from './add-item'
|
import addItem from './add-item'
|
||||||
import updateItem from './handlers/update-item'
|
import updateItem from './handlers/update-item'
|
||||||
@ -10,25 +10,27 @@ import type {
|
|||||||
RemoveCartItemHandlerBody,
|
RemoveCartItemHandlerBody,
|
||||||
Cart,
|
Cart,
|
||||||
} from '../../types'
|
} from '../../types'
|
||||||
import type { CommerceAPIEndpoints } from '..'
|
import type { CommerceAPI } from '..'
|
||||||
|
|
||||||
export type CartEndpointSchema = EndpointSchema<
|
export type CartAPI = GetAPISchema<
|
||||||
'cart',
|
CommerceAPI,
|
||||||
{
|
{
|
||||||
options: {}
|
endpoint: {
|
||||||
operations: {
|
options: {}
|
||||||
getCart: {
|
operations: {
|
||||||
data: Cart | null
|
getCart: {
|
||||||
body: GetCartHandlerBody
|
data: Cart | null
|
||||||
options: { yay: string }
|
body: GetCartHandlerBody
|
||||||
|
options: { yay: string }
|
||||||
|
}
|
||||||
|
addItem: { data: Cart; body: AddCartItemHandlerBody; options: {} }
|
||||||
|
updateItem: { data: Cart; body: UpdateCartItemHandlerBody; options: {} }
|
||||||
|
removeItem: { data: Cart; body: RemoveCartItemHandlerBody; options: {} }
|
||||||
}
|
}
|
||||||
addItem: { data: Cart; body: AddCartItemHandlerBody; options: {} }
|
|
||||||
updateItem: { data: Cart; body: UpdateCartItemHandlerBody; options: {} }
|
|
||||||
removeItem: { data: Cart; body: RemoveCartItemHandlerBody; options: {} }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
|
|
||||||
export type CartAPI = CommerceAPIEndpoints['cart']
|
export type CartEndpoint = CartAPI['endpoint']
|
||||||
|
|
||||||
export const operations = { getCart, addItem }
|
export const operations = { getCart, addItem }
|
||||||
|
@ -109,15 +109,17 @@ export type Provider = typeof provider
|
|||||||
export type EndpointsSchema = { cart: CartEndpointSchema }
|
export type EndpointsSchema = { cart: CartEndpointSchema }
|
||||||
|
|
||||||
export class CommerceAPI<
|
export class CommerceAPI<
|
||||||
P extends Provider = Provider,
|
P extends Provider = Provider
|
||||||
E extends EndpointsSchema = EndpointsSchema
|
> extends CoreCommerceAPI<P> {
|
||||||
> extends CoreCommerceAPI<P, E> {
|
|
||||||
constructor(readonly provider: P = provider) {
|
constructor(readonly provider: P = provider) {
|
||||||
super(provider)
|
super(provider)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CommerceAPIEndpoints = GetEndpointsSchema<CommerceAPI>
|
export type CommerceAPIEndpoints = GetEndpointsSchema<
|
||||||
|
CommerceAPI,
|
||||||
|
EndpointsSchema
|
||||||
|
>
|
||||||
|
|
||||||
export function getConfig(userConfig?: Partial<BigcommerceConfig>) {
|
export function getConfig(userConfig?: Partial<BigcommerceConfig>) {
|
||||||
return config.getConfig(userConfig)
|
return config.getConfig(userConfig)
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
import type { APIEndpoint } from '../utils/types'
|
import type { APIEndpoint } from '../utils/types'
|
||||||
import { CommerceAPIError } from '../utils/errors'
|
import { CommerceAPIError } from '../utils/errors'
|
||||||
import isAllowedOperation from '../utils/is-allowed-operation'
|
import isAllowedOperation from '../utils/is-allowed-operation'
|
||||||
import type { CommerceAPI, CartHandlers } from '..'
|
import type { GetAPISchema, CommerceAPI, CartSchema } from '..'
|
||||||
|
|
||||||
const cartApi: APIEndpoint<CommerceAPI, CartHandlers<any>> = async (ctx) => {
|
const cartApi: GetAPISchema<
|
||||||
|
CommerceAPI,
|
||||||
|
CartSchema
|
||||||
|
>['endpoint']['handler'] = async (ctx) => {
|
||||||
const { req, res, handlers, config } = ctx
|
const { req, res, handlers, config } = ctx
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -46,6 +46,36 @@ export type CartHandlers2<
|
|||||||
removeItem: APIHandler<any, CartHandlersBase<C>, Cart, any>
|
removeItem: APIHandler<any, CartHandlersBase<C>, Cart, any>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CartSchema = {
|
||||||
|
endpoint: {
|
||||||
|
options: {}
|
||||||
|
operations: {
|
||||||
|
getCart: { data?: Cart | null; body?: any }
|
||||||
|
addItem: { data?: Cart; body?: any }
|
||||||
|
updateItem: { data?: Cart; body?: any }
|
||||||
|
removeItem: { data?: Cart; body?: any }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type APISchemas = CartSchema
|
||||||
|
|
||||||
|
export type GetAPISchema<
|
||||||
|
C extends CommerceAPI,
|
||||||
|
S extends APISchemas = APISchemas
|
||||||
|
> = {
|
||||||
|
schema: S
|
||||||
|
endpoint: EndpointContext2<C, S['endpoint']>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type EndpointContext2<
|
||||||
|
C extends CommerceAPI,
|
||||||
|
E extends EndpointSchemaBase
|
||||||
|
> = {
|
||||||
|
handler: Endpoint<C, E>
|
||||||
|
operations: EndpointHandlers<C, E>
|
||||||
|
}
|
||||||
|
|
||||||
export type EndpointsSchema = {
|
export type EndpointsSchema = {
|
||||||
cart?: {
|
cart?: {
|
||||||
options: {}
|
options: {}
|
||||||
@ -60,17 +90,16 @@ export type EndpointsSchema = {
|
|||||||
|
|
||||||
export type GetEndpointsSchema<
|
export type GetEndpointsSchema<
|
||||||
C extends CommerceAPI,
|
C extends CommerceAPI,
|
||||||
Schema extends EndpointsSchema = C extends CommerceAPI<any, infer E>
|
Schema extends EndpointsSchema
|
||||||
? E
|
|
||||||
: never
|
|
||||||
> = {
|
> = {
|
||||||
[E in keyof EndpointsSchema]-?: Schema[E] & {
|
[E in keyof EndpointsSchema]-?: {
|
||||||
endpoint: Endpoint<C, NonNullable<Schema[E]>>
|
schema: Schema[E]
|
||||||
handlers: EndpointHandlers<C, NonNullable<Schema[E]>>
|
} & EndpointContext<C, NonNullable<Schema[E]>>
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type X = Endpoint<CommerceAPI, NonNullable<EndpointsSchema['cart']>>
|
export type GetEndpointsFromSchema<T> = T[keyof T] extends { endpoint: infer E }
|
||||||
|
? E
|
||||||
|
: never
|
||||||
|
|
||||||
export type EndpointSchemaBase = {
|
export type EndpointSchemaBase = {
|
||||||
options: {}
|
options: {}
|
||||||
@ -111,10 +140,6 @@ export type EndpointHandlers<
|
|||||||
>
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CommerceEndpointsSchema<C> = C extends CommerceAPI<any, infer E>
|
|
||||||
? E
|
|
||||||
: never
|
|
||||||
|
|
||||||
export type HandlerOperations<E> = E extends APIEndpoint<any, any, infer T>
|
export type HandlerOperations<E> = E extends APIEndpoint<any, any, infer T>
|
||||||
? T
|
? T
|
||||||
: never
|
: never
|
||||||
@ -127,10 +152,15 @@ export type APIProvider = {
|
|||||||
config: CommerceAPIConfig
|
config: CommerceAPIConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CommerceAPI<
|
export type EndpointContext<
|
||||||
P extends APIProvider = APIProvider,
|
C extends CommerceAPI,
|
||||||
E extends EndpointsSchema = EndpointsSchema
|
E extends EndpointSchemaBase
|
||||||
> {
|
> = {
|
||||||
|
endpoint: Endpoint<C, E>
|
||||||
|
operations: EndpointHandlers<C, E>
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CommerceAPI<P extends APIProvider = APIProvider> {
|
||||||
constructor(readonly provider: P) {
|
constructor(readonly provider: P) {
|
||||||
this.provider = provider
|
this.provider = provider
|
||||||
}
|
}
|
||||||
@ -146,12 +176,12 @@ export class CommerceAPI<
|
|||||||
Object.assign(this.provider.config, newConfig)
|
Object.assign(this.provider.config, newConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint(context: {
|
endpoint<E extends GetAPISchema<this>>(
|
||||||
handler: E
|
context: E['endpoint'] & {
|
||||||
config?: P['config']
|
config?: P['config']
|
||||||
operations: HandlerOperations<typeof context.handler>
|
options?: E['schema']['endpoint']['options']
|
||||||
options?: HandlerOptions<typeof context.handler>
|
}
|
||||||
}): NextApiHandler {
|
): NextApiHandler {
|
||||||
const commerce = this
|
const commerce = this
|
||||||
const cfg = this.getConfig(context.config)
|
const cfg = this.getConfig(context.config)
|
||||||
|
|
||||||
@ -162,7 +192,7 @@ export class CommerceAPI<
|
|||||||
commerce,
|
commerce,
|
||||||
config: cfg,
|
config: cfg,
|
||||||
handlers: context.operations,
|
handlers: context.operations,
|
||||||
options: context.options,
|
options: context.options ?? {},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user