• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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
16export interface FetchResponse {
17  /**
18   * Server status code.
19   * @since 3
20   */
21  code: number;
22
23  /**
24   * Data returned by the success function.
25   * @since 3
26   */
27  data: string | object;
28
29  /**
30   * All headers in the response from the server.
31   * @since 3
32   */
33  headers: Object;
34}
35
36/**
37 * @Syscap SysCap.ACE.UIEngine
38 */
39export default class Fetch {
40  /**
41   * Obtains data through the network.
42   * @param options
43   */
44  static fetch(options: {
45    /**
46     * Resource URL.
47     * @since 3
48     */
49    url: string;
50
51    /**
52     * Request parameter, which can be of the string type or a JSON object.
53     * @since 3
54     */
55    data?: string | object;
56
57    /**
58     * Request header, which accommodates all attributes of the request.
59     * @since 3
60     */
61    header?: Object;
62
63    /**
64     * Request methods available: OPTIONS, GET, HEAD, POST, PUT, DELETE and TRACE. The default value is GET.
65     * @since 3
66     */
67    method?: string;
68
69    /**
70     * 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.
71     * @since 3
72     */
73    responseType?: string;
74
75    /**
76     * Called when the network data is obtained successfully.
77     * @since 3
78     */
79    success?: (data: FetchResponse) => void;
80
81    /**
82     * Called when the network data fails to be obtained.
83     * @since 3
84     */
85    fail?: (data: any, code: number) => void;
86
87    /**
88     * Called when the execution is completed.
89     * @since 3
90     */
91    complete?: () => void;
92  }): void;
93}