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: introduce snippet manager #482

Merged
merged 6 commits into from
Mar 12, 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
3 changes: 3 additions & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@
},
"dependencies": {
"openai": "^4.28.4",
"postman-code-generators": "^1.9.0",
"postman-collection": "^4.4.0",
"simple-git": "^3.22.0"
},
"devDependencies": {
"@podman-desktop/api": "^1.8.0",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20",
"@types/postman-collection": "^3.5.10",
"vitest": "^1.3.1"
}
}
79 changes: 79 additions & 0 deletions packages/backend/src/managers/SnippetManager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { beforeEach, expect, test, vi } from 'vitest';
import { SnippetManager } from './SnippetManager';
import type { Webview } from '@podman-desktop/api';
import { Messages } from '@shared/Messages';

const webviewMock = {
postMessage: vi.fn(),
} as unknown as Webview;

beforeEach(() => {
vi.resetAllMocks();
vi.mocked(webviewMock.postMessage).mockResolvedValue(undefined);
});

test('expect init to notify webview', () => {
const manager = new SnippetManager(webviewMock);
manager.init();

expect(webviewMock.postMessage).toHaveBeenCalledWith({
id: Messages.MSG_SUPPORTED_LANGUAGES_UPDATE,
body: manager.getLanguageList(),
});
});

test('expect postman-code-generators to have many languages available.', () => {
const manager = new SnippetManager(webviewMock);
manager.init();

expect(manager.getLanguageList().length).toBeGreaterThan(0);
});

test('expect postman-code-generators to have nodejs supported.', () => {
const manager = new SnippetManager(webviewMock);
manager.init();

const languages = manager.getLanguageList();
const nodejs = languages.find(language => language.key === 'nodejs');
expect(nodejs).toBeDefined();
expect(nodejs.variants.length).toBeGreaterThan(0);

const native = nodejs.variants.find(variant => variant.key === 'Request');
expect(native).toBeDefined();
});

test('expect postman-code-generators to generate proper nodejs native code', async () => {
const manager = new SnippetManager(webviewMock);

const snippet = await manager.generate('http://localhost:8080', 'nodejs', 'Request');
expect(snippet).toBe(`var request = require('request');
var options = {
'method': 'GET',
'url': 'http://localhost:8080',
'headers': {
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
`);
});
52 changes: 52 additions & 0 deletions packages/backend/src/managers/SnippetManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type { Disposable, Webview } from '@podman-desktop/api';
axel7083 marked this conversation as resolved.
Show resolved Hide resolved
import { getLanguageList, convert, type Language } from 'postman-code-generators';
import { Request } from 'postman-collection';
import { Publisher } from '../utils/Publisher';
import { Messages } from '@shared/Messages';

export class SnippetManager extends Publisher<Language[]> implements Disposable {
constructor(webview: Webview) {
super(webview, Messages.MSG_SUPPORTED_LANGUAGES_UPDATE, () => this.getLanguageList());
}

getLanguageList(): Language[] {
return getLanguageList();
}

generate(url: string, language: string, variant: string): Promise<string> {
return new Promise((resolve, reject) => {
const request = new Request(url);
convert(language, variant, request, {}, (error: unknown, snippet: string) => {
if (error) {
reject(error);
return;
}
resolve(snippet);
});
});
}

init() {
// Notify the publisher
this.notify();
}

dispose(): void {}
}
1 change: 1 addition & 0 deletions packages/shared/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export enum Messages {
MSG_APPLICATIONS_STATE_UPDATE = 'applications-state-update',
MSG_LOCAL_REPOSITORY_UPDATE = 'local-repository-update',
MSG_INFERENCE_SERVERS_UPDATE = 'inference-servers-update',
MSG_SUPPORTED_LANGUAGES_UPDATE = 'supported-languages-supported',
}
45 changes: 45 additions & 0 deletions types/postman-code-generators.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
declare module 'postman-code-generators' {
benoitf marked this conversation as resolved.
Show resolved Hide resolved
axel7083 marked this conversation as resolved.
Show resolved Hide resolved
import type { Request } from 'postman-collection';

export function getLanguageList(): Language[];

export interface Language {
key: string;
label: string;
syntax_mode: string;
variants: LanguageVariant[],
}

export interface LanguageVariant {
key: string;
}

export function getOptions(language: string, variant: string, callback: (error: unknown, options: Option[]) => void): void;

export interface Option {
name: string;
id: string;
type: string;
default: string | number | boolean;
description: string;
}

export function convert(language: string, variant: string, request: Request, options: Record<string, string | number | boolean>, callback: (error: unknown, snippet: string | undefined) => void): void;
}
Loading