| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import { internal_getRequestContext } from '@vendure/core';
- import { Request } from 'express';
- import { AssetServerOptions, ImageTransformFormat } from './types';
- export function getAssetUrlPrefixFn(options: AssetServerOptions) {
- const { assetUrlPrefix, route } = options;
- if (assetUrlPrefix == null) {
- return (request: Request, identifier: string) => {
- const protocol = request.headers['x-forwarded-proto'] ?? request.protocol;
- return `${Array.isArray(protocol) ? protocol[0] : protocol}://${
- request.get('host') ?? 'could-not-determine-host'
- }/${route}/`;
- };
- }
- if (typeof assetUrlPrefix === 'string') {
- return (...args: any[]) => assetUrlPrefix;
- }
- if (typeof assetUrlPrefix === 'function') {
- return (request: Request, identifier: string) => {
- const ctx = internal_getRequestContext(request);
- return assetUrlPrefix(ctx, identifier);
- };
- }
- throw new Error(`The assetUrlPrefix option was of an unexpected type: ${JSON.stringify(assetUrlPrefix)}`);
- }
- export function getValidFormat(format?: unknown): ImageTransformFormat | undefined {
- if (typeof format !== 'string') {
- return undefined;
- }
- switch (format) {
- case 'jpg':
- case 'jpeg':
- case 'png':
- case 'webp':
- case 'avif':
- return format;
- default:
- return undefined;
- }
- }
|