1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @since 3
18 * @syscap SystemCapability.Communication.NetStack
19 */
20export interface FetchResponse {
21  /**
22   * Server status code.
23   * @since 3
24   */
25  code: number;
26
27  /**
28   * Data returned by the success function.
29   * @since 3
30   */
31  data: string | object;
32
33  /**
34   * All headers in the response from the server.
35   * @since 3
36   */
37  headers: Object;
38}
39
40/**
41 * @since 3
42 * @syscap SystemCapability.Communication.NetStack
43 */
44export default class Fetch {
45  /**
46   * Obtains data through the network.
47   * @param options
48   */
49  static fetch(options: {
50    /**
51     * Resource URL.
52     * @since 3
53     */
54    url: string;
55
56    /**
57     * Request parameter, which can be of the string type or a JSON object.
58     * @since 3
59     */
60    data?: string | object;
61
62    /**
63     * Request header, which accommodates all attributes of the request.
64     * @since 3
65     */
66    header?: Object;
67
68    /**
69     * Request methods available: OPTIONS, GET, HEAD, POST, PUT, DELETE and TRACE. The default value is GET.
70     * @since 3
71     */
72    method?: string;
73
74    /**
75     * The return type can be text, or JSON. By default, the return type is determined based on Content-Type in the header returned by the server.
76     * @since 3
77     */
78    responseType?: string;
79
80    /**
81     * Called when the network data is obtained successfully.
82     * @since 3
83     */
84    success?: (data: FetchResponse) => void;
85
86    /**
87     * Called when the network data fails to be obtained.
88     * @since 3
89     */
90    fail?: (data: any, code: number) => void;
91
92    /**
93     * Called when the execution is completed.
94     * @since 3
95     */
96    complete?: () => void;
97  }): void;
98}
99