import {computed, getCurrentInstance, Ref} from '@vue/composition-api'; declare type OmitFirstArg = F extends (x: any, ...args: infer P) => any ? (...args: P) => TReturn : never; declare type InferType = T extends (...args: any) => any ? OmitFirstArg> : T extends unknown ? TUnknown : T; declare type InferGetterType = T extends (...args: any) => any ? ReturnType : any; export declare type Nullish = null | undefined; export declare type Namespace = string | Nullish; export declare type ExtractTypes = { readonly [K in keyof O]: InferType; }; export declare type ExtractGetterTypes = { readonly [K in keyof O]: Ref>; }; declare type KnownKeysWithAllPrimitiveTypes = { [K in keyof T]: string extends K ? (T extends any ? any : never) : number extends K ? never : K } extends { [_ in keyof T]: infer U } ? U : never; export declare type KnownKeys = Exclude, symbol> export declare type RefTypes = { readonly [Key in keyof T]: Ref } function runCB(cb: Function, store: any, namespace: string | null, prop: KnownKeys | string) { if (cb.length === 3) { // choose which signature to pass to cb function return cb(store, namespace, prop); } else { return cb(store, namespace ? `${namespace}/${prop}` : prop); } } function useFromArray(store: any, namespace: string | null, props: Array, cb: Function) { return props.reduce((result, prop) => { result[prop] = runCB(cb, store, namespace, prop) return result; }, {} as any); } function useFromObject(store: any, namespace: string | null, props: KnownKeys[], cb: Function) { const obj: any = {}; for (let key in props) { if (props.hasOwnProperty(key)) { obj[key] = runCB(cb, store, namespace, props[key]); } } return obj; } export function computedGetter(store: any, prop: string) { return computed(() => store.getters[prop]); } export function getMutation(store: any, mutation: string): Function { return function () { return store.commit.apply(store, [mutation, ...arguments]); } } export function getAction(store: any, action: string): Function { return function () { return store.dispatch.apply(store, [action, ...arguments]); } } export function useMapping(store: any, namespace: string | null, map: KnownKeys[] | Array | undefined, cb: Function) { if (!map) { return {}; } if (map instanceof Array) { return useFromArray(store, namespace, map as Array, cb); } return useFromObject(store, namespace, map, cb); } export function getStoreFromInstance() { const vm = getCurrentInstance(); if (!vm) { throw new Error('You must use this function within the "setup()" method, or insert the store as first argument.') } const {$store} = 'proxy' in vm ? vm.proxy : vm; return $store; }