Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing notification store #15331

Merged
merged 18 commits into from
Jan 10, 2025
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createAPIClient } from "@budibase/frontend-core"
import { authStore } from "../stores/auth.js"
import { notificationStore, devToolsEnabled, devToolsStore } from "../stores/"
import { authStore } from "../stores/auth"
import {
notificationStore,
devToolsEnabled,
devToolsStore,
} from "../stores/index.js"
adrinr marked this conversation as resolved.
Show resolved Hide resolved
import { get } from "svelte/store"

export const API = createAPIClient({
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { API } from "./api.js"
import { API } from "./api.ts"
import { patchAPI } from "./patches.js"

// Certain endpoints which return rows need patched so that they transform
Expand Down
5 changes: 5 additions & 0 deletions packages/client/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Window {
"##BUDIBASE_APP_ID##": string
"##BUDIBASE_IN_BUILDER##": string
MIGRATING_APP: boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { API } from "api"
import { writable } from "svelte/store"

const createAuthStore = () => {
const store = writable(null)
const store = writable<{
csrfToken?: string
} | null>(null)

// Fetches the user object if someone is logged in and has reloaded the page
const fetchUser = async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/stores/derived/currentRole.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { derived } from "svelte/store"
import { Constants } from "@budibase/frontend-core"
import { devToolsStore } from "../devTools.js"
import { authStore } from "../auth.js"
import { authStore } from "../auth"
import { devToolsEnabled } from "./devToolsEnabled.js"

// Derive the current role of the logged-in user
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/stores/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { authStore } from "./auth"
export { appStore } from "./app"
export { notificationStore } from "./notification"
export { notificationStore } from "./notification.ts"
export { routeStore } from "./routes"
export { screenStore } from "./screens"
export { builderStore } from "./builder"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ const DEFAULT_NOTIFICATION_TIMEOUT = 3000
const createNotificationStore = () => {
let block = false

const store = writable([])
const store = writable<{ id: string; message: string; count: number }[]>([])

const blockNotifications = (timeout = 1000) => {
block = true
setTimeout(() => (block = false), timeout)
}

const send = (
message,
message: string,
type = "info",
icon,
icon: string,
autoDismiss = true,
duration,
duration?: number,
count = 1
) => {
if (block) {
Expand Down Expand Up @@ -66,7 +66,7 @@ const createNotificationStore = () => {
}
}

const dismiss = id => {
const dismiss = (id: string) => {
store.update(state => {
return state.filter(n => n.id !== id)
})
Expand All @@ -76,13 +76,13 @@ const createNotificationStore = () => {
subscribe: store.subscribe,
actions: {
send,
info: (msg, autoDismiss, duration) =>
info: (msg: string, autoDismiss?: boolean, duration?: number) =>
send(msg, "info", "Info", autoDismiss ?? true, duration),
success: (msg, autoDismiss, duration) =>
success: (msg: string, autoDismiss?: boolean, duration?: number) =>
send(msg, "success", "CheckmarkCircle", autoDismiss ?? true, duration),
warning: (msg, autoDismiss, duration) =>
warning: (msg: string, autoDismiss?: boolean, duration?: number) =>
send(msg, "warning", "Alert", autoDismiss ?? true, duration),
error: (msg, autoDismiss, duration) =>
error: (msg: string, autoDismiss?: boolean, duration?: number) =>
send(msg, "error", "Alert", autoDismiss ?? false, duration),
blockNotifications,
dismiss,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@ import { API } from "api"
import { peekStore } from "./peek"
import { builderStore } from "./builder"

interface Route {
path: string
screenId: string
}

interface StoreType {
routes: Route[]
routeParams: {}
activeRoute?: Route | null
routeSessionId: number
routerLoaded: boolean
queryParams?: {
peek?: boolean
}
}

const createRouteStore = () => {
const initialState = {
const initialState: StoreType = {
routes: [],
routeParams: {},
activeRoute: null,
Expand All @@ -22,7 +38,7 @@ const createRouteStore = () => {
} catch (error) {
routeConfig = null
}
let routes = []
const routes: Route[] = []
Object.values(routeConfig?.routes || {}).forEach(route => {
Object.entries(route.subpaths || {}).forEach(([path, config]) => {
routes.push({
Expand All @@ -43,13 +59,13 @@ const createRouteStore = () => {
return state
})
}
const setRouteParams = routeParams => {
const setRouteParams = (routeParams: StoreType["routeParams"]) => {
store.update(state => {
state.routeParams = routeParams
return state
})
}
const setQueryParams = queryParams => {
const setQueryParams = (queryParams: { peek?: boolean }) => {
store.update(state => {
state.queryParams = {
...queryParams,
Expand All @@ -60,13 +76,13 @@ const createRouteStore = () => {
return state
})
}
const setActiveRoute = route => {
const setActiveRoute = (route: string) => {
store.update(state => {
state.activeRoute = state.routes.find(x => x.path === route)
return state
})
}
const navigate = (url, peek, externalNewTab) => {
const navigate = (url: string, peek: boolean, externalNewTab: boolean) => {
if (get(builderStore).inBuilder) {
return
}
Expand All @@ -93,7 +109,7 @@ const createRouteStore = () => {
const setRouterLoaded = () => {
store.update(state => ({ ...state, routerLoaded: true }))
}
const createFullURL = relativeURL => {
const createFullURL = (relativeURL: string) => {
if (!relativeURL?.startsWith("/")) {
return relativeURL
}
Expand Down
12 changes: 4 additions & 8 deletions packages/frontend-core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => {
): Promise<APIError> => {
// Try to read a message from the error
let message = response.statusText
let json: any = null
let json = null
try {
json = await response.json()
if (json?.message) {
message = json.message
} else if (json?.error) {
message = json.error
message = JSON.stringify(json.error)
}
} catch (error) {
// Do nothing
Expand All @@ -93,7 +93,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => {
// Generates an error object from a string
const makeError = (
message: string,
url?: string,
url: string,
method?: HTTPMethod
): APIError => {
return {
Expand Down Expand Up @@ -226,7 +226,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => {
return await handler(callConfig)
} catch (error) {
if (config?.onError) {
config.onError(error)
config.onError(error as APIError)
}
throw error
}
Expand All @@ -239,13 +239,9 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => {
patch: requestApiCall(HTTPMethod.PATCH),
delete: requestApiCall(HTTPMethod.DELETE),
put: requestApiCall(HTTPMethod.PUT),
error: (message: string) => {
throw makeError(message)
},
invalidateCache: () => {
cache = {}
},

// Generic utility to extract the current app ID. Assumes that any client
// that exists in an app context will be attaching our app ID header.
getAppID: (): string => {
Expand Down
5 changes: 2 additions & 3 deletions packages/frontend-core/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type Headers = Record<string, string>
export type APIClientConfig = {
enableCaching?: boolean
attachHeaders?: (headers: Headers) => void
onError?: (error: any) => void
onError?: (error: APIError) => void
onMigrationDetected?: (migration: string) => void
}

Expand Down Expand Up @@ -86,14 +86,13 @@ export type BaseAPIClient = {
patch: <RequestT = null, ResponseT = void>(
params: APICallParams<RequestT, ResponseT>
) => Promise<ResponseT>
error: (message: string) => void
invalidateCache: () => void
getAppID: () => string
}

export type APIError = {
message?: string
url?: string
url: string
method?: HTTPMethod
json: any
status: number
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/api/web/global/self.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export interface GetGlobalSelfResponse extends User {
license: License
budibaseAccess: boolean
accountPortalAccess: boolean
csrfToken: boolean
csrfToken: string
}
7 changes: 1 addition & 6 deletions packages/types/src/documents/app/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ export interface ScreenRoutesViewOutput extends Document {
export type ScreenRoutingJson = Record<
string,
{
subpaths: Record<
string,
{
screens: Record<string, string>
}
>
subpaths: Record<string, any>
}
>
Loading