From f5610405484ffd714754176b62ae04fefd66a840 Mon Sep 17 00:00:00 2001 From: Tokesh Date: Tue, 7 Jan 2025 19:44:53 +0500 Subject: [PATCH] feat: generating import file for postman Signed-off-by: Tokesh --- tools/src/tester/ChapterReader.ts | 9 ++++- tools/src/tester/PostmanManager.ts | 65 ++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tools/src/tester/PostmanManager.ts diff --git a/tools/src/tester/ChapterReader.ts b/tools/src/tester/ChapterReader.ts index c43698bb7..1415b86b0 100644 --- a/tools/src/tester/ChapterReader.ts +++ b/tools/src/tester/ChapterReader.ts @@ -18,14 +18,17 @@ import CBOR from 'cbor' import SMILE from 'smile-js' import { APPLICATION_CBOR, APPLICATION_JSON, APPLICATION_SMILE, APPLICATION_YAML, TEXT_PLAIN } from "./MimeTypes"; import _ from 'lodash' +import { PostmanManager } from './PostmanManager' export default class ChapterReader { private readonly _client: OpenSearchHttpClient private readonly logger: Logger + private readonly postmanManager: PostmanManager; - constructor (client: OpenSearchHttpClient, logger: Logger) { + constructor (client: OpenSearchHttpClient, logger: Logger, collectionPath: string = './postman_collection.json') { this._client = client this.logger = logger + this.postmanManager = new PostmanManager(collectionPath); } async read (chapter: ChapterRequest, story_outputs: StoryOutputs): Promise { @@ -37,6 +40,9 @@ export default class ChapterReader { story_outputs.resolve_value(chapter.request.payload), content_type ) : undefined + + this.postmanManager.addToCollection(this._client.getUrl(), chapter.method, url_path, headers, params, request_data, content_type); + this.logger.info(`=> ${chapter.method} ${url_path} (${to_json(params)}) [${content_type}] ${_.compact([to_json(headers), to_json(request_data)]).join(' | ')}`) await this._client.request({ url: url_path, @@ -66,6 +72,7 @@ export default class ChapterReader { this.logger.info(`<= ${response.status} (${response.content_type}) | ${response.payload !== undefined ? to_json(response.payload) : response.message}`) } }) + this.postmanManager.saveCollection(); return response as ActualResponse } diff --git a/tools/src/tester/PostmanManager.ts b/tools/src/tester/PostmanManager.ts new file mode 100644 index 000000000..1d0c4a739 --- /dev/null +++ b/tools/src/tester/PostmanManager.ts @@ -0,0 +1,65 @@ +import fs from 'fs'; + +export class PostmanManager { + private readonly collection: any; + private readonly collectionPath: string; + + constructor(collectionPath: string = './postman_collection.json') { + this.collectionPath = collectionPath; + this.collection = { + info: { + name: "Generated Collection", + schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + }, + item: [], + }; + } + + addToCollection( + url: string | undefined, + method: string, + path: string, + headers: Record | undefined, + params: Record, + body: any, + contentType: string + ): void { + const bodyContent = body + ? (() => { + switch (contentType) { + case 'application/json': + return { mode: 'raw', raw: JSON.stringify(body) }; + case 'text/plain': + return { mode: 'raw', raw: body.toString() }; + case 'application/x-www-form-urlencoded': + return { + mode: 'urlencoded', + urlencoded: Object.entries(body).map(([key, value]) => ({ key, value: value.toString() })), + }; + default: + throw new Error(`Unsupported content type: ${contentType}`); + } + })() + : undefined; + + const item = { + name: path, + request: { + method, + header: Object.entries(headers || {}).map(([key, value]) => ({ key, value })), + url: { + raw: `${url}${path}`, + path: path.split('/').filter(Boolean), + query: Object.entries(params).map(([key, value]) => ({ key, value: value.toString() })), + }, + body: bodyContent, + }, + }; + + this.collection.item.push(item); + } + + saveCollection(): void { + fs.writeFileSync(this.collectionPath, JSON.stringify(this.collection, null, 2)); + } +}