[axios-mock-adapter] add typings (#19973)

* [axios-mock-adapter] add typings

* [axios-mock-adapter] include dependencies and fix linting issues
This commit is contained in:
Tomasz Kryśkiewicz
2017-09-25 12:28:52 -07:00
committed by Mohamed Hegazy
parent 430f4cf0be
commit 5aaf1436d3
5 changed files with 198 additions and 0 deletions
@@ -0,0 +1,113 @@
import axios from "axios";
import MockAdapter = require("axios-mock-adapter");
const instance = axios.create();
const mock = new MockAdapter(instance);
namespace AllowsConstructing {
new MockAdapter(instance);
}
namespace AllowsConstructingWithOptions {
new MockAdapter(instance, {
delayResponse: 2000,
});
}
namespace ExposesAdapter {
mock.adapter();
}
namespace SupportsReset {
mock.reset();
}
namespace SupportsRestore {
mock.restore();
}
namespace SupportsAllHttpVerbs {
mock.onGet;
mock.onPost;
mock.onPut;
mock.onHead;
mock.onDelete;
mock.onPatch;
}
namespace SupportsAnyVerb {
mock.onAny;
}
namespace AllowsVerbOnlyMatcher {
mock.onGet();
}
namespace AllowsUrlMatcher {
mock.onGet("/foo");
}
namespace AllowsUrlRegExpMatcher {
mock.onGet(/\/fo+/);
}
namespace AllowsStringBodyMatcher {
mock.onPatch("/foo", "bar");
}
namespace AllowsBodyMatcher {
mock.onGet("/foo", {
id: 4,
name: "foo",
});
}
namespace AllowsParameterMatcher {
mock.onGet("/foo", {
params: {
searchText: "John",
},
});
}
namespace AllowsReplyWithStatus {
mock.onGet().reply(200);
}
namespace SupportsReplyOnce {
mock.onGet().replyOnce(200);
}
namespace SupportsPassThrough {
mock.onGet().passThrough();
}
namespace SupportsTimeout {
mock.onGet().timeout();
}
namespace AllowsFunctionReply {
mock.onGet().reply((config) => {
return [
200,
{ data: "foo" },
{ RequestedURL: config.url },
];
});
}
namespace AllowsPromiseReply {
mock.onGet().reply((config) => {
return Promise.resolve([
200,
{ data: "bar" },
{ RequestedURL: config.url },
]);
});
}
namespace SupportsChanining {
mock
.onGet("/users").reply(200, [/* users */])
.onGet("/posts").reply(200, [/* posts */]);
}
+51
View File
@@ -0,0 +1,51 @@
// Type definitions for axios-mock-adapter 1.9
// Project: https://github.com/ctimmerm/axios-mock-adapter
// Definitions by: Tomasz Kryskiewicz <https://github.com/tkryskiewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version 2.1
import { AxiosAdapter, AxiosInstance, AxiosRequestConfig } from "axios";
type CallbackResponseSpecFunc = (config: AxiosRequestConfig) => any[] | Promise<any[]>;
type ResponseSpecFunc = (statusOrCallback: number | CallbackResponseSpecFunc, data?: any, headers?: any) => MockAdapter;
interface RequestHandler {
reply: ResponseSpecFunc;
replyOnce: ResponseSpecFunc;
passThrough(): void;
networkError(): void;
timeout(): void;
}
interface MockAdapterOptions {
delayResponse?: number;
}
interface RequestDataMatcher {
[index: string]: any;
params?: {
[index: string]: any,
};
}
type RequestMatcherFunc = (matcher?: string | RegExp, body?: string | RequestDataMatcher) => RequestHandler;
declare class MockAdapter {
constructor(axiosInstance: AxiosInstance, options?: MockAdapterOptions);
adapter(): AxiosAdapter;
reset(): void;
restore(): void;
onGet: RequestMatcherFunc;
onPost: RequestMatcherFunc;
onPut: RequestMatcherFunc;
onHead: RequestMatcherFunc;
onDelete: RequestMatcherFunc;
onPatch: RequestMatcherFunc;
onAny: RequestMatcherFunc;
}
export = MockAdapter;
+6
View File
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"axios": "^0.16.2"
}
}
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"baseUrl": "../",
"types": [],
"typeRoots": [
"../"
],
"module": "commonjs",
"lib": [
"es6"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true
},
"files": [
"index.d.ts",
"axios-mock-adapter-tests.ts"
]
}
+6
View File
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"no-namespace": false
}
}