Update attachment endpoints to use TS

This commit is contained in:
Andrew Kingston 2024-11-27 10:26:17 +00:00
parent 1e535d36b7
commit 9c35a7758c
No known key found for this signature in database
7 changed files with 117 additions and 99 deletions

View File

@ -49,10 +49,7 @@
data.append("file", fileList[i])
}
try {
return await API.uploadAttachment({
data,
tableId: formContext?.dataSource?.tableId,
})
return await API.uploadAttachment(formContext?.dataSource?.tableId, data)
} catch (error) {
return []
}

View File

@ -80,12 +80,7 @@
const upload = async () => {
loading = true
try {
const res = await API.externalUpload({
datasourceId,
bucket,
key,
data,
})
const res = await API.externalUpload(datasourceId, bucket, key, data)
notificationStore.actions.success("File uploaded successfully")
loading = false
return res

View File

@ -31,10 +31,10 @@
let attachRequest = new FormData()
attachRequest.append("file", signatureFile)
const resp = await API.uploadAttachment({
data: attachRequest,
tableId: formContext?.dataSource?.tableId,
})
const resp = await API.uploadAttachment(
formContext?.dataSource?.tableId,
attachRequest
)
const [signatureAttachment] = resp
updateValue = signatureAttachment
} else {

View File

@ -454,12 +454,7 @@ const downloadFileHandler = async action => {
const { type } = action.parameters
if (type === "attachment") {
const { tableId, rowId, attachmentColumn } = action.parameters
const res = await API.downloadAttachment(
tableId,
rowId,
attachmentColumn,
{ suppressErrors: true }
)
const res = await API.downloadAttachment(tableId, rowId, attachmentColumn)
await downloadStream(res)
return
}

View File

@ -1,78 +0,0 @@
export const buildAttachmentEndpoints = API => {
/**
* Generates a signed URL to upload a file to an external datasource.
* @param datasourceId the ID of the datasource to upload to
* @param bucket the name of the bucket to upload to
* @param key the name of the file to upload to
*/
const getSignedDatasourceURL = async ({ datasourceId, bucket, key }) => {
return await API.post({
url: `/api/attachments/${datasourceId}/url`,
body: { bucket, key },
})
}
return {
getSignedDatasourceURL,
/**
* Uploads an attachment to the server.
* @param data the attachment to upload
* @param tableId the table ID to upload to
*/
uploadAttachment: async ({ data, tableId }) => {
return await API.post({
url: `/api/attachments/${tableId}/upload`,
body: data,
json: false,
})
},
/**
* Uploads an attachment to the server as a builder user from the builder.
* @param data the data to upload
*/
uploadBuilderAttachment: async data => {
return await API.post({
url: "/api/attachments/process",
body: data,
json: false,
})
},
/**
* Uploads a file to an external datasource.
* @param datasourceId the ID of the datasource to upload to
* @param bucket the name of the bucket to upload to
* @param key the name of the file to upload to
* @param data the file to upload
*/
externalUpload: async ({ datasourceId, bucket, key, data }) => {
const { signedUrl, publicUrl } = await getSignedDatasourceURL({
datasourceId,
bucket,
key,
})
await API.put({
url: signedUrl,
body: data,
json: false,
external: true,
})
return { publicUrl }
},
/**
* Download an attachment from a row given its column name.
* @param datasourceId the ID of the datasource to download from
* @param rowId the ID of the row to download from
* @param columnName the column name to download
*/
downloadAttachment: async (datasourceId, rowId, columnName, options) => {
return await API.get({
url: `/api/${datasourceId}/rows/${rowId}/attachment/${columnName}`,
parseResponse: response => response,
suppressErrors: options?.suppressErrors,
})
},
}
}

View File

@ -0,0 +1,109 @@
import { ProcessAttachmentResponse } from "@budibase/types"
import { BaseAPIClient } from "./types"
export interface AttachmentEndpoints {
getSignedDatasourceURL: (
datasourceId: string,
bucket: string,
key: string
) => Promise<{ signedUrl: string; publicUrl: string }>
uploadAttachment: (
tableId: string,
data: any
) => Promise<ProcessAttachmentResponse>
uploadBuilderAttachment: (data: any) => Promise<ProcessAttachmentResponse>
externalUpload: (
datasourceId: string,
bucket: string,
key: string,
data: any
) => Promise<{ publicUrl: string }>
downloadAttachment: (
datasourceId: string,
rowId: string,
columnName: string
) => Promise<any>
}
export const buildAttachmentEndpoints = (
API: BaseAPIClient
): AttachmentEndpoints => {
const endpoints: Pick<AttachmentEndpoints, "getSignedDatasourceURL"> = {
/**
* Generates a signed URL to upload a file to an external datasource.
* @param datasourceId the ID of the datasource to upload to
* @param bucket the name of the bucket to upload to
* @param key the name of the file to upload to
*/
getSignedDatasourceURL: async (datasourceId, bucket, key) => {
return await API.post({
url: `/api/attachments/${datasourceId}/url`,
body: { bucket, key },
})
},
}
return {
...endpoints,
/**
* Uploads an attachment to the server.
* @param data the attachment to upload
* @param tableId the table ID to upload to
*/
uploadAttachment: async (tableId, data) => {
return await API.post({
url: `/api/attachments/${tableId}/upload`,
body: data,
json: false,
})
},
/**
* Uploads an attachment to the server as a builder user from the builder.
* @param data the data to upload
*/
uploadBuilderAttachment: async data => {
return await API.post({
url: "/api/attachments/process",
body: data,
json: false,
})
},
/**
* Uploads a file to an external datasource.
* @param datasourceId the ID of the datasource to upload to
* @param bucket the name of the bucket to upload to
* @param key the name of the file to upload to
* @param data the file to upload
*/
externalUpload: async (datasourceId, bucket, key, data) => {
const { signedUrl, publicUrl } = await endpoints.getSignedDatasourceURL(
datasourceId,
bucket,
key
)
await API.put({
url: signedUrl,
body: data,
json: false,
external: true,
})
return { publicUrl }
},
/**
* Download an attachment from a row given its column name.
* @param datasourceId the ID of the datasource to download from
* @param rowId the ID of the row to download from
* @param columnName the column name to download
*/
downloadAttachment: async (datasourceId, rowId, columnName) => {
return await API.get({
url: `/api/${datasourceId}/rows/${rowId}/attachment/${columnName}`,
parseResponse: response => response as any,
suppressErrors: true,
})
},
}
}

View File

@ -27,7 +27,7 @@ export type APICallConfig = {
suppressErrors: boolean
cache: boolean
body?: any
parseResponse?: <T>(response: Response) => Promise<T>
parseResponse?: <T>(response: Response) => Promise<T> | T
}
export type APICallParams = Pick<APICallConfig, "url"> & Partial<APICallConfig>