Socket
Book a DemoInstallSign in
Socket

@wisdomgarden/cloak-plugin-http

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wisdomgarden/cloak-plugin-http

Making native HTTP requests within Cloak applications.

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
0
Created
Source

English Version | 中文版

CloakPluginNativeHttp

CloakPluginHttp 是 Cloak 框架的插件之一,用于在 Cloak 应用中进行 HTTP 请求。

使用方法

前置条件

安装 Cloak 框架 @wisdomgarden/cloak

具体细节参考 @wisdomgarden/cloak

  • 安装 CloakPluginHttp 插件

    ohpm install @wisdomgarden/cloak-plugin-http
    
  • 在项目 entry/src/main/module.json5 中声明网络访问权限。

    官方文档

    示例:

    "requestPermissions": [
     // ...
     {
       "name": "ohos.permission.INTERNET",
       "reason": "$string:permission_internet_reason",
       "usedScene": {
         "abilities": [
           "EntryAbility"
         ],
         "when": "inuse"
       }
     },
     // ...
    ]
    
  • 在 H5 逻辑代码中就可以直接调用该插件

示例代码

发送 GET 请求

const httpGet = async () => {
  const url = "https://api.restful-api.dev/objects";
  const response = await Cloak.plugins.Http.sendRequest(url);
  alert(JSON.stringify(response.data, null , 2));
};

发送 POST 请求

const httpPost = async () => {
  const url = "https://api.restful-api.dev/objects";
  const response = await Cloak.plugins.Http.sendRequest(url, {
    method: "post",
    headers: {
      "Content-Type": "application/json",
    },
    data: {
      username: "account",
      password: "password",
    },
  });
  alert(JSON.stringify(response.data, null , 2));
};

核心接口

发送 HTTP 请求

export interface HttpPlugin extends Plugin {
  sendRequest(url: string, options?: RequestOptions): Promise<HTTPResponse>;
}

接口类型定义

请求选项

interface HTTPResponse {
  status: number;
  headers: { [key: string]: string };
  url: string;
  data?: ESObject;
  error?: ESObject;
}

HTTP 响应

interface HTTPResponse {
  status: number;
  headers: { [key: string]: string };
  url: string;
  data?: ESObject;
  error?: ESObject;
}

关于 Cloak

Cloak 是专为 HarmonyOS 设计的混合开发框架,类似 CordovaCapacitor,但具备 更轻量更高性能 的特性。

该框架可将 Web 应用快速转换为原生应用,同时通过插件机制访问 HarmonyOS 原生能力。

核心特性

  • 快速打包:将 H5 应用快速编译为 HarmonyOS 应用。
  • 原生能力访问:通过插件机制调用原生接口。
  • WebView 支持:提供高性能 WebView 容器,确保 H5 应用流畅运行。
  • 插件开发:支持开发者自定义插件以扩展原生功能。

更多关于 Cloak 框架信息,请查看: https://github.com/WisdomGardenInc/Cloak

Keywords

Cloak

FAQs

Package last updated on 18 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts