From 1a420fd43ecd127f6352fb8ff1a6b7bf30cbb045 Mon Sep 17 00:00:00 2001 From: Andrew Short Date: Mon, 17 Oct 2016 00:36:08 +1000 Subject: [PATCH] Add typings for xmlrpc (#12021) --- xmlrpc/xmlrpc-tests.ts | 28 +++++++++++++ xmlrpc/xmlrpc.d.ts | 90 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 xmlrpc/xmlrpc-tests.ts create mode 100644 xmlrpc/xmlrpc.d.ts diff --git a/xmlrpc/xmlrpc-tests.ts b/xmlrpc/xmlrpc-tests.ts new file mode 100644 index 0000000000..a31d6a9e5d --- /dev/null +++ b/xmlrpc/xmlrpc-tests.ts @@ -0,0 +1,28 @@ +/// + +import * as xmlrpc from 'xmlrpc'; + +const serverOpts = { + host: 'localhost', + port: 9000 +}; + +const server = xmlrpc.createServer(serverOpts, () => { + server.on('NotFound', method => { + console.log(`Method ${method} not found`); + }) + + server.on('hello', (err, params, cb) => { + cb(null, `Hello, ${params[0]}!`); + }); + + var client = xmlrpc.createClient({ + host: 'localhost', + port: 9000, + path: '/' + }); + + client.methodCall('hello', ['world'], (err, val) => { + console.log(val); + }); +}); diff --git a/xmlrpc/xmlrpc.d.ts b/xmlrpc/xmlrpc.d.ts new file mode 100644 index 0000000000..3e167de970 --- /dev/null +++ b/xmlrpc/xmlrpc.d.ts @@ -0,0 +1,90 @@ +// Type definitions for xmlrpc 1.3.2 +// Project: https://github.com/baalexander/node-xmlrpc +// Definitions by: Andrew Short +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module 'xmlrpc' { + import { EventEmitter } from 'events'; + import { Server as HttpServer } from 'http'; + import { Server as HttpsServer } from 'https'; + import { TlsOptions } from 'tls'; + + interface ClientOptions { + host?: string; + path?: string; + port?: number; + url?: string; + cookies?: boolean; + headers?: { [header: string]: string }; + basic_auth?: { user: string, pass: string }; + method?: string; + } + + interface ServerOptions { + host?: string; + path?: string; + port?: number; + } + + interface DateFormatterOptions { + colons?: boolean; + hyphens?: boolean; + local?: boolean; + ms?: boolean; + offset?: boolean; + } + + class Cookies { + get(name: string): string; + set(name: string, value: string, options?: { secure: boolean, expires: Date }): void; + toString(): string; + } + + namespace xmlrpc { + function createClient(options: string | ClientOptions): Client; + function createSecureClient(options: string | ClientOptions): Client; + + function createServer(options: string | ServerOptions, callback: () => void): Server; + function createSecureServer(options: string | TlsOptions, callback: () => void): Server; + + interface Client { + options: ClientOptions; + isSecure: boolean; + headersProcessors: { processors: HeadersProcessor[] }; + cookies?: Cookies; + + methodCall(method: string, params: any[], callback: (error: Object, value: any) => void): void; + + getCookie(name: string): string; + setCookie(name: string, value: string): this; + } + + type ServerFunction = (error: any, params: any, callback: (error: any, value: any) => void) => void; + type ServerNotFoundFunction = (methodName: string, params: any[]) => void; + + interface Server extends EventEmitter { + httpServer: HttpServer | HttpsServer; + + on(eventName: 'NotFound', callback: ServerNotFoundFunction): this; + on(eventName: string, callback: ServerFunction): this; + } + + type Headers = { [header: string]: string }; + + interface HeadersProcessor { + composeRequest(headers: Headers): void; + parseResponse(headers: Headers): void; + } + + export var dateFormatter: { + setOpts(opts: DateFormatterOptions): void; + + decodeIso8601(time: string): Date; + encodeIso8601(date: Date): string; + } + } + + export = xmlrpc; +}