Skip to content

Commit

Permalink
Improved kras request typing and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Sep 16, 2024
1 parent 5e427bd commit 4acd6e0
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.17.0

- Fixed issue with type of `content` of `request`
- Added `rawContent` and `formData` fields to `request` for more fine grained control

## 0.16.1

- Fixed issue with proxy injector not returning response
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kras",
"version": "0.16.1",
"version": "0.17.0",
"description": "Efficient server proxying and mocking in Node.js.",
"main": "dist/server/index.js",
"types": "dist/server/index.d.ts",
Expand Down
9 changes: 7 additions & 2 deletions src/server/injectors/har-injector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type FormData from 'form-data';
import {
asJson,
watch,
Expand All @@ -10,7 +11,7 @@ import {
HarHeaders,
compareRequests,
} from '../helpers';
import {
import type {
KrasInjector,
KrasInjectorConfig,
KrasRequest,
Expand Down Expand Up @@ -77,6 +78,8 @@ interface HarFileEntry {
url: string;
target: string;
content: string;
rawContent: any;
formData?: FormData;
headers: Headers;
query: Headers;
};
Expand Down Expand Up @@ -211,11 +214,13 @@ export default class HarInjector implements KrasInjector {
private transformEntry(file: string, entry: HttpArchive) {
const original = entry.request;
const response = entry.response;
const content = (original.postData || {}).text || '';
const request = {
method: original.method,
url: getUrl(original.url),
target: original.target || this.findTarget(original.url),
content: (original.postData || {}).text || '',
content,
rawContent: content,
headers: ato(original.headers),
query: ato(original.queryString),
};
Expand Down
20 changes: 15 additions & 5 deletions src/server/injectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import FormData from 'form-data';
import { existsSync } from 'fs';
import { resolve, basename } from 'path';
import { EventEmitter } from 'events';
import { Request, Response } from 'express';
import { parse } from 'url';
import type { EventEmitter } from 'events';
import type { Request, Response } from 'express';
import { fromMissing, isEncrypted, getPort, deepMerge, getLast } from '../helpers';
import { injectorDebug, injectorConfig, injectorMain } from '../info';
import { KrasConfiguration, KrasServer, KrasAnswer, KrasInjector, KrasInjectorConfig, KrasRequest } from '../types';
import type {
KrasConfiguration,
KrasServer,
KrasAnswer,
KrasInjector,
KrasInjectorConfig,
KrasRequest,
} from '../types';

import HarInjector from './har-injector';
import JsonInjector from './json-injector';
Expand Down Expand Up @@ -74,10 +81,11 @@ function normalizeRequest(targets: Array<string>, req: Request): KrasRequest {
const query: Record<string, string | Array<string>> = deepMerge({ ...req.query }, req.addedQuery);
const method = typeof req.method === 'string' ? req.method : 'GET';

let content: any;
let content: string | FormData;
let formData: FormData;

if (req.headers['content-type'] && req.headers['content-type'].search('multipart/form-data') !== -1) {
const formData = new FormData();
formData = new FormData();
typeof req.body === 'object' &&
Object.keys(req.body).map((field: any) => {
return formData.append(field, req.body[field]);
Expand Down Expand Up @@ -113,6 +121,8 @@ function normalizeRequest(targets: Array<string>, req: Request): KrasRequest {
method,
headers,
content,
rawContent: req.body,
formData,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/injectors/json-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
compareRequests,
getFirst,
} from '../helpers';
import {
import type {
KrasInjectorConfig,
KrasAnswer,
KrasInjector,
Expand Down
6 changes: 3 additions & 3 deletions src/server/injectors/proxy-injector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import WebSocket from 'ws';
import { EventEmitter } from 'events';
import { proxyRequest, defaultProxyHeaders, getPort, isEncrypted } from '../helpers';
import {
import type { EventEmitter } from 'events';
import type {
KrasInjector,
KrasAnswer,
KrasInjectorConfig,
Expand Down Expand Up @@ -229,7 +229,7 @@ export default class ProxyInjector implements KrasInjector {
headers,
url: target.address + req.url,
method: req.method,
body: req.content,
body: req.rawContent,
agentOptions: this.config.agentOptions,
proxy: this.config.proxy,
injector: {
Expand Down
4 changes: 2 additions & 2 deletions src/server/injectors/script-injector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import { asScript, watch, Watcher, editFileOption, editDirectoryOption, fromJson } from '../helpers';
import {
import type { EventEmitter } from 'events';
import type {
KrasRequest,
KrasInjectorConfig,
KrasInjector,
Expand Down
2 changes: 1 addition & 1 deletion src/server/injectors/store-injector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path';
import { open, JsonStore } from '../helpers';
import { KrasInjectorOptions, KrasRequest, KrasInjectorConfig, KrasInjector, KrasConfiguration } from '../types';
import type { KrasInjectorOptions, KrasRequest, KrasInjectorConfig, KrasInjector, KrasConfiguration } from '../types';

export interface StoreInjectorConfig {
directory?: string;
Expand Down
6 changes: 3 additions & 3 deletions src/server/types/kras-injector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { KrasRequest } from './kras-request';
import { Headers, Dict } from './kras-basics';
import { KrasInjectorOptions } from './kras-injector-options';
import type { KrasRequest } from './kras-request';
import type { Headers, Dict } from './kras-basics';
import type { KrasInjectorOptions } from './kras-injector-options';

export interface KrasAnswer {
headers: Headers;
Expand Down
6 changes: 3 additions & 3 deletions src/server/types/kras-recorder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { KrasRequest } from './kras-request';
import { KrasAnswer } from './kras-injector';
import { EventEmitter } from 'events';
import type { KrasRequest } from './kras-request';
import type { KrasAnswer } from './kras-injector';
import type { EventEmitter } from 'events';

export interface RecordedRequest {
id: string;
Expand Down
40 changes: 38 additions & 2 deletions src/server/types/kras-request.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
import { IncomingHttpHeaders } from 'http';
import type FormData from 'form-data';
import type { IncomingHttpHeaders } from 'http';

export interface KrasRequestQuery {
[key: string]: string | Array<string>;
}

export interface BasicKrasRequest {
/**
* The URL used for the request.
*/
url: string;
/**
* The target path of the request.
*/
target: string;
/**
* The query parameters of the request.
*/
query: KrasRequestQuery;
/**
* The method to trigger the request.
*/
method: string;
/**
* The headers used for the request.
*/
headers: IncomingHttpHeaders;
content: string;
/**
* The content of the request.
*/
content: string | FormData;
/**
* The raw content of the request.
*/
rawContent: any;
/**
* The form data, in case a form was given.
*/
formData?: FormData;
}

export interface KrasRequest extends BasicKrasRequest {
/**
* Indicates if the request has been encrypted.
*/
encrypted: boolean;
/**
* The remote address triggering the request.
*/
remoteAddress: string;
/**
* The port used for the request.
*/
port: string;
}

0 comments on commit 4acd6e0

Please sign in to comment.