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 
22 #include "constant.h"
23 #include "context.h"
24 #include "napi/native_api.h"
25 #include "napi/native_common.h"
26 #include "napi/native_node_api.h"
27 #include "upload/upload_common.h"
28 #include "upload/upload_hilog_wrapper.h"
29 #include "upload_config.h"
30 
31 #define DECLARE_NAPI_METHOD(name, func)         \
32     {                                           \
33         name, 0, func, 0, 0, 0, napi_default, 0 \
34     }
35 namespace OHOS::Request::Upload {
36 class JSUtil {
37 public:
38     static constexpr int32_t MAX_ARGC = 6;
39     static constexpr int32_t MAX_NUMBER_BYTES = 8;
40     static constexpr int32_t MAX_LEN = 4096;
41     static constexpr const char *SEPARATOR = ": ";
42 
43     static std::string Convert2String(napi_env env, napi_value jsString);
44     static napi_value Convert2JSString(napi_env env, const std::string &cString);
45     static napi_value Convert2JSValue(napi_env env, int32_t value);
46     static napi_value Convert2JSUploadResponse(napi_env env, const Upload::UploadResponse &response);
47     static bool ParseFunction(napi_env env, napi_value &object, const char *name, napi_ref &output);
48     static std::shared_ptr<Upload::UploadConfig> ParseUploadConfig(
49         napi_env env, napi_value jsConfig, const std::string &version);
50 
51     static std::vector<Upload::File> Convert2FileVector(napi_env env, napi_value jsFiles, const std::string &version);
52 
53     static Upload::RequestData Convert2RequestData(napi_env env, napi_value jsRequestData);
54     static std::vector<Upload::RequestData> Convert2RequestDataVector(napi_env env, napi_value jsRequestDatas);
55 
56     static bool CheckConfig(const Upload::UploadConfig &config);
57     static bool CheckMethod(const std::string &method);
58     static bool CheckUrl(const std::string &url);
59     static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
60     static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
61     static bool ToUploadOption(napi_env env, napi_value jsConfig, Upload::UploadConfig &config);
62     static bool SetData(napi_env env, napi_value jsConfig, Upload::UploadConfig &config);
63     static bool SetFiles(napi_env env, napi_value jsConfig, Upload::UploadConfig &config);
64     static bool Convert2FileL5(napi_env env, napi_value jsFile, Upload::File &file);
65     static bool SetMandatoryParam(napi_env env, napi_value jsValue, const std::string &str, std::string &out);
66     static bool SetOptionalParam(napi_env env, napi_value jsValue, const std::string &str, std::string &out);
67     static bool ParseHeader(napi_env env, napi_value configValue, std::map<std::string, std::string> &header);
68     static napi_value CreateBusinessError(
69         napi_env env, const ExceptionErrorCode &errorCode, const std::string &errorMessage);
70 };
71 } // namespace OHOS::Request::Upload
72 #endif // REQUEST_JS_UTIL_H
73