/
index.d.ts
161 lines (137 loc) · 5.15 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import Vue from 'vue'
import { ResponsePromise, Options, BeforeRequestHook, AfterResponseHook, HTTPError } from 'ky'
import './vuex'
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type RequestBody = string | number | boolean | null | object | BodyInit
interface NuxtHTTPInstance {
/**
* Fetches the `url` with the option `{method: 'get'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
get(url: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise;
/**
* Fetches the `url` with the option `{method: 'post'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
post(url: Request | URL | string, body?: RequestBody, options?: Options): ResponsePromise;
/**
* Fetches the `url` with the option `{method: 'put'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
put(url: Request | URL | string, body?: RequestBody, options?: Options): ResponsePromise;
/**
* Fetches the `url` with the option `{method: 'patch'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
patch(url: Request | URL | string, body?: RequestBody, options?: Options): ResponsePromise;
/**
* Fetches the `url` with the option `{method: 'head'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
head(url: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise;
/**
* Fetches the `url` with the option `{method: 'delete'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
delete(url: Request | URL | string, options?: Options): ResponsePromise;
/**
* Fetches the `url` with the option `{method: 'get'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise that resolves to JSON parsed value.
*/
$get<T = unknown>(url: Request | URL | string, options?: Omit<Options, 'body'>): Promise<T>;
/**
* Fetches the `url` with the option `{method: 'post'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise that resolves to JSON parsed value.
*/
$post<T = unknown>(url: Request | URL | string, body?: RequestBody, options?: Options): Promise<T>;
/**
* Fetches the `url` with the option `{method: 'put'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise that resolves to JSON parsed value.
*/
$put<T = unknown>(url: Request | URL | string, body?: RequestBody, options?: Options): Promise<T>;
/**
* Fetches the `url` with the option `{method: 'patch'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise that resolves to JSON parsed value.
*/
$patch<T = unknown>(url: Request | URL | string, body?: RequestBody, options?: Options): Promise<T>;
/**
* Fetches the `url` with the option `{method: 'head'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise that resolves to JSON parsed value.
*/
$head<T = unknown>(url: Request | URL | string, options?: Omit<Options, 'body'>): Promise<T>;
/**
* Fetches the `url` with the option `{method: 'delete'}`.
*
* @param url - `Request` object, `URL` object, or URL string.
* @returns Promise that resolves to JSON parsed value.
*/
$delete<T = unknown>(url: Request | URL | string, options?: Options): Promise<T>;
/**
* Set a header on all subsequent requests.
* @param name - Header name.
* @param value - Header value.
*/
setHeader(name: string, value?: string | false): void
/**
* Set `Authorization` header on all subsequent requests.
* @param name - Header name.
* @param value - Header value.
*/
setToken(token: string | false, type?: string): void
/**
* Set a hook on `beforeRequest` (Before request is sent)
*
* This hook enables you to globally modify the requests right before it is sent. It will make no further changes to the request after this. The hook function receives the normalized options as the first argument. You could, for example, modify `options.headers` here.
*/
onRequest(hook: BeforeRequestHook): void
/**
* Set a hook on `afterResponse` (After the response is received)
*
* This hook enables you to globally read and optionally modify the responses. The return value of the hook function will be used as the response object if it's an instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).
*/
onResponse(hook: AfterResponseHook): void
/**
* Set a hook on `onError` (When request failed)
*
* This hook enables you to globally handle request errors.
*/
onError(hook: (error: HTTPError) => void): void
}
declare module '@nuxt/vue-app' {
interface Context {
$http: NuxtHTTPInstance
}
}
// Nuxt 2.9+
declare module '@nuxt/types' {
interface Context {
$http: NuxtHTTPInstance
}
}
declare module 'vue/types/vue' {
interface Vue {
$http: NuxtHTTPInstance
}
}