diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 000000000..4432d08ad --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,3 @@ +import * as v1 from "./v1/index"; +export { v1 } +export default v1; diff --git a/lib/v1/driver.d.ts b/lib/v1/driver.d.ts new file mode 100644 index 000000000..ca737a7bb --- /dev/null +++ b/lib/v1/driver.d.ts @@ -0,0 +1,54 @@ +import Session from "./session"; +import Pool from "./internal/pool"; +import Integer from "./integer"; +import { connect, Connection } from "./internal/connector"; +import StreamObserver from "./internal/stream-observer"; +import { newError, SERVICE_UNAVAILABLE } from "./error"; + +interface AuthCredentials { + scheme: string; + principal: string; + credentials: string; + realm?: string; + parameters?: { [key: string]: any }; +} + +interface ConfigurationOptions { + encrypted?: string; + trust?: string; + trustedCertificates?: any[]; + knownHosts?: string; +} + +declare const READ: string; +declare const WRITE: string; + +declare class Driver { + constructor( + url: string, + userAgent: string, + token: AuthCredentials, + config?: ConfigurationOptions + ); + + protected _destroyConnection(conn: Connection): void; + protected _acquireConnection(mode: string): PromiseLike; + protected _createSession(connectionPromise: PromiseLike, cb: Function): Session; + protected _createConnection(url: string, + release: (url: string, conn: Connection) => void + ): Connection; + static _validateConnection(conn: Connection): Boolean + session(mode?: string): Session; + close(): void; +} + +declare class _ConnectionStreamObserver extends StreamObserver { + constructor(driver: Driver, conn: Connection); + + onError(error: Error): void; + onCompleted(message: any): void; +} + +export { Driver, READ, WRITE, AuthCredentials, ConfigurationOptions } + +export default Driver; diff --git a/lib/v1/error.d.ts b/lib/v1/error.d.ts new file mode 100644 index 000000000..023585aa0 --- /dev/null +++ b/lib/v1/error.d.ts @@ -0,0 +1,18 @@ +declare const SERVICE_UNAVAILABLE: string; +declare const SESSION_EXPIRED: string; + +declare function newError(message: any, code: string): Neo4jError; + +declare class Neo4jError extends Error { + code: string; + message: string; + + constructor(message: any, code: string); +} + +export { + newError, + Neo4jError, + SERVICE_UNAVAILABLE, + SESSION_EXPIRED, +} diff --git a/lib/v1/graph-types.d.ts b/lib/v1/graph-types.d.ts new file mode 100644 index 000000000..a3ff3b768 --- /dev/null +++ b/lib/v1/graph-types.d.ts @@ -0,0 +1,68 @@ +declare class Node { + constructor( + identity: string, + labels: string[], + properties: Object + ) + + toString(): string; +} + +declare class Relationship { + identity: string; + start: string; + end: string; + type: string; + properties: Object; + + constructor( + identity: string, + start: string, + end: string, + type: string, + properties: Object + ); + + toString(): string; +} + +declare class UnboundRelationship { + identity: string; + type: string; + properties: Object; + + constructor( + identity: string, + type: string, + properties: Object + ); + + bind(start: string, end: string): Relationship; + toString(): string; +} + +declare class PathSegment { + start: string; + rel: Relationship; + end: string; + + constructor( + start: string, + rel: Relationship, + end: string + ); +} + +declare class Path { + start: Node; + end: Node; + segments: PathSegment[]; +} + +export { + Node, + Relationship, + UnboundRelationship, + Path, + PathSegment +} diff --git a/lib/v1/index.d.ts b/lib/v1/index.d.ts new file mode 100644 index 000000000..e466c1064 --- /dev/null +++ b/lib/v1/index.d.ts @@ -0,0 +1,88 @@ +import { int, isInt, inSafeRange, toNumber, toString } from "./integer"; +import { Node, Relationship, UnboundRelationship, PathSegment, Path } from "./graph-types"; +import { Neo4jError, SERVICE_UNAVAILABLE, SESSION_EXPIRED } from "./error"; +import Result from "./result"; +import ResultSummary from "./result-summary"; +import Record from "./record"; +import Session from './session'; +import { Driver, READ, WRITE, AuthCredentials, ConfigurationOptions } from "./driver"; +import RoutingDriver from "./routing-driver"; +import VERSION from "../version"; +import { parseScheme, parseUrl } from "./internal/connector"; + +declare const auth: { + basic: (username: string, + password: string, + realm?: string) => AuthCredentials, + custom: (principal: string, + credentials: string, + realm: string, + scheme: string, + parameters?: { [key: string]: any }) => AuthCredentials, +}; + +declare const USER_AGENT: string; + +declare function driver(url: string, + authToken: AuthCredentials, + config?: ConfigurationOptions): Driver; + +declare const types: { + Node: typeof Node; + Relationship: typeof Relationship; + UnboundRelationship: typeof UnboundRelationship; + PathSegment: typeof PathSegment; + Path: typeof Path; + Result: typeof Result; + ResultSummary: typeof ResultSummary; + Record: typeof Record; +}; + +declare const session: { + READ: typeof READ; + WRITE: typeof WRITE; +}; + +declare const error: { + SERVICE_UNAVAILABLE: typeof SERVICE_UNAVAILABLE; + SESSION_EXPIRED: typeof SESSION_EXPIRED; +}; + +declare const integer: { + toNumber: typeof toNumber; + toString: typeof toString; + inSafeRange: typeof inSafeRange; +}; + +declare const forExport: { + driver: typeof driver; + int: typeof int; + isInt: typeof isInt; + integer: typeof integer; + Neo4jError: typeof Neo4jError; + auth: typeof auth; + types: typeof types; + session: typeof session; + error: typeof error; + Driver: Driver; + AuthCredentials: AuthCredentials; + ConfigurationOptions: ConfigurationOptions; + Session: Session; +}; + +export { + driver, + int, + isInt, + integer, + Neo4jError, + auth, + types, + session, + error, + AuthCredentials, + Session, + ConfigurationOptions, +} + +export default forExport; diff --git a/lib/v1/integer.d.ts b/lib/v1/integer.d.ts new file mode 100644 index 000000000..23bd60c4e --- /dev/null +++ b/lib/v1/integer.d.ts @@ -0,0 +1,84 @@ +import { newError } from "./error"; + +declare class Integer { + low: number; + high: number; + + constructor(low: number, high: number) + + inSafeRange(): boolean; + toInt(): number; + toNumber(): number; + toString(): string; + getHighBits(): number; + getLowBits(): number; + getNumBitsAbs(): number; + isZero(): boolean; + isNegative(): boolean; + isPositive(): boolean; + isOdd(): boolean; + isEven(): boolean; + equals(other: Integer | number | string): boolean; + notEquals(other: Integer | number | string): boolean; + lessThan(other: Integer | number | string): boolean; + lessThanOrEqual(other: Integer | number | string): boolean; + greaterThan(other: Integer | number | string): boolean; + greaterThanOrEqual(other: Integer | number | string): boolean; + compare(other: Integer | number | string): number; + negate(): Integer; + add(addend: Integer | number | string): Integer; + subtract(subtrahend: Integer | number | string): Integer; + multiply(multiplier: Integer | number | string): Integer; + div(divisor: Integer | number | string): Integer; + modulo(divisor: Integer | number | string): Integer; + not(): Integer; + and(other: Integer | number | string): Integer; + or(other: Integer | number | string): Integer; + xor(other: Integer | number | string): Integer; + shiftLeft(numBits: Integer | number): Integer; + shiftRight(numBits: Integer | number): Integer; + + static __isInteger__: true; + static isInteger(obj: Object): boolean; + static fromInt(value: number): Integer; + static fromNumber(value: number): Integer; + static fromBits(lowBits: number, highBits: number): Integer; + static fromString(str: string, radix?: number): Integer; + static fromValue(val: Integer | number | string | { low: number, high: number }): Integer; + static toNumber(val: Integer | number | string | { low: number, high: number }): number; + static toString(val: Integer | number | string | { low: number, high: number }, radix?: number): Integer; + static inSafeRange(val: Integer | number | string | { low: number, high: number }): boolean; + + static ZERO: Integer; + static ONE: Integer; + static NEG_ONE: Integer; + static MAX_VALUE: Integer; + static MIN_VALUE: Integer; + static MIN_SAFE_VALUE: Integer; + static MAX_SAFE_VALUE: Integer; +} + +declare function int(val: Integer | number | string | { low: number, high: number }): Integer; +declare function isInt(obj: Object): boolean; +declare function inSafeRange(val: Integer | number | string | { low: number, high: number }): boolean; +declare function toNumber(val: Integer | number | string | { low: number, high: number }): number; +declare function toString(val: Integer | number | string | { low: number, high: number }, radix?: number): Integer; + +declare const TWO_PWR_16_DBL: number; +declare const TWO_PWR_24_DBL: number; +declare const TWO_PWR_32_DBL: number; +declare const TWO_PWR_64_DBL: number; +declare const TWO_PWR_63_DBL: number; +declare const TWO_PWR_24: Integer; + +declare const INT_CACHE: Object; + +export { + int, + isInt, + inSafeRange, + toNumber, + toString +} + +export default Integer; diff --git a/lib/v1/internal/connector.d.ts b/lib/v1/internal/connector.d.ts new file mode 100644 index 000000000..04a2f97c4 --- /dev/null +++ b/lib/v1/internal/connector.d.ts @@ -0,0 +1,41 @@ +import { ConfigurationOptions } from "../driver"; + +declare class Connection { + constructor(channel: { write: Function, onMessage: Function }, url: string); + + initialise( + clientName: string, + token: any, + observer: any + ): void; + + run( + statement: any, + params: Object, + observer: any + ): void; + + pullAll(observer: any): void; + discardAll(observer: any): void; + reset(observer: any): void; + sync(): any; + isOpen(): boolean; + isEncrypted(): boolean; + close(cb?: Function): void; + setServerVersion(version: string): void; +} + +declare function connect( + url: string, + config?: ConfigurationOptions +): Connection; + +declare function parseScheme(url: string): string; +declare function parseUrl(url: string): string; + +export { + connect, + Connection, + parseScheme, + parseUrl, +} \ No newline at end of file diff --git a/lib/v1/internal/pool.d.ts b/lib/v1/internal/pool.d.ts new file mode 100644 index 000000000..c3bf11145 --- /dev/null +++ b/lib/v1/internal/pool.d.ts @@ -0,0 +1,14 @@ +declare class Pool { + constructor( + create: Function, + destroy: Function, + validate: Function, + maxIdle: number + ); + aquire(key: string | number): any; + purge(key: string | number): any; + purgeAll(): void; + has(key: string | number): any; +} + +export default Pool; diff --git a/lib/v1/internal/round-robin-array.d.ts b/lib/v1/internal/round-robin-array.d.ts new file mode 100644 index 000000000..78b67e75c --- /dev/null +++ b/lib/v1/internal/round-robin-array.d.ts @@ -0,0 +1,14 @@ +declare class RoundRobinArray { + constructor(items: any); + + next(): any; + push(elem: any): any; + pushAll(elem: any): any; + empty(): number; + clear(): void; + size(): number; + toArray(): any[]; + remove(item: any): any; +} + +export default RoundRobinArray; diff --git a/lib/v1/internal/stream-observer.d.ts b/lib/v1/internal/stream-observer.d.ts new file mode 100644 index 000000000..e65fbebdd --- /dev/null +++ b/lib/v1/internal/stream-observer.d.ts @@ -0,0 +1,15 @@ +import ResultSummary from '../result-summary' + +declare type ErrorTransformer = (err: Error) => T; + +export declare interface StreamObserverInterface { + onNext?(record: T): void; + onCompleted?(summary?: ResultSummary): void; + onError?(err: Error): void; +} + +declare class StreamObserver implements StreamObserverInterface { + constructor(errorTransformer: ErrorTransformer); +} + +export default StreamObserver; diff --git a/lib/v1/record.d.ts b/lib/v1/record.d.ts new file mode 100644 index 000000000..bd0648427 --- /dev/null +++ b/lib/v1/record.d.ts @@ -0,0 +1,19 @@ +declare function generateFieldLookup(keys: any): any; + +declare type Visitor = (field: string, value: T, context: Record) => void; + +declare class Record { + keys: string[]; + length: number; + _fields: T[]; + + constructor(keys: string[], fields: string[], fieldLookup: { [index: string]: string }); + + forEach(visitor: Visitor): void; + toObject(): Partial<{ [ret: string]: T }>; + get(key: K): T[K]; + get(key: number): K; + has(key: string | number): boolean; +} + +export default Record; diff --git a/lib/v1/result-summary.d.ts b/lib/v1/result-summary.d.ts new file mode 100644 index 000000000..393fa18e3 --- /dev/null +++ b/lib/v1/result-summary.d.ts @@ -0,0 +1,111 @@ +import Integer, { int, isInt } from "./integer"; + +declare class ResultSummary { + statement: { text: string, parameters: { [index: string]: any } }; + statementType: string; + counters: StatementStatistic; + //for backwards compatibility, remove in future version + updateStatistics: StatementStatistic; + plan: Plan; + profile: ProfiledPlan; + notifications: Notification[]; + server: ServerInfo; + resultConsumedAfter: any | undefined; + resultAvailableAfter: any | undefined; + + constructor(statement: string, parameters?: { [index: string]: any }, metadata?: { [index: string]: any }) + + protected _buildNotifications(notifications: any): Notification[]; + hasPlan(): boolean; + hasProfile(): boolean; +} + +declare class Plan { + operatorType: any; + identifiers: any; + arguments: any; + children: Plan[]; + constructor(plan: Object) +} + +declare class ProfiledPlan { + operatorType: any; + identifiers: any; + arguments: any; + dbhits: Integer; + rows: Integer; + children: Plan[]; + constructor(plan: Object) +} + +interface Statistics { + nodesCreated?: Integer; + nodesDeleted?: Integer; + relationshipsCreated?: Integer; + relationshipsDeleted?: Integer; + propertiesSet?: Integer; + labelsAdded?: Integer; + labelsRemoved?: Integer; + indexesAdded?: Integer; + indexesRemoved?: Integer; + constraintsAdded?: Integer; + constraintsRemoved?: Integer; +} + +declare class StatementStatistic { + protected _stats: Statistics; + + constructor(statistics: Statistics); + + containsUpdates(): boolean; + + nodesCreated(): Integer; + nodesDeleted(): Integer; + relationshipsCreated(): Integer; + relationshipsDeleted(): Integer; + propertiesSet(): Integer; + labelsAdded(): Integer; + labelsRemoved(): Integer; + indexesAdded(): Integer; + indexesRemoved(): Integer; + constraintsAdded(): Integer; + constraintsRemoved(): Integer; +} + +declare interface NotificationPosition { + offset: Integer; + line: Integer; + column: Integer; +} + +declare interface NotificationStructure { + code: any; + title: string; + description: string; + severity: string; + position: NotificationPosition; +} + +declare class Notification implements Partial { + constructor(notification: NotificationStructure); + + _constructPosition(pos: NotificationPosition): NotificationPosition; +} + +declare class ServerInfo { + address: string; + version: string; + + constructor(serverMeta: { address: string, version: string }); +} + +declare const statementType: { + READ_ONLY: "r"; + READ_WRITE: "rw"; + WRITE_ONLY: "w"; + SCHEMA_WRITE: "s"; +}; + +export { statementType } + +export default ResultSummary; diff --git a/lib/v1/result.d.ts b/lib/v1/result.d.ts new file mode 100644 index 000000000..4b0daacb9 --- /dev/null +++ b/lib/v1/result.d.ts @@ -0,0 +1,30 @@ +import ResultSummary from "./result-summary"; +import Record from "./record"; +import StreamObserver, { StreamObserverInterface } from "./internal/stream-observer"; + +declare type ResultType = { + records: Array>; + summary: ResultSummary; +} + +declare type PromiseResult = PromiseLike> + +declare class Result extends Promise> { + _streamObserver: StreamObserver; + _p: PromiseResult | null; + _statement: string; + _parameters: Params; + _metaSupplier: () => any; + + constructor( + streamObserver: StreamObserver, + statement: string, + parameters?: Params, + metaSupplier?: () => any + ); + + protected _createPromise(): PromiseResult; + subscribe(observer: StreamObserverInterface>): void; +} + +export default Result; diff --git a/lib/v1/routing-driver.d.ts b/lib/v1/routing-driver.d.ts new file mode 100644 index 000000000..54c999360 --- /dev/null +++ b/lib/v1/routing-driver.d.ts @@ -0,0 +1,59 @@ +import Session from "./session"; +import { Connection } from './internal/connector'; +import { Driver, READ, WRITE, AuthCredentials, ConfigurationOptions } from "./driver"; +import { newError, SERVICE_UNAVAILABLE, SESSION_EXPIRED } from "./error"; +import RoundRobinArray from "./internal/round-robin-array"; +import Integer, { int } from "./integer"; + +declare class RoutingDriver extends Driver { + _clusterView: ClusterView; + + constructor( + url: string, + userAgent: string, + token: AuthCredentials, + config: ConfigurationOptions + ); + + protected _createSession(connectionPromise: Promise, cb: Function): RoutingSession; + protected _updatedClusterView(): Promise; + protected _diff(oldView: any, updatedView: any): any; + protected _acquireConnection(mode: string): Promise; + protected _forget(url: string): void; + + static _validateConfig(config: ConfigurationOptions): ConfigurationOptions; +} + +declare class ClusterView { + routers: RoundRobinArray; + readers: RoundRobinArray; + writers: RoundRobinArray; + protected _expires: Integer; + + constructor( + routers: RoundRobinArray, + readers: RoundRobinArray, + writers: RoundRobinArray, + expires: Integer + ); + + needsUpdate(): boolean; + all(): Set; + remove(): void; +} + +declare class RoutingSession extends Session { + protected _onFailedConnection: Function; + + constructor( + connectionPromise: Promise, + onClose: Function, + onFailedConnection: Function + ); + + _onRunFailure(): Function; +} + +declare function newClusterView(session: Session): Promise; + +export default RoutingDriver; diff --git a/lib/v1/session.d.ts b/lib/v1/session.d.ts new file mode 100644 index 000000000..e80e1616a --- /dev/null +++ b/lib/v1/session.d.ts @@ -0,0 +1,35 @@ +import { Connection } from './internal/connector' +import StreamObserver from "./internal/stream-observer"; +import Result from "./result"; +import Transaction from "./transaction"; +import Integer, { int } from "./integer"; +import { newError } from "./error"; + +declare class Session { + _connectionPromise: PromiseLike; + _onClose: Function; + _hasTx: boolean; + + constructor( + connectionPromise: PromiseLike, + onClose: Function + ); + + run(statement: string, parameters?: Params): Result; + run(statement: string): Result; + beginTransaction(bookmark?: any): Transaction; + lastBookmark(): any; + close(cb?: () => void): void; + protected _onRunFailure(): Function; +} + +declare class _RunObserver extends StreamObserver { + constructor( + onError: Function + ); + + onCompleted(meta: Object): void; + meta(): Object; +} + +export default Session; diff --git a/lib/v1/transaction.d.ts b/lib/v1/transaction.d.ts new file mode 100644 index 000000000..6f66bd643 --- /dev/null +++ b/lib/v1/transaction.d.ts @@ -0,0 +1,27 @@ +import { Connection } from './internal/connector' +import StreamObserver from "./internal/stream-observer"; +import Result from "./result"; + +declare class Transaction { + constructor( + connectionPromise: PromiseLike, + onClose: Function, + errorTransformer: Function, + bookmark?: any, + onBookmark?: Function + ); + + run(statement: any, parameters: Params): Result; + run(statement: any): Result; + commit(): Result; + rollback(): Result; + _onError(): void; +} + +declare function _runDiscardAll( + msg: any, + connectionPromise: PromiseLike, + observer: StreamObserver +): Result; + +export default Transaction; diff --git a/lib/version.d.ts b/lib/version.d.ts new file mode 100644 index 000000000..199360e32 --- /dev/null +++ b/lib/version.d.ts @@ -0,0 +1,3 @@ +declare type VERSION = string; + +export default VERSION; diff --git a/package.json b/package.json index c205d570e..d2d845fea 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,9 @@ "vinyl-buffer": "^1.0.0", "vinyl-source-stream": "^1.1.0" }, + "typings": "./lib/index.d.ts", "dependencies": { + "@types/node": "^6.0.54", "babel-runtime": "^6.18.0" } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..a8685bde8 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "module": "es6", + "target": "es6", + "noImplicitAny": true, + "noImplicitReturns": true + } +} \ No newline at end of file