Skip to content

Commit b1516c7

Browse files
committed
feat: add composable to support composition api
1 parent 70dde6f commit b1516c7

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/composables/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useKuzzle';

src/composables/useKuzzle.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getCurrentInstance } from 'vue';
2+
import { Kuzzle } from 'kuzzle-sdk';
3+
4+
export function useKuzzle(): Kuzzle {
5+
const vueInstance = getCurrentInstance();
6+
// Check only in development mode otherwise throw a false error
7+
if (process.env.NODE_ENV !== 'production' && vueInstance === null) {
8+
throw new Error(
9+
`Missing current instance. useKuzzle() must be called inside <script setup> or setup().`,
10+
);
11+
}
12+
13+
const vue = (vueInstance as NonNullable<ReturnType<typeof getCurrentInstance>>).proxy;
14+
return vue.$root.$kuzzle;
15+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import '../vue-kuzzle.d';
2-
export * from './types';
2+
export * from './composables';
33
export * from './helpers';
4+
export * from './types';
45
export * from './plugin';

0 commit comments

Comments
 (0)