Skip to content

Commit

Permalink
fix(core): Add property resolver for Collection.featuredAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed May 17, 2019
1 parent 089282e commit cd367a7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/asset-server-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/e2e/config/testing-asset-storage-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -17,6 +18,7 @@ export class CollectionEntityResolver {
constructor(
private productVariantService: ProductVariantService,
private collectionService: CollectionService,
private assetService: AssetService,
) {}

@ResolveProperty()
Expand Down Expand Up @@ -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<Asset | undefined> {
if (collection.featuredAsset) {
return collection.featuredAsset;
}
return this.assetService.getFeaturedAsset(collection);
}
}

0 comments on commit cd367a7

Please sign in to comment.