mirror of
https://github.com/logto-io/logto.git
synced 2025-03-24 22:41:28 -05:00
feat(settings): add settings related queries and apis (#174)
* feat(settings): add settings related queries and APIs * feat: correct setting router type * chore: hide settingId from response * chore: reset default settingId for better readability * chore: fix input params check
This commit is contained in:
parent
f218667ba4
commit
dfaf79bc5f
3 changed files with 55 additions and 0 deletions
packages/core/src
25
packages/core/src/queries/setting.ts
Normal file
25
packages/core/src/queries/setting.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { Setting, SettingUpdate, Settings } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import pool from '@/database/pool';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
import { convertToIdentifiers, OmitAutoSetFields } from '@/database/utils';
|
||||
|
||||
export const defaultSettingId = 'default';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(Settings);
|
||||
|
||||
export const getSetting = async () =>
|
||||
pool.one<Setting>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id}=${defaultSettingId}
|
||||
`);
|
||||
|
||||
export const updateSetting = async (setting: Partial<OmitAutoSetFields<SettingUpdate>>) => {
|
||||
return buildUpdateWhere<SettingUpdate, Setting>(
|
||||
pool,
|
||||
Settings,
|
||||
true
|
||||
)({ set: setting, where: { id: defaultSettingId } });
|
||||
};
|
|
@ -6,6 +6,7 @@ import { Provider } from 'oidc-provider';
|
|||
import koaAuth from '@/middleware/koa-auth';
|
||||
import applicationRoutes from '@/routes/application';
|
||||
import sessionRoutes from '@/routes/session';
|
||||
import settingRoutes from '@/routes/setting';
|
||||
import statusRoutes from '@/routes/status';
|
||||
import swaggerRoutes from '@/routes/swagger';
|
||||
import userRoutes from '@/routes/user';
|
||||
|
@ -23,6 +24,7 @@ const createRouters = (provider: Provider) => {
|
|||
const router: AuthedRouter = new Router();
|
||||
router.use(koaAuth());
|
||||
applicationRoutes(router);
|
||||
settingRoutes(router);
|
||||
|
||||
return [anonymousRouter, router];
|
||||
};
|
||||
|
|
28
packages/core/src/routes/setting.ts
Normal file
28
packages/core/src/routes/setting.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { Settings } from '@logto/schemas';
|
||||
|
||||
import koaGuard from '@/middleware/koa-guard';
|
||||
import { getSetting, updateSetting } from '@/queries/setting';
|
||||
|
||||
import { AuthedRouter } from './types';
|
||||
|
||||
export default function settingRoutes<T extends AuthedRouter>(router: T) {
|
||||
router.get('/settings', async (ctx, next) => {
|
||||
const { id, ...rest } = await getSetting();
|
||||
ctx.body = rest;
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
router.patch(
|
||||
'/settings',
|
||||
koaGuard({
|
||||
body: Settings.guard.omit({ id: true }).partial(),
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
const { body: setting } = ctx.guard;
|
||||
ctx.body = await updateSetting(setting);
|
||||
|
||||
return next();
|
||||
}
|
||||
);
|
||||
}
|
Loading…
Add table
Reference in a new issue