Convert portal feature store to TS
This commit is contained in:
parent
799b6b15d7
commit
c2fbdf505e
|
@ -20,7 +20,7 @@ interface BudiStoreOpts {
|
|||
|
||||
export class BudiStore<T> {
|
||||
store: Writable<T>
|
||||
subscribe: Writable<T>["subscribe"]
|
||||
subscribe: Readable<T>["subscribe"]
|
||||
update: Writable<T>["update"]
|
||||
set: Writable<T>["set"]
|
||||
|
||||
|
@ -53,17 +53,24 @@ export class BudiStore<T> {
|
|||
}
|
||||
}
|
||||
|
||||
export class DerivedBudiStore<T, DerivedT extends T> extends BudiStore<T> {
|
||||
// This deliberately does not extend a BudiStore as doing so imposes a requirement that
|
||||
// DerivedT must extend T, which is not desirable, due to the type of the subscribe property.
|
||||
export class DerivedBudiStore<T, DerivedT> {
|
||||
store: BudiStore<T>
|
||||
derivedStore: Readable<DerivedT>
|
||||
subscribe: Readable<DerivedT>["subscribe"]
|
||||
update: Writable<T>["update"]
|
||||
set: Writable<T>["set"]
|
||||
|
||||
constructor(
|
||||
init: T,
|
||||
makeDerivedStore: (store: Writable<T>) => Readable<DerivedT>,
|
||||
opts?: BudiStoreOpts
|
||||
) {
|
||||
super(init, opts)
|
||||
this.store = new BudiStore(init, opts)
|
||||
this.derivedStore = makeDerivedStore(this.store)
|
||||
this.subscribe = this.derivedStore.subscribe
|
||||
this.update = this.store.update
|
||||
this.set = this.store.set
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
import { writable } from "svelte/store"
|
||||
import { API } from "@/api"
|
||||
import { licensing } from "./licensing"
|
||||
import { ConfigType } from "@budibase/types"
|
||||
|
||||
export const createFeatureStore = () => {
|
||||
const internalStore = writable({
|
||||
scim: {
|
||||
isFeatureFlagEnabled: false,
|
||||
isConfigFlagEnabled: false,
|
||||
},
|
||||
})
|
||||
|
||||
const store = writable({
|
||||
isScimEnabled: false,
|
||||
})
|
||||
|
||||
internalStore.subscribe(s => {
|
||||
store.update(state => ({
|
||||
...state,
|
||||
isScimEnabled: s.scim.isFeatureFlagEnabled && s.scim.isConfigFlagEnabled,
|
||||
}))
|
||||
})
|
||||
|
||||
licensing.subscribe(v => {
|
||||
internalStore.update(state => ({
|
||||
...state,
|
||||
scim: {
|
||||
...state.scim,
|
||||
isFeatureFlagEnabled: v.scimEnabled,
|
||||
},
|
||||
}))
|
||||
})
|
||||
|
||||
const actions = {
|
||||
init: async () => {
|
||||
const scimConfig = await API.getConfig(ConfigType.SCIM)
|
||||
internalStore.update(state => ({
|
||||
...state,
|
||||
scim: {
|
||||
...state.scim,
|
||||
isConfigFlagEnabled: scimConfig?.config?.enabled,
|
||||
},
|
||||
}))
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
subscribe: store.subscribe,
|
||||
...actions,
|
||||
}
|
||||
}
|
||||
|
||||
export const features = createFeatureStore()
|
|
@ -0,0 +1,35 @@
|
|||
import { derived, Writable } from "svelte/store"
|
||||
import { API } from "@/api"
|
||||
import { licensing } from "./licensing"
|
||||
import { ConfigType, isConfig, isSCIMConfig } from "@budibase/types"
|
||||
import { DerivedBudiStore } from "../BudiStore"
|
||||
|
||||
interface FeatureState {
|
||||
scimConfigEnabled: Boolean
|
||||
}
|
||||
|
||||
interface DerivedFeatureState {
|
||||
isScimEnabled: Boolean
|
||||
}
|
||||
|
||||
class FeatureStore extends DerivedBudiStore<FeatureState, DerivedFeatureState> {
|
||||
constructor() {
|
||||
const makeDerivedStore = (store: Writable<FeatureState>) => {
|
||||
return derived([store, licensing], ([$state, $licensing]) => ({
|
||||
isScimEnabled: $state.scimConfigEnabled && $licensing.scimEnabled,
|
||||
}))
|
||||
}
|
||||
super({ scimConfigEnabled: false }, makeDerivedStore)
|
||||
}
|
||||
|
||||
async init() {
|
||||
const config = await API.getConfig(ConfigType.SCIM)
|
||||
this.update(state => ({
|
||||
...state,
|
||||
scimConfigEnabled:
|
||||
isConfig(config) && isSCIMConfig(config) && config.config.enabled,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
export const features = new FeatureStore()
|
|
@ -127,6 +127,9 @@ export interface AIInnerConfig {
|
|||
|
||||
export interface AIConfig extends Config<AIInnerConfig> {}
|
||||
|
||||
export const isConfig = (config: Object): config is Config =>
|
||||
"type" in config && "config" in config
|
||||
|
||||
export const isSettingsConfig = (config: Config): config is SettingsConfig =>
|
||||
config.type === ConfigType.SETTINGS
|
||||
|
||||
|
|
Loading…
Reference in New Issue