mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 14:57:10 +00:00
Merge pull request #31083 from BendingBender/xmpp-js-xml
Add types for @xmpp/xml
This commit is contained in:
Vendored
+66
@@ -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 <https://github.com/BendingBender>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
@@ -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!);
|
||||
@@ -0,0 +1,26 @@
|
||||
/** @jsx xml */
|
||||
|
||||
import xml = require('@xmpp/xml');
|
||||
|
||||
const recipient = 'user@example.com';
|
||||
const days = ['Monday', 'Tuesday'];
|
||||
const message = (
|
||||
<message to={recipient}>
|
||||
<body>{1 + 2}</body>
|
||||
<days>
|
||||
{days.map(day => (
|
||||
<day>${day}</day>
|
||||
))}
|
||||
</days>
|
||||
</message>
|
||||
);
|
||||
|
||||
// write
|
||||
message.append(
|
||||
<myevent xmlns="xmpp:example.org">
|
||||
<json xmlns="urn:xmpp:json:0">{JSON.stringify(days)}</json>
|
||||
</myevent>
|
||||
);
|
||||
|
||||
// read
|
||||
JSON.parse(message.getChild('myevent', 'xmpp:example.org')!.getChildText('json', 'urn:xmpp:json:0')!);
|
||||
Reference in New Issue
Block a user