Skip to content

Commit

Permalink
fix: fix conflicting any promise types
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesfer committed Nov 6, 2018
1 parent 9285e3c commit 13a9eff
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@
]
},
"dependencies": {
"@types/lodash": "^4.14.117",
"@types/node": "^8.0.33",
"any-observable": "^0.3.0",
"any-promise": "^1.3.0",
"lodash": "^4.17.11",
"neo4j-driver": "^1.5.0",
"node-cleanup": "^2.1.2",
"rxjs": "^5.5.6",
"tslib": "^1.9.3"
},
"devDependencies": {
Expand All @@ -102,9 +105,7 @@
"@semantic-release/git": "^5.0.0",
"@types/chai": "^4.0.4",
"@types/chai-as-promised": "^7.1.0",
"@types/lodash": "^4.14.117",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.33",
"@types/sinon": "^4.1.2",
"babel-plugin-lodash": "^3.3.4",
"chai": "^4.0.2",
Expand All @@ -119,7 +120,6 @@
"rollup-plugin-commonjs": "^9.1.8",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-typescript": "^1.0.0",
"rxjs": "^5.5.6",
"semantic-release": "^15.5.1",
"sinon": "^4.1.3",
"source-map-support": "^0.5.0",
Expand Down
2 changes: 1 addition & 1 deletion src/clause-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class ClauseCollection extends Clause {
* Returns all clauses in this collection.
* @returns {Clause[]}
*/
getClauses() {
getClauses(): Clause[] {
return this.clauses;
}

Expand Down
4 changes: 2 additions & 2 deletions src/clause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export abstract class Clause extends ParameterContainer {
* Turns the clause into a query string.
* @return {string} Partial query string.
*/
toString() {
toString(): string {
return this.build();
}

Expand All @@ -38,7 +38,7 @@ export abstract class Clause extends ParameterContainer {
* interpolated into the string. For debugging purposes only.
* @return {string}
*/
interpolate() {
interpolate(): string {
let query = this.build();
const params = this.getParams();
for (const name in params) {
Expand Down
25 changes: 18 additions & 7 deletions src/connection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// tslint:disable-next-line import-name
import Promise from 'any-promise';
import AnyPromise from 'any-promise';
// tslint:disable-next-line import-name
import Observable from 'any-observable';
import nodeCleanup from 'node-cleanup';
import { Observable, Observer } from 'rxjs';
import { Observable as RxObservable } from 'rxjs';
import { Dictionary, isFunction } from 'lodash';
import { AuthToken, Config, Driver, Session } from 'neo4j-driver/types/v1';
import { Transformer } from './transformer';
Expand All @@ -18,15 +20,24 @@ nodeCleanup(() => {
connections = [];
});

export interface Credentials { username: string; password: string; }
export interface Observer<T> {
closed?: boolean;
next: (value: T) => void;
error: (error: any) => void;
complete: () => void;
}

export type DriverConstructor = typeof neo4j.driver;

export interface FullConnectionOptions {
driverConstructor: DriverConstructor;
driverConfig: Config;
}

export type ConnectionOptions = Partial<FullConnectionOptions>;

export interface Credentials { username: string; password: string; }

function isCredentials(credentials: any): credentials is Credentials {
return 'username' in credentials && 'password' in credentials;
}
Expand Down Expand Up @@ -137,7 +148,7 @@ export class Connection extends Builder<Query> {
* new chainable query for you.
* @return {Query}
*/
query() {
query(): Query {
return new Query(this);
}

Expand Down Expand Up @@ -208,15 +219,15 @@ export class Connection extends Builder<Query> {
const session = this.session();

// Need to wrap promise in an any-promise
return Promise.resolve(session.run(queryObj.query, queryObj.params))
return AnyPromise.resolve(session.run(queryObj.query, queryObj.params))
.then((result) => {
session.close();
return this.transformer.transformRecords<R>(result.records);
})
.catch((error) => {
session.close();
return Promise.reject(error);
});
}) as any;
}

/**
Expand Down Expand Up @@ -286,7 +297,7 @@ export class Connection extends Builder<Query> {
* ```
* In practice this should never happen unless you're doing some strange things.
*/
stream<R = any>(query: Query): Observable<Dictionary<R>> {
stream<R = any>(query: Query): RxObservable<Dictionary<R>> {
if (!this.open) {
throw Error('Cannot run query; connection is not open.');
}
Expand Down
24 changes: 12 additions & 12 deletions src/query.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// tslint:disable-next-line import-name
import Promise from 'any-promise';
import { Observable } from 'rxjs';
import AnyPromise from 'any-promise';
import { Observable as RxObservable } from 'rxjs';
import { Dictionary } from 'lodash';
import { Connection } from './connection';
import { Builder } from './builder';
import { ClauseCollection } from './clause-collection';
import { Clause } from './clause';
import { Clause, QueryObject } from './clause';

export class Query extends Builder<Query> {
protected clauses = new ClauseCollection();
Expand Down Expand Up @@ -75,15 +75,15 @@ export class Query extends Builder<Query> {
*/
run<R = any>(): Promise<Dictionary<R>[]> {
if (!this.connection) {
return Promise.reject(Error('Cannot run query; no connection object available.'));
return AnyPromise.reject(Error('Cannot run query; no connection object available.')) as any;
}

// connection.run can throw errors synchronously. This is highly inconsistent and will be
// fixed in the future, but for now we need to catch synchronous errors and reject them.
try {
return this.connection.run<R>(this);
} catch (error) {
return Promise.reject(error);
return AnyPromise.reject(error) as any;
}
}

Expand Down Expand Up @@ -133,7 +133,7 @@ export class Query extends Builder<Query> {
* Throws an exception if this query does not have a connection or has no
* clauses.
*/
stream<R = any>(): Observable<Dictionary<R>> {
stream<R = any>(): RxObservable<Dictionary<R>> {
if (!this.connection) {
throw Error('Cannot run query; no connection object available.');
}
Expand Down Expand Up @@ -181,23 +181,23 @@ export class Query extends Builder<Query> {
*
* @returns {string}
*/
build() {
build(): string {
return this.clauses.build();
}

/**
* Synonym for `build()`.
* @returns {string}
*/
toString() {
toString(): string {
return this.clauses.toString();
}

/**
* Returns an object that includes both the query and the params ready to be
* passed to the neo4j driver.
*/
buildQueryObject() {
buildQueryObject(): QueryObject {
return this.clauses.buildQueryObject();
}

Expand All @@ -221,15 +221,15 @@ export class Query extends Builder<Query> {
*
* @returns {string}
*/
interpolate() {
interpolate(): string {
return this.clauses.interpolate();
}

/**
* Returns an array of all the clauses in this query.
* @returns {Clause[]}
*/
getClauses() {
getClauses(): Clause[] {
return this.clauses.getClauses();
}

Expand All @@ -240,7 +240,7 @@ export class Query extends Builder<Query> {
* @param {Clause} clause
* @returns {this}
*/
addClause(clause: Clause) {
addClause(clause: Clause): this {
this.clauses.addClause(clause);
return this;
}
Expand Down
13 changes: 7 additions & 6 deletions tests/connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Connection, Node, Query } from '../src';
import { NodePattern } from '../src/clauses';
import { expect } from '../test-setup';
import { v1 as neo4j } from 'neo4j-driver';
import { SinonSpy, spy } from 'sinon';
// tslint:disable-next-line import-name
import Observable from 'any-observable';
import { Dictionary, each } from 'lodash';
import { Observable } from 'rxjs';
import { SinonSpy, spy } from 'sinon';
import { v1 as neo4j } from 'neo4j-driver';
import { AuthToken, Config } from 'neo4j-driver/types/v1/driver';
import { Driver } from 'neo4j-driver/types/v1';
import { Connection, Node, Query } from '../src';
import { NodePattern } from '../src/clauses';
import { expect } from '../test-setup';
import { neo4jCredentials, neo4jUrl, waitForNeo } from './utils';

describe('Connection', () => {
Expand Down
4 changes: 4 additions & 0 deletions typings/any-observable.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'any-observable' {
import { Observable } from 'rxjs';
export = Observable;
}

0 comments on commit 13a9eff

Please sign in to comment.