Skip to content

Commit

Permalink
chore: linter fix
Browse files Browse the repository at this point in the history
Signed-off-by: Tokesh <tokesh789@gmail.com>
  • Loading branch information
Tokesh committed Jan 27, 2025
1 parent 229fe06 commit 8dab3ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
44 changes: 20 additions & 24 deletions tools/src/exporter/ExportChapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import fs from 'fs'
import { read_yaml, to_ndjson } from '../helpers'
import { basename, resolve } from 'path'
import _ from 'lodash'
import { StoryEvaluations, StoryFile } from 'tester/types/eval.types'
import { StoryFile } from 'tester/types/eval.types'
import { Logger } from 'Logger'
import StoryParser from './StoryParser'
import { PostmanManager } from './PostmanManager'
Expand All @@ -28,29 +28,25 @@ export default class ExportChapters {
this._postman_manager = postman_manager
}

async run (story_path: string): Promise<{ results: StoryEvaluations, failed: boolean }> {
let failed = false
run (story_path: string): void {
const story_files = this.story_files(story_path)
const results: StoryEvaluations = { evaluations: [] }

for (const story_file of story_files) {
for(const chapter of story_file.story.chapters) {
const [headers, content_type] = this.#serialize_headers(chapter.request?.headers, chapter.request?.content_type)
let params = {}, url_path = {};
if(chapter.parameters !== undefined) {
[url_path, params] = this.#parse_url(chapter.path, chapter.parameters)
}
const request_data = chapter.request?.payload !== undefined ? this.#serialize_payload(
chapter.request.payload,
content_type
) : {}
this._postman_manager.add_to_collection('url', chapter.method, chapter.path, headers, params, request_data, content_type, story_file.full_path);
for (const chapter of story_file.story.chapters) {
const [headers, content_type] = this.#serialize_headers(chapter.request?.headers, chapter.request?.content_type)
let params = {};
if (chapter.parameters !== undefined) {
params = this.#parse_url(chapter.path, chapter.parameters)
}
const request_data = chapter.request?.payload !== undefined ? this.#serialize_payload(
chapter.request.payload,
content_type
) : {}
this._postman_manager.add_to_collection('url', chapter.method, chapter.path, headers, params, request_data, content_type, story_file.full_path);
}
this._logger.info(`Evaluating ${story_file.display_path} ...`)
}
this._postman_manager.save_collection()

return { results, failed }
}

story_files(story_path: string): StoryFile[] {
Expand Down Expand Up @@ -93,8 +89,8 @@ export default class ExportChapters {
if (!headers) return [headers, content_type]
_.forEach(headers, (v, k) => {
if (k.toLowerCase() == 'content-type') {
content_type = v.toString()
if (headers) delete headers[k]
content_type = v.toString()
if (headers) delete headers[k]
}
})
return [headers, content_type]
Expand All @@ -103,8 +99,8 @@ export default class ExportChapters {
#serialize_payload(payload: any, content_type: string): any {
if (payload === undefined) return undefined
switch (content_type) {
case 'application/x-ndjson': return to_ndjson(payload as any[])
default: return payload
case 'application/x-ndjson': return to_ndjson(payload as any[])
default: return payload
}
}

Expand All @@ -120,13 +116,13 @@ export default class ExportChapters {
return resolved_params
}

#parse_url (path: string, parameters: Record<string, Parameter>): [string, Record<string, Parameter>] {
#parse_url (path: string, parameters: Record<string, Parameter>): Record<string, Parameter> {
const path_params = new Set<string>()
const parsed_path = path.replace(/{(\w+)}/g, (_, key) => {
path.replace(/{(\w+)}/g, (_, key) => {
path_params.add(key as string)
return parameters[key] as string
})
const query_params = Object.fromEntries(Object.entries(parameters).filter(([key]) => !path_params.has(key)))
return [parsed_path, query_params]
return query_params
}
}
38 changes: 18 additions & 20 deletions tools/src/exporter/PostmanManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class PostmanManager {
item: [],
};
}

add_to_collection(
url: string | undefined,
method: string,
Expand All @@ -35,28 +35,26 @@ export class PostmanManager {
full_path?: string
): void {
const folders: string[] = [];
console.log(full_path)
if (full_path != null && full_path) {
const path_parts = full_path.split('/').filter(Boolean);

if (full_path) {
const pathParts = full_path.split('/').filter(Boolean);
const start_index = path_parts.indexOf('tests');

const startIndex = pathParts.indexOf('tests');

if (startIndex !== -1) {
folders.push(...pathParts.slice(startIndex + 1));
if (start_index !== -1) {
folders.push(...path_parts.slice(start_index + 1));
}
}

let currentFolder = this.collection.item;

let current_folder = this.collection.item;
folders.forEach(folder => {
let existingFolder = currentFolder.find((item: any) => item.name === folder);
let existing_folder = current_folder.find((item: any) => item.name === folder);

if (!existingFolder) {
existingFolder = { name: folder, item: [] };
currentFolder.push(existingFolder);
if (existing_folder == null) {
existing_folder = { name: folder, item: [] };
current_folder.push(existing_folder);
}

currentFolder = existingFolder.item;
current_folder = existing_folder.item;
});

const item = {
Expand All @@ -70,17 +68,17 @@ export class PostmanManager {
path: path.split('/').filter(Boolean),
query: Object.entries(params).map(([key, value]) => ({ key, value: String(value) })),
},
body: body ? { mode: content_type === 'application/json' ? 'raw' : 'formdata', raw: JSON.stringify(body) } : undefined,
body: body != null ? { mode: content_type === 'application/json' ? 'raw' : 'formdata', raw: JSON.stringify(body) } : undefined,
},
};

const exists = currentFolder.some((existingItem: any) => existingItem.name === item.name);
if (!exists) {
currentFolder.push(item);
const exists = current_folder.some((existing_item: any) => existing_item.name === item.name);
if (exists != null) {
current_folder.push(item);
}
}

save_collection(): void {
fs.writeFileSync(this.collection_path, JSON.stringify(this.collection, null, 2));
}
}
}

0 comments on commit 8dab3ed

Please sign in to comment.