Added types for react-native-pdf-lib (#37314)

* Added types for react-native-pdf-lib.

* Making linter happy with default export.
This commit is contained in:
Kevin Brown 2019-08-03 09:07:06 +10:00 committed by Jesse Trinity
parent 57cfe86813
commit 388b2e06bd
4 changed files with 110 additions and 0 deletions

57
types/react-native-pdf-lib/index.d.ts vendored Normal file
View File

@ -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 <https://github.com/thekevinbrown>
// 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<string>;
}
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;
}

View File

@ -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<string>
// 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

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }