From 345782feb2525373689bc8de68ea52777ac8533c Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Wed, 21 Nov 2018 01:09:59 +0100 Subject: [PATCH] Add types for @xmpp/xml --- types/xmpp__xml/index.d.ts | 66 +++++++++++++++++++++++++++++ types/xmpp__xml/tsconfig.json | 30 +++++++++++++ types/xmpp__xml/tslint.json | 1 + types/xmpp__xml/xmpp__xml-tests.ts | 46 ++++++++++++++++++++ types/xmpp__xml/xmpp__xml-tests.tsx | 26 ++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 types/xmpp__xml/index.d.ts create mode 100644 types/xmpp__xml/tsconfig.json create mode 100644 types/xmpp__xml/tslint.json create mode 100644 types/xmpp__xml/xmpp__xml-tests.ts create mode 100644 types/xmpp__xml/xmpp__xml-tests.tsx diff --git a/types/xmpp__xml/index.d.ts b/types/xmpp__xml/index.d.ts new file mode 100644 index 0000000000..b69eacee0f --- /dev/null +++ b/types/xmpp__xml/index.d.ts @@ -0,0 +1,66 @@ +// Type definitions for @xmpp/xml 0.6 +// Project: https://github.com/xmppjs/xmpp.js/tree/master/packages/xml +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +export = xml; + +import * as ltx from 'ltx'; +import * as escape from 'ltx/lib/escape'; +import { EventEmitter } from 'events'; + +declare function xml( + name: string, + attrs?: string | { [key: string]: any }, + ...children: xml.ElementChild[] +): xml.Element; + +declare namespace xml { + function x( + name: string, + attrs?: string | { [key: string]: any }, + ...children: ElementChild[] + ): Element; + + const escapeXML: typeof escape.escapeXML; + const unescapeXML: typeof escape.unescapeXML; + const escapeXMLText: typeof escape.escapeXMLText; + const unescapeXMLText: typeof escape.unescapeXMLText; + + class Element extends ltx.Element { + attrs: { [key: string]: any }; + append(nodes: ElementChild): Element; + prepend(nodes: ElementChild): Element; + } + type ElementChild = Element | Element[] | string | number | boolean; + + class Parser extends EventEmitter { + readonly XMLError: typeof XMLError; + readonly parser: ltx.Parser; + root: Element | null; + cursor: Element | null; + + onStartElement(name: string, attrs?: string | { [attrName: string]: any }): void; + onEndElement(name: string): void; + onText(str: string): void; + write(data: Buffer): void; + end(data: Buffer): void; + } +} + +declare class XMLError extends Error { + readonly name: 'XMLError'; +} + +declare global { + namespace JSX { + interface IntrinsicElements { + [elemName: string]: any; + } + + type Element = xml.Element; + } +} diff --git a/types/xmpp__xml/tsconfig.json b/types/xmpp__xml/tsconfig.json new file mode 100644 index 0000000000..3fbf44dc71 --- /dev/null +++ b/types/xmpp__xml/tsconfig.json @@ -0,0 +1,30 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "paths": { + "@xmpp/xml": [ + "xmpp__xml" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "jsx": "preserve" + }, + "files": [ + "index.d.ts", + "xmpp__xml-tests.ts", + "xmpp__xml-tests.tsx" + ] +} diff --git a/types/xmpp__xml/tslint.json b/types/xmpp__xml/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/xmpp__xml/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/xmpp__xml/xmpp__xml-tests.ts b/types/xmpp__xml/xmpp__xml-tests.ts new file mode 100644 index 0000000000..84820feaaa --- /dev/null +++ b/types/xmpp__xml/xmpp__xml-tests.ts @@ -0,0 +1,46 @@ +import xml = require('@xmpp/xml'); + +xml.escapeXML(''); +xml.unescapeXML(''); +xml.escapeXMLText(''); +xml.unescapeXMLText(''); + +const parser = new xml.Parser(); +parser.onStartElement('el'); +parser.onEndElement('el'); +parser.onText('text'); +parser.write((null as any) as Buffer); +parser.end((null as any) as Buffer); + +const recipient = 'user@example.com'; +const days = ['Monday', 'Tuesday']; +const message = xml( + 'message', + { to: recipient }, + xml('body', {}, 1 + 2), + xml('days', days.map(day => xml('day', {}, day))) +); +xml.x( + 'message', + { to: recipient }, + xml('body', {}, 1 + 2), + xml('days', days.map(day => xml('day', {}, day))) +); + +message.attrs; +message.getChild('body')!.text(); +message.getChild('body')!.toString(); +message.getChild('days')!.getChildren('day'); +message.getChildText('body'); + +message.attrs.type = 'chat'; +Object.assign(message.attrs, { type: 'chat' }); +message.getChild('body')!.text('Hello world'); +message.append(xml('foo')); +message.append('bar'); +message.append(days.map(day => xml('day', {}, day))); +message.prepend(xml('foo')); +message.prepend('bar'); +message.prepend(days.map(day => xml('day', {}, day))); +const body = message.getChild('body'); +message.remove(body!); diff --git a/types/xmpp__xml/xmpp__xml-tests.tsx b/types/xmpp__xml/xmpp__xml-tests.tsx new file mode 100644 index 0000000000..010cc43f2a --- /dev/null +++ b/types/xmpp__xml/xmpp__xml-tests.tsx @@ -0,0 +1,26 @@ +/** @jsx xml */ + +import xml = require('@xmpp/xml'); + +const recipient = 'user@example.com'; +const days = ['Monday', 'Tuesday']; +const message = ( + + {1 + 2} + + {days.map(day => ( + ${day} + ))} + + +); + +// write +message.append( + + {JSON.stringify(days)} + +); + +// read +JSON.parse(message.getChild('myevent', 'xmpp:example.org')!.getChildText('json', 'urn:xmpp:json:0')!);