Skip to content

Commit 87ef775

Browse files
committed
🗑️ Add deprecation warnings
1 parent cac00d2 commit 87ef775

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Minimum build requirements have been raised to Node 20 and npm 10.
5151
- The `images` property in a document definition.
5252
- The `makePdf` function in favor of the `makePdf` method on the
5353
`PdfMaker` class.
54+
- Using file paths as image sources in favor of `file:` URLs.
5455

5556
## [0.5.4] - 2024-02-25
5657

src/api/PdfMaker.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,18 @@ export class PdfMaker {
5757
async makePdf(definition: DocumentDefinition): Promise<Uint8Array> {
5858
const def = readAs(definition, 'definition', readDocumentDefinition);
5959
const ctx = { ...this.#ctx };
60-
if (def.fonts) ctx.fontStore = new FontStore(def.fonts);
61-
if (def.images) ctx.imageStore = new ImageStore(def.images);
60+
if (def.fonts) {
61+
ctx.fontStore = new FontStore(def.fonts);
62+
console.warn(
63+
'Registering fonts via document definition is deprecated. Use PdfMaker.registerFont() instead.',
64+
);
65+
}
66+
if (def.images) {
67+
ctx.imageStore = new ImageStore(def.images);
68+
console.warn(
69+
'Registering images via document definition is deprecated. Use URLs to include images instead.',
70+
);
71+
}
6272
if (def.dev?.guides != null) ctx.guides = def.dev.guides;
6373
const pages = await layoutPages(def, ctx);
6474
return await renderDocument(def, pages);

src/api/document.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export type DocumentDefinition = {
6363
* Pre-defined image data. These images can be used by their name in
6464
* the document. This is only needed if images cannot be loaded
6565
* directly from the file system.
66+
*
67+
* @deprecated Use URLs to include images.
6668
*/
6769
images?: ImagesDefinition;
6870

@@ -191,6 +193,8 @@ export type FontDefinition = {
191193
* Pre-defined image data. These images can be used by their name in the
192194
* document. This is only needed if images cannot be loaded directly
193195
* from the file system.
196+
*
197+
* @deprecated Use URLs to include images.
194198
*/
195199
export type ImagesDefinition = { [name: string]: ImageDefinition };
196200

@@ -201,6 +205,8 @@ export type ImageDefinition = {
201205
/**
202206
* The image data, as a Uint8Array, ArrayBuffer, or a base64-encoded string.
203207
* Supported image formats are PNG and JPEG.
208+
*
209+
* @deprecated Use URLs to include images.
204210
*/
205211
data: string | Uint8Array | ArrayBuffer;
206212
};

src/api/make-pdf.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import { FontStore } from '../font-store.ts';
2-
import { ImageStore } from '../image-store.ts';
3-
import { layoutPages } from '../layout/layout.ts';
4-
import { readDocumentDefinition } from '../read-document.ts';
5-
import { renderDocument } from '../render/render-document.ts';
6-
import { readAs } from '../types.ts';
71
import type { DocumentDefinition } from './document.ts';
2+
import { PdfMaker } from './PdfMaker.ts';
83

94
/**
105
* Generates a PDF from the given document definition.
@@ -16,11 +11,9 @@ import type { DocumentDefinition } from './document.ts';
1611
* that instance.
1712
*/
1813
export async function makePdf(definition: DocumentDefinition): Promise<Uint8Array> {
19-
const def = readAs(definition, 'definition', readDocumentDefinition);
20-
const fontStore = new FontStore(def.fonts ?? []);
21-
const imageStore = new ImageStore(def.images ?? []);
22-
const guides = !!def.dev?.guides;
23-
const ctx = { fontStore, imageStore, guides };
24-
const pages = await layoutPages(def, ctx);
25-
return await renderDocument(def, pages);
14+
console.warn(
15+
'makePdf is deprecated. Create an instance of `PdfMaker` and call `makePdf` on that instance.',
16+
);
17+
const pdfMaker = new PdfMaker();
18+
return await pdfMaker.makePdf(definition);
2619
}

src/image-store.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export class ImageStore {
4242
const { data } = await this.#dataLoader(url);
4343
return data;
4444
}
45+
console.warn(
46+
`Loading images from file names is deprecated ('${url}'). Use file:/ URLs instead.`,
47+
);
4548
const data = await readRelativeFile('/', url.replace(/^\/+/, ''));
4649
return new Uint8Array(data);
4750
} catch (error) {

0 commit comments

Comments
 (0)