From 2c6725404fe292f455286a17c8112e8f827aa62e Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 25 Jul 2023 17:35:05 +0200 Subject: [PATCH] Types --- packages/server/src/api/controllers/row/index.ts | 6 ++++-- .../server/src/sdk/app/rows/search/internal.ts | 8 ++++---- .../server/src/utilities/rowProcessor/index.ts | 15 +++++++++------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 79cd5fbfe0..4f0f8df975 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -12,6 +12,7 @@ import { SearchResponse, SortOrder, SortType, + UserCtx, ViewV2, } from "@budibase/types" import * as utils from "./utils" @@ -29,7 +30,7 @@ function pickApi(tableId: any) { return internal } -export async function patch(ctx: any): Promise { +export async function patch(ctx: UserCtx): Promise { const appId = ctx.appId const tableId = utils.getTableId(ctx) const body = ctx.request.body @@ -53,7 +54,7 @@ export async function patch(ctx: any): Promise { ctx.message = `${table.name} updated successfully.` ctx.body = row gridSocket?.emitRowUpdate(ctx, row) - } catch (err) { + } catch (err: any) { ctx.throw(400, err) } } @@ -78,6 +79,7 @@ export const save = async (ctx: any) => { ctx.body = row || squashed gridSocket?.emitRowUpdate(ctx, row || squashed) } + export async function fetchView(ctx: any) { const tableId = utils.getTableId(ctx) const viewName = decodeURIComponent(ctx.params.viewName) diff --git a/packages/server/src/sdk/app/rows/search/internal.ts b/packages/server/src/sdk/app/rows/search/internal.ts index 5a29541705..e7f0aadfd6 100644 --- a/packages/server/src/sdk/app/rows/search/internal.ts +++ b/packages/server/src/sdk/app/rows/search/internal.ts @@ -147,8 +147,8 @@ export async function exportRows( export async function fetch(tableId: string) { const db = context.getAppDB() - let table = await sdk.tables.getTable(tableId) - let rows = await getRawTableData(db, tableId) + const table = await sdk.tables.getTable(tableId) + const rows = await getRawTableData(db, tableId) const result = await outputProcessing(table, rows) return result } @@ -171,7 +171,7 @@ async function getRawTableData(db: Database, tableId: string) { export async function fetchView( viewName: string, options: { calculation: string; group: string; field: string } -) { +): Promise { // if this is a table view being looked for just transfer to that if (viewName.startsWith(DocumentType.TABLE)) { return fetch(viewName) @@ -197,7 +197,7 @@ export async function fetchView( ) } - let rows + let rows: Row[] = [] if (!calculation) { response.rows = response.rows.map(row => row.doc) let table: Table diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 4b6e0f6e87..8e95a15dca 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -186,18 +186,21 @@ export function inputProcessing( * @param {object} opts used to set some options for the output, such as disabling relationship squashing. * @returns {object[]|object} the enriched rows will be returned. */ -export async function outputProcessing( +export async function outputProcessing( table: Table, - rows: Row[] | Row, + rows: T, opts = { squash: true } -) { +): Promise { + let safeRows: Row[] let wasArray = true if (!(rows instanceof Array)) { - rows = [rows] + safeRows = [rows] wasArray = false + } else { + safeRows = rows } // attach any linked row information - let enriched = await linkRows.attachFullLinkedDocs(table, rows as Row[]) + let enriched = await linkRows.attachFullLinkedDocs(table, safeRows) // process formulas enriched = processFormulas(table, enriched, { dynamic: true }) as Row[] @@ -221,7 +224,7 @@ export async function outputProcessing( enriched )) as Row[] } - return wasArray ? enriched : enriched[0] + return (wasArray ? enriched : enriched[0]) as T } /**