From 45ee1570370c9e41fe503a12401ac5664081a832 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 11:24:37 +0100 Subject: [PATCH 01/16] Rename file --- packages/client/src/stores/index.js | 2 +- packages/client/src/stores/{notification.js => notification.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/client/src/stores/{notification.js => notification.ts} (100%) diff --git a/packages/client/src/stores/index.js b/packages/client/src/stores/index.js index f2b80ed732..e099434b3d 100644 --- a/packages/client/src/stores/index.js +++ b/packages/client/src/stores/index.js @@ -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" diff --git a/packages/client/src/stores/notification.js b/packages/client/src/stores/notification.ts similarity index 100% rename from packages/client/src/stores/notification.js rename to packages/client/src/stores/notification.ts From 96cee21792413d638191e4a6872bf5e388d1786a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 11:32:10 +0100 Subject: [PATCH 02/16] Type store --- packages/client/src/stores/notification.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/client/src/stores/notification.ts b/packages/client/src/stores/notification.ts index 054117aaba..6f5fcc9ef4 100644 --- a/packages/client/src/stores/notification.ts +++ b/packages/client/src/stores/notification.ts @@ -6,7 +6,7 @@ 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 @@ -14,17 +14,18 @@ const createNotificationStore = () => { } const send = ( - message, + message: string, type = "info", - icon, + icon: string, autoDismiss = true, - duration, + duration: number, count = 1 ) => { if (block) { return } + // @ts-expect-error if (get(routeStore).queryParams?.peek) { window.parent.postMessage({ type: "notification", @@ -66,7 +67,7 @@ const createNotificationStore = () => { } } - const dismiss = id => { + const dismiss = (id: string) => { store.update(state => { return state.filter(n => n.id !== id) }) @@ -76,13 +77,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, From f938eb3297e5dd3de249d41c36ef7c0565f1996a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 11:34:15 +0100 Subject: [PATCH 03/16] api to ts --- packages/client/src/api/{api.js => api.ts} | 6 +++++- packages/client/src/api/index.js | 2 +- packages/client/src/stores/notification.ts | 10 +++++----- 3 files changed, 11 insertions(+), 7 deletions(-) rename packages/client/src/api/{api.js => api.ts} (96%) diff --git a/packages/client/src/api/api.js b/packages/client/src/api/api.ts similarity index 96% rename from packages/client/src/api/api.js rename to packages/client/src/api/api.ts index d4c8faa4d2..59430e4ebd 100644 --- a/packages/client/src/api/api.js +++ b/packages/client/src/api/api.ts @@ -1,6 +1,10 @@ import { createAPIClient } from "@budibase/frontend-core" import { authStore } from "../stores/auth.js" -import { notificationStore, devToolsEnabled, devToolsStore } from "../stores/" +import { + notificationStore, + devToolsEnabled, + devToolsStore, +} from "../stores/index.js" import { get } from "svelte/store" export const API = createAPIClient({ diff --git a/packages/client/src/api/index.js b/packages/client/src/api/index.js index 5eb6b2b6f4..a63e19bfbb 100644 --- a/packages/client/src/api/index.js +++ b/packages/client/src/api/index.js @@ -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 diff --git a/packages/client/src/stores/notification.ts b/packages/client/src/stores/notification.ts index 6f5fcc9ef4..5e1a4774c1 100644 --- a/packages/client/src/stores/notification.ts +++ b/packages/client/src/stores/notification.ts @@ -18,7 +18,7 @@ const createNotificationStore = () => { type = "info", icon: string, autoDismiss = true, - duration: number, + duration?: number, count = 1 ) => { if (block) { @@ -77,13 +77,13 @@ const createNotificationStore = () => { subscribe: store.subscribe, actions: { send, - info: (msg: string, autoDismiss: boolean, duration: number) => + info: (msg: string, autoDismiss?: boolean, duration?: number) => send(msg, "info", "Info", autoDismiss ?? true, duration), - success: (msg: string, autoDismiss: boolean, duration: number) => + success: (msg: string, autoDismiss?: boolean, duration?: number) => send(msg, "success", "CheckmarkCircle", autoDismiss ?? true, duration), - warning: (msg: string, autoDismiss: boolean, duration: number) => + warning: (msg: string, autoDismiss?: boolean, duration?: number) => send(msg, "warning", "Alert", autoDismiss ?? true, duration), - error: (msg: string, autoDismiss: boolean, duration: number) => + error: (msg: string, autoDismiss?: boolean, duration?: number) => send(msg, "error", "Alert", autoDismiss ?? false, duration), blockNotifications, dismiss, From a0950f15d9bb4d77dbdcc98d8f96fa93b78ef085 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 11:38:16 +0100 Subject: [PATCH 04/16] Add window typings --- packages/client/src/index.d.ts | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 packages/client/src/index.d.ts diff --git a/packages/client/src/index.d.ts b/packages/client/src/index.d.ts new file mode 100644 index 0000000000..7e13670b33 --- /dev/null +++ b/packages/client/src/index.d.ts @@ -0,0 +1,5 @@ +interface Window { + "##BUDIBASE_APP_ID##": string + "##BUDIBASE_IN_BUILDER##": string + MIGRATING_APP: boolean +} From 60c23ae021614dee53eb62a08e0ae9c4da974ff9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 11:42:35 +0100 Subject: [PATCH 05/16] Type auth store --- packages/client/src/stores/{auth.js => auth.ts} | 4 +++- packages/types/src/api/web/global/self.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) rename packages/client/src/stores/{auth.js => auth.ts} (94%) diff --git a/packages/client/src/stores/auth.js b/packages/client/src/stores/auth.ts similarity index 94% rename from packages/client/src/stores/auth.js rename to packages/client/src/stores/auth.ts index 214cc7bce2..15f44e7c32 100644 --- a/packages/client/src/stores/auth.js +++ b/packages/client/src/stores/auth.ts @@ -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 () => { diff --git a/packages/types/src/api/web/global/self.ts b/packages/types/src/api/web/global/self.ts index 517559d1ca..9d99a1f1a5 100644 --- a/packages/types/src/api/web/global/self.ts +++ b/packages/types/src/api/web/global/self.ts @@ -15,5 +15,5 @@ export interface GetGlobalSelfResponse extends User { license: License budibaseAccess: boolean accountPortalAccess: boolean - csrfToken: boolean + csrfToken: string } From 6e816fb6a2ae3aa8ec13f8d5fc887bc88b3c25fe Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 11:51:27 +0100 Subject: [PATCH 06/16] Fixes --- packages/client/src/api/api.ts | 2 +- packages/client/src/stores/derived/currentRole.js | 2 +- packages/client/src/stores/notification.ts | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/client/src/api/api.ts b/packages/client/src/api/api.ts index 59430e4ebd..93f59f6e9a 100644 --- a/packages/client/src/api/api.ts +++ b/packages/client/src/api/api.ts @@ -1,5 +1,5 @@ import { createAPIClient } from "@budibase/frontend-core" -import { authStore } from "../stores/auth.js" +import { authStore } from "../stores/auth" import { notificationStore, devToolsEnabled, diff --git a/packages/client/src/stores/derived/currentRole.js b/packages/client/src/stores/derived/currentRole.js index 8bb4c5a25d..056a05f8ab 100644 --- a/packages/client/src/stores/derived/currentRole.js +++ b/packages/client/src/stores/derived/currentRole.js @@ -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 diff --git a/packages/client/src/stores/notification.ts b/packages/client/src/stores/notification.ts index 5e1a4774c1..fa28b9f40a 100644 --- a/packages/client/src/stores/notification.ts +++ b/packages/client/src/stores/notification.ts @@ -25,7 +25,6 @@ const createNotificationStore = () => { return } - // @ts-expect-error if (get(routeStore).queryParams?.peek) { window.parent.postMessage({ type: "notification", From 1b6dc51f018c80938c8f45aaf2852734f3981076 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 11:51:38 +0100 Subject: [PATCH 07/16] Type routes --- .../src/stores/{routes.js => routes.ts} | 30 ++++++++++++++----- packages/types/src/documents/app/screen.ts | 1 + 2 files changed, 24 insertions(+), 7 deletions(-) rename packages/client/src/stores/{routes.js => routes.ts} (82%) diff --git a/packages/client/src/stores/routes.js b/packages/client/src/stores/routes.ts similarity index 82% rename from packages/client/src/stores/routes.js rename to packages/client/src/stores/routes.ts index 8e318af2e3..3f200a9c88 100644 --- a/packages/client/src/stores/routes.js +++ b/packages/client/src/stores/routes.ts @@ -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, @@ -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({ @@ -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, @@ -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 } @@ -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 } diff --git a/packages/types/src/documents/app/screen.ts b/packages/types/src/documents/app/screen.ts index b2cedf31a9..e85fd5953b 100644 --- a/packages/types/src/documents/app/screen.ts +++ b/packages/types/src/documents/app/screen.ts @@ -36,6 +36,7 @@ export type ScreenRoutingJson = Record< subpaths: Record< string, { + screenId: string screens: Record } > From 59bdd201585b90c1e87e6cfaa7b82becf2a3ef01 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 12:12:43 +0100 Subject: [PATCH 08/16] Type onError --- packages/frontend-core/src/api/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index 0db1049591..940927f499 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -46,7 +46,7 @@ export type Headers = Record export type APIClientConfig = { enableCaching?: boolean attachHeaders?: (headers: Headers) => void - onError?: (error: any) => void + onError?: (error: APIError) => void onMigrationDetected?: (migration: string) => void } From e6c45d934b70ae496a8827b5f17d75cc5c8af5b8 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 12:39:44 +0100 Subject: [PATCH 09/16] Stringify api errors --- packages/frontend-core/src/api/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index f7b05c338a..44e85e5a68 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -74,7 +74,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => { if (json?.message) { message = json.message } else if (json?.error) { - message = json.error + message = JSON.stringify(json.error) } } catch (error) { // Do nothing From 2cbf7a47c16194741d49dc654d1d0ea059c3d54d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 12:40:23 +0100 Subject: [PATCH 10/16] Type --- packages/frontend-core/src/api/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index 44e85e5a68..b0e1970e6f 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -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 } From 2b90d972b21325608f0f1e4f51d0d1a850be2e26 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 12:50:19 +0100 Subject: [PATCH 11/16] Clean code --- packages/frontend-core/src/api/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index b0e1970e6f..92dc2d9ea3 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -68,7 +68,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => { ): Promise => { // 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) { From 71c6480c1b3ea08cd69bc8a0cfe015aadbaa773b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 13:00:02 +0100 Subject: [PATCH 12/16] Remove unused function --- packages/frontend-core/src/api/index.ts | 6 +----- packages/frontend-core/src/api/types.ts | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index 92dc2d9ea3..6efc90023a 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -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 { @@ -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 => { diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index 940927f499..4819b4cd3b 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -86,14 +86,13 @@ export type BaseAPIClient = { patch: ( params: APICallParams ) => Promise - error: (message: string) => void invalidateCache: () => void getAppID: () => string } export type APIError = { message?: string - url?: string + url: string method?: HTTPMethod json: any status: number From 70a8b9e4688249211f5e9d56463e3524a97b2539 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 13:55:00 +0100 Subject: [PATCH 13/16] Remove screen id --- packages/types/src/documents/app/screen.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/types/src/documents/app/screen.ts b/packages/types/src/documents/app/screen.ts index e85fd5953b..b2cedf31a9 100644 --- a/packages/types/src/documents/app/screen.ts +++ b/packages/types/src/documents/app/screen.ts @@ -36,7 +36,6 @@ export type ScreenRoutingJson = Record< subpaths: Record< string, { - screenId: string screens: Record } > From 88e245a5bb7a97eedfc07ea6104e44c5d2dfbd2a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 9 Jan 2025 14:01:07 +0100 Subject: [PATCH 14/16] Fix typing --- packages/types/src/documents/app/screen.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/types/src/documents/app/screen.ts b/packages/types/src/documents/app/screen.ts index b2cedf31a9..a8c32118d3 100644 --- a/packages/types/src/documents/app/screen.ts +++ b/packages/types/src/documents/app/screen.ts @@ -33,11 +33,6 @@ export interface ScreenRoutesViewOutput extends Document { export type ScreenRoutingJson = Record< string, { - subpaths: Record< - string, - { - screens: Record - } - > + subpaths: Record } > From 23025f46396b828e27c356ae21c7bddb473c90c9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 10 Jan 2025 15:19:55 +0100 Subject: [PATCH 15/16] Clean --- packages/client/src/api/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/api/api.ts b/packages/client/src/api/api.ts index 93f59f6e9a..b944f7bd7c 100644 --- a/packages/client/src/api/api.ts +++ b/packages/client/src/api/api.ts @@ -4,7 +4,7 @@ import { notificationStore, devToolsEnabled, devToolsStore, -} from "../stores/index.js" +} from "../stores/index" import { get } from "svelte/store" export const API = createAPIClient({ From 9d0e3a17222f303e1f3a952e7c5e298d7187f070 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 10 Jan 2025 15:22:34 +0100 Subject: [PATCH 16/16] Remove unnecessary extensions --- packages/client/src/api/index.js | 4 ++-- packages/client/src/stores/index.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/client/src/api/index.js b/packages/client/src/api/index.js index a63e19bfbb..3c53045cfd 100644 --- a/packages/client/src/api/index.js +++ b/packages/client/src/api/index.js @@ -1,5 +1,5 @@ -import { API } from "./api.ts" -import { patchAPI } from "./patches.js" +import { API } from "./api" +import { patchAPI } from "./patches" // Certain endpoints which return rows need patched so that they transform // and enrich the row docs, so that they can be correctly handled by the diff --git a/packages/client/src/stores/index.js b/packages/client/src/stores/index.js index e099434b3d..f2b80ed732 100644 --- a/packages/client/src/stores/index.js +++ b/packages/client/src/stores/index.js @@ -1,6 +1,6 @@ export { authStore } from "./auth" export { appStore } from "./app" -export { notificationStore } from "./notification.ts" +export { notificationStore } from "./notification" export { routeStore } from "./routes" export { screenStore } from "./screens" export { builderStore } from "./builder"