diff --git a/packages/asset-server-plugin/src/plugin.ts b/packages/asset-server-plugin/src/plugin.ts index 762b4b9618..b7a1fcdc07 100644 --- a/packages/asset-server-plugin/src/plugin.ts +++ b/packages/asset-server-plugin/src/plugin.ts @@ -221,10 +221,11 @@ export class AssetServerPlugin implements VendurePlugin { private createAssetStorageStrategy() { const toAbsoluteUrlFn = (request: Request, identifier: string): string => { + const prefix = `${request.protocol}://${request.get('host')}/${this.options.route}/`; if (!identifier) { return ''; } - return `${request.protocol}://${request.get('host')}/${this.options.route}/${identifier}`; + return identifier.startsWith(prefix) ? identifier : `${prefix}${identifier}`; }; return new LocalAssetStorageStrategy(this.options.assetUploadDir, toAbsoluteUrlFn); } diff --git a/packages/core/e2e/config/testing-asset-storage-strategy.ts b/packages/core/e2e/config/testing-asset-storage-strategy.ts index 36f99ea5db..09bbf435d5 100644 --- a/packages/core/e2e/config/testing-asset-storage-strategy.ts +++ b/packages/core/e2e/config/testing-asset-storage-strategy.ts @@ -19,7 +19,8 @@ export class TestingAssetStorageStrategy implements AssetStorageStrategy { } toAbsoluteUrl(reqest: Request, identifier: string): string { - return `test-url/${identifier}`; + const prefix = `test-url/`; + return identifier.startsWith(prefix) ? identifier : `${prefix}${identifier}`; } writeFileFromBuffer(fileName: string, data: Buffer): Promise { diff --git a/packages/core/src/api/resolvers/entity/collection-entity.resolver.ts b/packages/core/src/api/resolvers/entity/collection-entity.resolver.ts index 40d13b3aa6..a24899f7ba 100644 --- a/packages/core/src/api/resolvers/entity/collection-entity.resolver.ts +++ b/packages/core/src/api/resolvers/entity/collection-entity.resolver.ts @@ -4,7 +4,8 @@ import { PaginatedList } from '@vendure/common/lib/shared-types'; import { ListQueryOptions } from '../../../common/types/common-types'; import { Translated } from '../../../common/types/locale-types'; -import { Collection, Product, ProductVariant } from '../../../entity'; +import { Asset, Collection, Product, ProductVariant } from '../../../entity'; +import { AssetService } from '../../../service/services/asset.service'; import { CollectionService } from '../../../service/services/collection.service'; import { ProductVariantService } from '../../../service/services/product-variant.service'; import { ApiType } from '../../common/get-api-type'; @@ -17,6 +18,7 @@ export class CollectionEntityResolver { constructor( private productVariantService: ProductVariantService, private collectionService: CollectionService, + private assetService: AssetService, ) {} @ResolveProperty() @@ -62,4 +64,12 @@ export class CollectionEntityResolver { } return this.collectionService.getChildren(ctx, collection.id) as any; } + + @ResolveProperty() + async featuredAsset(@Ctx() ctx: RequestContext, @Parent() collection: Collection): Promise { + if (collection.featuredAsset) { + return collection.featuredAsset; + } + return this.assetService.getFeaturedAsset(collection); + } }