• 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 "context.h"
22 #include "constant.h"
23 #include "upload_common.h"
24 #include "upload_config.h"
25 #include "upload_hilog_wrapper.h"
26 #include "napi/native_common.h"
27 #include "napi/native_api.h"
28 #include "napi/native_node_api.h"
29 
30 #define DECLARE_NAPI_METHOD(name, func) { name, 0, func, 0, 0, 0, napi_default, 0 }
31 namespace OHOS::Request::UploadNapi {
32 class JSUtil {
33 public:
34     static constexpr int32_t MAX_ARGC = 6;
35     static constexpr int32_t MAX_NUMBER_BYTES = 8;
36     static constexpr int32_t MAX_LEN = 4096;
37     static constexpr const char *SEPARATOR = ": ";
38 
39     static std::string Convert2String(napi_env env, napi_value jsString);
40     static napi_value Convert2JSString(napi_env env, const std::string &cString);
41     static napi_value Convert2JSValue(napi_env env, int32_t value);
42     static std::vector<std::string> Convert2StrVector(napi_env env, napi_value value);
43     static napi_value Convert2JSStringVector(napi_env env, const std::vector<std::string> &cStrings);
44     static napi_value Convert2JSUploadResponse(napi_env env, const Upload::UploadResponse &response);
45     static bool ParseFunction(napi_env env, napi_value &object, const char *name, napi_ref &output);
46     static std::vector<std::string> Convert2Header(napi_env env, napi_value value);
47     static std::shared_ptr<Upload::UploadConfig> ParseUploadConfig(napi_env env, napi_value jsConfig,
48         const std::string &version);
49     static bool ToUploadConfig(napi_env env, napi_value jsConfig, Upload::UploadConfig &config);
50     static napi_value Convert2JSUploadConfig(napi_env env, const Upload::UploadConfig &config);
51 
52     static bool Convert2File(napi_env env, napi_value jsFile, Upload::File &file);
53     static napi_value Convert2JSFile(napi_env env, const Upload::File &file);
54 
55     static std::vector<Upload::File> Convert2FileVector(napi_env env, napi_value jsFiles, const std::string &version);
56     static napi_value Convert2JSFileVector(napi_env env, const std::vector<Upload::File> &files);
57 
58     static Upload::RequestData Convert2RequestData(napi_env env, napi_value jsRequestData);
59     static napi_value Convert2JSRequestData(napi_env env, const Upload::RequestData &requestData);
60 
61     static std::vector<Upload::RequestData> Convert2RequestDataVector(napi_env env, napi_value jsRequestDatas);
62     static napi_value Convert2JSRequestDataVector(napi_env env, const std::vector<Upload::RequestData> &requestDatas);
63 
64     static napi_value Convert2JSValue(napi_env env, const std::vector<int32_t> &cInts);
65     static napi_value Convert2JSValue(napi_env env, const std::vector<Upload::TaskState> &taskStates);
66     static bool Equals(napi_env env, napi_value value, napi_ref copy);
67     static void ThrowError(napi_env env, Download::ExceptionErrorCode code, const std::string &msg);
68     static bool CheckConfig(const Upload::UploadConfig &config);
69     static bool CheckMethod(const std::string &method);
70     static bool CheckParamType(napi_env env, napi_value jsType, napi_valuetype type);
71     static bool CheckParamNumber(size_t argc, bool IsRequiredParam);
72     static bool CheckUrl(const std::string &url);
73     static napi_value CreateBusinessError(napi_env env, const
74         Download::ExceptionErrorCode &errorCode, const std::string &errorMessage);
75     static void GetMessage(const std::vector<Upload::TaskState> &taskStates, std::string &msg);
76     static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
77     static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
78     static bool ToUploadOption(napi_env env, napi_value jsConfig, Upload::UploadConfig &config);
79     static bool SetData(napi_env env, napi_value jsConfig, Upload::UploadConfig &config);
80     static bool SetFiles(napi_env env, napi_value jsConfig, Upload::UploadConfig &config);
81     static bool SetHeader(napi_env env, napi_value jsConfig, Upload::UploadConfig &config);
82     static bool Convert2FileL5(napi_env env, napi_value jsFile, Upload::File &file);
83     static bool SetMandatoryParam(napi_env env, napi_value jsValue, const std::string &str, std::string &out);
84     static bool SetOptionalParam(napi_env env, napi_value jsValue, const std::string &str, std::string &out);
85 };
86 }
87 #endif // REQUEST_JS_UTIL_H
88