Skip to content

Commit

Permalink
feat(service-betterportal): add capability support
Browse files Browse the repository at this point in the history
Add support for adding new capabilities to the BetterPortal service. The
`addCapability` method was added to the `BetterPortalCallable` interface and
implemented in the `Service` class. The `BetterPortalCapabilityConfigurable`
enum was added to the `index.ts` file to define the configurable capabilities.
The `BetterPortalCapabilityInternal` enum was also added to define the internal
capabilities. The `BetterPortalCapability` type was added to define the
capabilities that can be used. The `capabilities` array was added to the
`Service` class to store the added capabilities. The `/bp/capabilities/` and
`/bp/capabilities/:capability/:optionalparam?/` routes were added to the
`Service` class to handle the capabilities.
  • Loading branch information
mrinc committed Mar 30, 2023
1 parent 0ad3b04 commit 5673e60
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 6 deletions.
25 changes: 23 additions & 2 deletions src/clients/service-betterportal/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
} from "@bettercorp/service-base";
import { MyPluginConfig } from "../../plugins/service-betterportal/sec.config";
import type {
BSBFastifyCallable,
BetterPortalCallable,
BetterPortalBasicEvents,
BetterPortalEvents,
} from "../../plugins/service-betterportal/plugin";
import type {
AuthToken,
BetterPortalCapabilityConfigurable,
FastifyBodyRequestHandler,
FastifyNoBodyRequestHandler,
} from "../../index";
Expand All @@ -20,7 +22,7 @@ export class betterPortal extends ServicesClient<
ServiceCallable,
ServiceCallable,
ServiceCallable,
BSBFastifyCallable,
BetterPortalCallable,
MyPluginConfig
> {
private readonly _serviceName: string;
Expand All @@ -40,6 +42,25 @@ export class betterPortal extends ServicesClient<
);
}

public async addCapability(
capability: BetterPortalCapabilityConfigurable,
capabilityCallback: {
(
token: AuthToken | null,
clientId: string | null,
param?: string,
optional?: Record<string, string>
): Promise<any>;
}
): Promise<void> {
return await this._plugin.callPluginMethod(
"addCapability",
this._serviceName,
capability,
capabilityCallback
);
}

public async get<Path extends string>(
path: Path,
permissionRequired: string,
Expand Down
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ import {
import type { ParamsFromPath } from "@bettercorp/service-base-plugin-web-server/lib/plugins/service-fastify/lib";
import { IncomingMessage } from "http";

export enum BetterPortalCapabilityConfigurable {
search = "search",
searchCache = "searchCache",
searchAuthed = "searchAuthed",
searchCacheAuthed = "searchCacheAuthed",
changelog = "changelog",
settings = "settings",
settingsAuthed = "settingsAuthed",
}
export enum BetterPortalCapabilityInternal {
uiServices = "uiServices",
permissions = "permissions",
}
export type BetterPortalCapability =
| BetterPortalCapabilityInternal
| BetterPortalCapabilityConfigurable;

export interface AuthToken {
host: string;
iss: string;
Expand Down
108 changes: 104 additions & 4 deletions src/plugins/service-betterportal/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ReplyRequestCacheConfig,
ReplyRequestCacheConfigAbility,
ClientPermissions,
BetterPortalCapabilityConfigurable,
} from "../../index";
import type { MyPluginConfig } from "./sec.config";
import path, { join, sep } from "path";
Expand All @@ -40,7 +41,19 @@ export interface BetterPortalEvents extends ServiceCallable {
meta: BetterPortalBasicEvents
): Promise<void>;
}
export interface BSBFastifyCallable extends ServiceCallable {
export interface BetterPortalCallable extends ServiceCallable {
addCapability(
serviceName: string,
capability: BetterPortalCapabilityConfigurable,
capabilityCallback: {
(
token: AuthToken | null,
clientId: string | null,
param?: string,
optional?: Record<string, string>
): Promise<any>;
}
): Promise<void>;
initBPUI(serviceName: string, path: string): Promise<void>;
get<Path extends string>(
serviceName: string,
Expand Down Expand Up @@ -104,10 +117,10 @@ export class Service
ServiceCallable,
ServiceCallable,
ServiceCallable,
ServiceCallable,
BetterPortalCallable,
MyPluginConfig
>
implements BSBFastifyCallable
implements BetterPortalCallable
{
public override readonly initAfterPlugins: string[] = [
"service-fastify",
Expand All @@ -118,6 +131,17 @@ export class Service
private fastify: fastify;
private webJwt!: webJwtLocal;
private canCache: boolean = true;
private capabilities: Array<{
capability: BetterPortalCapabilityConfigurable;
capabilityCallback: {
(
token: AuthToken | null,
clientId: string | null,
param?: string | undefined,
optional?: Record<string, string> | undefined
): Promise<any>;
};
}> = [];
constructor(
pluginName: string,
cwd: string,
Expand Down Expand Up @@ -408,9 +432,34 @@ export class Service
);
}
}
public async addCapability(
serviceName: string,
capability: BetterPortalCapabilityConfigurable,
capabilityCallback: {
(
token: AuthToken | null,
clientId: string | null,
param?: string | undefined,
optional?: Record<string, string> | undefined
): Promise<any>;
}
): Promise<void> {
await this.log.info(
"Adding new capability [{capability}] for [{serviceName}]",
{
capability: capability,
serviceName: serviceName,
}
);
this.capabilities.push({
capability,
capabilityCallback,
});
}
public override async init(): Promise<void> {
const self = this;
this.canCache = await (await this.getPluginConfig()).canCache;
this.webJwt.init(
await this.webJwt.init(
{
bearerStr: "Bearer",
queryKey: "auth",
Expand All @@ -429,6 +478,57 @@ export class Service
issuer: (await this.getPluginConfig()).issuer,
}
);
await this.get(
"betterportal",
"/bp/capabilities/",
null,
async (
reply,
token,
clientId,
roles,
params,
query,
checkCacheCanSendData,
req
): Promise<any> => {
let capas = self.capabilities
.map((x) => self.capabilities.map((x) => x.capability))
.filter((x, i, a) => a.indexOf(x) === i);
let hash = await this.createMD5(capas.join("-"));
if (
this.canSendNewDocumentCache(req as any, reply, hash, {
cacheAbility: ReplyRequestCacheConfigAbility.all,
maxAge: 60 * 60 * 24,
})
) {
return reply.status(202).send(capas);
}
}
);
await this.get(
"betterportal",
"/bp/capabilities/:capability/:optionalparam?/",
"",
async (reply, token, clientId, roles, params, query) => {
let responses: Array<any> = [];
for (let cap of self.capabilities) {
if (cap.capability !== params.capability) continue;
let res = await cap.capabilityCallback(
token ?? null,
clientId ?? null,
params.optionalparam,
query
);
if (Tools.isNullOrUndefined(res)) continue;
responses.push(res);
}
return reply.status(202).send(responses);
},
undefined,
undefined,
true
);
}

public async get<Path extends string>(
Expand Down

0 comments on commit 5673e60

Please sign in to comment.