Comparing version 1.0.0 to 1.1.0
@@ -0,3 +1,9 @@ | ||
## 1.1.0 | ||
### Added | ||
- add `startsWith`, `endsWith` and `includes` helper methods. | ||
## 1.0.0 | ||
Initial version | ||
Initial version |
@@ -14,6 +14,2 @@ import { getInputUrl, getOptionMethod } from './utils.js'; | ||
} | ||
function fetchMock(matcher, response) { | ||
matchers.push(new Matcher(matcher, response)); | ||
overrideFetch(); | ||
} | ||
function overrideFetch() { | ||
@@ -29,7 +25,12 @@ globalThis.fetch = async (input, options) => { | ||
} | ||
const fetchMock = (matcher, response) => { | ||
matchers.push(new Matcher(matcher, response)); | ||
overrideFetch(); | ||
}; | ||
/** | ||
* Simple matcher for quick mocking | ||
* Simple matcher for quick mocking. | ||
* For example `fetchMock.post(url: string | RegExp, response: Response)` helper. | ||
*/ | ||
function generateFetchMockHelper(method) { | ||
return (url, response) => { | ||
const fn = (url, response) => { | ||
return fetchMock((input, options) => { | ||
@@ -48,14 +49,23 @@ if (getOptionMethod(options) !== method) { | ||
}; | ||
fn.startsWith = generateFetchMockStringHelper(method, 'startsWith'); | ||
fn.endsWith = generateFetchMockStringHelper(method, 'endsWith'); | ||
fn.includes = generateFetchMockStringHelper(method, 'includes'); | ||
return fn; | ||
} | ||
const get = (url, response) => generateFetchMockHelper('GET')(url, response); | ||
const post = (url, response) => generateFetchMockHelper('POST')(url, response); | ||
const put = (url, response) => generateFetchMockHelper('PUT')(url, response); | ||
const patch = (url, response) => generateFetchMockHelper('PATCH')(url, response); | ||
const deleteFn = (url, response) => generateFetchMockHelper('DELETE')(url, response); | ||
fetchMock.get = get; | ||
fetchMock.post = post; | ||
fetchMock.put = put; | ||
fetchMock.patch = patch; | ||
fetchMock.delete = deleteFn; | ||
/** | ||
* This function generate string helper functions. | ||
* For example `fetchMock.post.startsWith(startUrl: string, response: Response)` helper. | ||
*/ | ||
function generateFetchMockStringHelper(method, methodName) { | ||
return (url, response) => { | ||
return fetchMock((input, options) => getOptionMethod(options) === method && | ||
getInputUrl(input)[methodName](url), response); | ||
}; | ||
} | ||
fetchMock.get = generateFetchMockHelper('GET'); | ||
fetchMock.post = generateFetchMockHelper('POST'); | ||
fetchMock.put = generateFetchMockHelper('PUT'); | ||
fetchMock.patch = generateFetchMockHelper('PATCH'); | ||
fetchMock.delete = generateFetchMockHelper('DELETE'); | ||
export default fetchMock; | ||
//# sourceMappingURL=fetchMock.js.map |
type MatcherFunction = (input: URL | RequestInfo, options: RequestInit | undefined) => boolean; | ||
export declare function resetMocks(): void; | ||
declare function fetchMock(matcher: MatcherFunction, response: Response): void; | ||
declare namespace fetchMock { | ||
export var get: FetchMockHelper; | ||
export var post: FetchMockHelper; | ||
export var put: FetchMockHelper; | ||
export var patch: FetchMockHelper; | ||
var _a: FetchMockHelper; | ||
export { _a as delete }; | ||
} | ||
type FetchMockHelper = (url: string | RegExp, response: Response) => void; | ||
type FetchMockHelper = { | ||
(url: string | RegExp, response: Response): void; | ||
startsWith: FetchMockStringHelper; | ||
endsWith: FetchMockStringHelper; | ||
includes: FetchMockStringHelper; | ||
}; | ||
type FetchMockStringHelper = (url: string, response: Response) => void; | ||
type FetchMockFunction = { | ||
(matcher: MatcherFunction, response: Response): void; | ||
get: FetchMockHelper; | ||
post: FetchMockHelper; | ||
put: FetchMockHelper; | ||
patch: FetchMockHelper; | ||
delete: FetchMockHelper; | ||
}; | ||
declare const fetchMock: FetchMockFunction; | ||
export default fetchMock; |
{ | ||
"name": "metch-fock", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -71,2 +71,3 @@ # metch-fock | ||
fetchMock.put(/https:\/\/match.put\//, expected); | ||
fetchMock.post.startsWith('https://match.post/', expected); | ||
@@ -82,2 +83,8 @@ const r1 = await doFetchWithToken('https://match.get/test'); | ||
expect(r2).toBe(expected); | ||
const r3 = await doFetchWithToken('https://match.post/test', { | ||
method: 'POST', | ||
}); | ||
expect(r3).toBe(expected); | ||
}); | ||
@@ -124,2 +131,7 @@ | ||
function fetchMock.delete(url: string | RegExp, response: Response): void; | ||
// foreach methods, there are string helper too | ||
function fetchMock.<httpVerb>.startsWith(url: string, response: Response): void; | ||
function fetchMock.<httpVerb>.endsWith(url: string, response: Response): void; | ||
function fetchMock.<httpVerb>.includes(url: string, response: Response): void; | ||
``` | ||
@@ -126,0 +138,0 @@ |
Sorry, the diff of this file is not supported yet
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
14651
128
149