Skip to content

Commit

Permalink
feat(preset-loader): add the possibility to not show local preset and…
Browse files Browse the repository at this point in the history
… create one from a ncr oas endpoint response (#33)

Co-authored-by: Puria Nafisi Azizi <[email protected]>
  • Loading branch information
matteo-cristino and puria authored Dec 23, 2024
1 parent ca0ef66 commit 670fc54
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export namespace Components {
}
interface DyneSlangroomPresetLoader {
"editorId": string;
"loadLocalPresets": boolean;
"oasEndpoint"?: string;
}
}
declare global {
Expand Down Expand Up @@ -148,6 +150,8 @@ declare namespace LocalJSX {
}
interface DyneSlangroomPresetLoader {
"editorId"?: string;
"loadLocalPresets"?: boolean;
"oasEndpoint"?: string;
}
interface IntrinsicElements {
"dyne-button": DyneButton;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export class DyneSlangroomPresetLoader {
@Element() el: HTMLElement;

@Prop({ reflect: true }) editorId: string;
@Prop() loadLocalPresets: boolean = true;
@Prop() oasEndpoint?: string;

@State() presets: SlangroomPreset[] = SlangroomPresets;

@State() filteredPresets: SlangroomPreset[] = this.presets;
@State() presets: SlangroomPreset[] = [];
@State() filteredPresets: SlangroomPreset[] = [];
@State() searchText = '';

get dialog() {
Expand All @@ -28,6 +29,13 @@ export class DyneSlangroomPresetLoader {
}

async componentDidLoad() {
if (this.loadLocalPresets) {
this.presets = SlangroomPresets;
}
if (this.oasEndpoint) {
const apiPresets = await this.fetchPresetsFromOas();
this.addPresets(apiPresets);
}
await this.loadPresetsFromElements();
lockScrollOnDialogOpen(this.dialog!);
}
Expand Down Expand Up @@ -72,6 +80,43 @@ export class DyneSlangroomPresetLoader {
this.presets = [...this.presets, ...presets];
}

private async fetchPresetsFromOas(): Promise<SlangroomPreset[]> {
if (!this.oasEndpoint) return [];

try {
const response = await fetch(this.oasEndpoint);
if (!response.ok) {
console.error('Failed to fetch presets:', response.statusText);
return [];
}
const oas = await response.json();
const oasPaths: OasPaths = oas.paths;
const res: SlangroomPreset[] = [];
for (const [contractPath, contractMethods] of Object.entries(oasPaths)) {
if (contractPath.endsWith('/app') || contractPath.endsWith('/raw')) {
continue;
}
const contractInfo = contractMethods?.post || contractMethods?.get;
if (!contractInfo) continue;
res.push({
name: contractPath,
contract: contractInfo.description,
keys: contractInfo.keys,
data: '',
meta: {
title: contractPath,
highlight: ""
},
group: contractInfo.tags[0]
});
}
return res;
} catch (error) {
console.error('Error fetching presets:', error);
return [];
}
}

@Watch('presets')
updatePresetsSearch() {
this.filteredPresets = this.presets;
Expand Down Expand Up @@ -142,6 +187,19 @@ type PresetsProps = {
presets?: SlangroomPreset[];
};

type OasPathMethod = {
description: string;
keys: string;
tags: string[];
};

type OasPathValue = {
get?: OasPathMethod;
post?: OasPathMethod;
};

type OasPaths = Record<string, OasPathValue>;

function PresetsSelect(props: PresetsProps) {
const { onPresetSelect = () => {}, presets = [] } = props;

Expand Down
8 changes: 5 additions & 3 deletions src/components/dyne-slangroom-preset-loader/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

## Properties

| Property | Attribute | Description | Type | Default |
| ---------- | ----------- | ----------- | -------- | ----------- |
| `editorId` | `editor-id` | | `string` | `undefined` |
| Property | Attribute | Description | Type | Default |
| ------------------ | -------------------- | ----------- | --------------------- | ----------- |
| `editorId` | `editor-id` | | `string` | `undefined` |
| `loadLocalPresets` | `load-local-presets` | | `boolean` | `true` |
| `oasEndpoint` | `oas-endpoint` | | `string \| undefined` | `undefined` |


## Dependencies
Expand Down
8 changes: 8 additions & 0 deletions vscode-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@
{
"name": "editor-id",
"description": ""
},
{
"name": "load-local-presets",
"description": ""
},
{
"name": "oas-endpoint",
"description": ""
}
]
}
Expand Down

0 comments on commit 670fc54

Please sign in to comment.