1
0
Fork 0
mirror of synced 2024-06-28 02:50:50 +12:00

Merge pull request #8422 from Budibase/bug/sev5/public-api-404

Public rows API: Return correct status codes
This commit is contained in:
Martin McKeaveney 2022-10-31 10:36:32 +00:00 committed by GitHub
commit 011bb990b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 82 deletions

View file

@ -30,21 +30,16 @@ export async function patch(ctx: any): Promise<any> {
if (body && !body._id) { if (body && !body._id) {
return save(ctx) return save(ctx)
} }
try { const { row, table } = await quotas.addQuery(
const { row, table } = await quotas.addQuery( () => pickApi(tableId).patch(ctx),
() => pickApi(tableId).patch(ctx), {
{ datasourceId: tableId,
datasourceId: tableId, }
} )
) ctx.status = 200
ctx.status = 200 ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:update`, appId, row, table)
ctx.eventEmitter && ctx.message = `${table.name} updated successfully.`
ctx.eventEmitter.emitRow(`row:update`, appId, row, table) ctx.body = row
ctx.message = `${table.name} updated successfully.`
ctx.body = row
} catch (err) {
ctx.throw(400, err)
}
} }
export const save = async (ctx: any) => { export const save = async (ctx: any) => {
@ -55,52 +50,35 @@ export const save = async (ctx: any) => {
if (body && body._id) { if (body && body._id) {
return patch(ctx) return patch(ctx)
} }
try { const { row, table } = await quotas.addRow(() =>
const { row, table } = await quotas.addRow(() => quotas.addQuery(() => pickApi(tableId).save(ctx), {
quotas.addQuery(() => pickApi(tableId).save(ctx), {
datasourceId: tableId,
})
)
ctx.status = 200
ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table)
ctx.message = `${table.name} saved successfully`
ctx.body = row
} catch (err) {
ctx.throw(400, err)
}
}
export async function fetchView(ctx: any) {
const tableId = getTableId(ctx)
try {
ctx.body = await quotas.addQuery(() => pickApi(tableId).fetchView(ctx), {
datasourceId: tableId, datasourceId: tableId,
}) })
} catch (err) { )
ctx.throw(400, err) ctx.status = 200
} ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table)
ctx.message = `${table.name} saved successfully`
ctx.body = row
}
export async function fetchView(ctx: any) {
const tableId = getTableId(ctx)
ctx.body = await quotas.addQuery(() => pickApi(tableId).fetchView(ctx), {
datasourceId: tableId,
})
} }
export async function fetch(ctx: any) { export async function fetch(ctx: any) {
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
try { ctx.body = await quotas.addQuery(() => pickApi(tableId).fetch(ctx), {
ctx.body = await quotas.addQuery(() => pickApi(tableId).fetch(ctx), { datasourceId: tableId,
datasourceId: tableId, })
})
} catch (err) {
ctx.throw(400, err)
}
} }
export async function find(ctx: any) { export async function find(ctx: any) {
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
try { ctx.body = await quotas.addQuery(() => pickApi(tableId).find(ctx), {
ctx.body = await quotas.addQuery(() => pickApi(tableId).find(ctx), { datasourceId: tableId,
datasourceId: tableId, })
})
} catch (err) {
ctx.throw(400, err)
}
} }
export async function destroy(ctx: any) { export async function destroy(ctx: any) {
@ -137,46 +115,30 @@ export async function destroy(ctx: any) {
export async function search(ctx: any) { export async function search(ctx: any) {
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
try { ctx.status = 200
ctx.status = 200 ctx.body = await quotas.addQuery(() => pickApi(tableId).search(ctx), {
ctx.body = await quotas.addQuery(() => pickApi(tableId).search(ctx), { datasourceId: tableId,
datasourceId: tableId, })
})
} catch (err) {
ctx.throw(400, err)
}
} }
export async function validate(ctx: any) { export async function validate(ctx: any) {
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
try { ctx.body = await pickApi(tableId).validate(ctx)
ctx.body = await pickApi(tableId).validate(ctx)
} catch (err) {
ctx.throw(400, err)
}
} }
export async function fetchEnrichedRow(ctx: any) { export async function fetchEnrichedRow(ctx: any) {
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
try { ctx.body = await quotas.addQuery(
ctx.body = await quotas.addQuery( () => pickApi(tableId).fetchEnrichedRow(ctx),
() => pickApi(tableId).fetchEnrichedRow(ctx), {
{ datasourceId: tableId,
datasourceId: tableId, }
} )
)
} catch (err) {
ctx.throw(400, err)
}
} }
export const exportRows = async (ctx: any) => { export const exportRows = async (ctx: any) => {
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
try { ctx.body = await quotas.addQuery(() => pickApi(tableId).exportRows(ctx), {
ctx.body = await quotas.addQuery(() => pickApi(tableId).exportRows(ctx), { datasourceId: tableId,
datasourceId: tableId, })
})
} catch (err) {
ctx.throw(400, err)
}
} }

View file

@ -118,7 +118,7 @@ exports.patch = async ctx => {
}) })
if (!validateResult.valid) { if (!validateResult.valid) {
throw { validation: validateResult.errors } ctx.throw(400, { validation: validateResult.errors })
} }
// returned row is cleaned and prepared for writing to DB // returned row is cleaned and prepared for writing to DB