Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit 9744ccc

Browse files
authored
Merge pull request #611 from dl00/develop
Add Vue 2.x typings for TypeScript
2 parents fe5b261 + 057b1a5 commit 9744ccc

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

docs/config.md

+4
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ If your web server can't handle REST/HTTP requests like `PUT`, `PATCH` and `DELE
4848
```js
4949
Vue.http.options.emulateHTTP = true;
5050
```
51+
52+
## Typescript Support
53+
54+
Typescript for vue-resource should work out of the box since the type definition files are included within the npm package.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"module": "dist/vue-resource.esm.js",
66
"unpkg": "dist/vue-resource.min.js",
77
"jsdelivr": "dist/vue-resource.min.js",
8+
"typings": "types/index.d.ts",
89
"description": "The HTTP client for Vue.js",
910
"homepage": "https://github.com/pagekit/vue-resource#readme",
1011
"license": "MIT",

types/index.d.ts

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import _Vue = require('vue');
2+
3+
// augment typings of Vue.js
4+
import './vue';
5+
6+
export interface HttpHeaders {
7+
put?: { [key: string]: string };
8+
post?: { [key: string]: string };
9+
patch?: { [key: string]: string };
10+
delete?: { [key: string]: string };
11+
common?: { [key: string]: string };
12+
custom?: { [key: string]: string };
13+
[key: string]: any;
14+
}
15+
16+
export interface HttpResponse {
17+
data: any;
18+
ok: boolean;
19+
status: number;
20+
statusText: string;
21+
headers: Function;
22+
text():string;
23+
json():any;
24+
blob():Blob;
25+
}
26+
27+
export interface HttpOptions {
28+
url?: string;
29+
method?: string;
30+
body?: any;
31+
params?: any;
32+
headers?: any;
33+
before?(request: any): any;
34+
progress?(event: any): any;
35+
credentials?:boolean;
36+
emulateHTTP?: boolean;
37+
emulateJSON?: boolean;
38+
}
39+
40+
export interface $http {
41+
(url: string, data?: any, options?: HttpOptions): PromiseLike<HttpResponse>;
42+
(url: string, options?: HttpOptions): PromiseLike<HttpResponse>;
43+
}
44+
45+
export interface HttpInterceptor {
46+
request?(request: HttpOptions): HttpOptions;
47+
response?(response: HttpResponse): HttpResponse;
48+
}
49+
50+
export interface Http {
51+
options: HttpOptions & { root: string };
52+
headers: HttpHeaders;
53+
interceptors: (HttpInterceptor | (() => HttpInterceptor))[];
54+
get: $http;
55+
post: $http;
56+
put: $http;
57+
patch: $http;
58+
delete: $http;
59+
jsonp: $http;
60+
}
61+
62+
export interface ResourceActions {
63+
get: { method: string };
64+
save: { method: string };
65+
query: { method: string };
66+
update: { method: string };
67+
remove: { method: string };
68+
delete: { method: string };
69+
}
70+
71+
export interface ResourceMethod {
72+
(params: any, data?: any, success?: Function, error?: Function): PromiseLike<HttpResponse>;
73+
(params: any, success?: Function, error?: Function): PromiseLike<HttpResponse>;
74+
(success?: Function, error?: Function): PromiseLike<HttpResponse>;
75+
}
76+
77+
export interface ResourceMethods {
78+
get: ResourceMethod;
79+
save: ResourceMethod;
80+
query: ResourceMethod;
81+
update: ResourceMethod;
82+
remove: ResourceMethod;
83+
delete: ResourceMethod;
84+
}
85+
86+
export interface $resource {
87+
(url: string, params?: any, actions?: any, options?: HttpOptions): ResourceMethods;
88+
}
89+
90+
export interface Resource extends $resource {
91+
actions: ResourceActions;
92+
}
93+
94+
export declare function install(vue: typeof _Vue): void;

types/vue.d.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Extends interfaces in Vue.js
3+
*/
4+
5+
import Vue from "vue";
6+
import { HttpHeaders, HttpOptions, HttpResponse, $http, $resource } from "./index";
7+
8+
declare module "vue/types/options" {
9+
interface ComponentOptions<V extends Vue> {
10+
http?: (HttpOptions & { headers?: HttpHeaders } & { [key: string]: any })
11+
}
12+
}
13+
14+
declare module "vue/types/vue" {
15+
interface Vue {
16+
$http: {
17+
(options: HttpOptions): PromiseLike<HttpResponse>;
18+
get: $http;
19+
post: $http;
20+
put: $http;
21+
patch: $http;
22+
delete: $http;
23+
jsonp: $http;
24+
};
25+
$resource: $resource;
26+
}
27+
}

0 commit comments

Comments
 (0)