-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
335 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
{ | ||
"Add AsyncAPI Skeleton": { | ||
"prefix": "add asyncapi skeleton", | ||
"body": [ | ||
"asyncapi: '2.5.0'", | ||
"info:", | ||
" title: ${1:Title}", | ||
" version: '1.0.0'", | ||
" description: |", | ||
" Add your multiline description here.", | ||
" contact:", | ||
" name: ${2:Contact Name}", | ||
" email: ${3:[email protected]}", | ||
"", | ||
"servers:", | ||
" production:", | ||
" url: server.url", | ||
" protocol: mqtt", | ||
" description: server description", | ||
"", | ||
"defaultContentType: application/json", | ||
"", | ||
"channels:", | ||
" this.is.one.channel.please.rename:", | ||
" description: A sample topic on which messages may be produced and consumed.", | ||
"${4:# type 'add publish' or 'add subscribe' to get snippets autocomplete}", | ||
"", | ||
"", | ||
"", | ||
"components:", | ||
" messages:", | ||
" description: add one message and remove this line", | ||
" # type here 'add message' to get snippets autocomplete", | ||
"", | ||
"", | ||
" schemas:", | ||
" OneEntity:", | ||
" type: object", | ||
"", | ||
"" | ||
] | ||
}, | ||
"Add Subscribe to Async Request": { | ||
"prefix": "add asyncapi subscribe to async request", | ||
"body": [ | ||
" subscribe:", | ||
" summary: ${1:Entity} Async Requests", | ||
" operationId: do${1:Entity}Request", | ||
" tags:", | ||
" - name: ${1:Entity}", | ||
" message:", | ||
" \\$ref: '#/components/messages/${1:Entity}RequestMessage'", | ||
"" | ||
] | ||
}, | ||
"Add Publish Event Operation": { | ||
"prefix": "add asyncapi publish event operation", | ||
"body": [ | ||
" publish:", | ||
" summary: ${1:Entity} Domain Events", | ||
" operationId: on${1:Entity}Event", | ||
" tags:", | ||
" - name: ${1:Entity}", | ||
" message:", | ||
" \\$ref: '#/components/messages/${1:Entity}EventMessage'", | ||
"" | ||
] | ||
}, | ||
"Add AsyncAPI Message": { | ||
"prefix": "add asyncapi message", | ||
"body": [ | ||
" ${1:Entity}${2|Request,Event|}Message:", | ||
" name: ${1}${2}Message", | ||
" title: Async ${2} for a ${1}", | ||
" summary: Async ${2} for a ${1}", | ||
" schemaFormat: application/vnd.aai.asyncapi;version=2.4.0", | ||
" payload:", | ||
" \\$ref: '#/components/schemas/${1}'", | ||
"" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
|
||
export function previewAsyncAPI(context: vscode.ExtensionContext) { | ||
return async (uri: vscode.Uri) => { | ||
uri = uri || (await promptForAsyncapiFile()) as vscode.Uri; | ||
if (uri) { | ||
console.log('Opening asyncapi file', uri.fsPath); | ||
openAsyncAPI(context, uri); | ||
} | ||
}; | ||
} | ||
|
||
export const openAsyncapiFiles: { [id: string]: vscode.WebviewPanel } = {}; // vscode.Uri.fsPath => vscode.WebviewPanel | ||
|
||
export function isAsyncAPIFile(document?: vscode.TextDocument) { | ||
if (!document) { | ||
return false; | ||
} | ||
if (document.languageId === 'json') { | ||
try { | ||
const json = JSON.parse(document.getText()); | ||
return json.asyncapi; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
if (document.languageId === 'yml' || document.languageId === 'yaml') { | ||
return document.getText().match('^asyncapi:') !== null; | ||
} | ||
return false; | ||
} | ||
|
||
export function openAsyncAPI(context: vscode.ExtensionContext, uri: vscode.Uri) { | ||
const localResourceRoots = [ | ||
vscode.Uri.file(path.dirname(uri.fsPath)), | ||
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/browser/standalone'), | ||
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/styles'), | ||
]; | ||
if (vscode.workspace.workspaceFolders) { | ||
vscode.workspace.workspaceFolders.forEach(folder => { | ||
localResourceRoots.push(folder.uri); | ||
}); | ||
} | ||
const panel: vscode.WebviewPanel = | ||
openAsyncapiFiles[uri.fsPath] || | ||
vscode.window.createWebviewPanel('asyncapi-preview', '', vscode.ViewColumn.Two, { | ||
enableScripts: true, | ||
retainContextWhenHidden: true, | ||
localResourceRoots, | ||
}); | ||
panel.title = path.basename(uri.fsPath); | ||
panel.webview.html = getWebviewContent(context, panel.webview, uri); | ||
|
||
panel.onDidDispose(() => { | ||
delete openAsyncapiFiles[uri.fsPath]; | ||
}); | ||
openAsyncapiFiles[uri.fsPath] = panel; | ||
} | ||
|
||
async function promptForAsyncapiFile() { | ||
if (isAsyncAPIFile(vscode.window.activeTextEditor?.document)) { | ||
return vscode.window.activeTextEditor?.document.uri; | ||
} | ||
return await vscode.window.showOpenDialog({ | ||
canSelectFiles: true, | ||
canSelectFolders: false, | ||
canSelectMany: false, | ||
openLabel: 'Open AsyncAPI file', | ||
filters: { | ||
AsyncAPI: ['yml', 'yaml', 'json'], | ||
}, | ||
}); | ||
} | ||
|
||
function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Webview, asyncapiFile: vscode.Uri) { | ||
const asyncapiComponentJs = webview.asWebviewUri( | ||
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/browser/standalone/index.js') | ||
); | ||
const asyncapiComponentCss = webview.asWebviewUri( | ||
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/styles/default.min.css') | ||
); | ||
const asyncapiWebviewUri = webview.asWebviewUri(asyncapiFile); | ||
const asyncapiBasePath = asyncapiWebviewUri.toString().replace('%2B', '+'); // this is loaded by a different library so it requires unescaping the + character | ||
const html = ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="${asyncapiComponentCss}"> | ||
</head> | ||
<body x-timestamp="${Date.now()}"> | ||
<div id="asyncapi"></div> | ||
<script src="${asyncapiComponentJs}"></script> | ||
<script> | ||
AsyncApiStandalone.render({ | ||
schema: { | ||
url: '${asyncapiWebviewUri}', | ||
options: { method: "GET", mode: "cors" }, | ||
}, | ||
config: { | ||
show: { | ||
sidebar: true, | ||
errors: true, | ||
}, | ||
parserOptions: { path: '${asyncapiBasePath}' } | ||
}, | ||
}, document.getElementById('asyncapi')); | ||
</script> | ||
</body> | ||
</html> | ||
`; | ||
return html; | ||
} |
Oops, something went wrong.