From 9b9f9496c3fc5c8c22fdf43e2b2f7c8610036b2c Mon Sep 17 00:00:00 2001 From: Kristian Duda Date: Fri, 28 Jun 2024 14:14:21 +0200 Subject: [PATCH] refactoring --- lib/shopify/index.ts | 32 +++++++++++++++-------------- lib/shopify/payload.ts | 46 ++++++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index cdbcdce40..5ac64b667 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -1,5 +1,5 @@ import { AjaxError } from 'lib/shopify/ajax'; -import { Where, create, find, findByID, update } from 'lib/shopify/payload'; +import { Payload, Where } from 'lib/shopify/payload'; import { Cart as PayloadCart, Category as PayloadCategory, @@ -20,6 +20,8 @@ import { ProductVariant } from './types'; +const payload = new Payload({ baseUrl: process.env.CMS_URL }); + const reshapeCartItems = (lines: PayloadCart['lines']): CartItem[] => { return (lines ?? []).map((item) => { const product = item.product as PayloadProduct; @@ -65,7 +67,7 @@ const reshapeCart = (cart: PayloadCart): Cart => { }; export async function createCart(): Promise { - const cart = await create('carts', { lines: [] }); + const cart = await payload.create('carts', { lines: [] }); return reshapeCart(cart.doc); } @@ -73,19 +75,19 @@ export async function addToCart( cartId: string, lines: { merchandiseId: string; quantity: number }[] ): Promise { - const prevCart = await findByID('carts', cartId); + const prevCart = await payload.findByID('carts', cartId); const cartItems = await mergeItems(prevCart.lines, lines, true); - const cart = await update('carts', cartId, { + const cart = await payload.update('carts', cartId, { lines: cartItems }); return reshapeCart(cart.doc); } export async function removeFromCart(cartId: string, lineIds: string[]): Promise { - const prevCart = await findByID('carts', cartId); + const prevCart = await payload.findByID('carts', cartId); const lines = prevCart?.lines?.filter((lineItem) => !lineIds.includes(lineItem.id!)) ?? []; - const cart = await update('carts', cartId, { lines }); + const cart = await payload.update('carts', cartId, { lines }); return reshapeCart(cart.doc); } @@ -96,7 +98,7 @@ const mergeItems = async ( ): Promise => { const map = new Map((cartItems ?? []).map((item) => [item.variant, item])); - const products = await find('products', { + const products = await payload.find('products', { where: { 'variants.id': { in: lines.map((line) => line.merchandiseId) @@ -133,16 +135,16 @@ export async function updateCart( cartId: string, lines: { id: string; merchandiseId: string; quantity: number }[] ): Promise { - const prevCart = await findByID('carts', cartId); + const prevCart = await payload.findByID('carts', cartId); const cartItems = await mergeItems(prevCart.lines, lines, false); - const cart = await update('carts', cartId, { lines: cartItems }); + const cart = await payload.update('carts', cartId, { lines: cartItems }); return reshapeCart(cart.doc); } export async function getCart(cartId: string): Promise { try { - const cart = await findByID('carts', cartId); + const cart = await payload.findByID('carts', cartId); return reshapeCart(cart); } catch (error: unknown) { if (error instanceof AjaxError) { @@ -156,7 +158,7 @@ export async function getCart(cartId: string): Promise { } export async function getCollection(handle: string): Promise { - const category = await findByID('categories', handle); + const category = await payload.findByID('categories', handle); return reshapeCategory(category); } @@ -270,7 +272,7 @@ export async function getCollectionProducts({ }); } - const products = await find('products', { + const products = await payload.find('products', { where: { and: filters } @@ -293,7 +295,7 @@ const reshapeCategory = (category: PayloadCategory): Collection => { }; export async function getCollections(): Promise { - const categories = await find('categories', {}); + const categories = await payload.find('categories', {}); return [ { handle: '', @@ -335,7 +337,7 @@ export async function getPages(): Promise { } export async function getProduct(handle: string): Promise { - const product = await findByID('products', handle); + const product = await payload.findByID('products', handle); return reshapeProduct(product); } @@ -369,6 +371,6 @@ export async function getProducts({ }; } - const products = await find('products', { where }); + const products = await payload.find('products', { where }); return products.docs.map(reshapeProduct); } diff --git a/lib/shopify/payload.ts b/lib/shopify/payload.ts index 73b764ed6..1279c1fec 100644 --- a/lib/shopify/payload.ts +++ b/lib/shopify/payload.ts @@ -59,24 +59,36 @@ type FindParams = { limit?: number; }; -export const find = (collection: Collection, params: FindParams) => { - const query = qs.stringify(params, { addQueryPrefix: true }); - - const url = `${process.env.CMS_URL}/api/${collection}${query}`; - return ajax>('GET', url); +type PayloadOptions = { + baseUrl?: string; }; -export const findByID = (collection: Collection, id: string) => { - const url = `${process.env.CMS_URL}/api/${collection}/${id}`; - return ajax('GET', url); -}; +export class Payload { + readonly baseUrl?: string; -export const create = (collection: Collection, body: Partial) => { - const url = `${process.env.CMS_URL}/api/${collection}`; - return ajax>('POST', url, body); -}; + constructor({ baseUrl }: PayloadOptions) { + this.baseUrl = baseUrl; + } -export const update = (collection: Collection, id: string, body: Partial) => { - const url = `${process.env.CMS_URL}/api/${collection}/${id}`; - return ajax>('PATCH', url, body); -}; + find = (collection: Collection, params: FindParams) => { + const query = qs.stringify(params, { addQueryPrefix: true }); + + const url = `${this.baseUrl}/api/${collection}${query}`; + return ajax>('GET', url); + }; + + findByID = (collection: Collection, id: string) => { + const url = `${this.baseUrl}/api/${collection}/${id}`; + return ajax('GET', url); + }; + + create = (collection: Collection, body: Partial) => { + const url = `${this.baseUrl}/api/${collection}`; + return ajax>('POST', url, body); + }; + + update = (collection: Collection, id: string, body: Partial) => { + const url = `${this.baseUrl}/api/${collection}/${id}`; + return ajax>('PATCH', url, body); + }; +}