From d8d7998c059b43937bb4f4c4163cf87f91ac58d9 Mon Sep 17 00:00:00 2001 From: Kaoru Hagihara Date: Sun, 6 Mar 2016 03:01:28 +0900 Subject: [PATCH] add vue-resource --- vue-resource/vue-resource-tests.ts | 60 ++++++++++++++ vue-resource/vue-resource.d.ts | 123 +++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 vue-resource/vue-resource-tests.ts create mode 100644 vue-resource/vue-resource.d.ts diff --git a/vue-resource/vue-resource-tests.ts b/vue-resource/vue-resource-tests.ts new file mode 100644 index 000000000..28ae5abe0 --- /dev/null +++ b/vue-resource/vue-resource-tests.ts @@ -0,0 +1,60 @@ +/// + +Vue.http.options.root = '/root'; +Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk'; + +new Vue({ + http: { + root: '/root', + headers: { + Authorization: 'Basic' + } + } +}); + +Vue.http.options.emulateJSON = true; +Vue.http.options.emulateHTTP = true; + +class App extends Vue { + ready() { + this.$http({ url: '/someUrl', method: 'GET' }).then((response) => { + // + }, (response) => { + // + }); + + this.$http.get('/someUrl').then((response) => { + const status = response.status; + const headers = response.headers(); + response.headers('expires'); + + this.$set('someData', response.data); + }); + + var resource = this.$resource('someItem/{id}'); + resource.get({id: 1}).then((response) => {}); + resource.save({id: 1}, {item: 30}).then((responce) => {}); + } +} + +Vue.http.get('/someUrl', {}, {}); + +Vue.http.interceptors.push({ + request: function(request) { + return request; + }, + response: function(response) { + return response; + } +}); + +Vue.http.interceptors.push(function() { + return { + request: function(request) { + return request; + }, + response: function(response) { + return response; + } + } +}); diff --git a/vue-resource/vue-resource.d.ts b/vue-resource/vue-resource.d.ts new file mode 100644 index 000000000..0c3d2a623 --- /dev/null +++ b/vue-resource/vue-resource.d.ts @@ -0,0 +1,123 @@ +// Type definitions for vue-resoure 0.7.0 +// Project: https://github.com/vuejs/vue-resource +// Definitions by: kaorun343 +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare namespace vuejs { + + interface HttpHeaders { + put?: { [key: string]: string }; + post?: { [key: string]: string }; + patch?: { [key: string]: string }; + delete?: { [key: string]: string }; + common?: { [key: string]: string }; + custom?: { [key: string]: string }; + [key: string]: any; + } + + interface HttpResponse { + data: Object; + ok: boolean; + status: number; + statusText: string; + headers: Function; + } + + interface HttpOptions { + url?: string; + method?: string; + data?: any; + params?: any; + headers?: any; + beforeSend?(request: any): any; + emulateHTTP?: boolean; + emulateJSON?: boolean; + xhr?: any; + upload?: any; + jsonp?: string; + timeout?: string; + } + + interface $http { + (url: string, data?: any, options?: HttpOptions): PromiseLike; + (url: string, options?: HttpOptions): PromiseLike; + } + + interface HttpInterceptor { + request?(request: HttpOptions): HttpOptions; + response?(response: HttpResponse): HttpResponse; + } + + interface Http { + options: HttpOptions & { root: string }; + headers: HttpHeaders; + interceptors: (HttpInterceptor | (() => HttpInterceptor))[]; + get: $http; + post: $http; + put: $http; + patch: $http; + delete: $http; + jsonp: $http; + } + + interface ResourceActions { + get: { method: string }; + save: { method: string }; + query: { method: string }; + update: { method: string }; + remove: { method: string }; + delete: { method: string }; + } + + interface ResourceMethod { + (params: any, data?: any, success?: Function, error?: Function): PromiseLike; + (params: any, success?: Function, error?: Function): PromiseLike; + (success?: Function, error?: Function): PromiseLike; + } + + interface ResourceMethods { + get: ResourceMethod; + save: ResourceMethod; + query: ResourceMethod; + update: ResourceMethod; + remove: ResourceMethod; + delete: ResourceMethod; + } + + interface $resource { + (url: string, params?: Object, actions?: any, options?: HttpOptions): ResourceMethods; + } + + interface Resource extends $resource { + actions: ResourceActions; + } + + interface Vue { + $http: { + (options: HttpOptions): PromiseLike; + get: $http; + post: $http; + put: $http; + patch: $http; + delete: $http; + jsonp: $http; + }; + $resource: $resource; + } + + interface VueStatic { + http: Http; + resource: Resource; + } + + interface ComponentOption { + http?: (HttpOptions & { headers?: HttpHeaders } & { [key: string]: any }) + } +} + +declare module "vue-resource" { + const install: (vue: vuejs.VueStatic) => void; + export = install; +}