MCPcopy
hub / github.com/logto-io/logto / roleScopeRoutes

Function roleScopeRoutes

packages/core/src/routes/role.scope.ts:13–136  ·  view source on GitHub ↗
(
  ...[router, { queries, libraries }]: RouterInitArgs<T>
)

Source from the content-addressed store, hash-verified

11import type { ManagementApiRouter, RouterInitArgs } from './types.js';
12
13export default function roleScopeRoutes<T extends ManagementApiRouter>(
14 ...[router, { queries, libraries }]: RouterInitArgs<T>
15) {
16 const {
17 rolesScopes: { deleteRolesScope, findRolesScopesByRoleId, insertRolesScopes },
18 roles: { findRoleById },
19 scopes: { findScopesByIds, countScopesByScopeIds, searchScopesByScopeIds },
20 } = queries;
21 const {
22 quota,
23 roleScopes: { validateRoleScopeAssignment },
24 scopes: { attachResourceToScopes },
25 } = libraries;
26
27 router.get(
28 '/roles/:id/scopes',
29 koaPagination({ isOptional: true }),
30 koaGuard({
31 params: object({ id: string().min(1) }),
32 response: scopeResponseGuard.array(),
33 status: [200, 400, 404],
34 }),
35 async (ctx, next) => {
36 const {
37 params: { id },
38 } = ctx.guard;
39 const { limit, offset, disabled } = ctx.pagination;
40 const { searchParams } = ctx.request.URL;
41 await findRoleById(id);
42
43 return tryThat(
44 async () => {
45 const search = parseSearchParamsForSearch(searchParams);
46
47 const rolesScopes = await findRolesScopesByRoleId(id);
48 const scopeIds = rolesScopes.map(({ scopeId }) => scopeId);
49
50 if (disabled) {
51 const scopes = await searchScopesByScopeIds(scopeIds, search);
52
53 ctx.body = await attachResourceToScopes(scopes);
54
55 return next();
56 }
57
58 const [{ count }, scopes] = await Promise.all([
59 countScopesByScopeIds(scopeIds, search),
60 searchScopesByScopeIds(scopeIds, search, limit, offset),
61 ]);
62
63 // Return totalCount to pagination middleware
64 ctx.pagination.totalCount = count;
65 ctx.body = await attachResourceToScopes(scopes);
66
67 return next();
68 },
69 (error) => {
70 if (error instanceof TypeError) {

Callers 1

createRoutersFunction · 0.85

Calls 15

koaPaginationFunction · 0.85
koaGuardFunction · 0.85
findRolesScopesByRoleIdFunction · 0.85
searchScopesByScopeIdsFunction · 0.85
attachResourceToScopesFunction · 0.85
nextFunction · 0.85
countScopesByScopeIdsFunction · 0.85
insertRolesScopesFunction · 0.85
findScopesByIdsFunction · 0.85
deleteRolesScopeFunction · 0.85

Tested by

no test coverage detected