Skip to content

Commit 9389585

Browse files
authored
Merge pull request #890 from Udhay-Adithya/add-feature-in-app-terminal
in app terminal logger
2 parents a9546be + 72800ae commit 9389585

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3376
-498
lines changed

lib/apitoolgen/request_consolidator.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class APIDashRequestDescription {
5050
if (bodyJSON != null) {
5151
getTyp(input, [i = 0]) {
5252
String indent = "\t";
53-
for (int j = 0; j < i; j++) indent += "\t";
53+
for (int j = 0; j < i; j++) {
54+
indent += "\t";
55+
}
5456
if (input.runtimeType.toString().toLowerCase().contains('map')) {
5557
String typd = '{';
5658
for (final z in input.keys) {

lib/consts.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,6 @@ const kMsgClearHistorySuccess = 'History cleared successfully';
511511
const kMsgClearHistoryError = 'Error clearing history';
512512
const kMsgShareError = "Unable to share";
513513
const kLabelGenerateUI = "Generate UI";
514+
// Terminal Page
515+
const kMsgNoLogs = 'No logs yet';
516+
const kMsgSendToView = 'Send a request to view its details in the console.';

lib/main.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ void main() async {
1919

2020
var settingsModel = await getSettingsFromSharedPrefs();
2121
var onboardingStatus = await getOnboardingStatusFromSharedPrefs();
22-
initializeJsRuntime();
2322
final initStatus = await initApp(
2423
kIsDesktop,
2524
settingsModel: settingsModel,

lib/providers/collection_providers.dart

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:apidash_core/apidash_core.dart';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_riverpod/flutter_riverpod.dart';
55
import 'package:apidash/consts.dart';
6+
import 'package:apidash/terminal/terminal.dart';
67
import 'providers.dart';
78
import '../models/models.dart';
89
import '../services/services.dart';
@@ -320,7 +321,9 @@ class CollectionStateNotifier
320321
RequestModel executionRequestModel = requestModel!.copyWith();
321322

322323
if (!requestModel.preRequestScript.isNullOrEmpty()) {
323-
executionRequestModel = await handlePreRequestScript(
324+
executionRequestModel = await ref
325+
.read(jsRuntimeNotifierProvider.notifier)
326+
.handlePreRequestScript(
324327
executionRequestModel,
325328
originalEnvironmentModel,
326329
(envModel, updatedValues) {
@@ -347,6 +350,18 @@ class CollectionStateNotifier
347350
executionRequestModel.httpRequestModel!);
348351
}
349352

353+
// Terminal: start network log
354+
final terminal = ref.read(terminalStateProvider.notifier);
355+
final logId = terminal.startNetwork(
356+
apiType: executionRequestModel.apiType,
357+
method: substitutedHttpRequestModel.method,
358+
url: substitutedHttpRequestModel.url,
359+
requestId: requestId,
360+
requestHeaders: substitutedHttpRequestModel.enabledHeadersMap,
361+
requestBodyPreview: substitutedHttpRequestModel.body,
362+
isStreaming: true,
363+
);
364+
350365
// Set model to working and streaming
351366
state = {
352367
...state!,
@@ -398,6 +413,17 @@ class CollectionStateNotifier
398413
...state!,
399414
requestId: newRequestModel,
400415
};
416+
// Terminal: append chunk preview
417+
if (response != null && response.body.isNotEmpty) {
418+
terminal.addNetworkChunk(
419+
logId,
420+
BodyChunk(
421+
ts: DateTime.now(),
422+
text: response.body,
423+
sizeBytes: response.body.codeUnits.length,
424+
),
425+
);
426+
}
401427
unsave();
402428

403429
if (historyModel != null && httpResponseModel != null) {
@@ -425,6 +451,7 @@ class CollectionStateNotifier
425451
if (!completer.isCompleted) {
426452
completer.complete((null, null, 'StreamError: $e'));
427453
}
454+
terminal.failNetwork(logId, 'StreamError: $e');
428455
});
429456

430457
final (response, duration, errorMessage) = await completer.future;
@@ -436,6 +463,7 @@ class CollectionStateNotifier
436463
isWorking: false,
437464
isStreaming: false,
438465
);
466+
terminal.failNetwork(logId, errorMessage ?? 'Unknown error');
439467
} else {
440468
final statusCode = response.statusCode;
441469
httpResponseModel = baseHttpResponseModel.fromResponse(
@@ -461,6 +489,14 @@ class CollectionStateNotifier
461489
isWorking: false,
462490
);
463491

492+
terminal.completeNetwork(
493+
logId,
494+
statusCode: statusCode,
495+
responseHeaders: response.headers,
496+
responseBodyPreview: httpResponseModel?.body,
497+
duration: duration,
498+
);
499+
464500
String newHistoryId = getNewUuid();
465501
historyModel = HistoryRequestModel(
466502
historyId: newHistoryId,
@@ -487,7 +523,9 @@ class CollectionStateNotifier
487523
.addHistoryRequest(historyModel!);
488524

489525
if (!requestModel.postRequestScript.isNullOrEmpty()) {
490-
newRequestModel = await handlePostResponseScript(
526+
newRequestModel = await ref
527+
.read(jsRuntimeNotifierProvider.notifier)
528+
.handlePostResponseScript(
491529
newRequestModel,
492530
originalEnvironmentModel,
493531
(envModel, updatedValues) {

0 commit comments

Comments
 (0)