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