Add types for 'html-docx-js' package (#46089)

* feat(html-docx-js): add new type definitions for 'html-docx-js' package

* fix(html-docx-js): correct url to my authorship of html-docx-js types
This commit is contained in:
Jacob Fischer 2020-07-15 18:36:52 -05:00 committed by GitHub
parent be64ee5f75
commit b7edd37079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,48 @@
import { asBlob } from 'html-docx-js';
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<!-- page content -->
</body>
</html>
`;
// Invalid ways to call
asBlob(); // $ExpectError
asBlob(1234); // $ExpectError
asBlob(html, null); // $ExpectError
asBlob(html, { orientation: 'invalid' }); // $ExpectError
asBlob(html, { foo: ['bar'] }); // $ExpectError
// Valid ways to call
asBlob(html); // $ExpectType Blob | Buffer
asBlob(html, {}); // $ExpectType Blob | Buffer
const result1 = asBlob(html, {
orientation: 'landscape',
margins: {
top: 100,
right: 100,
bottom: 100,
left: 100,
header: 100,
footer: 100,
gutter: 100,
},
});
result1; // $ExpectType Blob | Buffer
const result2 = asBlob(html, {
margins: {
top: 75,
right: 75,
bottom: 75,
left: 75,
},
});
result2; // $ExpectType Blob | Buffer

48
types/html-docx-js/index.d.ts vendored Normal file
View File

@ -0,0 +1,48 @@
// Type definitions for html-docx-js 0.3
// Project: https://github.com/evidenceprime/html-docx-js#readme
// Definitions by: Jacob Fischer <https://github.com/JacobFischer>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
/**
* To generate DOCX, simply pass a HTML document (as string) to this method
* to receive `Blob` (or `Buffer`) containing the output file.
*/
export function asBlob(
/**
* An HTML formatted string. It should be a complete, valid HTML
* (including DOCTYPE, `html` and `body` tags).
* This may be less convenient, but gives you possibility of including
* CSS rules in `style` tags.
*/
html: string,
/** Additional options for controlling page setup for the document. */
options?: {
/**
* Page orientation. Must be `landscape` or `portrait` (default).
*/
orientation?: 'landscape' | 'portrait';
/**
* A map of margin sizes (expressed in twentieths of point, see
* [WordprocessingML documentation](http://officeopenxml.com/WPsectionPgMar.php)
* for details).
*/
margins?: {
/** The top page margin (default: 1440, i.e. 2.54 cm). */
top?: number;
/** The right page margin (default: 1440). */
right?: number;
/** The bottom page margin (default: 1440). */
bottom?: number;
/** The left page margin (default: 1440). */
left?: number;
/** The margin for the header (default: 720). */
header?: number;
/** The margin for the footer (default: 720). */
footer?: number;
/** The margin for the gutter (default: 0). */
gutter?: number;
};
},
): Blob | Buffer;

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"html-docx-js-tests.ts"
]
}

View File

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