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