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 #include "contacts_napi_utils.h"
17
18 namespace OHOS {
19 namespace ContactsApi {
20 static constexpr const char *JS_ERROR_INVALID_INPUT_PARAMETER_STRING =
21 "parameter error. The type of parameter should match or the number of parameters should match.";
22 static constexpr const char *JS_ERROR_PERMISSION_DENIED_STRING = "Permission denied";
ToInt32Value(napi_env env,int32_t value)23 napi_value ContactsNapiUtils::ToInt32Value(napi_env env, int32_t value)
24 {
25 napi_value staticValue = nullptr;
26 napi_create_int32(env, value, &staticValue);
27 return staticValue;
28 }
29
CreateClassConstructor(napi_env env,napi_callback_info info)30 napi_value ContactsNapiUtils::CreateClassConstructor(napi_env env, napi_callback_info info)
31 {
32 napi_value thisArg = nullptr;
33 void *data = nullptr;
34 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
35 napi_value global = nullptr;
36 napi_get_global(env, &global);
37 return thisArg;
38 }
39
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)40 bool ContactsNapiUtils::MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
41 {
42 napi_valuetype valueType = napi_undefined;
43 napi_typeof(env, value, &valueType);
44 return valueType == targetType;
45 }
46
MatchParameters(napi_env env,const napi_value parameters[],std::initializer_list<napi_valuetype> valueTypes)47 bool ContactsNapiUtils::MatchParameters(
48 napi_env env, const napi_value parameters[], std::initializer_list<napi_valuetype> valueTypes)
49 {
50 if (parameters == nullptr) {
51 return false;
52 }
53 int i = 0;
54 for (auto beg = valueTypes.begin(); beg != valueTypes.end(); ++beg) {
55 if (!MatchValueType(env, parameters[i], *beg)) {
56 return false;
57 }
58 ++i;
59 }
60 return true;
61 }
62
CreateError(napi_env env,int32_t err)63 napi_value ContactsNapiUtils::CreateError(napi_env env, int32_t err)
64 {
65 napi_value businessError = nullptr;
66 napi_value errorCode = nullptr;
67 napi_value errorMessage = nullptr;
68 if (err == PERMISSION_ERROR) {
69 napi_create_string_utf8(env, JS_ERROR_PERMISSION_DENIED_STRING, NAPI_AUTO_LENGTH, &errorMessage);
70 }
71 if (err == PARAMETER_ERROR) {
72 napi_create_string_utf8(env, JS_ERROR_INVALID_INPUT_PARAMETER_STRING, NAPI_AUTO_LENGTH, &errorMessage);
73 }
74 napi_create_int32(env, err, &errorCode);
75 napi_create_error(env, nullptr, errorMessage, &businessError);
76 napi_set_named_property(env, businessError, "code", errorCode);
77 return businessError;
78 }
79 } // namespace ContactsApi
80 } // namespace OHOS
81