Skip to content

Commit

Permalink
feat: Improved config
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Aug 15, 2022
1 parent c75e8ef commit 51548d9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 23 deletions.
25 changes: 3 additions & 22 deletions src/config.ts → src/config/Config.ts
@@ -1,5 +1,5 @@
/*!
* Copyright 2021 WPPConnect Team
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,8 @@
* ```
*/
export interface Config {
[key: string | symbol]: any;

/**
* Set the device name connected, false to disable
* @default 'WPPConnect'
Expand Down Expand Up @@ -67,24 +69,3 @@ export interface Config {
*/
poweredBy: string | null;
}

export const defaultConfig: Config = {
deviceName: false,
liveLocationLimit: 10,
disableGoogleAnalytics: false,
googleAnalyticsId: null,
googleAnalyticsUserProperty: {},
linkPreviewApiServers: null,
poweredBy: 'WA-JS',
};

export const config: Config = defaultConfig;

// Init config from global environment;
const w = window as unknown as { WPPConfig: Config };

w.WPPConfig = w.WPPConfig || defaultConfig;

for (const key of Object.keys(w.WPPConfig)) {
(config as any)[key] = (w.WPPConfig as any)[key];
}
27 changes: 27 additions & 0 deletions src/config/defaultConfig.ts
@@ -0,0 +1,27 @@
/*!
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Config } from './Config';

export const defaultConfig: Config = {
deviceName: false,
liveLocationLimit: 10,
disableGoogleAnalytics: false,
googleAnalyticsId: null,
googleAnalyticsUserProperty: {},
linkPreviewApiServers: null,
poweredBy: 'WA-JS',
};
19 changes: 19 additions & 0 deletions src/config/eventTypes.ts
@@ -0,0 +1,19 @@
/*!
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface ConfigEventTypes {
'config.update': { key: string | symbol; value: any };
}
44 changes: 44 additions & 0 deletions src/config/index.ts
@@ -0,0 +1,44 @@
/*!
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { internalEv } from '../eventEmitter';
import { Config } from './Config';
import { defaultConfig } from './defaultConfig';

export { Config } from './Config';

declare global {
interface Window {
WPPConfig: Config;
}
}

const setted = window.WPPConfig || {};

const merged = { ...defaultConfig, ...setted };

export const config: Config = new Proxy<Config>(merged, {
set: (target, key, value) => {
target[key] = value;
try {
internalEv.emitAsync('config.update', { key, value });
} catch (error) {}

return true;
},
});

window.WPPConfig = config;
3 changes: 3 additions & 0 deletions src/eventEmitter/eventTypes.ts
Expand Up @@ -17,6 +17,7 @@
import { BlocklistEventTypes } from '../blocklist/events/eventTypes';
import { CallEventTypes } from '../call/events/eventTypes';
import { ChatEventTypes } from '../chat/events/eventTypes';
import { ConfigEventTypes } from '../config/eventTypes';
import { ConnEventTypes } from '../conn/events/eventTypes';
import { GroupEventTypes } from '../group/events/eventTypes';
import { StatusEventTypes } from '../status/events/eventTypes';
Expand All @@ -25,6 +26,7 @@ import { WebpackEvents } from '../webpack/eventTypes';
export { BlocklistEventTypes } from '../blocklist/events/eventTypes';
export { CallEventTypes } from '../call/events/eventTypes';
export { ChatEventTypes } from '../chat/events/eventTypes';
export { ConfigEventTypes } from '../config/eventTypes';
export { ConnEventTypes } from '../conn/events/eventTypes';
export { GroupEventTypes } from '../group/events/eventTypes';
export { StatusEventTypes } from '../status/events/eventTypes';
Expand All @@ -33,6 +35,7 @@ export { WebpackEvents } from '../webpack/eventTypes';
export type EventTypes = BlocklistEventTypes &
CallEventTypes &
ChatEventTypes &
ConfigEventTypes &
ConnEventTypes &
GroupEventTypes &
StatusEventTypes &
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -25,7 +25,7 @@ import * as webpack from './webpack';
export { webpack };
export { isInjected, isReady } from './webpack';

export { config } from './config';
export { config, Config } from './config';

export * as blocklist from './blocklist';
export * as call from './call';
Expand Down

0 comments on commit 51548d9

Please sign in to comment.