Total Complexity | 1 |
Complexity/F | 1 |
Lines of Code | 32 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import * as pathlib from 'path'; |
||
2 | import { Inject, Injectable } from '@nestjs/common'; |
||
3 | import { Packer } from 'docx'; |
||
4 | import { IDocxService } from 'src/Application/IDocxService'; |
||
5 | import { |
||
6 | DocxOptions, |
||
7 | DocxFunction, |
||
8 | DOCX_OPTIONS_TOKEN |
||
9 | } from '../docx.interfaces'; |
||
10 | |||
11 | @Injectable() |
||
12 | export class DocxAdapter implements IDocxService { |
||
13 | constructor( |
||
14 | @Inject(DOCX_OPTIONS_TOKEN) |
||
15 | private readonly options: DocxOptions |
||
16 | ) {} |
||
17 | |||
18 | public async toBuffer( |
||
19 | name: string, |
||
20 | locals: Record<string, any> = {} |
||
21 | ): Promise<Buffer> { |
||
22 | // Dynamically load docx generator function from: |
||
23 | // {root}/{name}/docx.ts |
||
24 | const path = pathlib.join(this.options.root, name, 'docx'); |
||
25 | const mod: any = await import(path); |
||
26 | const fn: DocxFunction = mod.fn; |
||
27 | |||
28 | const doc = fn(locals); |
||
29 | return Packer.toBuffer(doc); |
||
30 | } |
||
31 | } |
||
32 |