-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Front: Add front endpoint & swr to fetch tags from core (#10565)
* Front: Add front endpoint & swr to fetch tags from core * types unhappy
- Loading branch information
Showing
6 changed files
with
179 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { CoreAPI } from "@dust-tt/types"; | ||
import { isLeft } from "fp-ts/Either"; | ||
import * as t from "io-ts"; | ||
import * as reporter from "io-ts-reporters"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { withSessionAuthenticationForWorkspace } from "@app/lib/api/auth_wrappers"; | ||
import apiConfig from "@app/lib/api/config"; | ||
import type { Authenticator } from "@app/lib/auth"; | ||
import { getFeatureFlags } from "@app/lib/auth"; | ||
import logger from "@app/logger/logger"; | ||
import { apiError } from "@app/logger/withlogging"; | ||
|
||
export const PostTagSearchBodySchema = t.type({ | ||
query: t.string, | ||
queryType: t.string, | ||
dataSources: t.array(t.string), | ||
}); | ||
|
||
export type PostTagSearchBody = t.TypeOf<typeof PostTagSearchBodySchema>; | ||
|
||
async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
auth: Authenticator | ||
) { | ||
const user = auth.getNonNullableUser(); | ||
if (!user || !auth.isUser()) { | ||
return apiError(req, res, { | ||
status_code: 401, | ||
api_error: { | ||
type: "data_source_auth_error", | ||
message: "You are not authorized to fetch tags.", | ||
}, | ||
}); | ||
} | ||
|
||
const owner = auth.getNonNullableWorkspace(); | ||
const flags = await getFeatureFlags(owner); | ||
|
||
if (!flags.includes("tags_filters")) { | ||
return apiError(req, res, { | ||
status_code: 403, | ||
api_error: { | ||
type: "feature_flag_not_found", | ||
message: "The feature is not enabled for this workspace.", | ||
}, | ||
}); | ||
} | ||
|
||
const { method } = req; | ||
|
||
if (method !== "POST") { | ||
return apiError(req, res, { | ||
status_code: 405, | ||
api_error: { | ||
type: "method_not_supported_error", | ||
message: "The method passed is not supported, POST is expected.", | ||
}, | ||
}); | ||
} | ||
|
||
const bodyValidation = PostTagSearchBodySchema.decode(req.body); | ||
if (isLeft(bodyValidation)) { | ||
const pathError = reporter.formatValidationErrors(bodyValidation.left); | ||
|
||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Invalid request body: ${pathError}`, | ||
}, | ||
}); | ||
} | ||
|
||
const { query, queryType, dataSources } = bodyValidation.right; | ||
|
||
const coreAPI = new CoreAPI(apiConfig.getCoreAPIConfig(), logger); | ||
const result = await coreAPI.searchTags({ | ||
query, | ||
queryType, | ||
dataSources, | ||
}); | ||
|
||
if (result.isErr()) { | ||
return apiError(req, res, { | ||
status_code: 500, | ||
api_error: { | ||
type: "internal_server_error", | ||
message: "Failed to search tags", | ||
}, | ||
}); | ||
} | ||
return res.status(200).json(result.value.response); | ||
} | ||
|
||
export default withSessionAuthenticationForWorkspace(handler); |
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
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