npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yao-app-client

v1.0.6

Published

yao application http client wrapper

Downloads

2

Readme

yao-app-client

yao application http client wrapper

install

To install dependencies:

bun install

To build:

bun run build

This project was created using bun init in bun v1.0.22. Bun is a fast all-in-one JavaScript runtime.

config

config your yao application

create the below http config file

/apis/proxy.http.yao

{
  "name": "代理yao的请求",
  "version": "1.0.0",
  "description": "调试本地yao js脚本",
  "group": "",
  "guard": "-",
  "paths": [
    {
      "guard": "scripts.jsproxy.CheckAccessKey",
      "path": "/call",
      "method": "POST",
      "process": "scripts.jsproxy.ProxyServer",
      "in": [":payload"],
      "out": {
        "status": 200,
        "type": "application/json"
      }
    }
  ]
}

create a new js file as below: /scripts/jsproxy.js

//代理js api请求
/**
 * api 代理服务,可以放在yao应用下
 * @param {object} payload
 * @returns
 */
function ProxyServer(payload) {
  let resp = {
    code: undefined,
    message: undefined,
    // error: null as Error, //undefined不会出现在返回json key中
    data: undefined,
  };
  try {
    const type = payload.type;
    const method = payload.method;
    const args = payload.args;
    const space = payload.space; //"dsl","script","system"
    const engine = payload.engine;
    let localParams = [];
    if (Array.isArray(args)) {
      localParams = args;
    } else {
      localParams.push(args);
    }
    switch (type) {
      case "Process":
        resp.data = Process(method, ...localParams);
        break;
      case "Studio":
        // @ts-ignore
        __YAO_SU_ROOT = true;
        //0.10.3-dev
        __yao_data = { ROOT: true };
        resp.data = Studio(method, ...localParams);
        break;
      case "Query":
        if (engine) {
          const query = new Query(engine);
          //@ts-ignore
          resp.data = query[method](args);
        } else {
          const query = new Query();
          //@ts-ignore
          resp.data = query[method](args);
        }
        break;
      case "FileSystem":
        const fs = new FS(space);
        //@ts-ignore
        resp.data = fs[method](...args);
        break;
      case "Store":
        const cache = new Store(space);
        if (method == "Set") {
          resp.data = cache.Set(payload.key, payload.value);
        } else if (method == "Get") {
          resp.data = cache.Get(payload.key);
        }
        break;
      case "Http":
        //@ts-ignore
        resp.data = http[method](...args);
        break;
      case "Log":
        //@ts-ignore
        log[method](...args);
        resp.data = {};
        break;
      case "WebSocket":
        //目前yao只是实现了push一个方法,也是ws服务连接后push一条信息
        const ws = new WebSocket(payload.url, payload.protocols);
        if (method == "push") {
          ws.push(payload.message);
          resp.data = {};
        }
        break;
      case "Translate":
        resp.data = $L(payload.message);
        break;
      default:
        resp.code = 500;
        resp.message = `不支持的方法调用${type}`;
    }
  } catch (error) {
    resp.code = error.code || 500;
    resp.message = error.message || "接口调用异常";
  }

  return resp;
}

/**
 * api guard
 * @param {string} path api path
 * @param {map} params api path params
 * @param {map} queries api queries in url query string
 * @param {object|string} payload json object or string
 * @param {map} headers request headers
 */
function CheckAccessKey(path, params, queries, payload, headers) {
  let token = null;
  let auth = headers["Authorization"];
  console.log("headers", headers);
  if (auth) {
    token = auth[0].replace("Bearer ", "");
  }
  token = token || (queries["token"] && queries["token"][0]);
  if (!token) {
    throw new Exception("Debug Proxy Call token Not set", 403);
  }
  const access_key = Process("yao.env.get", "YAO_API_ACCESS_KEY");
  if (!access_key) {
    throw new Exception("YAO_API_ACCESS_KEY Not set", 403);
  }
  if (access_key !== token) {
    throw new Exception("YAO_API_ACCESS_KEY not equal token", 403);
  }
}

create new env config in .env file

YAO_API_ACCESS_KEY=123456

use in browser

<script type="module">
  import { YaoApplication } from "./index.js";
  const yao = new YaoApplication("/api/proxy/call", "123456");
  const models = await yao.Model("admin.user").Get({});
  console.log(models);
</script>

use in bun

bun init

bun install yao-app-client

bun js demo code:

import { YaoApplication } from "yao-app-client";
const yao = new YaoApplication(
  "http://localhost:5099/api/proxy/call",
  "123456"
);

/**
 * Model=> admin.user (用户表)
 *
 * Table=> admin_user (用户表)
 */
interface admin_user {
  /**ID */
  id?: number;
  /**类型 */
  type?: "super" | "admin" | "staff" | "user" | "robot";
  /**邮箱 */
  email?: string;
  /**手机号 */
  mobile?: string;
  /**登录密码 */
  password?: string;
  /**操作密码 */
  password2nd?: string;
  /**姓名 */
  name?: string;
  /**角色 */
  role?: any;
  /**状态 */
  status?: "enabled" | "disabled";
}
// define new user
const model = yao.Model < admin_user > "admin.user";
// create model
const id = await model.Create({
  name: "kaidi",
  email: "[email protected]",
  mobile: "13956420154",
  type: "admin",
  status: "enabled",
});
console.log(`new user id:${id}`);
const models = await model.Get({});

console.log(models);

build standalone binary using bun

bun build index.ts --compile --outfile yao-demo