Add helpers to shopify items

This commit is contained in:
2024-04-19 16:02:09 -06:00
parent aa1277fafd
commit 4e07eee0b9
4 changed files with 43 additions and 9 deletions

View File

@@ -1,16 +1,20 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ShopifyProduct, parseShopifyProduct } from './shopifyUtils'; import { ShopifyProduct, ShopifyProductWithHelpers, addShopifyHelperProperties, parseShopifyProduct } from './shopifyUtils';
import axios from 'axios'; import axios from 'axios';
import { map, pipe } from 'ramda';
@Injectable() @Injectable()
export class BindleService { export class BindleService {
public readonly shopifyUrl = 'https://bindlecoffee.com/products.json' public readonly shopifyUrl = 'https://bindlecoffee.com/products.json'
public async fetchProducts(): Promise<ShopifyProduct<Date>[]> { public async fetchProducts(): Promise<ShopifyProductWithHelpers[]> {
const response = await axios.get(this.shopifyUrl) const response = await axios.get(this.shopifyUrl)
if (response.status !== 200) { if (response.status !== 200) {
throw new Error('Failed to fetch products') 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<string>[])
} }
} }

View File

@@ -1,16 +1,20 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ShopifyProduct, parseShopifyProduct } from './shopifyUtils'; import { ShopifyProduct, ShopifyProductWithHelpers, addShopifyHelperProperties, parseShopifyProduct } from './shopifyUtils';
import axios from 'axios'; import axios from 'axios';
import { map, pipe } from 'ramda';
@Injectable() @Injectable()
export class HarbingerService { export class HarbingerService {
public readonly shopifyUrl = 'https://harbingercoffee.com/products.json' public readonly shopifyUrl = 'https://harbingercoffee.com/products.json'
public async fetchProducts(): Promise<ShopifyProduct<Date>[]> { public async fetchProducts(): Promise<ShopifyProductWithHelpers[]> {
const response = await axios.get(this.shopifyUrl) const response = await axios.get(this.shopifyUrl)
if (response.status !== 200) { if (response.status !== 200) {
throw new Error('Failed to fetch products') 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<string>[])
} }
} }

View File

@@ -1,16 +1,20 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ShopifyProduct, parseShopifyProduct } from './shopifyUtils'; import { ShopifyProduct, ShopifyProductWithHelpers, addShopifyHelperProperties, parseShopifyProduct } from './shopifyUtils';
import axios from 'axios'; import axios from 'axios';
import { map, pipe } from 'ramda';
@Injectable() @Injectable()
export class LimaService { export class LimaService {
public readonly shopifyUrl = 'https://www.limacoffeeroasters.com/products.json' public readonly shopifyUrl = 'https://www.limacoffeeroasters.com/products.json'
public async fetchProducts(): Promise<ShopifyProduct<Date>[]> { public async fetchProducts(): Promise<ShopifyProductWithHelpers[]> {
const response = await axios.get(this.shopifyUrl) const response = await axios.get(this.shopifyUrl)
if (response.status !== 200) { if (response.status !== 200) {
throw new Error('Failed to fetch products') 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<string>[])
} }
} }

View File

@@ -51,6 +51,28 @@ export interface ShopifyProduct<T> {
} }
export interface ShopifyProductWithHelpers extends Omit<ShopifyProduct<Date>, "images"> {
productUrl: string;
images: ({
imaginaryUrl: string;
} | ShopifyImage<Date>)[]
}
export interface helperData {
shopBaseUrl: string,
}
export const addShopifyHelperProperties = (data: helperData) => (productIn: ShopifyProduct<Date>): 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<string | Date>): ShopifyProduct<Date> => { export const parseShopifyProductDates = (productIn: ShopifyProduct<string | Date>): ShopifyProduct<Date> => {
return { return {
...productIn, ...productIn,