@oicana/node 0.1.0-alpha.2 → 0.1.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,21 +8,18 @@ import type { BlobWithMetadata } from './inputs/index.js';
8
8
  */
9
9
  export declare class Template {
10
10
  private readonly template;
11
- private defaultCompilationMode;
12
11
  /**
13
- * Register a template with the given name and template file
14
- * @param name of the template
12
+ * Register a template with the given template file
15
13
  * @param template - the packed Oicana template file
16
14
  */
17
- constructor(name: string, template: Uint8Array);
15
+ constructor(template: Uint8Array);
18
16
  /**
19
- * Register a template with the given name, template file, and inputs
20
- * @param name of the template
17
+ * Register a template with the given template file and inputs
21
18
  * @param template - the packed Oicana template file
22
- * @param jsonInputs for the initial rendering to warm up the cache
23
- * @param blobInputs for the initial rendering to warm up the cache
19
+ * @param jsonInputs for the initial compilation to warm up the cache
20
+ * @param blobInputs for the initial compilation to warm up the cache
24
21
  */
25
- constructor(name: string, template: Uint8Array, jsonInputs: Map<string, string>, blobInputs: Map<string, BlobWithMetadata>);
22
+ constructor(template: Uint8Array, jsonInputs: Map<string, string>, blobInputs: Map<string, BlobWithMetadata>);
26
23
  /**
27
24
  * Compile the template to a PDF file without any inputs in production mode
28
25
  */
@@ -37,18 +34,17 @@ export declare class Template {
37
34
  * Compile the template with the given inputs
38
35
  * @param jsonInputs
39
36
  * @param blobInputs
40
- * @param exportFormat
37
+ * @param exportOptions
41
38
  */
42
- compile(jsonInputs: Map<string, string>, blobInputs: Map<string, BlobWithMetadata>, exportFormat: ExportFormat): Uint8Array;
39
+ compile(jsonInputs: Map<string, string>, blobInputs: Map<string, BlobWithMetadata>, exportOptions: ExportFormat): Uint8Array;
43
40
  /**
44
- * Get the default compilation mode of this template
45
- */
46
- defaultMode(): CompilationMode;
47
- /**
48
- * Set the default compilation mode of this template
49
- * @param compilationMode to use as default when compiling this template
41
+ * Compile the template with the given inputs
42
+ * @param jsonInputs
43
+ * @param blobInputs
44
+ * @param exportOptions
45
+ * @param compilationOptions
50
46
  */
51
- setDefaultMode(compilationMode: CompilationMode): void;
47
+ compile(jsonInputs: Map<string, string>, blobInputs: Map<string, BlobWithMetadata>, exportOptions: ExportFormat, compilationOptions: CompilationMode): Uint8Array;
52
48
  private convertBlobWithMetadata;
53
49
  private mapCompilationMode;
54
50
  }
package/dist/Template.js CHANGED
@@ -1,4 +1,5 @@
1
- import { compileTemplate, registerTemplate, } from '@oicana/node-native';
1
+ import { randomUUID } from 'node:crypto';
2
+ import { compileTemplate, exportDocument, registerTemplate, removeDocument, } from '@oicana/node-native';
2
3
  import { CompilationMode } from './CompilationMode.js';
3
4
  /**
4
5
  * A template
@@ -7,44 +8,33 @@ import { CompilationMode } from './CompilationMode.js';
7
8
  */
8
9
  export class Template {
9
10
  template;
10
- defaultCompilationMode;
11
11
  /**
12
- * Register a template with the given name, template file, and inputs
13
- * @param name of the template
12
+ * Register a template with the given template file and inputs
14
13
  * @param template - the packed Oicana template file
15
- * @param jsonInputs for the initial rendering to warm up the cache
16
- * @param blobInputs for the initial rendering to warm up the cache
17
- * @param compilation mode for the initial rendering to warm up the cache
14
+ * @param jsonInputs for the initial compilation to warm up the cache (defaults to empty map)
15
+ * @param blobInputs for the initial compilation to warm up the cache (defaults to empty map)
16
+ * @param compilationOptions for the initial compilation to warm up the cache (defaults to Development)
18
17
  */
19
- constructor(name, template, jsonInputs, blobInputs, compilationMode) {
20
- this.template = name;
21
- this.defaultCompilationMode = CompilationMode.Production;
22
- const format = { format: 'pdf' };
23
- registerTemplate(this.template, template, Object.fromEntries(jsonInputs ?? new Map()), this.convertBlobWithMetadata(blobInputs ?? new Map()), JSON.stringify(format), this.mapCompilationMode(compilationMode ?? CompilationMode.Development));
18
+ constructor(template, jsonInputs, blobInputs, compilationOptions) {
19
+ this.template = randomUUID();
20
+ registerTemplate(this.template, template, Object.fromEntries(jsonInputs ?? new Map()), this.convertBlobWithMetadata(blobInputs ?? new Map()), this.mapCompilationMode(compilationOptions ?? CompilationMode.Development));
24
21
  }
25
22
  /**
26
23
  * Compile the template with the given inputs
27
- * @param jsonInputs
28
- * @param blobInputs
29
- * @param exportFormat
30
- * @param compilationMode
24
+ * @param jsonInputs - JSON inputs for the template (defaults to empty map)
25
+ * @param blobInputs - Blob inputs for the template (defaults to empty map)
26
+ * @param exportOptions - Export format specification (defaults to PDF)
27
+ * @param compilationOptions - Compilation mode (defaults to Production)
31
28
  */
32
- compile(jsonInputs, blobInputs, exportFormat, compilationMode) {
33
- const format = exportFormat ?? { format: 'pdf' };
34
- return compileTemplate(this.template, Object.fromEntries(jsonInputs ?? new Map()), this.convertBlobWithMetadata(blobInputs ?? new Map()), JSON.stringify(format), this.mapCompilationMode(compilationMode ?? CompilationMode.Production));
35
- }
36
- /**
37
- * Get the default compilation mode of this template
38
- */
39
- defaultMode() {
40
- return this.defaultCompilationMode;
41
- }
42
- /**
43
- * Set the default compilation mode of this template
44
- * @param compilationMode to use as default when compiling this template
45
- */
46
- setDefaultMode(compilationMode) {
47
- this.defaultCompilationMode = compilationMode;
29
+ compile(jsonInputs, blobInputs, exportOptions, compilationOptions) {
30
+ const format = exportOptions ?? { format: 'pdf' };
31
+ const document = compileTemplate(this.template, Object.fromEntries(jsonInputs ?? new Map()), this.convertBlobWithMetadata(blobInputs ?? new Map()), this.mapCompilationMode(compilationOptions ?? CompilationMode.Production));
32
+ try {
33
+ return exportDocument(document, JSON.stringify(format));
34
+ }
35
+ finally {
36
+ removeDocument(document);
37
+ }
48
38
  }
49
39
  convertBlobWithMetadata(blobInputs) {
50
40
  return Object.fromEntries(Array.from(blobInputs.entries(), ([key, value]) => {
@@ -1 +1 @@
1
- {"version":3,"file":"Template.js","sourceRoot":"","sources":["../src/Template.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,eAAe,EAEf,gBAAgB,GACjB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAIvD;;;;GAIG;AACH,M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WAAW,CAAC,CAAC;QAC5B,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;IAEO,kBAAkB,CAAC,IAAqB;QAC9C,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,eAAe,CAAC,WAAW;gBAC9B,iDAAyC;YAC3C,KAAK,eAAe,CAAC,UAAU;gBAC7B,gDAAwC;QAC5C,CAAC;IACH,CAAC;CACF"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oicana/node",
3
3
  "author": "Oicana <hello@oicana.com>",
4
- "version": "0.1.0-alpha.2",
4
+ "version": "0.1.0-alpha.4",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "readme": "README.md",
@@ -22,7 +22,7 @@
22
22
  "lint": "biome ci"
23
23
  },
24
24
  "dependencies": {
25
- "@oicana/node-native": "0.1.0-alpha.1"
25
+ "@oicana/node-native": "0.1.0-alpha.3"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@biomejs/biome": "2.2.4",
package/src/Template.ts CHANGED
@@ -1,8 +1,11 @@
1
+ import { randomUUID } from 'node:crypto';
1
2
  import {
2
3
  type BlobWithMetadata as BlobWithMetadataNative,
3
4
  compileTemplate,
5
+ exportDocument,
4
6
  CompilationMode as NativeCompilationMode,
5
7
  registerTemplate,
8
+ removeDocument,
6
9
  } from '@oicana/node-native';
7
10
  import { CompilationMode } from './CompilationMode.js';
8
11
  import type { ExportFormat } from './ExportFormat.js';
@@ -15,47 +18,39 @@ import type { BlobWithMetadata } from './inputs/index.js';
15
18
  */
16
19
  export class Template {
17
20
  private readonly template: string;
18
- private defaultCompilationMode: CompilationMode;
19
21
 
20
22
  /**
21
- * Register a template with the given name and template file
22
- * @param name of the template
23
+ * Register a template with the given template file
23
24
  * @param template - the packed Oicana template file
24
25
  */
25
- public constructor(name: string, template: Uint8Array);
26
+ public constructor(template: Uint8Array);
26
27
 
27
28
  /**
28
- * Register a template with the given name, template file, and inputs
29
- * @param name of the template
29
+ * Register a template with the given template file and inputs
30
30
  * @param template - the packed Oicana template file
31
- * @param jsonInputs for the initial rendering to warm up the cache
32
- * @param blobInputs for the initial rendering to warm up the cache
31
+ * @param jsonInputs for the initial compilation to warm up the cache
32
+ * @param blobInputs for the initial compilation to warm up the cache
33
33
  */
34
34
  public constructor(
35
- name: string,
36
35
  template: Uint8Array,
37
36
  jsonInputs: Map<string, string>,
38
37
  blobInputs: Map<string, BlobWithMetadata>,
39
38
  );
40
39
 
41
40
  /**
42
- * Register a template with the given name, template file, and inputs
43
- * @param name of the template
41
+ * Register a template with the given template file and inputs
44
42
  * @param template - the packed Oicana template file
45
- * @param jsonInputs for the initial rendering to warm up the cache
46
- * @param blobInputs for the initial rendering to warm up the cache
47
- * @param compilation mode for the initial rendering to warm up the cache
43
+ * @param jsonInputs for the initial compilation to warm up the cache (defaults to empty map)
44
+ * @param blobInputs for the initial compilation to warm up the cache (defaults to empty map)
45
+ * @param compilationOptions for the initial compilation to warm up the cache (defaults to Development)
48
46
  */
49
47
  public constructor(
50
- name: string,
51
48
  template: Uint8Array,
52
49
  jsonInputs?: Map<string, string>,
53
50
  blobInputs?: Map<string, BlobWithMetadata>,
54
- compilationMode?: CompilationMode,
51
+ compilationOptions?: CompilationMode,
55
52
  ) {
56
- this.template = name;
57
- this.defaultCompilationMode = CompilationMode.Production;
58
- const format = { format: 'pdf' };
53
+ this.template = randomUUID();
59
54
 
60
55
  registerTemplate(
61
56
  this.template,
@@ -64,8 +59,9 @@ export class Template {
64
59
  this.convertBlobWithMetadata(
65
60
  blobInputs ?? new Map<string, BlobWithMetadata>(),
66
61
  ),
67
- JSON.stringify(format),
68
- this.mapCompilationMode(compilationMode ?? CompilationMode.Development),
62
+ this.mapCompilationMode(
63
+ compilationOptions ?? CompilationMode.Development,
64
+ ),
69
65
  );
70
66
  }
71
67
 
@@ -88,53 +84,56 @@ export class Template {
88
84
  * Compile the template with the given inputs
89
85
  * @param jsonInputs
90
86
  * @param blobInputs
91
- * @param exportFormat
87
+ * @param exportOptions
92
88
  */
93
89
  public compile(
94
90
  jsonInputs: Map<string, string>,
95
91
  blobInputs: Map<string, BlobWithMetadata>,
96
- exportFormat: ExportFormat,
92
+ exportOptions: ExportFormat,
97
93
  ): Uint8Array;
98
94
 
99
95
  /**
100
96
  * Compile the template with the given inputs
101
97
  * @param jsonInputs
102
98
  * @param blobInputs
103
- * @param exportFormat
104
- * @param compilationMode
99
+ * @param exportOptions
100
+ * @param compilationOptions
101
+ */
102
+ public compile(
103
+ jsonInputs: Map<string, string>,
104
+ blobInputs: Map<string, BlobWithMetadata>,
105
+ exportOptions: ExportFormat,
106
+ compilationOptions: CompilationMode,
107
+ ): Uint8Array;
108
+
109
+ /**
110
+ * Compile the template with the given inputs
111
+ * @param jsonInputs - JSON inputs for the template (defaults to empty map)
112
+ * @param blobInputs - Blob inputs for the template (defaults to empty map)
113
+ * @param exportOptions - Export format specification (defaults to PDF)
114
+ * @param compilationOptions - Compilation mode (defaults to Production)
105
115
  */
106
116
  public compile(
107
117
  jsonInputs?: Map<string, string>,
108
118
  blobInputs?: Map<string, BlobWithMetadata>,
109
- exportFormat?: ExportFormat,
110
- compilationMode?: CompilationMode,
119
+ exportOptions?: ExportFormat,
120
+ compilationOptions?: CompilationMode,
111
121
  ): Uint8Array {
112
- const format: ExportFormat = exportFormat ?? { format: 'pdf' };
122
+ const format: ExportFormat = exportOptions ?? { format: 'pdf' };
113
123
 
114
- return compileTemplate(
124
+ const document = compileTemplate(
115
125
  this.template,
116
126
  Object.fromEntries(jsonInputs ?? new Map<string, string>()),
117
127
  this.convertBlobWithMetadata(
118
128
  blobInputs ?? new Map<string, BlobWithMetadata>(),
119
129
  ),
120
- JSON.stringify(format),
121
- this.mapCompilationMode(compilationMode ?? CompilationMode.Production),
130
+ this.mapCompilationMode(compilationOptions ?? CompilationMode.Production),
122
131
  );
123
- }
124
-
125
- /**
126
- * Get the default compilation mode of this template
127
- */
128
- public defaultMode(): CompilationMode {
129
- return this.defaultCompilationMode;
130
- }
131
-
132
- /**
133
- * Set the default compilation mode of this template
134
- * @param compilationMode to use as default when compiling this template
135
- */
136
- public setDefaultMode(compilationMode: CompilationMode) {
137
- this.defaultCompilationMode = compilationMode;
132
+ try {
133
+ return exportDocument(document, JSON.stringify(format));
134
+ } finally {
135
+ removeDocument(document);
136
+ }
138
137
  }
139
138
 
140
139
  private convertBlobWithMetadata(