From: Eduardo San Martin Morote Date: Thu, 31 Dec 2020 14:45:16 +0000 (+0100) Subject: feat(types): export used types X-Git-Tag: v2.0.0-alpha.6~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dc56fbafa21d8efa2a4b61ffb464f1befa25e34c;p=thirdparty%2Fvuejs%2Fpinia.git feat(types): export used types Close #315 --- diff --git a/src/deprecated.ts b/src/deprecated.ts index 06791aaf..1d08727c 100644 --- a/src/deprecated.ts +++ b/src/deprecated.ts @@ -1,5 +1,9 @@ import { defineStore } from './store' +/** + * {@inheritDoc defineStore} + * @deprecated Use {@link defineStore} + */ export const createStore = ((options: any) => { console.warn( '[🍍]: "createStore" has been deprecated and will be removed on the sable release, use "defineStore" instead.' diff --git a/src/index.ts b/src/index.ts index 2a82b601..7b86b398 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,4 +6,11 @@ export { Pinia, } from './rootStore' export { defineStore } from './store' +export { + StateTree, + Store, + StoreWithGetters, + StoreWithActions, + StoreWithState, +} from './types' export { createStore } from './deprecated' diff --git a/src/rootStore.ts b/src/rootStore.ts index fc20db7f..6b85bd54 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -12,9 +12,19 @@ import { * `fetch`, `setup`, `serverPrefetch` and others */ export let activePinia: Pinia | undefined + +/** + * Sets or unsets the active pinia. Used in SSR and internally when calling + * actions and getters + * + * @param pinia - Pinia instance + */ export const setActivePinia = (pinia: Pinia | undefined) => (activePinia = pinia) +/** + * Get the currently active pinia + */ export const getActivePinia = () => { if (__DEV__ && !activePinia) { warn( @@ -76,6 +86,9 @@ export let clientApp: App | undefined export const setClientApp = (app: App) => (clientApp = app) export const getClientApp = () => clientApp +/** + * Every application must own its own pinia to be able to create stores + */ export interface Pinia { install: Exclude @@ -98,6 +111,9 @@ export const piniaSymbol = (__DEV__ ? Symbol('pinia') : Symbol()) as InjectionKey +/** + * Creates a Pinia instance to be used by the application + */ export function createPinia(): Pinia { const state = ref({}) diff --git a/src/types.ts b/src/types.ts index a8a418ed..eee9dd3a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,9 @@ import { Ref } from 'vue' import { Pinia } from './rootStore' +/** + * Generic state of a Store + */ export type StateTree = Record /** @@ -23,6 +26,10 @@ export function isPlainObject( ) } +/** + * Store Getter + * @internal + */ export interface StoreGetter { (state: S, getters: Record>): T } @@ -35,6 +42,10 @@ export type SubscriptionCallback = ( state: S ) => void +/** + * Base store with state and functions + * @internal + */ export interface StoreWithState { /** * Unique identifier of the store @@ -83,6 +94,10 @@ export type Method = (...args: any[]) => any // } // in this type we forget about this because otherwise the type is recursive +/** + * Store augmented for actions + * @internal + */ export type StoreWithActions = { [k in keyof A]: A[k] extends (...args: infer P) => infer R ? (...args: P) => R @@ -94,6 +109,10 @@ export type StoreWithActions = { // (state: S, getters: Record>): T // } +/** + * Store augmented with getters + * @internal + */ export type StoreWithGetters = { [k in keyof G]: G[k] extends (this: infer This, store?: any) => infer R ? R @@ -109,6 +128,9 @@ export type StoreWithGetters = { // } // has the actions without the context (this) for typings +/** + * Store type to build a store + */ export type Store< Id extends string, S extends StateTree, @@ -116,6 +138,9 @@ export type Store< A > = StoreWithState & S & StoreWithGetters & StoreWithActions +/** + * Generic store type + */ export type GenericStore = Store< string, StateTree,