Skip to content

Commit

Permalink
feat: update Intellij d.ts Stubs and add Window.btoa/atob (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Jan 6, 2025
1 parent 3174b19 commit 3f12e64
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features
- logger supports call to parent logger (AnWeber/vscode-httpyac#347)
- update Intellij d.ts Stubs and add Window.btoa/atob (#839)

### Fix
- ensure correct order if cli args are used (#773)
Expand Down
1 change: 1 addition & 0 deletions src/plugins/intellij/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './intellijHttpRequest';
export * from './intellijHttpResponse';
export * from './intellijRandom';
export * from './intellijVariables';
export * from './intellijWindow';
export * from './stubs';
11 changes: 11 additions & 0 deletions src/plugins/intellij/api/intellijWindow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CommonWindow } from './stubs';

export class IntellijWindow implements CommonWindow {
btoa(bytes: string): string {
return global.btoa(bytes);
}

atob(bytes: string): string {
return global.atob(bytes);
}
}
30 changes: 26 additions & 4 deletions src/plugins/intellij/api/stubs/http-client.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export interface Variables {
/**
* Saves variable with name 'varName' and sets its value to 'varValue'.
*/
set(varName: string, varValue: string): void;
set(varName: string, varValue: unknown): void;

/**
* Returns value of variable 'varName'.
*/
get(varName: string): string;
get(varName: string): unknown;

/**
* Checks no variables are defined.
Expand Down Expand Up @@ -84,6 +84,11 @@ export interface CommonHttpClientRequest {
* Method of the current request
*/
method: string;

// currently not supported
// iteration(): number;
// currently not supported
// templateValue(expressionNumber: number): string | boolean | number;
}

/**
Expand Down Expand Up @@ -117,12 +122,12 @@ export interface RequestEnvironment {
/**
* Variables for constructing current request. Can be updated in Pre-request handler script.
*/
export interface RequestVariables {
interface RequestVariables {
/**
* Retrieves request variable value by its name. Returns null if there is no such variable
* @param name request variable name
*/
get(name: string): string | null;
get(name: string): unknown;
}

/**
Expand All @@ -134,6 +139,7 @@ export interface CommonRequestHeader {
*/
name: string;
}

/**
* Retrieves a value from a JSON object using a JSONPath expression.
*
Expand All @@ -142,3 +148,19 @@ export interface CommonRequestHeader {
* @return {any} - The value found in the JSON object using the JSONPath expression.
*/
export type jsonPath = (obj: unknown, expression: string) => unknown;

/**
* Retrieves a value from a XML object using a XPath expression.
*
* @param {any} obj - The obj to search in with XPath expression:
* - if obj is Node, Element or Document returns result of search with expression.
* - null in all other cases.
* @param {string} expression - The XPath expression to use for searching.
* @return {any} - The value found in the XML object using the XPath expression.
*/
export type xpath = (obj: unknown, expression: string) => unknown;

export interface CommonWindow {
atob(str: string): string;
btoa(bytes: string): string;
}
4 changes: 2 additions & 2 deletions src/plugins/intellij/api/stubs/http-client.pre-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export interface PreRequestRequestVariables {
/**
* Saves variable with name 'varName' and sets its value to 'varValue'.
*/
set(varName: string, varValue: string): void;
set(varName: string, varValue: unknown): void;

/**
* Returns value of variable 'varName'.
*/
get(varName: string): string | undefined;
get(varName: string): unknown;

/**
* Checks no variables are defined.
Expand Down
15 changes: 11 additions & 4 deletions src/plugins/intellij/api/stubs/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { CommonHttpClient, CommonHttpClientRequest, CommonRequestHeader } from './http-client.common';
import { PreRequestRequestHeader } from './http-client.pre-request';

import { Document } from '@xmldom/xmldom';
// declare const request: HttpClientFinalRequest;

export interface HttpClient extends CommonHttpClient {
Expand Down Expand Up @@ -34,7 +34,10 @@ export interface TextStreamResponse {
* @param subscriber function to be called on each line of the stream
* @param onFinish function to be called after the end of the stream
*/
onEachLine(subscriber: (line: string | object, unsubscribe: () => void) => void, onFinish?: () => void): void;
onEachLine(
subscriber: (line: string | Document | object, unsubscribe: () => void) => void,
onFinish?: () => void
): void;

/**
* Subscribes user to each message sent by server. This method should be used with well-defined streaming protocols,
Expand All @@ -44,7 +47,11 @@ export interface TextStreamResponse {
* @param onFinish function to be called after the end of the stream
*/
onEachMessage(
subscriber: (message: string | object, unsubscribe: () => void, output?: (answer: string) => void) => void,
subscriber: (
message: string | Document | object,
unsubscribe: () => void,
output?: (answer: string) => void
) => void,
onFinish?: () => void
): void;
}
Expand All @@ -56,7 +63,7 @@ export interface HttpResponse {
/**
* Response content, it is a string or JSON object if response content-type is json.
*/
body: string | TextStreamResponse | unknown;
body: string | TextStreamResponse | Document | object;

/**
* Response headers storage.
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/intellij/api/stubs/http-declare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpClient, HttpClientRequest, HttpResponse } from './http-client';
import { jsonPath } from './http-client.common';
import { CommonWindow, jsonPath, xpath } from './http-client.common';
import { CryptoSupport } from './http-client.crypto';
import { PreRequestHttpClientRequest } from './http-client.pre-request';

Expand All @@ -8,6 +8,8 @@ export interface IntellijJavascriptGlobal extends Record<string, unknown> {
client: HttpClient;
crypto: CryptoSupport;
jsonPath?: jsonPath; // not implemented
xpath?: xpath; // not implemented
Window: CommonWindow;
$exampleServer?: string; // not implemented
/**
* The object holds information about HTTP Response.
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/intellij/intellijAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ function initIntellijVariables(context: models.ProcessorContext): intellij.Intel
crypto: new intellij.IntellijCryptoSupport(),
$random: new intellij.IntellijRandom(),
$env: process.env,
Window: new intellij.IntellijWindow(),
xpath:
};
if (context.request) {
if (context.httpRegion.response) {
Expand Down

0 comments on commit 3f12e64

Please sign in to comment.