Passed
Pull Request — master (#220)
by
unknown
02:21
created

DocxAdapter   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 19
dl 0
loc 20
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A toBuffer 0 13 1
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