diff --git a/packages/server/src/api/routes/public/middleware/mapper.ts b/packages/server/src/api/routes/public/middleware/mapper.ts index 138a5ac23f..f3c994bb9b 100644 --- a/packages/server/src/api/routes/public/middleware/mapper.ts +++ b/packages/server/src/api/routes/public/middleware/mapper.ts @@ -1,3 +1,4 @@ +import { Ctx } from "@budibase/types" import mapping from "../../../controllers/public/mapping" enum Resources { @@ -9,11 +10,19 @@ enum Resources { SEARCH = "search", } -function isArrayResponse(ctx: any) { +function isAttachment(ctx: Ctx) { + return ctx.body?.path && ctx.body?.flags && ctx.body?.mode +} + +function isArrayResponse(ctx: Ctx) { return ctx.url.endsWith(Resources.SEARCH) || Array.isArray(ctx.body) } -function processApplications(ctx: any) { +function noResponse(ctx: Ctx) { + return Object.keys(ctx.body).length === 0 +} + +function processApplications(ctx: Ctx) { if (isArrayResponse(ctx)) { return mapping.mapApplications(ctx) } else { @@ -21,7 +30,7 @@ function processApplications(ctx: any) { } } -function processTables(ctx: any) { +function processTables(ctx: Ctx) { if (isArrayResponse(ctx)) { return mapping.mapTables(ctx) } else { @@ -29,7 +38,7 @@ function processTables(ctx: any) { } } -function processRows(ctx: any) { +function processRows(ctx: Ctx) { if (isArrayResponse(ctx)) { return mapping.mapRowSearch(ctx) } else { @@ -37,7 +46,7 @@ function processRows(ctx: any) { } } -function processUsers(ctx: any) { +function processUsers(ctx: Ctx) { if (isArrayResponse(ctx)) { return mapping.mapUsers(ctx) } else { @@ -45,7 +54,7 @@ function processUsers(ctx: any) { } } -function processQueries(ctx: any) { +function processQueries(ctx: Ctx) { if (isArrayResponse(ctx)) { return mapping.mapQueries(ctx) } else { @@ -53,8 +62,8 @@ function processQueries(ctx: any) { } } -export default async (ctx: any, next: any) => { - if (!ctx.body) { +export default async (ctx: Ctx, next: any) => { + if (!ctx.body || noResponse(ctx) || isAttachment(ctx)) { return await next() } let urlParts = ctx.url.split("/") diff --git a/packages/server/src/api/routes/public/tests/applications.spec.ts b/packages/server/src/api/routes/public/tests/applications.spec.ts index fae049da75..0a2ffe9e95 100644 --- a/packages/server/src/api/routes/public/tests/applications.spec.ts +++ b/packages/server/src/api/routes/public/tests/applications.spec.ts @@ -70,12 +70,22 @@ describe("check export/import", () => { it("should be able to export app", async () => { mocks.licenses.useExpandedPublicApi() const res = await runExport() + expect(res.headers["content-disposition"]).toMatch( + /attachment; filename=".*-export-.*\.tar.gz"/g + ) + expect(res.body instanceof Buffer).toBe(true) expect(res.status).toBe(200) }) it("should be able to import app", async () => { mocks.licenses.useExpandedPublicApi() const res = await runImport() + expect(Object.keys(res.body).length).toBe(0) + // check screens imported correctly + const screens = await config.api.screen.list() + expect(screens.length).toBe(2) + expect(screens[0].routing.route).toBe("/derp") + expect(screens[1].routing.route).toBe("/blank") expect(res.status).toBe(204) }) })