mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-08-16 11:14:44 +08:00
Merge pull request #959 from LAION-AI/oasst-header
Send `x-oasst-user` header to backend
This commit is contained in:
@@ -21,14 +21,14 @@ const withoutRole = (role: Role, handler: (arg0: NextApiRequest, arg1: NextApiRe
|
||||
* Wraps any API Route handler and verifies that the user has the appropriate
|
||||
* role before running the handler. Returns a 403 otherwise.
|
||||
*/
|
||||
const withRole = (role: Role, handler: (arg0: NextApiRequest, arg1: NextApiResponse) => void) => {
|
||||
const withRole = (role: Role, handler: (arg0: NextApiRequest, arg1: NextApiResponse, token: JWT) => void) => {
|
||||
return async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const token = await getToken({ req });
|
||||
if (!token || token.role !== role) {
|
||||
res.status(403).end();
|
||||
return;
|
||||
}
|
||||
return handler(req, res);
|
||||
return handler(req, res, token);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -18,10 +18,16 @@ export class OasstError {
|
||||
export class OasstApiClient {
|
||||
oasstApiUrl: string;
|
||||
oasstApiKey: string;
|
||||
userHeaders: Record<string, string> = {};
|
||||
|
||||
constructor(oasstApiUrl: string, oasstApiKey: string) {
|
||||
constructor(oasstApiUrl: string, oasstApiKey: string, user?: BackendUserCore) {
|
||||
this.oasstApiUrl = oasstApiUrl;
|
||||
this.oasstApiKey = oasstApiKey;
|
||||
if (user) {
|
||||
this.userHeaders = {
|
||||
"X-OASST-USER": `${user.auth_method}:${user.id}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
// TODO return a strongly typed Task?
|
||||
// This method is used to store a task in RegisteredTask.task.
|
||||
@@ -215,9 +221,10 @@ export class OasstApiClient {
|
||||
method,
|
||||
...init,
|
||||
headers: {
|
||||
...init?.headers,
|
||||
...this.userHeaders,
|
||||
"X-API-Key": this.oasstApiKey,
|
||||
"Content-Type": "application/json",
|
||||
...init?.headers,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -227,8 +234,7 @@ export class OasstApiClient {
|
||||
|
||||
if (resp.status >= 300) {
|
||||
const errorText = await resp.text();
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let error: any;
|
||||
let error;
|
||||
try {
|
||||
error = JSON.parse(errorText);
|
||||
} catch (e) {
|
||||
@@ -239,8 +245,24 @@ export class OasstApiClient {
|
||||
|
||||
return await resp.json();
|
||||
}
|
||||
|
||||
fetch_my_messages(user: BackendUserCore) {
|
||||
const params = new URLSearchParams({
|
||||
username: user.id,
|
||||
auth_method: user.auth_method,
|
||||
});
|
||||
return this.get<Message[]>(`/api/v1/messages?${params}`);
|
||||
}
|
||||
|
||||
fetch_recent_messages() {
|
||||
return this.get<Message[]>(`/api/v1/messages`);
|
||||
}
|
||||
|
||||
fetch_message_children(messageId: string) {
|
||||
return this.get<Message[]>(`/api/v1/messages/${messageId}/children`);
|
||||
}
|
||||
|
||||
fetch_conversation(messageId: string) {
|
||||
return this.get(`/api/v1/messages/${messageId}/conversation`);
|
||||
}
|
||||
}
|
||||
|
||||
const oasstApiClient = new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY);
|
||||
|
||||
export { oasstApiClient };
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { JWT } from "next-auth/jwt";
|
||||
import { OasstApiClient } from "src/lib/oasst_api_client";
|
||||
import { getBackendUserCore } from "src/lib/users";
|
||||
import { BackendUserCore } from "src/types/Users";
|
||||
|
||||
export const createApiClientFromUser = (user: BackendUserCore) =>
|
||||
new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY, user);
|
||||
|
||||
export const createApiClient = async (token: JWT) => createApiClientFromUser(await getBackendUserCore(token.sub));
|
||||
|
||||
export const userlessApiClient = new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY);
|
||||
Reference in New Issue
Block a user