http-testify
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -47,3 +47,3 @@ "use strict"; | ||
function requestInstance(server, configOptions) { | ||
const { timeout = 30 * 1000 } = configOptions || {}; | ||
const { timeout = 30 * 1000 } = configOptions !== null && configOptions !== void 0 ? configOptions : {}; | ||
const instance = axios_1.default.create({ | ||
@@ -50,0 +50,0 @@ baseURL: server.baseUrl, |
@@ -74,22 +74,20 @@ "use strict"; | ||
function serverInstance(app, configOptions) { | ||
const { port = 65025 } = configOptions || {}; | ||
const { port = 65025 } = configOptions !== null && configOptions !== void 0 ? configOptions : {}; | ||
let server; | ||
if (typeof app === "function") { | ||
server = http.createServer(app); | ||
let keepOpen = false; | ||
switch (typeof app) { | ||
case "function": | ||
server = http.createServer(app); | ||
server.baseUrl = `http://localhost:${port}`; | ||
break; | ||
case "object": | ||
server = app; | ||
server.baseUrl = `http://localhost:${port}`; | ||
break; | ||
default: | ||
server = {}; | ||
server.baseUrl = app; | ||
} | ||
else if (typeof app === "object") { | ||
server = app; | ||
} | ||
else { | ||
server = {}; | ||
} | ||
if (["function", "object"].includes(typeof app)) { | ||
server.baseUrl = `http://localhost:${port}`; | ||
} | ||
else { | ||
server.baseUrl = typeof app == "string" ? app : undefined; | ||
} | ||
server.start = function () { | ||
if (["function", "object"].includes(typeof app) && | ||
typeof server.address != "undefined") { | ||
if (typeof server.address != "undefined" && !keepOpen) { | ||
server = server.listen(port); | ||
@@ -99,9 +97,20 @@ } | ||
server.end = function () { | ||
if (["function", "object"].includes(typeof app) && | ||
typeof server.getConnections !== "undefined") { | ||
if (typeof server.getConnections !== "undefined" && !keepOpen) { | ||
server.close(); | ||
} | ||
}; | ||
server.stayConnected = function () { | ||
if (!keepOpen) { | ||
server.start(); | ||
keepOpen = true; | ||
} | ||
}; | ||
server.closeConnection = function () { | ||
if (keepOpen) { | ||
keepOpen = false; | ||
server.close(); | ||
} | ||
}; | ||
return server; | ||
} | ||
exports.serverInstance = serverInstance; |
@@ -55,7 +55,13 @@ "use strict"; | ||
const module = (0, default_1.defaultModule)(server, configOptions); | ||
module.all = (0, promise_1.promiseModule)(server, configOptions).all; | ||
module.allSettled = (0, promise_1.promiseModule)(server, configOptions).allSettled; | ||
module.race = (0, promise_1.promiseModule)(server, configOptions).race; | ||
const promiseModuleInstance = (0, promise_1.promiseModule)(server, configOptions); | ||
module.all = promiseModuleInstance.all; | ||
module.allSettled = promiseModuleInstance.allSettled; | ||
module.race = promiseModuleInstance.race; | ||
module.closeConnection = server.closeConnection; | ||
module.stayConnected = () => { | ||
server.stayConnected(); | ||
return module; | ||
}; | ||
return module; | ||
} | ||
exports.request = request; |
@@ -5,2 +5,3 @@ /// <reference types="node" /> | ||
import { Express } from "express"; | ||
export type ANY = any; | ||
/** | ||
@@ -10,24 +11,49 @@ * Represents a callback function that provides an Axios instance and returns an array of promises for Axios responses. | ||
* @param {AxiosInstance} instance - The Axios instance to use for making requests. | ||
* @returns {Array<Promise<AxiosResponse<any, any>>>} - An array of promises for Axios responses. | ||
* @returns {Array<Promise<AxiosResponse<ANY, ANY>>>} - An array of promises for Axios responses. | ||
*/ | ||
export type RequestPromiseCallback = (instance: AxiosInstance) => Array<Promise<AxiosResponse<any, any>>>; | ||
export type RequestPromiseCallback = (instance: AxiosInstance) => Array<Promise<AxiosResponse<ANY, ANY>>>; | ||
/** | ||
* Represents an array of Axios responses returned from multiple concurrent requests using `Axios.all`. | ||
*/ | ||
export type RequestAllResponse = AxiosResponse<any, any>[]; | ||
export type RequestAllResponse = AxiosResponse<ANY, ANY>[]; | ||
/** | ||
* Represents an array of promise settlement results from multiple concurrent requests using `Axios.allSettled`. | ||
*/ | ||
export type RequestAllSettledResponse = PromiseSettledResult<AxiosResponse<any, any>>[]; | ||
export type RequestAllSettledResponse = PromiseSettledResult<AxiosResponse<ANY, ANY>>[]; | ||
/** | ||
* Represents an Axios response from a request that wins a race among multiple concurrent requests using `Axios.race`. | ||
*/ | ||
export type RequestRaceResponse = AxiosResponse<any, any>; | ||
export type RequestRaceResponse = AxiosResponse<ANY, ANY>; | ||
/** | ||
* Represents an instance of Axios, extended with additional methods like `all`, `allSettled`, and `race`. | ||
* RequestInstance represents an AxiosInstance with extended functionality for making HTTP requests. | ||
*/ | ||
export type RequestInstance = AxiosInstance & { | ||
/** | ||
* Performs multiple HTTP requests in parallel and returns an array of responses. | ||
* @param callback - A function that specifies the requests to be made. | ||
* @returns A Promise that resolves to an array of responses. | ||
*/ | ||
all: (callback: RequestPromiseCallback) => Promise<RequestAllResponse>; | ||
/** | ||
* Performs multiple HTTP requests in parallel and returns an array of response states. | ||
* Response states include both successful responses and errors. | ||
* @param callback - A function that specifies the requests to be made. | ||
* @returns A Promise that resolves to an array of response states. | ||
*/ | ||
allSettled: (callback: RequestPromiseCallback) => Promise<RequestAllSettledResponse>; | ||
/** | ||
* Races multiple HTTP requests and returns the response of the first completed request. | ||
* @param callback - A function that specifies the requests to be raced. | ||
* @returns A Promise that resolves to the response of the first completed request. | ||
*/ | ||
race: (callback: RequestPromiseCallback) => Promise<RequestRaceResponse>; | ||
/** | ||
* Ensures that the connection to the server is maintained, preventing it from being closed. | ||
* @returns The modified RequestInstance with a maintained connection. | ||
*/ | ||
stayConnected: () => RequestInstance; | ||
/** | ||
* Closes the connection to the server. | ||
*/ | ||
closeConnection: () => void; | ||
}; | ||
@@ -38,3 +64,3 @@ /** | ||
*/ | ||
export type RequestApp = Express | http.Server | ((...res: any) => void | Promise<void>) | string; | ||
export type RequestApp = Express | http.Server | ((...res: ANY) => void | Promise<void>) | string; | ||
/** | ||
@@ -54,3 +80,3 @@ * Represents configuration options that can be used to customize request-related settings. | ||
/** | ||
* Represents an instance of an HTTP server, extended with methods to start and end the server. | ||
* Represents an instance of an HTTP server, extended with methods to start, end, stay connected, and close the server. | ||
*/ | ||
@@ -60,2 +86,12 @@ export type ServerInstance = http.Server & { | ||
* Starts the server instance and makes it listen on the specified port. | ||
* | ||
* @remarks | ||
* This method starts the server instance and makes it listen on the specified port. If the server is already running, calling this method may have no effect. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Example: Start the server instance | ||
* serverInstance.start(); | ||
* // The server starts listening on the specified port. | ||
* ``` | ||
*/ | ||
@@ -65,2 +101,12 @@ start: () => void; | ||
* Ends the server instance, stopping it from listening. | ||
* | ||
* @remarks | ||
* This method stops the server instance from listening, effectively ending its operation. Subsequent requests to the server will not be accepted after calling this method. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Example: End the server instance | ||
* serverInstance.end(); | ||
* // The server stops listening and is no longer active. | ||
* ``` | ||
*/ | ||
@@ -72,2 +118,30 @@ end: () => void; | ||
baseUrl?: string; | ||
/** | ||
* Ensures that the server remains connected by starting it if not already running. | ||
* | ||
* @remarks | ||
* This method checks if the server is not already running (based on the `keepOpen` flag). If the server is not running, it starts the server instance using the `start` method. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Example: Ensure the server remains connected | ||
* serverInstance.stayConnected(); | ||
* // If the server was not already running, it starts and remains connected. | ||
* ``` | ||
*/ | ||
stayConnected: () => void; | ||
/** | ||
* Closes the server connection if it is currently open. | ||
* | ||
* @remarks | ||
* This method checks if the server connection is currently open (based on the `keepOpen` flag). If the server connection is open, it closes the server instance using the `end` method. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Example: Close the server connection if it is open | ||
* serverInstance.closeConnection(); | ||
* // If the server connection was open, it is closed. | ||
* ``` | ||
*/ | ||
closeConnection: () => void; | ||
}; |
@@ -47,3 +47,3 @@ "use strict"; | ||
function requestInstance(server, configOptions) { | ||
const { timeout = 30 * 1000 } = configOptions || {}; | ||
const { timeout = 30 * 1000 } = configOptions !== null && configOptions !== void 0 ? configOptions : {}; | ||
const instance = axios_1.default.create({ | ||
@@ -50,0 +50,0 @@ baseURL: server.baseUrl, |
@@ -74,22 +74,20 @@ "use strict"; | ||
function serverInstance(app, configOptions) { | ||
const { port = 65025 } = configOptions || {}; | ||
const { port = 65025 } = configOptions !== null && configOptions !== void 0 ? configOptions : {}; | ||
let server; | ||
if (typeof app === "function") { | ||
server = http.createServer(app); | ||
let keepOpen = false; | ||
switch (typeof app) { | ||
case "function": | ||
server = http.createServer(app); | ||
server.baseUrl = `http://localhost:${port}`; | ||
break; | ||
case "object": | ||
server = app; | ||
server.baseUrl = `http://localhost:${port}`; | ||
break; | ||
default: | ||
server = {}; | ||
server.baseUrl = app; | ||
} | ||
else if (typeof app === "object") { | ||
server = app; | ||
} | ||
else { | ||
server = {}; | ||
} | ||
if (["function", "object"].includes(typeof app)) { | ||
server.baseUrl = `http://localhost:${port}`; | ||
} | ||
else { | ||
server.baseUrl = typeof app == "string" ? app : undefined; | ||
} | ||
server.start = function () { | ||
if (["function", "object"].includes(typeof app) && | ||
typeof server.address != "undefined") { | ||
if (typeof server.address != "undefined" && !keepOpen) { | ||
server = server.listen(port); | ||
@@ -99,9 +97,20 @@ } | ||
server.end = function () { | ||
if (["function", "object"].includes(typeof app) && | ||
typeof server.getConnections !== "undefined") { | ||
if (typeof server.getConnections !== "undefined" && !keepOpen) { | ||
server.close(); | ||
} | ||
}; | ||
server.stayConnected = function () { | ||
if (!keepOpen) { | ||
server.start(); | ||
keepOpen = true; | ||
} | ||
}; | ||
server.closeConnection = function () { | ||
if (keepOpen) { | ||
keepOpen = false; | ||
server.close(); | ||
} | ||
}; | ||
return server; | ||
} | ||
exports.serverInstance = serverInstance; |
@@ -55,7 +55,13 @@ "use strict"; | ||
const module = (0, default_1.defaultModule)(server, configOptions); | ||
module.all = (0, promise_1.promiseModule)(server, configOptions).all; | ||
module.allSettled = (0, promise_1.promiseModule)(server, configOptions).allSettled; | ||
module.race = (0, promise_1.promiseModule)(server, configOptions).race; | ||
const promiseModuleInstance = (0, promise_1.promiseModule)(server, configOptions); | ||
module.all = promiseModuleInstance.all; | ||
module.allSettled = promiseModuleInstance.allSettled; | ||
module.race = promiseModuleInstance.race; | ||
module.closeConnection = server.closeConnection; | ||
module.stayConnected = () => { | ||
server.stayConnected(); | ||
return module; | ||
}; | ||
return module; | ||
} | ||
exports.request = request; |
@@ -5,2 +5,3 @@ /// <reference types="node" /> | ||
import { Express } from "express"; | ||
export type ANY = any; | ||
/** | ||
@@ -10,24 +11,49 @@ * Represents a callback function that provides an Axios instance and returns an array of promises for Axios responses. | ||
* @param {AxiosInstance} instance - The Axios instance to use for making requests. | ||
* @returns {Array<Promise<AxiosResponse<any, any>>>} - An array of promises for Axios responses. | ||
* @returns {Array<Promise<AxiosResponse<ANY, ANY>>>} - An array of promises for Axios responses. | ||
*/ | ||
export type RequestPromiseCallback = (instance: AxiosInstance) => Array<Promise<AxiosResponse<any, any>>>; | ||
export type RequestPromiseCallback = (instance: AxiosInstance) => Array<Promise<AxiosResponse<ANY, ANY>>>; | ||
/** | ||
* Represents an array of Axios responses returned from multiple concurrent requests using `Axios.all`. | ||
*/ | ||
export type RequestAllResponse = AxiosResponse<any, any>[]; | ||
export type RequestAllResponse = AxiosResponse<ANY, ANY>[]; | ||
/** | ||
* Represents an array of promise settlement results from multiple concurrent requests using `Axios.allSettled`. | ||
*/ | ||
export type RequestAllSettledResponse = PromiseSettledResult<AxiosResponse<any, any>>[]; | ||
export type RequestAllSettledResponse = PromiseSettledResult<AxiosResponse<ANY, ANY>>[]; | ||
/** | ||
* Represents an Axios response from a request that wins a race among multiple concurrent requests using `Axios.race`. | ||
*/ | ||
export type RequestRaceResponse = AxiosResponse<any, any>; | ||
export type RequestRaceResponse = AxiosResponse<ANY, ANY>; | ||
/** | ||
* Represents an instance of Axios, extended with additional methods like `all`, `allSettled`, and `race`. | ||
* RequestInstance represents an AxiosInstance with extended functionality for making HTTP requests. | ||
*/ | ||
export type RequestInstance = AxiosInstance & { | ||
/** | ||
* Performs multiple HTTP requests in parallel and returns an array of responses. | ||
* @param callback - A function that specifies the requests to be made. | ||
* @returns A Promise that resolves to an array of responses. | ||
*/ | ||
all: (callback: RequestPromiseCallback) => Promise<RequestAllResponse>; | ||
/** | ||
* Performs multiple HTTP requests in parallel and returns an array of response states. | ||
* Response states include both successful responses and errors. | ||
* @param callback - A function that specifies the requests to be made. | ||
* @returns A Promise that resolves to an array of response states. | ||
*/ | ||
allSettled: (callback: RequestPromiseCallback) => Promise<RequestAllSettledResponse>; | ||
/** | ||
* Races multiple HTTP requests and returns the response of the first completed request. | ||
* @param callback - A function that specifies the requests to be raced. | ||
* @returns A Promise that resolves to the response of the first completed request. | ||
*/ | ||
race: (callback: RequestPromiseCallback) => Promise<RequestRaceResponse>; | ||
/** | ||
* Ensures that the connection to the server is maintained, preventing it from being closed. | ||
* @returns The modified RequestInstance with a maintained connection. | ||
*/ | ||
stayConnected: () => RequestInstance; | ||
/** | ||
* Closes the connection to the server. | ||
*/ | ||
closeConnection: () => void; | ||
}; | ||
@@ -38,3 +64,3 @@ /** | ||
*/ | ||
export type RequestApp = Express | http.Server | ((...res: any) => void | Promise<void>) | string; | ||
export type RequestApp = Express | http.Server | ((...res: ANY) => void | Promise<void>) | string; | ||
/** | ||
@@ -54,3 +80,3 @@ * Represents configuration options that can be used to customize request-related settings. | ||
/** | ||
* Represents an instance of an HTTP server, extended with methods to start and end the server. | ||
* Represents an instance of an HTTP server, extended with methods to start, end, stay connected, and close the server. | ||
*/ | ||
@@ -60,2 +86,12 @@ export type ServerInstance = http.Server & { | ||
* Starts the server instance and makes it listen on the specified port. | ||
* | ||
* @remarks | ||
* This method starts the server instance and makes it listen on the specified port. If the server is already running, calling this method may have no effect. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Example: Start the server instance | ||
* serverInstance.start(); | ||
* // The server starts listening on the specified port. | ||
* ``` | ||
*/ | ||
@@ -65,2 +101,12 @@ start: () => void; | ||
* Ends the server instance, stopping it from listening. | ||
* | ||
* @remarks | ||
* This method stops the server instance from listening, effectively ending its operation. Subsequent requests to the server will not be accepted after calling this method. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Example: End the server instance | ||
* serverInstance.end(); | ||
* // The server stops listening and is no longer active. | ||
* ``` | ||
*/ | ||
@@ -72,2 +118,30 @@ end: () => void; | ||
baseUrl?: string; | ||
/** | ||
* Ensures that the server remains connected by starting it if not already running. | ||
* | ||
* @remarks | ||
* This method checks if the server is not already running (based on the `keepOpen` flag). If the server is not running, it starts the server instance using the `start` method. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Example: Ensure the server remains connected | ||
* serverInstance.stayConnected(); | ||
* // If the server was not already running, it starts and remains connected. | ||
* ``` | ||
*/ | ||
stayConnected: () => void; | ||
/** | ||
* Closes the server connection if it is currently open. | ||
* | ||
* @remarks | ||
* This method checks if the server connection is currently open (based on the `keepOpen` flag). If the server connection is open, it closes the server instance using the `end` method. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Example: Close the server connection if it is open | ||
* serverInstance.closeConnection(); | ||
* // If the server connection was open, it is closed. | ||
* ``` | ||
*/ | ||
closeConnection: () => void; | ||
}; |
{ | ||
"name": "http-testify", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A Node.js library for testing HTTP servers.", | ||
@@ -45,4 +45,4 @@ "author": "Alok Shete", | ||
"test-suite": "yarn test test/**/*.test.ts", | ||
"prepare": "npm run build", | ||
"prepublishOnly": "npm run test-suite" | ||
"prepublishOnly": "npm run test-suite", | ||
"format": "prettier --write . && eslint --fix . " | ||
}, | ||
@@ -58,5 +58,13 @@ "files": [ | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^20.6.2", | ||
"@typescript-eslint/eslint-plugin": "^6.7.0", | ||
"@typescript-eslint/parser": "^6.7.0", | ||
"chai": "^4.3.8", | ||
"eslint": "^8.49.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-prettier": "^5.0.0", | ||
"express": "^4.18.2", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^3.0.3", | ||
"ts-node": "^10.9.1", | ||
@@ -66,5 +74,4 @@ "typescript": "^4.9.5" | ||
"dependencies": { | ||
"axios": "^1.4.0", | ||
"express": "^4.18.2" | ||
"axios": "^1.4.0" | ||
} | ||
} |
@@ -76,14 +76,14 @@ # HTTPtestify | ||
HTTPtestify provides a variety of methods to facilitate testing HTTP interactions. Check the [documentation](https://httptestify.web.app) for detailed information. | ||
HTTPtestify provides a variety of methods to facilitate testing HTTP interactions. Check the <a href = "https://httptestify.web.app" target="_blank">documentation</a> for detailed information. | ||
## Contributing | ||
Contributions to HTTPtestify are welcome! Feel free to submit issues and pull requests on the [GitHub repository](https://github.com/alok-shete/http-testify). | ||
Contributions to HTTPtestify are welcome! Feel free to submit issues and pull requests on the <a href = "https://github.com/alok-shete/http-testify" target="_blank">GitHub repository<a>. | ||
## Donate | ||
Please consider donating if you think HTTPtestify is helpful to you or that my work is valuable. I am happy if you can help me [buy a cup of coffee. ❤️](https://www.buymeacoffee.com/shetealok) | ||
Please consider donating if you think HTTPtestify is helpful to you or that my work is valuable. I am happy if you can help me <a href = "https://www.buymeacoffee.com/shetealok" target="_blank">buy a cup of coffee. ❤️ | ||
## License | ||
This project is licensed under the [MIT License](https://github.com/alok-shete/http-testify/blob/main/LICENSE). | ||
This project is licensed under the<a href = "https://github.com/alok-shete/http-testify/blob/main/LICENSE" target="_blank">MIT License</a> |
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
88867
1
1744
16
- Removedexpress@^4.18.2
- Removedaccepts@1.3.8(transitive)
- Removedarray-flatten@1.1.1(transitive)
- Removedbody-parser@1.20.3(transitive)
- Removedbytes@3.1.2(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedcontent-disposition@0.5.4(transitive)
- Removedcontent-type@1.0.5(transitive)
- Removedcookie@0.7.1(transitive)
- Removedcookie-signature@1.0.6(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddepd@2.0.0(transitive)
- Removeddestroy@1.2.0(transitive)
- Removedee-first@1.1.1(transitive)
- Removedencodeurl@1.0.22.0.0(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedescape-html@1.0.3(transitive)
- Removedetag@1.8.1(transitive)
- Removedexpress@4.21.1(transitive)
- Removedfinalhandler@1.3.1(transitive)
- Removedforwarded@0.2.0(transitive)
- Removedfresh@0.5.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhttp-errors@2.0.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedinherits@2.0.4(transitive)
- Removedipaddr.js@1.9.1(transitive)
- Removedmedia-typer@0.3.0(transitive)
- Removedmerge-descriptors@1.0.3(transitive)
- Removedmethods@1.1.2(transitive)
- Removedmime@1.6.0(transitive)
- Removedms@2.0.02.1.3(transitive)
- Removednegotiator@0.6.3(transitive)
- Removedobject-inspect@1.13.3(transitive)
- Removedon-finished@2.4.1(transitive)
- Removedparseurl@1.3.3(transitive)
- Removedpath-to-regexp@0.1.10(transitive)
- Removedproxy-addr@2.0.7(transitive)
- Removedqs@6.13.0(transitive)
- Removedrange-parser@1.2.1(transitive)
- Removedraw-body@2.5.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsend@0.19.0(transitive)
- Removedserve-static@1.16.2(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedsetprototypeof@1.2.0(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedstatuses@2.0.1(transitive)
- Removedtoidentifier@1.0.1(transitive)
- Removedtype-is@1.6.18(transitive)
- Removedunpipe@1.0.0(transitive)
- Removedutils-merge@1.0.1(transitive)
- Removedvary@1.1.2(transitive)