From 388b2e06bd30df9455911398394a4dd9baedb78f Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 3 Aug 2019 09:07:06 +1000 Subject: [PATCH] Added types for react-native-pdf-lib (#37314) * Added types for react-native-pdf-lib. * Making linter happy with default export. --- types/react-native-pdf-lib/index.d.ts | 57 +++++++++++++++++++ .../react-native-pdf-lib-tests.ts | 29 ++++++++++ types/react-native-pdf-lib/tsconfig.json | 23 ++++++++ types/react-native-pdf-lib/tslint.json | 1 + 4 files changed, 110 insertions(+) create mode 100644 types/react-native-pdf-lib/index.d.ts create mode 100644 types/react-native-pdf-lib/react-native-pdf-lib-tests.ts create mode 100644 types/react-native-pdf-lib/tsconfig.json create mode 100644 types/react-native-pdf-lib/tslint.json diff --git a/types/react-native-pdf-lib/index.d.ts b/types/react-native-pdf-lib/index.d.ts new file mode 100644 index 0000000000..c774280cde --- /dev/null +++ b/types/react-native-pdf-lib/index.d.ts @@ -0,0 +1,57 @@ +// Type definitions for react-native-pdf-lib 0.2 +// Project: https://github.com/Hopding/react-native-pdf-lib#readme +// Definitions by: Kevin Brown +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare let PDFLib: { + getDocumentsDirectory(): string; +}; + +export default PDFLib; + +export class PDFDocument { + static create(path: string): PDFDocument; + + addPages(pages: PDFPage[]): PDFDocument; + + /* Saves the document and returns the path to the file it wrote */ + write(): Promise; +} + +export interface SetMediaBoxOptions { + x?: number; + y?: number; +} + +export interface TextDrawingOptions { + x?: number; + y?: number; + color?: string; + fontName?: string; + fontSize?: number; +} + +export interface RectangleDrawingOptions { + x?: number; + y?: number; + width?: number; + height?: number; + color?: string; +} + +export interface ImageDrawingOptions { + x?: number; + y?: number; + width?: number; + height?: number; +} + +export class PDFPage { + static create(): PDFPage; + + setMediaBox(width: number, height: number, options?: SetMediaBoxOptions): PDFPage; + + drawText(text: string, options?: TextDrawingOptions): PDFPage; + drawRectangle(options?: RectangleDrawingOptions): PDFPage; + drawImage(imageUri: string, options?: ImageDrawingOptions): PDFPage; +} diff --git a/types/react-native-pdf-lib/react-native-pdf-lib-tests.ts b/types/react-native-pdf-lib/react-native-pdf-lib-tests.ts new file mode 100644 index 0000000000..ac681ec50e --- /dev/null +++ b/types/react-native-pdf-lib/react-native-pdf-lib-tests.ts @@ -0,0 +1,29 @@ +import PDFLib, { PDFDocument, PDFPage } from 'react-native-pdf-lib'; + +PDFLib.getDocumentsDirectory(); // $ExpectType string + +// Document Tests +PDFDocument.create('/some/path'); // $ExpectType PDFDocument + +const document = PDFDocument.create('/some/path'); +document.addPages([PDFPage.create()]); // $ExpectType PDFDocument +document.write(); // $ExpectType Promise + +// Page Tests +PDFPage.create(); // $ExpectType PDFPage + +const page = PDFPage.create(); +page.setMediaBox(10, 10); // $ExpectType PDFPage +page.setMediaBox(10, 10, { x: 10, y: 10 }); // $ExpectType PDFPage + +page.drawRectangle(); // $ExpectType PDFPage +page.drawRectangle({}); // $ExpectType PDFPage +page.drawRectangle({ x: 10, y: 10, width: 10, height: 10, color: '#fff' }); // $ExpectType PDFPage + +page.drawImage('ph:/some/uri'); // $ExpectType PDFPage +page.drawImage('ph:/some/uri', {}); // $ExpectType PDFPage +page.drawImage('ph:/some/uri', { x: 10, y: 10, width: 10, height: 10 }); // $ExpectType PDFPage + +page.drawText('Some string'); // $ExpectType PDFPage +page.drawText('Some string', {}); // $ExpectType PDFPage +page.drawText('Some string', { x: 10, y: 10, color: '#fff', fontName: 'TimesNewRoman', fontSize: 10 }); // $ExpectType PDFPage diff --git a/types/react-native-pdf-lib/tsconfig.json b/types/react-native-pdf-lib/tsconfig.json new file mode 100644 index 0000000000..d26a281b1f --- /dev/null +++ b/types/react-native-pdf-lib/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "react-native-pdf-lib-tests.ts" + ] +} diff --git a/types/react-native-pdf-lib/tslint.json b/types/react-native-pdf-lib/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-native-pdf-lib/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }