@pactflow/pact-msw-adapter
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [1.4.0](https://github.com/pactflow/pact-msw-adapter/compare/v1.3.1...v1.4.0) (2023-05-17) | ||
### Features | ||
* Add option to set logger ([6d45541](https://github.com/pactflow/pact-msw-adapter/commit/6d4554158d4ef2671e88e556604319379659b5a2)) | ||
### [1.3.1](https://github.com/pactflow/pact-msw-adapter/compare/v1.3.0...v1.3.1) (2023-02-08) | ||
@@ -7,0 +14,0 @@ |
/// <reference types="node" /> | ||
import { DefaultRequestBody, MockedRequest, SetupWorkerApi } from "msw"; | ||
import { Logger } from "./utils/utils"; | ||
import { convertMswMatchToPact } from "./convertMswMatchToPact"; | ||
@@ -18,2 +19,3 @@ import { EventEmitter } from "events"; | ||
excludeHeaders?: string[]; | ||
logger?: Logger; | ||
} | ||
@@ -31,2 +33,3 @@ export interface PactMswAdapterOptionsInternal { | ||
excludeHeaders?: string[]; | ||
logger: Logger; | ||
} | ||
@@ -33,0 +36,0 @@ export interface PactMswAdapter { |
@@ -19,2 +19,3 @@ "use strict"; | ||
const options = { | ||
logger: console, | ||
...externalOptions, | ||
@@ -25,8 +26,8 @@ timeout: externalOptions.timeout || 200, | ||
}; | ||
(0, utils_1.logGroup)(`Adapter enabled${options.debug ? " on debug mode" : ""}`); | ||
(0, utils_1.logGroup)(`Adapter enabled${options.debug ? " on debug mode" : ""}`, { logger: options.logger }); | ||
if (options.debug) { | ||
(0, utils_1.logGroup)(["options:", options], { endGroup: true }); | ||
(0, utils_1.logGroup)(["options:", options], { endGroup: true, mode: "debug", logger: options.logger }); | ||
} | ||
else { | ||
console.groupEnd(); | ||
options.logger.groupEnd(); | ||
} | ||
@@ -46,3 +47,3 @@ // This can include expired requests | ||
if (options.debug) { | ||
(0, utils_1.logGroup)(["Matching request", req], { endGroup: true }); | ||
(0, utils_1.logGroup)(["Matching request", req], { endGroup: true, mode: "debug", logger: options.logger }); | ||
} | ||
@@ -72,3 +73,3 @@ const startTime = Date.now(); | ||
: response.body; | ||
(0, utils_1.logGroup)(JSON.stringify(response), { endGroup: true }); | ||
(0, utils_1.logGroup)(JSON.stringify(response), { endGroup: true, logger: options.logger }); | ||
const reqIdx = pendingRequests.findIndex((req) => req.id === reqId); | ||
@@ -87,4 +88,5 @@ if (reqIdx < 0) | ||
(0, utils_1.log)(`Orphan response: ${request.url}`, { | ||
mode: "warning", | ||
mode: "warn", | ||
group: expiredReq !== undefined, | ||
logger: options.logger, | ||
}); | ||
@@ -95,11 +97,12 @@ } | ||
(0, utils_1.log)(`Expired request to ${request.url.pathname}`, { | ||
mode: "warning", | ||
mode: "warn", | ||
group: true, | ||
logger: options.logger, | ||
}); | ||
} | ||
expiredReq.duration = endTime - expiredReq.startTime; | ||
console.log("url:", request.url); | ||
console.log("timeout:", options.timeout); | ||
console.log("duration:", expiredReq.duration); | ||
console.groupEnd(); | ||
options.logger.info("url:", request.url); | ||
options.logger.info("timeout:", options.timeout); | ||
options.logger.info("duration:", expiredReq.duration); | ||
options.logger.groupEnd(); | ||
} | ||
@@ -109,3 +112,3 @@ return; | ||
if (options.debug) { | ||
(0, utils_1.logGroup)(["Mocked response", response], { endGroup: true }); | ||
(0, utils_1.logGroup)(["Mocked response", response], { endGroup: true, mode: "debug", logger: options.logger }); | ||
} | ||
@@ -126,3 +129,3 @@ activeRequestIds.splice(activeReqIdx, 1); | ||
unhandledRequests.push(url); | ||
(0, utils_1.warning)(`Unhandled request: ${url}`); | ||
(0, utils_1.log)(`Unhandled request: ${url}`, { mode: "warn", logger: options.logger }); | ||
}); | ||
@@ -161,3 +164,3 @@ return { | ||
}, | ||
writeToFile: async (writer = utils_1.writeData2File) => { | ||
writeToFile: async (writer = (0, utils_1.createWriter)(options)) => { | ||
// TODO - dedupe pactResults so we only have one file per consumer/provider pair | ||
@@ -168,4 +171,4 @@ // Note: There are scenarios such as feature flagging where you want more than one file per consumer/provider pair | ||
matches.length, | ||
], { endGroup: true }); | ||
(0, utils_1.logGroup)(JSON.stringify(matches), { endGroup: true }); | ||
], { endGroup: true, logger: options.logger }); | ||
(0, utils_1.logGroup)(JSON.stringify(matches), { endGroup: true, logger: options.logger }); | ||
let pactFiles; | ||
@@ -176,3 +179,3 @@ try { | ||
catch (error) { | ||
(0, utils_1.logGroup)(["An error occurred parsing the JSON file", error]); | ||
(0, utils_1.logGroup)(["An error occurred parsing the JSON file", error], { logger: options.logger }); | ||
throw new Error("error generating pact files"); | ||
@@ -184,3 +187,3 @@ } | ||
matches.length, | ||
], { endGroup: true }); | ||
], { endGroup: true, logger: options.logger }); | ||
} | ||
@@ -262,7 +265,7 @@ pactFiles.forEach((pactFile) => { | ||
err = new Error(err); | ||
console.groupCollapsed("%c[pact-msw-adapter] Unexpected error.", "color:coral;font-weight:bold;"); | ||
console.log(err); | ||
console.groupEnd(); | ||
options.logger.groupCollapsed("%c[pact-msw-adapter] Unexpected error.", "color:coral;font-weight:bold;"); | ||
options.logger.info(err); | ||
options.logger.groupEnd(); | ||
throw err; | ||
} | ||
}; |
@@ -1,13 +0,17 @@ | ||
import { PactMswAdapterOptions } from "../pactMswAdapter"; | ||
declare const log: (message: any, options?: { | ||
group?: boolean | undefined; | ||
mode?: "log" | "warning" | "error" | undefined; | ||
} | undefined) => void; | ||
declare const warning: (message: any) => void; | ||
declare const logGroup: (message: any | any[], options?: { | ||
endGroup?: boolean | undefined; | ||
} | undefined) => void; | ||
declare const writeData2File: (filePath: string, data: Object) => void; | ||
declare const checkUrlFilters: (urlString: string, options: PactMswAdapterOptions) => boolean; | ||
import { PactMswAdapterOptionsInternal } from "../pactMswAdapter"; | ||
type LogLevel = 'debug' | 'info' | 'warn' | 'error'; | ||
export type Logger = Pick<typeof console, LogLevel | 'groupEnd' | 'groupCollapsed'>; | ||
declare const log: (message: any, options: { | ||
group?: boolean; | ||
mode?: LogLevel; | ||
logger: Logger; | ||
}) => void; | ||
declare const logGroup: (message: any | any[], options: { | ||
endGroup?: boolean; | ||
mode?: LogLevel; | ||
logger: Logger; | ||
}) => void; | ||
declare const createWriter: (options: PactMswAdapterOptionsInternal) => (filePath: string, data: Object) => void; | ||
declare const checkUrlFilters: (urlString: string, options: PactMswAdapterOptionsInternal) => boolean; | ||
declare const addTimeout: <T>(promise: Promise<T>, label: string, timeout: number) => Promise<void | T>; | ||
export { log, warning, logGroup, writeData2File, checkUrlFilters, addTimeout }; | ||
export { log, logGroup, createWriter, checkUrlFilters, addTimeout }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.addTimeout = exports.checkUrlFilters = exports.writeData2File = exports.logGroup = exports.warning = exports.log = void 0; | ||
exports.addTimeout = exports.checkUrlFilters = exports.createWriter = exports.logGroup = exports.log = void 0; | ||
var path = require("path"); | ||
@@ -8,4 +8,5 @@ let fs; // dynamic import | ||
const logColors = { | ||
log: 'forestgreen', | ||
warning: 'gold', | ||
debug: 'gray', | ||
info: 'forestgreen', | ||
warn: 'gold', | ||
error: 'coral' | ||
@@ -15,10 +16,8 @@ }; | ||
const group = (options === null || options === void 0 ? void 0 : options.group) || false; | ||
const mode = (options === null || options === void 0 ? void 0 : options.mode) || 'log'; | ||
const mode = (options === null || options === void 0 ? void 0 : options.mode) || 'info'; | ||
const color = logColors[mode]; | ||
const logFunction = group ? console.groupCollapsed : console.log; | ||
const logFunction = group ? options.logger.groupCollapsed : options.logger[mode]; | ||
logFunction(`%c${logPrefix} %c${message}`, `color:${color}`, 'color:inherit'); | ||
}; | ||
exports.log = log; | ||
const warning = (message) => log(message, { mode: 'warning' }); | ||
exports.warning = warning; | ||
const logGroup = (message, options) => { | ||
@@ -28,10 +27,10 @@ const isArray = message instanceof Array; | ||
const [label, ...content] = message; | ||
log(label, { group: true }); | ||
content.forEach((c) => console.log(c)); | ||
log(label, { group: true, mode: options.mode, logger: options.logger }); | ||
content.forEach((c) => options.logger[(options === null || options === void 0 ? void 0 : options.mode) || 'info'](c)); | ||
} | ||
else { | ||
log(message, { group: true }); | ||
log(message, { group: true, mode: options.mode, logger: options.logger }); | ||
} | ||
if (options === null || options === void 0 ? void 0 : options.endGroup) { | ||
console.groupEnd(); | ||
options.logger.groupEnd(); | ||
} | ||
@@ -48,3 +47,3 @@ }; | ||
}; | ||
const writeData2File = (filePath, data) => { | ||
const createWriter = (options) => (filePath, data) => { | ||
var _a; | ||
@@ -58,6 +57,6 @@ if (!fs) { | ||
if (!(fs === null || fs === void 0 ? void 0 : fs.existsSync)) { | ||
log('You need a node environment to save files.', { mode: 'warning', group: true }); | ||
console.log('filePath:', filePath); | ||
console.log('contents:', data); | ||
console.groupEnd(); | ||
log('You need a node environment to save files.', { mode: 'warn', group: true, logger: options.logger }); | ||
options.logger.info('filePath:', filePath); | ||
options.logger.info('contents:', data); | ||
options.logger.groupEnd(); | ||
} | ||
@@ -69,3 +68,3 @@ else { | ||
}; | ||
exports.writeData2File = writeData2File; | ||
exports.createWriter = createWriter; | ||
const checkUrlFilters = (urlString, options) => { | ||
@@ -78,3 +77,3 @@ var _a; | ||
if (options.debug) { | ||
logGroup(['Checking request against url filters', { urlString, providerFilter, includeFilter, excludeFilter, matchIsAllowed }]); | ||
logGroup(['Checking request against url filters', { urlString, providerFilter, includeFilter, excludeFilter, matchIsAllowed }], { logger: options.logger }); | ||
} | ||
@@ -81,0 +80,0 @@ return matchIsAllowed; |
{ | ||
"name": "@pactflow/pact-msw-adapter", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"main": "./dist/pactMswAdapter.js", | ||
@@ -47,4 +47,4 @@ "keywords": [ | ||
"@types/jest": "27.4.1", | ||
"@typescript-eslint/eslint-plugin": "5.14.0", | ||
"@typescript-eslint/parser": "5.14.0", | ||
"@typescript-eslint/eslint-plugin": "5.56.0", | ||
"@typescript-eslint/parser": "5.59.5", | ||
"axios": "0.26.0", | ||
@@ -57,6 +57,6 @@ "babel-jest": "27.5.1", | ||
"regenerator-runtime": "0.13.9", | ||
"rimraf": "3.0.2", | ||
"standard-version": "9.3.2", | ||
"rimraf": "4.4.0", | ||
"standard-version": "9.5.0", | ||
"ts-jest": "27.1.3", | ||
"typescript": "4.6.2" | ||
"typescript": "4.9.5" | ||
}, | ||
@@ -63,0 +63,0 @@ "dependencies": { |
@@ -61,2 +61,3 @@ # pact-msw-adapter | ||
| debug | `false` | `boolean` | `false` | prints verbose information about pact-msw-adapter events | | ||
| logger | `false` | `console` | `console` | logger used to print messages to console | | ||
@@ -63,0 +64,0 @@ ## Route filtering |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38199
514
219