From 7753adde2cf573bfd1a898a2e690b5cc630f83a9 Mon Sep 17 00:00:00 2001 From: Bret Little Date: Tue, 18 Nov 2014 15:53:04 -0700 Subject: [PATCH] Add a TypeScript definition for Axios with included tests --- .gitignore | 3 ++ Gruntfile.js | 14 ++++- README.md | 10 +++- axios.d.ts | 55 +++++++++++++++++++ package.json | 17 +++--- test/typescript/axios.ts | 111 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 199 insertions(+), 11 deletions(-) create mode 100644 axios.d.ts create mode 100644 test/typescript/axios.ts diff --git a/.gitignore b/.gitignore index ff7dfd2..92a163e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ .idea *.iml node_modules +.tscache +test/typescript/axios.js* + diff --git a/Gruntfile.js b/Gruntfile.js index 084f0c5..3923fc6 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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; } -}; \ No newline at end of file +}; diff --git a/README.md b/README.md index c74435d..6407c23 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,14 @@ axios.get('/user/12345') }); ``` +## TypeScript Definition +Axios includes a [TypeScript](http://typescriptlang.org) definition. +```typescript +/// +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 \ No newline at end of file +MIT diff --git a/axios.d.ts b/axios.d.ts new file mode 100644 index 0000000..43c4e2f --- /dev/null +++ b/axios.d.ts @@ -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; +} diff --git a/package.json b/package.json index 2daf5f7..046aced 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts new file mode 100644 index 0000000..e957b50 --- /dev/null +++ b/test/typescript/axios.ts @@ -0,0 +1,111 @@ +/// + +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 +});