From 591cd6d4e6f74c4a195aab17a95a39610fa32000 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Fri, 14 May 2021 14:52:14 -0500 Subject: [PATCH] Changed the way ops are created --- framework/bigcommerce/api/cart/index.ts | 4 +-- framework/bigcommerce/api/index.ts | 5 +++- framework/commerce/api/index.ts | 35 ++++++++++++++++++++----- framework/commerce/api/operations.ts | 22 +++------------- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/framework/bigcommerce/api/cart/index.ts b/framework/bigcommerce/api/cart/index.ts index 3eb330643..bea1eb028 100644 --- a/framework/bigcommerce/api/cart/index.ts +++ b/framework/bigcommerce/api/cart/index.ts @@ -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 +export type CartAPI = GetAPISchema export type CartEndpoint = CartAPI['endpoint'] diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index bd4d368f7..50ea960b7 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -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

= CommerceAPI

+ export function getCommerceApi

( customProvider: P = provider as any -) { +): BigcommerceAPI

{ const api = commerceApi(customProvider) return Object.assign(api, { diff --git a/framework/commerce/api/index.ts b/framework/commerce/api/index.ts index 569d0f4a4..e656e1872 100644 --- a/framework/commerce/api/index.ts +++ b/framework/commerce/api/index.ts @@ -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, S extends APISchemas = APISchemas > = { schema: S @@ -52,7 +57,11 @@ export type APIProvider = { operations: APIOperations } -export class CommerceAPI

{ +export type CommerceAPI< + P extends APIProvider = APIProvider +> = CommerceAPICore

& AllOperations

+ +export class CommerceAPICore

{ constructor(readonly provider: P) {} getConfig(userConfig: Partial = {}): P['config'] { @@ -67,11 +76,23 @@ export class CommerceAPI

{ } } -export function getCommerceApi

(customProvider: P) { - const commerce = new CommerceAPI(customProvider) - const operations = getOperations(customProvider.operations, { commerce }) +export function getCommerceApi

( + customProvider: P +): CommerceAPI

{ + const commerce = Object.assign( + new CommerceAPICore(customProvider), + defaultOperations as AllOperations

+ ) + const ops = customProvider.operations - return Object.assign(commerce, operations) + OPERATIONS.forEach((k) => { + const op = ops[k] + if (op) { + commerce[k] = op({ commerce }) as AllOperations

[typeof k] + } + }) + + return commerce } export function getEndpoint< diff --git a/framework/commerce/api/operations.ts b/framework/commerce/api/operations.ts index 4e511b5d9..e9ef288a8 100644 --- a/framework/commerce/api/operations.ts +++ b/framework/commerce/api/operations.ts @@ -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

( - ops: P['operations'], - ctx: { commerce: CommerceAPI

} -) { - return OPERATIONS.reduce>((carry, k) => { - const op = ops[k] - if (op) { - carry[k] = op({ ...ctx, operations: carry }) - } - return carry - }, defaultOperations) as AllOperations

-} - export type AllowedOperations = typeof OPERATIONS[number] export type LoginResult = T @@ -51,7 +38,7 @@ export type APIOperations

= { } export type AllOperations

= { - [K in keyof APIOperations

]: P['operations'][K] extends ( + [K in keyof APIOperations

]-?: P['operations'][K] extends ( ...args: any ) => any ? ReturnType @@ -60,5 +47,4 @@ export type AllOperations

= { export type OperationContext

= { commerce: CommerceAPI

- operations: Operations

}