Home / File/ image-service.ts — astro Source File

image-service.ts — astro Source File

Architecture documentation for image-service.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.

File typescript CoreAstro RoutingSystem 4 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  e047b00d_e2bb_b33f_0ef7_57adb682ce73["image-service.ts"]
  f16d8c76_2866_6150_bd14_0347b59abfe9["astro"]
  e047b00d_e2bb_b33f_0ef7_57adb682ce73 --> f16d8c76_2866_6150_bd14_0347b59abfe9
  01c5fbc2_ddb6_180b_d98d_2ff488540314["assets"]
  e047b00d_e2bb_b33f_0ef7_57adb682ce73 --> 01c5fbc2_ddb6_180b_d98d_2ff488540314
  f8ab2297_5406_4f9e_e15c_c4eb026871f9["utils"]
  e047b00d_e2bb_b33f_0ef7_57adb682ce73 --> f8ab2297_5406_4f9e_e15c_c4eb026871f9
  b77270e1_f0f2_7ea7_00a0_eedcb9ad6bdb["errors"]
  e047b00d_e2bb_b33f_0ef7_57adb682ce73 --> b77270e1_f0f2_7ea7_00a0_eedcb9ad6bdb
  style e047b00d_e2bb_b33f_0ef7_57adb682ce73 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { ExternalImageService } from 'astro';
import { baseService } from 'astro/assets';
import { isESMImportedImage } from 'astro/assets/utils';
import { AstroError } from 'astro/errors';

const SUPPORTED_FORMATS = ['avif', 'jpg', 'png', 'webp'];
const QUALITY_NAMES: Record<string, number> = { low: 25, mid: 50, high: 90, max: 100 };

function removeLeadingForwardSlash(path: string) {
	return path.startsWith('/') ? path.substring(1) : path;
}

const service: ExternalImageService = {
	getURL(options) {
		// For SVG files, return the original source path
		if (isESMImportedImage(options.src) && options.src.format === 'svg') {
			return options.src.src;
		}

		// For non-SVG files, continue with the Netlify's image processing
		const query = new URLSearchParams();

		const fileSrc = isESMImportedImage(options.src)
			? removeLeadingForwardSlash(options.src.src)
			: options.src;

		query.set('url', fileSrc);

		if (options.format) query.set('fm', options.format);
		if (options.width) query.set('w', `${options.width}`);
		if (options.height) query.set('h', `${options.height}`);
		if (options.quality) query.set('q', `${options.quality}`);

		return `/.netlify/images?${query}`;
	},
	getHTMLAttributes: baseService.getHTMLAttributes,
	getSrcSet: baseService.getSrcSet,
	validateOptions(options) {
		if (options.format && !SUPPORTED_FORMATS.includes(options.format)) {
			throw new AstroError(
				`Unsupported image format "${options.format}"`,
				`Use one of ${SUPPORTED_FORMATS.join(', ')} instead.`,
			);
		}

		if (options.quality) {
			options.quality =
				typeof options.quality === 'string' ? QUALITY_NAMES[options.quality] : options.quality;
			if (options.quality < 1 || options.quality > 100) {
				throw new AstroError(
					`Invalid quality for picture "${options.src}"`,
					'Quality needs to be between 1 and 100.',
				);
			}
		}
		return options;
	},
};

export default service;

Domain

Subdomains

Dependencies

  • assets
  • astro
  • errors
  • utils

Frequently Asked Questions

What does image-service.ts do?
image-service.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in image-service.ts?
image-service.ts defines 3 function(s): removeLeadingForwardSlash, service.getURL, service.validateOptions.
What does image-service.ts depend on?
image-service.ts imports 4 module(s): assets, astro, errors, utils.
Where is image-service.ts in the architecture?
image-service.ts is located at packages/integrations/netlify/src/image-service.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/integrations/netlify/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free