From 4e07eee0b9077b885948e5c4a0153a13e93ad0df Mon Sep 17 00:00:00 2001 From: Chip Wasson Date: Fri, 19 Apr 2024 16:02:09 -0600 Subject: [PATCH] Add helpers to shopify items --- src/fococoffee/bindle.service.ts | 10 +++++++--- src/fococoffee/harbinger.service.ts | 10 +++++++--- src/fococoffee/lima.service.ts | 10 +++++++--- src/fococoffee/shopifyUtils.ts | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/fococoffee/bindle.service.ts b/src/fococoffee/bindle.service.ts index 896cd13..1da567b 100644 --- a/src/fococoffee/bindle.service.ts +++ b/src/fococoffee/bindle.service.ts @@ -1,16 +1,20 @@ import { Injectable } from '@nestjs/common'; -import { ShopifyProduct, parseShopifyProduct } from './shopifyUtils'; +import { ShopifyProduct, ShopifyProductWithHelpers, addShopifyHelperProperties, parseShopifyProduct } from './shopifyUtils'; import axios from 'axios'; +import { map, pipe } from 'ramda'; @Injectable() export class BindleService { public readonly shopifyUrl = 'https://bindlecoffee.com/products.json' - public async fetchProducts(): Promise[]> { + public async fetchProducts(): Promise { const response = await axios.get(this.shopifyUrl) if (response.status !== 200) { throw new Error('Failed to fetch products') } - return response.data.products.map(parseShopifyProduct); + return pipe( + map(parseShopifyProduct), + map(addShopifyHelperProperties({ shopBaseUrl: 'https://bindlecoffee.com' })) + )(response.data.products as ShopifyProduct[]) } } diff --git a/src/fococoffee/harbinger.service.ts b/src/fococoffee/harbinger.service.ts index 2f72398..acb9b83 100644 --- a/src/fococoffee/harbinger.service.ts +++ b/src/fococoffee/harbinger.service.ts @@ -1,16 +1,20 @@ import { Injectable } from '@nestjs/common'; -import { ShopifyProduct, parseShopifyProduct } from './shopifyUtils'; +import { ShopifyProduct, ShopifyProductWithHelpers, addShopifyHelperProperties, parseShopifyProduct } from './shopifyUtils'; import axios from 'axios'; +import { map, pipe } from 'ramda'; @Injectable() export class HarbingerService { public readonly shopifyUrl = 'https://harbingercoffee.com/products.json' - public async fetchProducts(): Promise[]> { + public async fetchProducts(): Promise { const response = await axios.get(this.shopifyUrl) if (response.status !== 200) { throw new Error('Failed to fetch products') } - return response.data.products.map(parseShopifyProduct); + return pipe( + map(parseShopifyProduct), + map(addShopifyHelperProperties({ shopBaseUrl: 'https://harbingercoffee.com' })) + )(response.data.products as ShopifyProduct[]) } } diff --git a/src/fococoffee/lima.service.ts b/src/fococoffee/lima.service.ts index 7ef59e1..95bc623 100644 --- a/src/fococoffee/lima.service.ts +++ b/src/fococoffee/lima.service.ts @@ -1,16 +1,20 @@ import { Injectable } from '@nestjs/common'; -import { ShopifyProduct, parseShopifyProduct } from './shopifyUtils'; +import { ShopifyProduct, ShopifyProductWithHelpers, addShopifyHelperProperties, parseShopifyProduct } from './shopifyUtils'; import axios from 'axios'; +import { map, pipe } from 'ramda'; @Injectable() export class LimaService { public readonly shopifyUrl = 'https://www.limacoffeeroasters.com/products.json' - public async fetchProducts(): Promise[]> { + public async fetchProducts(): Promise { const response = await axios.get(this.shopifyUrl) if (response.status !== 200) { throw new Error('Failed to fetch products') } - return response.data.products.map(parseShopifyProduct); + return pipe( + map(parseShopifyProduct), + map(addShopifyHelperProperties({ shopBaseUrl: 'https://www.limacoffeeroasters.com' })) + )(response.data.products as ShopifyProduct[]) } } diff --git a/src/fococoffee/shopifyUtils.ts b/src/fococoffee/shopifyUtils.ts index 63780c1..2ff11e3 100644 --- a/src/fococoffee/shopifyUtils.ts +++ b/src/fococoffee/shopifyUtils.ts @@ -51,6 +51,28 @@ export interface ShopifyProduct { } +export interface ShopifyProductWithHelpers extends Omit, "images"> { + productUrl: string; + images: ({ + imaginaryUrl: string; + } | ShopifyImage)[] +} + +export interface helperData { + shopBaseUrl: string, +} + +export const addShopifyHelperProperties = (data: helperData) => (productIn: ShopifyProduct): ShopifyProductWithHelpers => { + return { + productUrl: `${data.shopBaseUrl}/products/${productIn.handle}`, + ...productIn, + images: productIn.images.map((image) => ({ + ...image, + imaginaryUrl: `https://imaginary.hooli.co/resize?width=250&height=250&url=${image.src}` + })), + } +} + export const parseShopifyProductDates = (productIn: ShopifyProduct): ShopifyProduct => { return { ...productIn,