Skip to content

Commit 70dde6f

Browse files
committed
refactor: split code to improve readability
1 parent c5856cf commit 70dde6f

File tree

14 files changed

+309
-153
lines changed

14 files changed

+309
-153
lines changed

.eslintrc.cjs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
/* eslint-disable sort-keys */
23
module.exports = {
34
root: true,
@@ -6,5 +7,33 @@ module.exports = {
67
node: true,
78
},
89
plugins: ['kuzzle'],
9-
extends: ['plugin:kuzzle/default', 'plugin:kuzzle/typescript'],
10+
extends: ['plugin:kuzzle/default', 'plugin:kuzzle/node'],
11+
ignorePatterns: ['dist'],
12+
overrides: [
13+
{
14+
files: ['*.ts'],
15+
extends: ['plugin:kuzzle/typescript'],
16+
parserOptions: {
17+
project: ['./tsconfig.json'],
18+
},
19+
rules: {
20+
'@typescript-eslint/prefer-nullish-coalescing': [
21+
'error',
22+
{ ignoreConditionalTests: false, ignoreMixedLogicalExpressions: false },
23+
],
24+
'@typescript-eslint/strict-boolean-expressions': [
25+
'error',
26+
{
27+
allowString: false,
28+
allowNumber: false,
29+
allowNullableObject: false,
30+
allowNullableBoolean: false,
31+
allowNullableString: false,
32+
allowNullableNumber: false,
33+
allowAny: false,
34+
},
35+
],
36+
},
37+
},
38+
],
1039
};

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
node_modules
2-
index.js
3-
index.js.map
4-
index.d.ts
2+
dist
3+
*.tgz

index.ts

Lines changed: 0 additions & 124 deletions
This file was deleted.

package-lock.json

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
"name": "vue-plugin-kuzzle",
33
"version": "4.4.0",
44
"description": "A Vuejs plugin shipping the Kuzzle SDK in your components",
5-
"main": "index.js",
6-
"types": "./index.d.ts",
5+
"main": "./dist/index.js",
6+
"module": "./dist/index.js",
7+
"types": "./dist/index.d.ts",
78
"scripts": {
89
"build": "tsc",
910
"prepack": "npm run build",
1011
"test": "npm run test:lint && npm run test:types",
1112
"test:lint": "eslint . --ext .ts",
13+
"test:lint:fix": "npm run test:lint -- --fix",
1214
"test:types": "tsc --noEmit"
1315
},
1416
"repository": {
@@ -27,15 +29,21 @@
2729
},
2830
"homepage": "https://github.com/kuzzleio/vue-plugin-kuzzle#readme",
2931
"peerDependencies": {
30-
"kuzzle-sdk": "~7.x"
32+
"kuzzle-sdk": "~7.x",
33+
"typescript": ">= 5.1",
34+
"vue": "2.7.*"
3135
},
3236
"dependencies": {
33-
"kuzzle-sdk": "^7.7.6",
34-
"vue": "^2.6.14"
37+
"kuzzle-sdk": "~7.x",
38+
"vue": "2.7.*"
3539
},
3640
"devDependencies": {
3741
"@types/node": "^16.10.1",
3842
"eslint-plugin-kuzzle": "^0.0.8",
39-
"typescript": "^4.4.3"
40-
}
43+
"typescript": "5.1.*"
44+
},
45+
"files": [
46+
"dist",
47+
"vue-kuzzle.d.ts"
48+
]
4149
}

src/helpers/getConfig.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Backends, Backend, KuzzleProtocol } from '../types';
2+
3+
const LS_KEY = 'kuzzle-backend';
4+
const GLOBAL_NAME = 'kuzzleBackend';
5+
6+
export function getBackendFromConf(backendsConfig: Backends = {}): Backend | null {
7+
/* eslint-disable sort-keys */
8+
const backends: Backends = {
9+
default: {
10+
host: process.env.VUE_APP_BACKEND_HOST ?? 'localhost',
11+
protocol:
12+
(process.env.VUE_APP_BACKEND_PROTO as KuzzleProtocol | undefined) ??
13+
KuzzleProtocol.WEBSOCKET,
14+
options: {
15+
port: parseInt(process.env.VUE_APP_BACKEND_PORT ?? '7512'),
16+
sslConnection: process.env.VUE_APP_BACKEND_SSL === 'true' || false,
17+
},
18+
},
19+
...backendsConfig,
20+
};
21+
/* eslint-enable sort-keys */
22+
23+
const backendName = process.env.VUE_APP_BACKEND ?? 'default';
24+
25+
if (backends[backendName] === undefined) {
26+
throw new Error(`Unable to find backend ${backendName} in configuration.`);
27+
}
28+
29+
return backends[backendName] ?? null;
30+
}
31+
32+
export function getBackendFromLocalStorage(): Backend | null {
33+
const lsItem = localStorage.getItem(LS_KEY);
34+
if (lsItem === null) {
35+
return null;
36+
}
37+
const backend = JSON.parse(lsItem);
38+
39+
if (typeof backend !== 'object') {
40+
throw new Error(
41+
`Item found in localStorage (${LS_KEY}) is malformed. Expected an object, found ${backend}`,
42+
);
43+
}
44+
45+
return backend;
46+
}
47+
48+
export function getBackendFromWindow(): Backend | null {
49+
if ((window as any)[GLOBAL_NAME] === undefined) {
50+
return null;
51+
}
52+
53+
const backend = JSON.parse((window as any)[GLOBAL_NAME]);
54+
55+
if (typeof backend !== 'object') {
56+
throw new Error(
57+
`Item found in global (${GLOBAL_NAME}) is malformed. Expected an object, found ${backend}`,
58+
);
59+
}
60+
61+
return backend;
62+
}

src/helpers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './getConfig';
2+
export * from './instantiateKuzzleSDK';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Kuzzle, KuzzleAbstractProtocol, Http, WebSocket } from 'kuzzle-sdk';
2+
import { Backends, Backend, KuzzleProtocol, SDKOptions } from '../types';
3+
import { getBackendFromLocalStorage, getBackendFromWindow, getBackendFromConf } from './getConfig';
4+
5+
function protocolFactory(backend: Backend): KuzzleAbstractProtocol {
6+
switch (backend.protocol) {
7+
case KuzzleProtocol.HTTP:
8+
return new Http(backend.host, backend.options);
9+
10+
case KuzzleProtocol.WEBSOCKET:
11+
default:
12+
return new WebSocket(backend.host, backend.options);
13+
}
14+
}
15+
16+
/**
17+
* Instantiates the Kuzzle SDK by resolving the backend from the given config.
18+
*
19+
* @param backendsConfig
20+
* @param sdkOptions
21+
*/
22+
export function instantiateKuzzleSDK(backendsConfig?: Backends, sdkOptions?: SDKOptions): Kuzzle {
23+
const backend: Backend | null =
24+
getBackendFromLocalStorage() ?? getBackendFromWindow() ?? getBackendFromConf(backendsConfig);
25+
26+
if (backend === null) {
27+
throw new Error('No backend resolved.');
28+
}
29+
30+
if (backend.host === undefined) {
31+
throw new Error(`Backend is malformed (missing host)`);
32+
}
33+
34+
return new Kuzzle(protocolFactory(backend), sdkOptions);
35+
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import '../vue-kuzzle.d';
2+
export * from './types';
3+
export * from './helpers';
4+
export * from './plugin';

src/plugin.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { PluginFunction } from 'vue';
2+
import { instantiateKuzzleSDK } from './helpers';
3+
import { Backends, SDKOptions } from './types';
4+
5+
export interface VueKuzzleOptions {
6+
backends: Backends;
7+
sdkOptions: SDKOptions;
8+
}
9+
10+
/**
11+
* The VueKuzzle plugin. Makes the Kuzzle SDK available in Vue components as
12+
* `this.$kuzzle`.
13+
*
14+
* @param Vue The Vue application to apply the plugin to
15+
* @param options Options passed to the Kuzzle SDK constructor
16+
*
17+
* @see https://docs.kuzzle.io/sdk/js/7/core-classes/kuzzle/constructor/#options
18+
*/
19+
export const VueKuzzle: PluginFunction<VueKuzzleOptions> = (Vue, options) => {
20+
const sdkOptions = options?.sdkOptions ?? {};
21+
Vue.prototype.$kuzzle = instantiateKuzzleSDK(options?.backends, sdkOptions);
22+
};

0 commit comments

Comments
 (0)