Skip to content

Commit

Permalink
Merge pull request #3 from radekBednarik/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
radekBednarik authored Dec 14, 2023
2 parents 0076b23 + a0aeee7 commit 7385e8a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.husky
node_modules
test
src
.eslintcache
.eslintrc.cjs
.gitignore
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bednarik_radek/mockserver-cli",
"version": "0.1.3",
"version": "0.1.4",
"description": "CLI utility for Mock-server client. Allows for easy creation/deletion of expectations on the Mock-server instance.",
"main": "index.js",
"type": "module",
Expand Down
3 changes: 3 additions & 0 deletions src/cli/handlers/clear.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ async function clearExpectations(client: Client, path: string) {
}

log.trace(`expectations cleared on the server - from path: ${path}`);

return;
} catch (error: any) {
log.error("Error clearing expectations:", error);
return null;
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/cli/handlers/reset.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { OptionValues } from "commander";
import Client from "../../client/client.js";
import { logger } from "../../log/logger.js";
import { globalOptsHandler } from "./globalOpts.handler.js";
import { SuccessFullRequest } from "mockserver-client/mockServerClient.js";

const log = logger.child({ module: "resetHandler" });

Expand All @@ -17,11 +18,13 @@ export async function resetHandler(options: OptionValues) {
port: opts["config"]["port"],
});

await client.reset();
const response = (await client.reset()) as SuccessFullRequest;

log.trace("Mockserver reset handler done.");

return response;
} catch (error: any) {
log.error(error.message);
throw error;
return null;
}
}
8 changes: 4 additions & 4 deletions src/cli/handlers/set.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import { logger } from "../../log/logger.js";
import { resolve } from "path";
import type { OptionValues } from "commander";
import type { Expectation } from "mockserver-client";
import { SuccessFullRequest } from "mockserver-client/mockServerClient.js";

const log = logger.child({ module: "setHandler" });

async function setExpectations(client: Client, expectations: Expectation | Expectation[]) {
try {
log.trace(`will set expectations: ${JSON.stringify(expectations)}`);
const response = (await client.set(expectations)) as SuccessFullRequest;

await client.set(expectations);

log.trace("expectations set");
return response;
} catch (error: any) {
log.error("Error setting expectations:", error);
return null;
}
}

Expand Down
29 changes: 21 additions & 8 deletions src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { mockServerClient } from "mockserver-client";
import type { MockServerClient, PathOrRequestDefinition, ClearType } from "mockserver-client/mockServerClient.js";
import type {
MockServerClient,
PathOrRequestDefinition,
ClearType,
SuccessFullRequest,
} from "mockserver-client/mockServerClient.js";
import type { Expectation } from "mockserver-client/index.js";
import { logger } from "../log/logger.js";

Expand Down Expand Up @@ -33,9 +38,11 @@ export default class Client {
try {
log.trace(`Setting expectations: ${JSON.stringify(expectations)}`);

await this.client.mockAnyResponse(expectations);
const response = (await this.client.mockAnyResponse(expectations)) as SuccessFullRequest;

log.trace(`Expectations set: ${JSON.stringify(expectations)}`);
log.trace(`Expectations ${JSON.stringify(expectations)} set request response: ${JSON.stringify(response)}`);

return response;
} catch (error: any) {
log.error(error.message);
throw error;
Expand All @@ -46,9 +53,15 @@ export default class Client {
try {
log.trace(`Clearing expectation: ${JSON.stringify({ pathOrRequestDefinition, type })}`);

await this.client.clear(pathOrRequestDefinition, type);
const response = (await this.client.clear(pathOrRequestDefinition, type)) as SuccessFullRequest;

log.trace(
`Expectations ${JSON.stringify({ pathOrRequestDefinition, type })} clear request response: ${JSON.stringify(
response,
)}`,
);

log.trace(`Expectations cleared: ${JSON.stringify({ pathOrRequestDefinition, type })}`);
return response;
} catch (error: any) {
log.error(error.message);
throw error;
Expand All @@ -59,11 +72,11 @@ export default class Client {
try {
log.trace("Resetting mockserver");

const result = await this.client.reset();
const response = await this.client.reset();

log.trace("Mockserver reset.");
log.trace(`Mockserver reset request response: ${JSON.stringify(response)}`);

return result;
return response;
} catch (error: any) {
log.error(error.message);
throw error;
Expand Down

0 comments on commit 7385e8a

Please sign in to comment.