diff --git a/types/pdf-fill-form/index.d.ts b/types/pdf-fill-form/index.d.ts new file mode 100644 index 0000000000..8747cfe868 --- /dev/null +++ b/types/pdf-fill-form/index.d.ts @@ -0,0 +1,50 @@ +// Type definitions for pdf-fill-form 5.0 +// Project: https://github.com/tpisto/pdf-fill-form#readme +// Definitions by: Grégoire Lodi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + +export interface WritableFields { + [key: string]: string; +} + +export type ReadableFields = Array<{ + name: string; + page: number; + value: string; + id: number; + type: string; +}>; + +export interface PdfOptions { + save?: string; + cores?: number; + scale?: number; + antialias?: boolean; +} + +export interface ImgPdfOptions extends PdfOptions { + startPage?: number; + endPage?: number; +} + +export type Options = PdfOptions | ImgPdfOptions; + +export type WriteAsyncCallback = (err: Error, result: Buffer) => void; + +export function read(sourceFile: string): Promise; +export function readSync(sourceFile: string): ReadableFields; +export function readBuffer(sourceBuffer: Buffer): Promise; +export function readBufferSync(sourceBuffer: Buffer): ReadableFields; + +export function write(sourceFile: string, fields: WritableFields, options?: Options): Promise; +export function writeBuffer(sourceBuffer: Buffer, fields: WritableFields, options?: Options): Promise; +export function writeBufferSync(sourceBuffer: Buffer, fields: WritableFields, options?: Options): Buffer; + +// Options are not optional here because the callback MUST be defined to avoid a crash +export function writeAsync( + sourceFile: string, + fields: WritableFields, + options: Options, + callback: WriteAsyncCallback, +): void; diff --git a/types/pdf-fill-form/pdf-fill-form-tests.ts b/types/pdf-fill-form/pdf-fill-form-tests.ts new file mode 100644 index 0000000000..82fd0728c1 --- /dev/null +++ b/types/pdf-fill-form/pdf-fill-form-tests.ts @@ -0,0 +1,24 @@ +import { write, writeBuffer, read, readBuffer, writeAsync, ReadableFields } from 'pdf-fill-form'; +import { readFileSync } from 'fs'; + +const buffer = readFileSync('test.pdf'); + +async function main() { + // Workaround because TS 3.9 resolves to ReadableFields and TS 3.6 to the object + // $ExpectType ReadableFields || { name: string; page: number; value: string; id: number; type: string; }[] + await read('./test.pdf'); + + // $ExpectType ReadableFields || { name: string; page: number; value: string; id: number; type: string; }[] + await readBuffer(buffer); + + // $ExpectType Buffer + await write('test.pdf', { field: 'test' }, { save: 'pdf' }); + + // $ExpectType Buffer + await writeBuffer(buffer, { otherField: '123' }, { save: 'imgpdf', antialias: true }); + + // $ExpectError + await writeBuffer(buffer, { field: 123 }); +} + +main(); diff --git a/types/pdf-fill-form/tsconfig.json b/types/pdf-fill-form/tsconfig.json new file mode 100644 index 0000000000..b5c6b00730 --- /dev/null +++ b/types/pdf-fill-form/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "pdf-fill-form-tests.ts"] +} diff --git a/types/pdf-fill-form/tslint.json b/types/pdf-fill-form/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/pdf-fill-form/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }