Skip to content

Commit d81b079

Browse files
committed
Add extension check function
1 parent aae6d58 commit d81b079

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

packages/data-core/lib/plugin-utils/plugin.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars */
22

3-
import { SchemaField } from '../schema/types';
3+
import { SchemaField, SchemaFieldObject } from '../schema/types';
4+
5+
export type PluginEditSchema =
6+
| (SchemaFieldObject & { type: 'root' })
7+
| undefined
8+
| symbol;
49

510
export abstract class Plugin {
611
static HIDDEN = Symbol('hidden');
712

813
name: string = 'Plugin';
914

10-
watchFields?: string[] = [];
11-
1215
init(data: any) {}
1316

14-
editSchema(formData?: any): SchemaField | undefined | symbol {
17+
editSchema(formData?: any): PluginEditSchema {
1518
return undefined;
1619
}
1720

packages/data-plugins/lib/collections/ext-item-assets.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { Plugin, SchemaFieldObject } from '@stac-manager/data-core';
2-
import { array2Object, object2Array } from '../utils';
1+
import { Plugin, PluginEditSchema } from '@stac-manager/data-core';
2+
import { array2Object, hasExtension, object2Array } from '../utils';
33

44
export class PluginItemAssets extends Plugin {
5-
name = 'Item Assets';
5+
name = 'Item Assets Extension';
6+
7+
editSchema(data: any): PluginEditSchema {
8+
if (!hasExtension(data, 'item-assets')) {
9+
return Plugin.HIDDEN;
10+
}
611

7-
editSchema(): SchemaFieldObject {
812
return {
913
type: 'root',
1014
properties: {

packages/data-plugins/lib/collections/ext-render.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import { Plugin, SchemaFieldObject } from '@stac-manager/data-core';
1+
import { Plugin, PluginEditSchema } from '@stac-manager/data-core';
22
import {
33
array2Object,
4+
hasExtension,
45
object2Array,
56
object2Tuple,
67
tuple2Object
78
} from '../utils';
89

910
export class PluginRender extends Plugin {
10-
name = 'Render';
11+
name = 'Render Extension';
12+
13+
editSchema(data: any): PluginEditSchema {
14+
if (!hasExtension(data, 'render')) {
15+
return Plugin.HIDDEN;
16+
}
1117

12-
editSchema(): SchemaFieldObject {
1318
return {
1419
type: 'root',
1520
properties: {

packages/data-plugins/lib/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,26 @@ export function tuple2Object(stack: string[][]) {
170170
return { ...acc, [key]: item };
171171
}, {});
172172
}
173+
174+
/**
175+
* Checks if the given data has a specific STAC extension.
176+
*
177+
* @param data - The data object to check for the extension.
178+
* @param extension - The name of the extension to look for.
179+
* @param version - An optional function that takes a version string and returns
180+
* a boolean indicating whether the version matches.
181+
*
182+
* @returns A boolean indicating whether the specified extension (and version,
183+
* if provided) is present in the data.
184+
*/
185+
export function hasExtension(
186+
data: any,
187+
extension: string,
188+
version?: (v: string) => boolean
189+
) {
190+
const regex = new RegExp(`/${extension}/v([0-9.]+)/schema.json`);
191+
return data?.stac_extensions?.some((ext: string) => {
192+
const match = ext.match(regex);
193+
return match && (!version || version(match[1]));
194+
});
195+
}

0 commit comments

Comments
 (0)