Skip to content

Commit

Permalink
Added pdf-fill-form's types (DefinitelyTyped#43608)
Browse files Browse the repository at this point in the history
  • Loading branch information
lodi-g authored Apr 3, 2020
1 parent cc78cc2 commit d72ac9b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
50 changes: 50 additions & 0 deletions types/pdf-fill-form/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/lodi-g>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />

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<ReadableFields>;
export function readSync(sourceFile: string): ReadableFields;
export function readBuffer(sourceBuffer: Buffer): Promise<ReadableFields>;
export function readBufferSync(sourceBuffer: Buffer): ReadableFields;

export function write(sourceFile: string, fields: WritableFields, options?: Options): Promise<Buffer>;
export function writeBuffer(sourceBuffer: Buffer, fields: WritableFields, options?: Options): Promise<Buffer>;
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;
24 changes: 24 additions & 0 deletions types/pdf-fill-form/pdf-fill-form-tests.ts
Original file line number Diff line number Diff line change
@@ -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();
16 changes: 16 additions & 0 deletions types/pdf-fill-form/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
1 change: 1 addition & 0 deletions types/pdf-fill-form/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

0 comments on commit d72ac9b

Please sign in to comment.