Skip to content

Commit

Permalink
Merge pull request #14 from Kazuki-tam/feature/img_url-filter
Browse files Browse the repository at this point in the history
Feature/img url filter
  • Loading branch information
Kazuki-tam authored Feb 5, 2023
2 parents 849e806 + bc4f1f9 commit 7d7e560
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shopify-email-notification",
"version": "0.0.4",
"version": "0.0.5",
"description": "shopify-email-notification is a template repository for email notifications.",
"author": "Kazuki Yonemoto (https://dev.to/tim_yone)",
"license": "MIT",
Expand Down
Binary file added public/assets/product/product_cropped_img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { paymentIconPngUrl } from './filters/paymentIconPngUrl';
import { money } from './filters/money';
import { moneyWithCurrency } from './filters/moneyWithCurrency';
import { moneyWithoutTrailingZeros } from './filters/moneyWithoutTrailingZeros';
import { imgUrl } from './filters/imgUrl';
import { shopCurrency, shopLocale } from './constants/shopEnv';

import type { Address } from './types/address';
import type { ProductType } from './types/product';

const app: express.Express = express();

Expand Down Expand Up @@ -50,6 +52,7 @@ engine.registerFilter('format_address', (address: Address) =>
engine.registerFilter('shopify_asset_url', (url: string) =>
shopifyAssetUrl(url),
);
engine.registerFilter('img_url', (item: ProductType | string) => imgUrl(item));
engine.registerFilter('cdn_asset_url', (url: string) => cdnAssetUrl(url));
engine.registerFilter('payment_icon_png_url', (payment: string) =>
paymentIconPngUrl(payment),
Expand Down
6 changes: 3 additions & 3 deletions server/constants/emailEnv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ItemArray = [
{
// image: "",
image: '/assets/product/product_cropped_img.jpg',
product: {
title: 'Aviator sunglasses',
},
Expand All @@ -14,7 +14,7 @@ const ItemArray = [
title: 'Aviator sunglasses',
},
{
// image: "",
// image: "https://cdn.shopify.com/s/files/*.jpg",
product: {
title: 'Mid-century lounger ',
},
Expand Down Expand Up @@ -245,4 +245,4 @@ const emailEnv = {
},
};

export { emailEnv };
export { emailEnv, ItemArray };
30 changes: 30 additions & 0 deletions server/filters/__tests__/imgUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { imgUrl } from '../imgUrl';

const productItem = {
image: '/assets/product/product_cropped_img.jpg',
product: {
title: 'Aviator sunglasses',
},
quantity: 1,
refunded_quantity: 0,
original_line_price: 200,
original_price: 200,
final_line_price: 200,
final_price: 200,
line_price: 200,
title: 'Aviator sunglasses',
};

/**
* Shopify img_url filter test
*/

test('Generates Image URL from a character string', () => {
expect(imgUrl('/assets/product/product_cropped_img.jpg')).toBe(
'/assets/product/product_cropped_img.jpg',
);
});

test('Generates Image URL from a product object', () => {
expect(imgUrl(productItem)).toBe('/assets/product/product_cropped_img.jpg');
});
21 changes: 21 additions & 0 deletions server/filters/imgUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { ProductType } from '@/types/product';

/**
* img_url filter
* @param { ProductType } productItem - Target url or Object
* @returns { string } - Formatted url
* https://shopify.dev/api/liquid/filters/img_url
*/

const imgUrl = (productItem: ProductType | string): string | undefined => {
let url;
if (typeof productItem === 'object') {
url = productItem.image;
}
if (typeof productItem === 'string') {
url = productItem;
}
return url;
};

export { imgUrl };
16 changes: 16 additions & 0 deletions server/types/product.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type ProductType = {
image: string;
product: {
title: string;
};
quantity: number;
refunded_quantity: number;
original_line_price: number;
original_price: number;
final_line_price: number;
final_price: number;
line_price: number;
title: string;
};

export { ProductType };

0 comments on commit 7d7e560

Please sign in to comment.