-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Databerry API Upload single endpoint
- Loading branch information
Showing
5 changed files
with
354 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
pages/api/external/datastores/file-upload/process/[id].ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { | ||
DatasourceStatus, | ||
DatasourceType, | ||
DatastoreVisibility, | ||
SubscriptionPlan, | ||
Usage, | ||
} from '@prisma/client'; | ||
import Cors from 'cors'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { z } from 'zod'; | ||
|
||
import { AppNextApiRequest } from '@app/types/index'; | ||
import { ApiError, ApiErrorType } from '@app/utils/api-error'; | ||
import { createApiHandler, respond } from '@app/utils/createa-api-handler'; | ||
import guardDataProcessingUsage from '@app/utils/guard-data-processing-usage'; | ||
import prisma from '@app/utils/prisma-client'; | ||
import runMiddleware from '@app/utils/run-middleware'; | ||
import triggerTaskLoadDatasource from '@app/utils/trigger-task-load-datasource'; | ||
import validate from '@app/utils/validate'; | ||
|
||
const cors = Cors({ | ||
methods: ['POST', 'HEAD'], | ||
}); | ||
|
||
const handler = createApiHandler(); | ||
|
||
const Schema = z.object({ | ||
id: z.string().cuid(), | ||
}); | ||
|
||
export const processUpload = async ( | ||
req: AppNextApiRequest, | ||
res: NextApiResponse | ||
) => { | ||
const datastoreId = req.query.id as string; | ||
|
||
const data = req.body as z.infer<typeof Schema>; | ||
const datasourceId = data?.id; | ||
|
||
// get Bearer token from header | ||
const authHeader = req.headers.authorization; | ||
const token = authHeader && authHeader.split(' ')?.[1]; | ||
|
||
if (!datastoreId) { | ||
throw new ApiError(ApiErrorType.INVALID_REQUEST); | ||
} | ||
|
||
const datasource = await prisma.appDatasource.findUnique({ | ||
where: { | ||
id: datasourceId, | ||
}, | ||
include: { | ||
datastore: true, | ||
owner: { | ||
include: { | ||
usage: true, | ||
apiKeys: true, | ||
subscriptions: { | ||
where: { | ||
status: 'active', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!datasource) { | ||
throw new ApiError(ApiErrorType.NOT_FOUND); | ||
} | ||
|
||
if ( | ||
datasource?.datastoreId !== datastoreId || | ||
(datasource?.datastore?.visibility === DatastoreVisibility.private && | ||
(!token || | ||
!datasource?.owner?.apiKeys.find((each) => each.key === token))) | ||
) { | ||
throw new ApiError(ApiErrorType.UNAUTHORIZED); | ||
} | ||
|
||
guardDataProcessingUsage({ | ||
usage: datasource?.owner?.usage as Usage, | ||
plan: | ||
datasource?.owner?.subscriptions?.[0]?.plan || SubscriptionPlan.level_0, | ||
}); | ||
|
||
await triggerTaskLoadDatasource([ | ||
{ | ||
userId: datasource.ownerId!, | ||
datasourceId, | ||
priority: 1, | ||
}, | ||
]); | ||
|
||
return datasource; | ||
}; | ||
|
||
handler.post( | ||
validate({ | ||
body: Schema, | ||
handler: respond(processUpload), | ||
}) | ||
); | ||
|
||
export default async function wrapper( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
await runMiddleware(req, res, cors); | ||
|
||
return handler(req, res); | ||
} |
Oops, something went wrong.
30f33b7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
dashboard – ./
www.databerry.ai
dashboard-git-main-databerry.vercel.app
dashboard-databerry.vercel.app
dashboard-nu-tawny.vercel.app
*.databerry.ai
databerry.ai
app.databerry.ai