Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preset-loader): add the possibility to not show local preset and create one from a ncr oas endpoint response #33

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading