diff --git a/CHANGES.md b/CHANGES.md index e48cf66..68870a9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,8 +4,9 @@ - Add utility functions. - In `BaseCustomizer`, the printExtent can be now set and get/set are dedicated methods. - `pdfA` (allow transparency) is now a spec.map optional param. -- spec.attributes are now partial and `datasources` attribute is removed. +- spec.attributes are now partial and `datasource` attribute is removed. - CreateSpecOptions accepts now every format. +- Add a timeout and manage errors on the `getDownloadUrl` utils function. ## v0.2.2 - Add optional MVTEncoder diff --git a/demo/demo.js b/demo/demo.js index 5809ba1..6a14a9e 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -34,7 +34,7 @@ document.querySelector('#print').addEventListener('click', async () => { dpi: 254, layout: layout, format: 'pdf', - customAttributes: {}, + customAttributes: {datasource: []}, customizer: customizer, }); @@ -54,10 +54,10 @@ document.querySelector('#print').addEventListener('click', async () => { document.location = url; return url; }, - (err) => { - console.log('result', 'error', err); - resultEl.innerHTML = 'Error'; - return err; + (error) => { + console.log('result', 'error', error); + resultEl.innerHTML = error; + return error; }, ); }); diff --git a/src/constants.ts b/src/constants.ts index cc1ca20..0afd574 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,13 +1,13 @@ export const Constants = { /** "Standardized rendering pixel size" is defined as 0.28 mm, see http://www.opengeospatial.org/standards/wmts */ WMTS_PIXEL_SIZE: 0.28e-3, - /** Standard PPI */ - POINTS_PER_INCH: 72, + /** Standard DPI */ + DOTS_PER_INCH: 72, /** According to the "international yard" definition 1 inch is defined as exactly 2.54 cm. */ METERS_PER_INCH: 0.0254, }; export const CalculatedConstants = { - /** Default to PPI / METERS per Inch */ - POINTS_PER_DISTANCE_UNIT: () => Constants.POINTS_PER_INCH / Constants.METERS_PER_INCH, + /** Default to DPI / METERS per Inch */ + DPI_PER_DISTANCE_UNIT: () => Constants.DOTS_PER_INCH / Constants.METERS_PER_INCH, }; diff --git a/src/types.ts b/src/types.ts index d249d0a..81ba338 100644 --- a/src/types.ts +++ b/src/types.ts @@ -149,6 +149,7 @@ export interface MFPStatusResponse { done: boolean; downloadURL: string; elapsedTime: number; + error?: string; status: string; waitingTime: number; } diff --git a/src/utils.ts b/src/utils.ts index e956dc5..2a7448b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,14 +6,14 @@ import type {Extent} from 'ol/extent'; import {Constants, CalculatedConstants} from './constants'; /** - * @param mapPageSize The page size (width, height) + * @param mapPageSize The page size in pixels (width, height) * @param center The coordinate of the extent's center. * @param scale The scale to calculate the extent width. - * @returns an extent that fit the page size. Calculated with POINTS_PER_DISTANCE_UNIT (by default using meters) + * @returns an extent that fit the page size. Calculated with DPI_PER_DISTANCE_UNIT (by default using meters) */ export function getPrintExtent(mapPageSize: number[], center: number[], scale: number): Extent { const [mapPageWidthMeters, mapPageHeightMeters] = mapPageSize.map( - (side) => ((side / CalculatedConstants.POINTS_PER_DISTANCE_UNIT()) * scale) / 2, + (side) => ((side / CalculatedConstants.DPI_PER_DISTANCE_UNIT()) * scale) / 2, ); return [ center[0] - mapPageWidthMeters, @@ -118,20 +118,40 @@ export async function requestReport(mfpBaseUrl: string, spec: MFPSpec): Promise< return await report.json(); } -// FIXME: add timeout -// FIXME: handle errors +/** + * @param requestReport the name of the requested report + * @param response The initial print response. + * @param interval (s) the internal to poll the download url. + * @param timeout (s) A timeout for this operation. + * @returns a Promise with the download url once the document is printed or an error. + */ export async function getDownloadUrl( requestReport: string, response: MFPReportResponse, interval = 1000, + timeout = 30000, ): Promise { + let totalDuration = 0 - interval; return new Promise((resolve, reject) => { const intervalId = setInterval(async () => { - const status = await getStatus(requestReport, response.ref); + let status: MFPStatusResponse | undefined; + try { + status = await getStatus(requestReport, response.ref); + if (status.error) { + throw new Error(status.error); + } + } catch (error) { + reject(error); + } if (status.done) { clearInterval(intervalId); resolve(`${requestReport}/report/${response.ref}`); } + totalDuration += interval; + if (totalDuration >= timeout) { + clearInterval(intervalId); + reject(new Error('Print duration exceeded')); + } }, interval); }); }