mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
feat(core): add resource apis (#180)
* feat(core): add resource apis add resource apis * fix(core): resources api CR fix CR fix of resoruces API * fix(core): resource api cr fix remove unnesessary input params destructor
This commit is contained in:
parent
49581d924e
commit
d88db54961
2 changed files with 108 additions and 0 deletions
|
@ -5,6 +5,7 @@ import { Provider } from 'oidc-provider';
|
|||
|
||||
import koaAuth from '@/middleware/koa-auth';
|
||||
import applicationRoutes from '@/routes/application';
|
||||
import resourceRoutes from '@/routes/resource';
|
||||
import sessionRoutes from '@/routes/session';
|
||||
import settingRoutes from '@/routes/setting';
|
||||
import statusRoutes from '@/routes/status';
|
||||
|
@ -25,6 +26,7 @@ const createRouters = (provider: Provider) => {
|
|||
router.use(koaAuth());
|
||||
applicationRoutes(router);
|
||||
settingRoutes(router);
|
||||
resourceRoutes(router);
|
||||
|
||||
return [anonymousRouter, router];
|
||||
};
|
||||
|
|
106
packages/core/src/routes/resource.ts
Normal file
106
packages/core/src/routes/resource.ts
Normal file
|
@ -0,0 +1,106 @@
|
|||
import { Resources } from '@logto/schemas';
|
||||
import { object, string } from 'zod';
|
||||
|
||||
import koaGuard from '@/middleware/koa-guard';
|
||||
import koaPagination from '@/middleware/koa-pagination';
|
||||
import {
|
||||
findTotalNumberOfResources,
|
||||
findAllResources,
|
||||
findResourceById,
|
||||
insertResource,
|
||||
updateResourceById,
|
||||
deleteResourceById,
|
||||
} from '@/queries/resource';
|
||||
import { findAllScopesWithResourceId } from '@/queries/scope';
|
||||
import { buildIdGenerator } from '@/utils/id';
|
||||
|
||||
import { AuthedRouter } from './types';
|
||||
|
||||
const resourceId = buildIdGenerator(21);
|
||||
|
||||
export default function resourceRoutes<T extends AuthedRouter>(router: T) {
|
||||
router.get('/resources', koaPagination(), async (ctx, next) => {
|
||||
const { limit, offset } = ctx.pagination;
|
||||
|
||||
const [{ count }, resources] = await Promise.all([
|
||||
findTotalNumberOfResources(),
|
||||
findAllResources(limit, offset),
|
||||
]);
|
||||
|
||||
ctx.pagination.totalCount = count;
|
||||
|
||||
// TODO: query scopes of all resources and insert into the response data
|
||||
ctx.body = resources;
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
router.post(
|
||||
'/resources',
|
||||
koaGuard({
|
||||
body: Resources.guard.omit({ id: true }),
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
const resource = await insertResource({
|
||||
id: resourceId(),
|
||||
...ctx.guard.body,
|
||||
});
|
||||
|
||||
ctx.body = { ...resource, scopes: [] };
|
||||
|
||||
return next();
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
'/resources/:id',
|
||||
koaGuard({ params: object({ id: string().min(1) }) }),
|
||||
async (ctx, next) => {
|
||||
const {
|
||||
params: { id },
|
||||
} = ctx.guard;
|
||||
|
||||
const [resource, scopes] = await Promise.all([
|
||||
findResourceById(id),
|
||||
findAllScopesWithResourceId(id),
|
||||
]);
|
||||
|
||||
ctx.body = { ...resource, scopes };
|
||||
return next();
|
||||
}
|
||||
);
|
||||
|
||||
router.patch(
|
||||
'/resources/:id',
|
||||
koaGuard({
|
||||
params: object({ id: string().min(1) }),
|
||||
body: Resources.guard.omit({ id: true }).partial(),
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
const {
|
||||
params: { id },
|
||||
body,
|
||||
} = ctx.guard;
|
||||
|
||||
const [scopes, resource] = await Promise.all([
|
||||
findAllScopesWithResourceId(id),
|
||||
updateResourceById(id, body),
|
||||
]);
|
||||
|
||||
ctx.body = { ...resource, scopes };
|
||||
return next();
|
||||
}
|
||||
);
|
||||
|
||||
router.delete(
|
||||
'/resources/:id',
|
||||
koaGuard({ params: object({ id: string().min(1) }) }),
|
||||
async (ctx, next) => {
|
||||
const { id } = ctx.guard.params;
|
||||
await findResourceById(id);
|
||||
await deleteResourceById(id);
|
||||
ctx.status = 204;
|
||||
return next();
|
||||
}
|
||||
);
|
||||
}
|
Loading…
Reference in a new issue