• 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 #ifndef REQUEST_JS_UTIL_H
16 #define REQUEST_JS_UTIL_H
17 
18 #include <cstdint>
19 #include <map>
20 #include <vector>
21 #include "upload_config.h"
22 #include "upload_hilog_wrapper.h"
23 #include "napi/native_common.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 
27 #define DECLARE_NAPI_METHOD(name, func) { name, 0, func, 0, 0, 0, napi_default, 0 }
28 namespace OHOS::Request::UploadNapi {
29 class JSUtil {
30 public:
31     static constexpr int32_t MAX_ARGC = 6;
32     static constexpr int32_t MAX_NUMBER_BYTES = 8;
33     static constexpr int32_t MAX_LEN = 4096;
34 
35     static std::string Convert2String(napi_env env, napi_value jsString);
36     static napi_value Convert2JSString(napi_env env, const std::string &cString);
37     static napi_value Convert2JSValue(napi_env env, int32_t value);
38     static std::vector<std::string> Convert2StrVector(napi_env env, napi_value value);
39     static napi_value Convert2JSStringVector(napi_env env, const std::vector<std::string> &cStrings);
40     static napi_value Convert2JSUploadResponse(napi_env env, const Upload::UploadResponse &response);
41     static void ParseFunction(napi_env env, napi_value &object, const char *name, bool &hasFunction, napi_ref &output);
42     static std::vector<std::string> Convert2Header(napi_env env, napi_value value);
43 
44     static std::shared_ptr<Upload::UploadConfig> Convert2UploadConfig(napi_env env, napi_value jsConfig);
45     static napi_value Convert2JSUploadConfig(napi_env env, const Upload::UploadConfig &config);
46 
47     static Upload::File Convert2File(napi_env env, napi_value jsFile);
48     static napi_value Convert2JSFile(napi_env env, const Upload::File &file);
49 
50     static std::vector<Upload::File> Convert2FileVector(napi_env env, napi_value jsFiles);
51     static napi_value Convert2JSFileVector(napi_env env, const std::vector<Upload::File> &files);
52 
53     static Upload::RequestData Convert2RequestData(napi_env env, napi_value jsRequestData);
54     static napi_value Convert2JSRequestData(napi_env env, const Upload::RequestData &requestData);
55 
56     static std::vector<Upload::RequestData> Convert2RequestDataVector(napi_env env, napi_value jsRequestDatas);
57     static napi_value Convert2JSRequestDataVector(napi_env env, const std::vector<Upload::RequestData> &requestDatas);
58 };
59 }
60 #endif // REQUEST_JS_UTIL_H
61