@@ -27,14 +27,10 @@ import * as httpm from 'typed-rest-client/HttpClient';
2727import { IRequestOptions } from "typed-rest-client/Interfaces" ;
2828import { BEELINE_API_URL , NOTLS } from "../config" ;
2929import { generateHmac } from "./hmac" ;
30+ import { CodeLensCommandArgs } from "../types/CodeLensCommandArgs" ;
3031
3132export function c4InsertSla ( ) {
32- commands . registerCommand ( "c4.insert.sla" , async ( ...args : any [ ] ) => {
33- /* const encodedWorkspaceJson = args[0]; */
34- const apiUrl = args [ 1 ] ;
35- const lastLine = args [ 2 ] ;
36- const padding = args [ 3 ] ;
37-
33+ commands . registerCommand ( "c4.insert.sla" , async ( args : CodeLensCommandArgs ) => {
3834 const options : IRequestOptions = < IRequestOptions > { } ;
3935 options . ignoreSslError = workspace . getConfiguration ( ) . get ( NOTLS ) as boolean ;
4036 const httpc = new httpm . HttpClient ( 'vscode-c4-dsl-plugin' , [ ] , options ) ;
@@ -56,12 +52,26 @@ export function c4InsertSla() {
5652 progress . report ( { message : "Формирование SLA..." } ) ;
5753 httpc . post ( beelineApiUrl + path , content , headers )
5854 . then ( ( result ) => { return result . readBody ( ) } ) . then ( ( body ) => {
59- var lines = body . split ( / \r ? \n / ) . map ( ( line ) => line . trim ( ) ) . filter ( ( line ) => line . length > 0 ) . map ( ( line ) => " " . repeat ( padding ) + line ) ;
55+ var lines = body . split ( / \r ? \n / ) . map ( ( line ) => line . trim ( ) ) . filter ( ( line ) => line . length > 0 ) . map ( ( line ) => " " . repeat ( args . padding ) + line ) ;
6056 const editor = window . activeTextEditor ;
6157 if ( editor ) {
6258 editor . edit ( editBuilder => {
59+ if ( lines . length === 1 ) {
60+ const json = lines . at ( 0 ) ;
61+ if ( json !== undefined ) {
62+ try {
63+ let obj = JSON . parse ( json ) ;
64+ if ( obj . message !== undefined ) {
65+ window . showErrorMessage ( obj . message ) ;
66+ }
67+ } catch ( e ) {
68+ window . showErrorMessage ( body ) ;
69+ }
70+ return ;
71+ }
72+ }
6373 var os = require ( 'os' ) ;
64- editBuilder . insert ( new Position ( lastLine , 0 ) , lines . join ( os . EOL ) + os . EOL ) ;
74+ editBuilder . insert ( new Position ( args . lastLine , 0 ) , lines . join ( os . EOL ) + os . EOL ) ;
6575 } ) ;
6676 }
6777 } )
@@ -71,16 +81,16 @@ export function c4InsertSla() {
7181 } ;
7282
7383 progress . report ( { message : "Запрос описания API..." } ) ;
74- if ( apiUrl . startsWith ( "http://" ) || apiUrl . startsWith ( "https://" ) ) {
75- httpc . get ( apiUrl ) . then ( ( result ) => { return result . readBody ( ) } ) . then ( ( body ) => {
84+ if ( args . apiUrl . startsWith ( "http://" ) || args . apiUrl . startsWith ( "https://" ) ) {
85+ httpc . get ( args . apiUrl ) . then ( ( result ) => { return result . readBody ( ) } ) . then ( ( body ) => {
7686 insertSla ( body ) ;
7787 } ) . catch ( ( error ) => {
7888 window . showErrorMessage ( error . message ) ;
7989 } ) . finally ( ( ) => {
8090 resolve ( ) ;
8191 } ) ;
8292 } else {
83- fs . readFile ( apiUrl , 'utf8' , ( error , data ) => {
93+ fs . readFile ( args . apiUrl , 'utf8' , ( error , data ) => {
8494 if ( error ) {
8595 window . showErrorMessage ( error . message ) ;
8696 } else {
0 commit comments