Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReferenceError: Cannot access 'TwitterApiReadWrite' before initialization #544

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
import TwitterApiv1 from '../v1/client.v1';
import TwitterApiv2 from '../v2/client.v2';
import { TwitterApiError } from '../types';
import TwitterApiReadWrite from './readwrite';
import TwitterApiBase from '../client.base';

// Lazy load TwitterApiReadWrite
const TwitterApiReadWritePromise = import('./readwrite').then(module => module.default);

// "Real" exported client for usage of TwitterApi.
/**
* Twitter v1.1 and v2 API client.
*/
export class TwitterApi extends TwitterApiReadWrite {
export class TwitterApi extends TwitterApiBase {
protected _v1?: TwitterApiv1;
protected _v2?: TwitterApiv2;
private _readWriteInstance?: any;

constructor(...args: ConstructorParameters<typeof TwitterApiBase>) {
super(...args);
TwitterApiReadWritePromise.then(TwitterApiReadWrite => {
this._readWriteInstance = new TwitterApiReadWrite(...args);
});
}

/* Direct access to subclients */
public get v1() {
if (this._v1) return this._v1;

return this._v1 = new TwitterApiv1(this);
return this._v1 = new TwitterApiv1(this as TwitterApiBase);
}

public get v2() {
if (this._v2) return this._v2;

return this._v2 = new TwitterApiv2(this);
return this._v2 = new TwitterApiv2(this as TwitterApiBase);
}

/**
* Get a client with read/write rights.
*/
public get readWrite() {
return this as TwitterApiReadWrite;
public async readWrite() {
if (!this._readWriteInstance) {
const TwitterApiReadWrite = await TwitterApiReadWritePromise;
this._readWriteInstance = new TwitterApiReadWrite(this);
}
return this._readWriteInstance;
}

/* Static helpers */
Expand Down Expand Up @@ -71,6 +85,6 @@ export class TwitterApi extends TwitterApiReadWrite {
}
}

export { default as TwitterApiReadWrite } from './readwrite';
export { TwitterApiReadWritePromise as TwitterApiReadWrite };
export { default as TwitterApiReadOnly } from './readonly';
export default TwitterApi;
Loading