feat(api-server): add endpoint to get shuffle state (#2792)

This commit is contained in:
Franz DC
2025-01-18 13:23:17 +08:00
committed by GitHub
parent 3339f997e3
commit 368b251e3f
6 changed files with 102 additions and 6 deletions

View File

@ -173,7 +173,24 @@ const routes = {
},
},
}),
getShuffleState: createRoute({
method: 'get',
path: `/api/${API_VERSION}/shuffle`,
summary: 'get shuffle state',
description: 'Get the current shuffle state',
responses: {
200: {
description: 'Success',
content: {
'application/json': {
schema: z.object({
state: z.boolean().nullable(),
}),
},
},
},
},
}),
shuffle: createRoute({
method: 'post',
path: `/api/${API_VERSION}/shuffle`,
@ -581,6 +598,25 @@ export const register = (
ctx.status(204);
return ctx.body(null);
});
app.openapi(routes.getShuffleState, async (ctx) => {
const stateResponsePromise = new Promise<boolean>((resolve) => {
ipcMain.once(
'ytmd:get-shuffle-response',
(_, isShuffled: boolean | undefined) => {
return resolve(!!isShuffled);
},
);
controller.requestShuffleInformation();
});
const isShuffled = await stateResponsePromise;
ctx.status(200);
return ctx.json({ state: isShuffled });
});
app.openapi(routes.shuffle, (ctx) => {
controller.shuffle();