Skip to content

Commit

Permalink
fix(core): Correctly intercept top-level Assets
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed May 17, 2019
1 parent e5f614e commit d767a9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
12 changes: 12 additions & 0 deletions packages/core/src/api/middleware/asset-interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 14 additions & 9 deletions packages/core/src/api/middleware/asset-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}),
);
Expand Down

0 comments on commit d767a9d

Please sign in to comment.