Skip to content

Commit

Permalink
fix: remove addHostToImage function
Browse files Browse the repository at this point in the history
  • Loading branch information
fpdrozd committed Mar 23, 2023
1 parent eacbc2b commit e45ca89
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 30 deletions.
5 changes: 2 additions & 3 deletions packages/api-client/src/api/getProduct/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Logger } from '@vue-storefront/core';
import { ApiContext, GetProductParams, ProductVariant } from '../../types';
import { addHostToProductImages, deserializeSingleProductVariants } from '../serializers/product';
import { deserializeSingleProductVariants } from '../serializers/product';

export default async function getProduct({ client, config }: ApiContext, { slug }: GetProductParams): Promise<ProductVariant[]> {
const currency = await config.internationalization.getCurrency();
Expand Down Expand Up @@ -30,8 +30,7 @@ export default async function getProduct({ client, config }: ApiContext, { slug
if (result.isSuccess()) {
try {
const data = result.success();
const productsData = addHostToProductImages(data, config);
const deserializedVariants = deserializeSingleProductVariants(productsData);
const deserializedVariants = deserializeSingleProductVariants(data);
return deserializedVariants;
} catch (e) {
Logger.error(e);
Expand Down
5 changes: 2 additions & 3 deletions packages/api-client/src/api/getProducts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Logger } from '@vue-storefront/core';
import type { ApiContext, GetProductsParams, ProductSearchResult } from '../../types';
import { addHostToProductImages, deserializeLimitedVariants } from '../serializers/product';
import { deserializeLimitedVariants } from '../serializers/product';
import { deserializeSearchMetadata } from '../serializers/search';

export default async function getProducts({ client, config }: ApiContext, params: GetProductsParams): Promise<ProductSearchResult> {
Expand Down Expand Up @@ -50,10 +50,9 @@ export default async function getProducts({ client, config }: ApiContext, params

if (result.isSuccess()) {
const data = result.success();
const productsData = addHostToProductImages(data, config);

return {
data: deserializeLimitedVariants(productsData),
data: deserializeLimitedVariants(data),
meta: deserializeSearchMetadata(data.meta, optionTypeFilters, productPropertyFilters)
};
} else {
Expand Down
25 changes: 1 addition & 24 deletions packages/api-client/src/api/serializers/product.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type {
JsonApiDocument,
JsonApiResponse,
IProduct,
IProducts,
ProductAttr,
RelationType
} from '@spree/storefront-api-v2-sdk/dist/server';
import type { ApiConfig, ProductVariant, OptionType, OptionValue, Image } from '../../types';
import type { ProductVariant, OptionType, OptionValue, Image } from '../../types';
import { extractRelationships, filterAttachments } from './common';

const groupIncluded = <Groups extends keyof any>(included, discriminators): { [key in Groups]: JsonApiDocument[] } => {
Expand Down Expand Up @@ -229,25 +228,3 @@ export const deserializeLimitedVariants = (apiProducts: IProducts): ProductVaria
};
});
};

const addHostToImage = (image, config: ApiConfig) => ({
...image,
attributes: {
...image.attributes,
styles: image.attributes?.styles ? image.attributes.styles.map((style) => ({
width: style.width,
height: style.height,
url: (config.assetsUrl || config.backendUrl) + style.url
})) : []
}
});

const addHostToIncluded = (included: JsonApiDocument[], config: ApiConfig) =>
included.map((e) =>
e.type === 'image' ? addHostToImage(e, config) : e
);

export const addHostToProductImages = <DocumentType extends JsonApiResponse>(apiProductsData: DocumentType, config: ApiConfig): DocumentType => ({
...apiProductsData,
included: addHostToIncluded(apiProductsData.included, config)
});

0 comments on commit e45ca89

Please sign in to comment.