refactor IdentityApi
This commit is contained in:
@@ -14,14 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Group, GroupsJson, GroupsResponse, IdentityApiAdapter } from './types';
|
||||
import {
|
||||
Group,
|
||||
GroupsJson,
|
||||
GroupsResponse,
|
||||
IdentityApi,
|
||||
GroupsRequest,
|
||||
} from './types';
|
||||
import fs from 'fs-extra';
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
|
||||
const GROUPS_JSON_FILE = path.join(__dirname, 'data', 'userGroups.json');
|
||||
|
||||
export class StaticJsonAdapter implements IdentityApiAdapter {
|
||||
export class StaticJsonAdapter implements IdentityApi {
|
||||
private readonly groups: Group[];
|
||||
|
||||
constructor() {
|
||||
@@ -31,16 +36,13 @@ export class StaticJsonAdapter implements IdentityApiAdapter {
|
||||
this.groups = groupsJson.groups;
|
||||
}
|
||||
|
||||
getUserGroups(
|
||||
req: express.Request,
|
||||
res: express.Response,
|
||||
): express.Response<GroupsResponse> {
|
||||
const user = req.params.user;
|
||||
const type = req.query.type?.toString() ?? '';
|
||||
|
||||
const userGroups = this._getUserGroups(this.groups, user);
|
||||
const groups = this.filterGroupsByType(userGroups, type);
|
||||
return res.json({ groups });
|
||||
getUserGroups(req: GroupsRequest): Promise<GroupsResponse> {
|
||||
return new Promise(resolve => {
|
||||
const { user, type } = req;
|
||||
const userGroups = this._getUserGroups(this.groups, user);
|
||||
const groups = this.filterGroupsByType(userGroups, type);
|
||||
resolve({ groups });
|
||||
});
|
||||
}
|
||||
|
||||
_getUserGroups(groups: Group[], user: string) {
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import express from 'express';
|
||||
|
||||
export type User = {
|
||||
name: string;
|
||||
};
|
||||
@@ -35,9 +33,10 @@ export type GroupsResponse = {
|
||||
groups: Group[];
|
||||
};
|
||||
|
||||
export interface IdentityApiAdapter {
|
||||
getUserGroups(
|
||||
req: express.Request,
|
||||
res: express.Response,
|
||||
): express.Response<GroupsResponse>;
|
||||
export type GroupsRequest = {
|
||||
user: string;
|
||||
type: string;
|
||||
};
|
||||
export interface IdentityApi {
|
||||
getUserGroups(req: GroupsRequest): Promise<GroupsResponse>;
|
||||
}
|
||||
|
||||
@@ -18,15 +18,21 @@ import express from 'express';
|
||||
import Router from 'express-promise-router';
|
||||
import { Logger } from 'winston';
|
||||
import { StaticJsonAdapter } from '../adapters';
|
||||
import { IdentityApiAdapter } from '../adapters/types';
|
||||
import { IdentityApi } from '../adapters/types';
|
||||
|
||||
export interface RouterOptions {
|
||||
logger: Logger;
|
||||
}
|
||||
|
||||
const makeRouter = (adapter: IdentityApiAdapter): express.Router => {
|
||||
const makeRouter = (adapter: IdentityApi): express.Router => {
|
||||
const router = Router();
|
||||
router.get('/users/:user/groups', adapter.getUserGroups.bind(adapter));
|
||||
router.get('/users/:user/groups', async (req, res) => {
|
||||
const user = req.params.user;
|
||||
const type = req.query.type?.toString() ?? '';
|
||||
|
||||
const response = await adapter.getUserGroups({ user, type });
|
||||
res.send(response);
|
||||
});
|
||||
return router;
|
||||
};
|
||||
|
||||
@@ -35,7 +41,7 @@ export async function createRouter(
|
||||
): Promise<express.Router> {
|
||||
const logger = options.logger;
|
||||
|
||||
logger.info('Initializing identity provider');
|
||||
logger.info('Initializing identity API backend');
|
||||
const adapter = new StaticJsonAdapter();
|
||||
return makeRouter(adapter);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user