Changed the way ops are created

This commit is contained in:
Luis Alvarez 2021-05-14 14:52:14 -05:00
parent f0413fa0a4
commit 591cd6d4e6
4 changed files with 38 additions and 28 deletions

View File

@ -1,12 +1,12 @@
import type { GetAPISchema } from '@commerce/api'
import type { CartSchema } from '../../types/cart'
import type { CommerceAPI } from '..'
import type { BigcommerceAPI } from '..'
import getCart from './get-cart'
import addItem from './add-item'
import updateItem from './update-item'
import removeItem from './remove-item'
export type CartAPI = GetAPISchema<CommerceAPI, CartSchema>
export type CartAPI = GetAPISchema<BigcommerceAPI, CartSchema>
export type CartEndpoint = CartAPI['endpoint']

View File

@ -1,6 +1,7 @@
import type { NextApiHandler } from 'next'
import type { RequestInit } from '@vercel/fetch'
import {
CommerceAPI,
CommerceAPIConfig,
getCommerceApi as commerceApi,
getEndpoint,
@ -112,9 +113,11 @@ export type Provider = typeof provider
export type APIs = CartAPI
export type BigcommerceAPI<P extends Provider = Provider> = CommerceAPI<P>
export function getCommerceApi<P extends Provider>(
customProvider: P = provider as any
) {
): BigcommerceAPI<P> {
const api = commerceApi(customProvider)
return Object.assign(api, {

View File

@ -2,12 +2,17 @@ import type { NextApiHandler } from 'next'
import type { RequestInit, Response } from '@vercel/fetch'
import type { APIEndpoint, APIHandler } from './utils/types'
import type { CartSchema } from '../types/cart'
import { APIOperations, getOperations } from './operations'
import {
defaultOperations,
OPERATIONS,
AllOperations,
APIOperations,
} from './operations'
export type APISchemas = CartSchema
export type GetAPISchema<
C extends CommerceAPI,
C extends CommerceAPI<any>,
S extends APISchemas = APISchemas
> = {
schema: S
@ -52,7 +57,11 @@ export type APIProvider = {
operations: APIOperations<any>
}
export class CommerceAPI<P extends APIProvider = APIProvider> {
export type CommerceAPI<
P extends APIProvider = APIProvider
> = CommerceAPICore<P> & AllOperations<P>
export class CommerceAPICore<P extends APIProvider = APIProvider> {
constructor(readonly provider: P) {}
getConfig(userConfig: Partial<P['config']> = {}): P['config'] {
@ -67,11 +76,23 @@ export class CommerceAPI<P extends APIProvider = APIProvider> {
}
}
export function getCommerceApi<P extends APIProvider>(customProvider: P) {
const commerce = new CommerceAPI(customProvider)
const operations = getOperations(customProvider.operations, { commerce })
export function getCommerceApi<P extends APIProvider>(
customProvider: P
): CommerceAPI<P> {
const commerce = Object.assign(
new CommerceAPICore(customProvider),
defaultOperations as AllOperations<P>
)
const ops = customProvider.operations
return Object.assign(commerce, operations)
OPERATIONS.forEach((k) => {
const op = ops[k]
if (op) {
commerce[k] = op({ commerce }) as AllOperations<P>[typeof k]
}
})
return commerce
}
export function getEndpoint<

View File

@ -1,30 +1,17 @@
import type { ServerResponse } from 'http'
import type { APIProvider, CommerceAPI, CommerceAPIConfig } from '.'
import type { APIProvider, CommerceAPI, CommerceAPICore } from '.'
const noop = () => {
throw new Error('Not implemented')
}
const OPERATIONS = ['login'] as const
export const OPERATIONS = ['login'] as const
const defaultOperations = OPERATIONS.reduce((ops, k) => {
export const defaultOperations = OPERATIONS.reduce((ops, k) => {
ops[k] = noop
return ops
}, {} as { [K in AllowedOperations]: typeof noop })
export function getOperations<P extends APIProvider>(
ops: P['operations'],
ctx: { commerce: CommerceAPI<P> }
) {
return OPERATIONS.reduce<Operations<P>>((carry, k) => {
const op = ops[k]
if (op) {
carry[k] = op({ ...ctx, operations: carry })
}
return carry
}, defaultOperations) as AllOperations<P>
}
export type AllowedOperations = typeof OPERATIONS[number]
export type LoginResult<T extends { result?: any } = { result?: string }> = T
@ -51,7 +38,7 @@ export type APIOperations<P extends APIProvider> = {
}
export type AllOperations<P extends APIProvider> = {
[K in keyof APIOperations<P>]: P['operations'][K] extends (
[K in keyof APIOperations<P>]-?: P['operations'][K] extends (
...args: any
) => any
? ReturnType<P['operations'][K]>
@ -60,5 +47,4 @@ export type AllOperations<P extends APIProvider> = {
export type OperationContext<P extends APIProvider> = {
commerce: CommerceAPI<P>
operations: Operations<P>
}