diff --git a/packages/core/src/api/middleware/asset-interceptor.spec.ts b/packages/core/src/api/middleware/asset-interceptor.spec.ts index 1928e39ae7..1cb3fc540b 100644 --- a/packages/core/src/api/middleware/asset-interceptor.spec.ts +++ b/packages/core/src/api/middleware/asset-interceptor.spec.ts @@ -47,6 +47,18 @@ describe('AssetInterceptor', () => { ), ); + it( + 'handles an Asset entity directly', + testInterceptor( + mockAsset(), + (response, result, toAbsoluteUrl) => { + expect(result.source).toBe('visited'); + expect(result.preview).toBe('visited'); + expect(toAbsoluteUrl).toHaveBeenCalledTimes(2); + }, + ), + ); + it( 'visits a top-level Asset', testInterceptor( diff --git a/packages/core/src/api/middleware/asset-interceptor.ts b/packages/core/src/api/middleware/asset-interceptor.ts index 673dcad8be..f57670753a 100644 --- a/packages/core/src/api/middleware/asset-interceptor.ts +++ b/packages/core/src/api/middleware/asset-interceptor.ts @@ -34,15 +34,20 @@ export class AssetInterceptor implements NestInterceptor { const request = ctx.req; return next.handle().pipe( map(data => { - visitType(data, [Asset, 'productPreview', 'productVariantPreview'], asset => { - if (asset instanceof Asset) { - asset.preview = toAbsoluteUrl(request, asset.preview); - asset.source = toAbsoluteUrl(request, asset.source); - } else { - asset = toAbsoluteUrl(request, asset); - } - return asset; - }); + if (data instanceof Asset) { + data.preview = toAbsoluteUrl(request, data.preview); + data.source = toAbsoluteUrl(request, data.source); + } else { + visitType(data, [Asset, 'productPreview', 'productVariantPreview'], asset => { + if (asset instanceof Asset) { + asset.preview = toAbsoluteUrl(request, asset.preview); + asset.source = toAbsoluteUrl(request, asset.source); + } else { + asset = toAbsoluteUrl(request, asset); + } + return asset; + }); + } return data; }), );