badge: type tweaks.

Signed-off-by: Andreas Stenius <git@astekk.se>
This commit is contained in:
Andreas Stenius
2021-02-23 13:24:51 +01:00
committed by Fredrik Adelöw
parent ac17aeb35d
commit 38cc67e551
4 changed files with 32 additions and 13 deletions
@@ -16,32 +16,36 @@
import { Logger } from 'winston';
import { makeBadge } from 'badge-maker';
import { JsonObject } from '@backstage/config';
import { BadgeBuilder, BadgeOptions } from './types';
import { Badge, BadgeStyle, BadgeStyles } from '../../types';
import { Badge, BadgeConfig, BadgeStyle, BadgeStyles } from '../../types';
import { interpolate } from '../../utils';
export class DefaultBadgeBuilder implements BadgeBuilder {
constructor(
private readonly logger: Logger,
private readonly config: JsonObject,
private readonly config: BadgeConfig,
) {
for (const [badgeId, badge] of Object.entries(config)) {
badge.id = badgeId;
if (badge) {
badge.id = badgeId;
}
}
}
public async getAllBadgeConfigs(): Promise<Badge[]> {
return Object.values(this.config) as Badge[];
return Object.values(this.config);
}
public async getBadgeConfig(badgeId: string): Promise<Badge> {
return ((this.config[badgeId] as unknown) ||
(this.config.default as unknown) || {
return (
this.config[badgeId] ||
this.config.default ||
({
label: 'Unknown badge ID',
message: badgeId,
color: 'red',
}) as Badge;
} as Badge)
);
}
public async createBadge(options: BadgeOptions): Promise<string> {
@@ -69,7 +73,7 @@ export class DefaultBadgeBuilder implements BadgeBuilder {
params.description = badge.description
? this.render(badge.description, context)
: badge.id;
params.markdown = this.getMarkdownCode(params, context.badge_url);
params.markdown = this.getMarkdownCode(params, context.badge_url!);
return JSON.stringify(
{
@@ -14,10 +14,18 @@
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { Badge } from '../../types';
export type BadgeOptions = {
context: object;
context: {
app?: {
title: string;
};
entity?: Entity;
entity_url?: string;
badge_url?: string;
};
config: Badge;
format: 'svg' | 'json';
};
+4 -3
View File
@@ -25,7 +25,7 @@ import {
import { Entity } from '@backstage/catalog-model';
import { Config, JsonObject } from '@backstage/config';
import { BadgeBuilder, DefaultBadgeBuilder } from '../lib/BadgeBuilder';
import { Badge, BadgeStyle, BadgeStyles } from '../types';
import { Badge, BadgeConfig, BadgeStyle, BadgeStyles } from '../types';
export interface RouterOptions {
badgeBuilder?: BadgeBuilder;
@@ -43,7 +43,7 @@ export async function createRouter(
const title = options.config.getString('app.title') || 'Backstage';
const catalogUrl = `${options.config.getString('app.baseUrl')}/catalog`;
const badgesConfig = (options.config.getOptional('badges') ??
{}) as JsonObject;
{}) as BadgeConfig;
for (const badge of options.badges || []) {
if (!badge.id) {
@@ -70,6 +70,7 @@ export async function createRouter(
const context = {
app: { title },
badge_url: '',
entity,
entity_url,
};
@@ -81,7 +82,7 @@ export async function createRouter(
context.badge_url = [
`${req.protocol}://`,
req.headers.host,
req.originalUrl.replace(/badge-specs$/, badge.id),
req.originalUrl.replace(/badge-specs$/, badge.id!),
].join('');
specs.push(
await badgeBuilder.createBadge({
+6
View File
@@ -45,4 +45,10 @@ export interface Badge {
message: string;
/** Badge style (apperance). One of "plastic", "flat", "flat-square", "for-the-badge" and "social" */
style?: BadgeStyle;
/** (generated) markdown code */
markdown?: string;
}
export interface BadgeConfig {
[id: string]: Badge;
}