2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00

Add a TypeScript definition for Axios with included tests

This commit is contained in:
Bret Little
2014-11-18 15:53:04 -07:00
parent d93df704a2
commit 7753adde2c
6 changed files with 199 additions and 11 deletions
+3
View File
@@ -1,3 +1,6 @@
.idea
*.iml
node_modules
.tscache
test/typescript/axios.js*
+12 -2
View File
@@ -11,6 +11,16 @@ module.exports = function(grunt) {
dist: 'dist/**'
},
ts: {
test: {
src: ['test/typescript/*.ts'],
out: 'test/typescript/out.js',
options: {
module: 'commonjs',
}
}
},
update_json: {
bower: {
src: 'package.json',
@@ -68,7 +78,7 @@ module.exports = function(grunt) {
}
});
grunt.registerTask('test', 'Run the jasmine and nodeunit tests', ['webpack:global', 'nodeunit', 'karma:single']);
grunt.registerTask('test', 'Run the jasmine and nodeunit tests', ['webpack:global', 'nodeunit', 'karma:single', 'ts']);
grunt.registerTask('build', 'Run webpack and bundle the source', ['webpack']);
grunt.registerTask('publish', 'Prepare the code for release', ['clean', 'test', 'build', 'usebanner', 'update_json']);
@@ -133,4 +143,4 @@ module.exports = function(grunt) {
return webpackConfig;
}
};
};
+9 -1
View File
@@ -234,6 +234,14 @@ axios.get('/user/12345')
});
```
## TypeScript Definition
Axios includes a [TypeScript](http://typescriptlang.org) definition.
```typescript
/// <reference path="axios.d.ts" />
import axios = require('axios');
axios.get('/user?ID=12345');
```
## Credits
axios is heavily inspired by the [$http service](https://docs.angularjs.org/api/ng/service/$http) provided in [Angular](https://angularjs.org/). Ultimately axios is an effort to provide a standalone `$http`-like service for use outside of Angular.
@@ -242,4 +250,4 @@ axios uses the [es6-promise](https://github.com/jakearchibald/es6-promise) polyf
## License
MIT
MIT
Vendored
+55
View File
@@ -0,0 +1,55 @@
// Type definitions for Axios v0.4.1
// Project: https://github.com/mzabriskie/axios
declare var axios: axios.AxiosStatic
declare module axios {
interface AxiosStatic {
(options: axios.RequestOptions): axios.Promise;
get(url: string): axios.Promise;
get(url: string, options?: any): axios.Promise;
post(url: string): axios.Promise;
post(url: string, data?: any): axios.Promise;
put(url: string): axios.Promise;
put(url: string, data?: any): axios.Promise;
delete(url: string): axios.Promise;
delete(url: string, data?: any): axios.Promise;
patch(url: string): axios.Promise;
patch(url: string, data?: any): axios.Promise;
head(url: string): axios.Promise;
head(url: string, options?: any): axios.Promise;
all(iterable: any): axios.Promise;
spread(callback: any): axios.Promise;
}
interface Response {
data?: any;
status?: number;
headers?: any;
config?: any;
}
interface Promise {
then(response: axios.Response): axios.Promise;
catch(response: axios.Response): axios.Promise;
}
interface RequestOptions {
url: string;
method?: string;
transformRequest?: (data: any) => any;
headers?: any;
params?: any;
data?: any;
withCredentials?: boolean;
responseType?: string;
xsrfCookieName?: string;
xsrfHeaderName?: string;
}
}
declare module "axios" {
export = axios;
}
+9 -8
View File
@@ -29,20 +29,21 @@
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-banner": "^0.2.3",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-contrib-watch": "^0.6.1",
"webpack": "^1.4.0-beta9",
"webpack-dev-server": "^1.4.10",
"grunt-karma": "^0.8.3",
"grunt-ts": "^1.12.1",
"grunt-update-json": "^0.1.3",
"grunt-webpack": "^1.0.8",
"load-grunt-tasks": "^0.6.0",
"karma": "^0.12.21",
"karma-jasmine": "^0.1.5",
"grunt-karma": "^0.8.3",
"karma-phantomjs-launcher": "^0.1.4",
"karma-jasmine-ajax": "^0.1.4",
"grunt-update-json": "^0.1.3",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-banner": "^0.2.3"
"karma-phantomjs-launcher": "^0.1.4",
"load-grunt-tasks": "^0.6.0",
"webpack": "^1.4.0-beta9",
"webpack-dev-server": "^1.4.10"
},
"browser": {
"./lib/adapters/http.js": "./lib/adapters/xhr.js"
+111
View File
@@ -0,0 +1,111 @@
/// <reference path="../../axios.d.ts" />
import axios = require('axios');
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
axios.head('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
axios.delete('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
axios.put('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
axios.patch('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response.data);
console.log(response.status + 324);
console.log(response.headers);
console.log(response.config);
})
.catch(function (response) {
console.log(response);
});
axios({
method: 'get',
url: '/user/12345'
});
axios({
method: 'get',
url: '/user/12345',
transformRequest: (data) => {
return data.doSomething();
}
});
axios({
url: "hi",
headers: {'X-Requested-With': 'XMLHttpRequest'},
params: {
ID: 12345
},
data: {
firstName: 'Fred'
},
withCredentials: false, // default
responseType: 'json', // default
xsrfCookieName: 'XSRF-TOKEN', // default
xsrfHeaderName: 'X-XSRF-TOKEN' // default
});