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

chore: hide private members and refine constructor args #693

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ node_modules/
# Docusaurus i18n
website/i18n/*
website/translated_docs


packages/bottender/docs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"compile": "tsc --build tsconfig.build.json",
"compile:clean": "tsc --build tsconfig.build.json --clean",
"postinstall": "yarn compile",
"lint": "eslint packages examples --ext=js,ts",
"lint": "eslint packages examples --ext=js,ts --ignore-pattern **/docs",
"lint:fix": "yarn lint:fix:md && yarn lint --fix",
"lint:fix:md": "prettier --write **/*.md",
"lint:staged": "lint-staged",
Expand Down
3 changes: 3 additions & 0 deletions packages/bottender-handlers/src/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ export function matchPattern(pattern: Pattern, text: string): boolean {
}

export default class Handler {
/** @hidden */
_handlers: PredicateHandler[] = [];

/** @hidden */
_errorHandler: FunctionalHandler | null = null;

/** @hidden */
_unhandledHandler: FunctionalHandler | null = null;

on(predicate: ContextPredicate, handler: FunctionalHandler | Builder) {
Expand Down
5 changes: 5 additions & 0 deletions packages/bottender/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,10 @@
],
"engines": {
"node": ">=8"
},
"scripts": {
"typedoc": "yarn typedoc:html && yarn typedoc:json",
"typedoc:html": "typedoc --out docs --exclude \"src/**/__tests__/**/*.ts\" src",
"typedoc:json": "typedoc --json docs/index.json --exclude \"src/**/__tests__/**/*.ts\" src"
}
}
23 changes: 18 additions & 5 deletions packages/bottender/src/bot/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,46 @@ type RequestHandler<B> = (
) => void | Promise<void>;

export default class Bot<B extends Body, C extends Client, E extends Event> {
/** @hidden */
_sessions: SessionStore;

/** @hidden */
_initialized: boolean;

/** @hidden */
_connector: Connector<B, C>;

/** @hidden */
_handler: Action<C, E> | null;

/** @hidden */
_errorHandler: Action<C, E> | null;

/** @hidden */
_initialState: Record<string, any> = {};

/** @hidden */
_plugins: Function[] = [];

/** @hidden */
_sync: boolean;

/** @hidden */
_emitter: EventEmitter;

constructor({
connector,
sessionStore = createMemorySessionStore(),
sync = false,
}: {
/**
* constructor
*/
constructor(options: {
connector: Connector<B, C>;
sessionStore?: SessionStore;
sync?: boolean;
}) {
const {
connector,
sessionStore = createMemorySessionStore(),
sync = false,
} = options;
this._sessions = sessionStore;
this._initialized = false;
this._connector = connector;
Expand Down
4 changes: 4 additions & 0 deletions packages/bottender/src/cache/MemoryCacheStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import cloneDeep from 'lodash/cloneDeep';
import CacheStore, { CacheValue } from './CacheStore';

export default class MemoryCacheStore implements CacheStore {
/** @hidden */
_lru: LRU<string, any>;

/**
* constructor
*/
constructor(max?: number) {
this._lru = new LRU({ max });
}
Expand Down
39 changes: 23 additions & 16 deletions packages/bottender/src/cache/RedisCacheStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@ import isNumber from 'lodash/isNumber';
import CacheStore, { CacheValue } from './CacheStore';

export default class RedisCacheStore implements CacheStore {
/** @hidden */
_redis: IORedis.Redis;

/** @hidden */
_prefix = '';

/*
Support all of args supported by `ioredis`:
- new Redis() // Connect to 127.0.0.1:6379
- new Redis(6380) // 127.0.0.1:6380
- new Redis(6379, '192.168.1.1') // 192.168.1.1:6379
- new Redis('/tmp/redis.sock')
- new Redis({
port: 6379, // Redis port
host: '127.0.0.1', // Redis host
family: 4, // 4 (IPv4) or 6 (IPv6)
password: 'auth',
db: 0
})
// Connect to 127.0.0.1:6380, db 4, using password "authpassword"
- new Redis('redis://:[email protected]:6380/4')
*/
/**
* constructor
* Support all of args supported by `ioredis`:
*
* ```
* new Redis() // Connect to 127.0.0.1:6379
* new Redis(6380) // 127.0.0.1:6380
* new Redis(6379, '192.168.1.1') // 192.168.1.1:6379
* new Redis('/tmp/redis.sock')
* new Redis({
* port: 6379, // Redis port
* host: '127.0.0.1', // Redis host
* family: 4, // 4 (IPv4) or 6 (IPv6)
* password: 'auth',
* db: 0
* })
*
* // Connect to 127.0.0.1:6380, db 4, using password "authpassword"
* new Redis('redis://:[email protected]:6380/4')
* ```
*/
constructor(...args: any) {
this._redis = new IORedis(...args);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/bottender/src/console/ConsoleBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export default class ConsoleBot extends Bot<
ConsoleClient,
ConsoleEvent
> {
/**
* constructor
*/
constructor({
sessionStore,
fallbackMethods,
Expand Down
13 changes: 8 additions & 5 deletions packages/bottender/src/console/ConsoleConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ type ConstructorOptions = {

export default class ConsoleConnector
implements Connector<ConsoleRequestBody, ConsoleClient> {
/** @hidden */
_client: ConsoleClient;

/** @hidden */
_fallbackMethods: boolean;

/** @hidden */
_platform: string;

constructor({
client,
fallbackMethods,
mockPlatform,
}: ConstructorOptions = {}) {
/**
* constructor
*/
constructor(options: ConstructorOptions = {}) {
const { client, fallbackMethods, mockPlatform } = options;
this._client = client || {
sendText: (text): void => {
process.stdout.write(`Bot > ${text}\n`);
Expand Down
19 changes: 8 additions & 11 deletions packages/bottender/src/console/ConsoleContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,18 @@ export default class ConsoleContext extends Context<
ConsoleClient,
ConsoleEvent
> {
/** @hidden */
_fallbackMethods = false;

/** @hidden */
_mockPlatform = 'console';

constructor({
client,
event,
session,
initialState,
requestContext,
fallbackMethods,
mockPlatform,
emitter,
}: Options) {
super({ client, event, session, initialState, requestContext, emitter });
/**
* constructor
*/
constructor(options: Options) {
super(options);
const { fallbackMethods, mockPlatform } = options;
this._mockPlatform = mockPlatform;
this._fallbackMethods = fallbackMethods;
if (fallbackMethods) {
Expand Down
4 changes: 4 additions & 0 deletions packages/bottender/src/console/ConsoleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ type PayloadEvent = {
export type ConsoleRawEvent = MessageEvent | PayloadEvent;

export default class ConsoleEvent implements Event<ConsoleRawEvent> {
/** @hidden */
_rawEvent: ConsoleRawEvent;

/**
* constructor
*/
constructor(rawEvent: ConsoleRawEvent) {
this._rawEvent = rawEvent;
}
Expand Down
29 changes: 21 additions & 8 deletions packages/bottender/src/context/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,47 @@ export default abstract class Context<C extends Client, E extends Event> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
abstract sendText(text: string, options?: Record<string, any>): any;

/** @hidden */
_isHandled: boolean | null = null;

/** @hidden */
_isSessionWritten = false;

/** @hidden */
_client: C;

/** @hidden */
_event: E;

/** @hidden */
_session: Session | null;

/** @hidden */
_initialState?: Record<string, any> | null;

/** @hidden */
_requestContext: RequestContext | null;

/** @hidden */
_emitter: EventEmitter | null;

/** @hidden */
_intent: string | null;

response: Response;

constructor({
client,
event,
session,
initialState,
requestContext,
emitter,
}: Options<C, E>) {
/**
* constructor
*/
constructor(options: Options<C, E>) {
const {
client,
event,
session,
initialState,
requestContext,
emitter,
} = options;
this._client = client;
this._event = event;
this._session = session || null;
Expand Down
26 changes: 15 additions & 11 deletions packages/bottender/src/line/LineBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ export default class LineBot extends Bot<
LineClient,
LineEvent
> {
constructor({
accessToken,
channelSecret,
sessionStore,
origin,
sync,
mapDestinationToAccessToken,
shouldBatch,
sendMethod,
skipLegacyProfile,
}: {
/**
* constructor
*/
constructor(options: {
accessToken: string;
channelSecret: string;
sessionStore?: SessionStore;
Expand All @@ -32,6 +25,17 @@ export default class LineBot extends Bot<
origin?: string;
skipLegacyProfile?: boolean;
}) {
const {
accessToken,
channelSecret,
sessionStore,
origin,
sync,
mapDestinationToAccessToken,
shouldBatch,
sendMethod,
skipLegacyProfile,
} = options;
const connector = new LineConnector({
accessToken,
channelSecret,
Expand Down
7 changes: 7 additions & 0 deletions packages/bottender/src/line/LineConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ type ConstructorOptions =

export default class LineConnector
implements Connector<LineRequestBody, LineClient> {
/** @hidden */
_client: LineClient;

/** @hidden */
_channelSecret: string;

/** @hidden */
_skipLegacyProfile: boolean;

/** @hidden */
_mapDestinationToAccessToken:
| ((destination: string) => Promise<string>)
| null;

/** @hidden */
_shouldBatch: boolean;

/** @hidden */
_sendMethod: string;

constructor(options: ConstructorOptions) {
Expand Down Expand Up @@ -99,6 +105,7 @@ export default class LineConnector
typeof skipLegacyProfile === 'boolean' ? skipLegacyProfile : true;
}

/** @hidden */
_isWebhookVerifyEvent(event: LineRawEvent): boolean {
return (
(event as any).replyToken === '00000000000000000000000000000000' ||
Expand Down
Loading