function stripImplementationDetails()

in fronts-client/src/util/validateImageSrc.ts [151:224]


function stripImplementationDetails(
	src: string,
	criteria?: Criteria,
): Promise<ImageDescription> {
	return new Promise((resolve, reject) => {
		const maybeFromGrid = grid.gridInstance.excractMediaId(src);
		if (src && urlConstants.media.imgIXDomainExpr.test(src)) {
			const localSource = src
				.substring(0, src.indexOf('?'))
				.replace(
					urlConstants.media.imgIXDomainExpr,
					urlConstants.media.staticImageCdnDomain,
				);
			resolve({
				path: localSource,
				criteria,
			});
		} else if (maybeFromGrid) {
			grid.gridInstance
				.getImage(maybeFromGrid.id)
				.catch((e: Error) =>
					reject(
						new Error(`There was a problem contacting The Grid - ${e.message}`),
					),
				)
				.then((gridImageJson: string) => ({
					crops: filterGridCrops(gridImageJson, maybeFromGrid, criteria),
					areNoCropsOfAnySize:
						filterGridCrops(gridImageJson, maybeFromGrid).length === 0,
				}))
				.then(
					({
						crops,
						areNoCropsOfAnySize,
					}: {
						crops: Crop[];
						areNoCropsOfAnySize: boolean;
					}) => {
						if (crops.length === 0 && areNoCropsOfAnySize) {
							return Promise.reject(
								new Error(
									'The image does not have any valid crops on the Grid',
								),
							);
						}

						return getSuitableImageDetails(
							crops,
							maybeFromGrid.id,
							criteria || {},
						);
					},
				)
				.then((asset: ImageDescription) =>
					resolve({
						...asset,
						criteria,
					}),
				)
				.catch(reject);
		} else if (!urlConstants.media.imageCdnDomainExpr.test(src)) {
			reject(
				new Error(
					`Images must come from ${urlConstants.media.imageCdnDomain} or the Grid`,
				),
			);
		} else {
			resolve({
				path: src,
				criteria,
			});
		}
	});
}