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

mp-resource

v1.0.18

Published

Access resource management for the service interface of wechat miniprogram, you can get two configuration lists

Downloads

10

Readme

mp-resource

Access resource management for the service interface of wechat miniprogram, you can get two configuration lists

Install

npm i mp-resource

Usage

Create your own type definition file my-miniprogram.d.ts:

declare namespace MyMiniprogram {
  type EnvKeys = "DEV" | "SIT" | "UAT" | "PROD";
  type HostKeys = "base" | "news" | "sport";
  type Resource<M, H, D> =
    import("../miniprogram/node_modules/mp-resource").IResource<M, H, D>;
  type IRestResponse<T> =
    import("../miniprogram/node_modules/mp-resource").IRestResponse<T>;

  interface RestResponse<
    T extends string | number | Record<string, any> | ArrayBuffer =
      | string
      | number
      | Record<string, any>
      | ArrayBuffer
  > extends IRestResponse<T> {
    errorCode: number | null;
    errorMessage: string;
  }

  interface IAppOption {
    globalData: {
      accessToken?: string;
      resource: Resource<RestResponse, HostKeys, EnvData>;
    };
  }
}

Create two configuration files.

// miniprogram/config/api-config.ts
import { ApiUrl } from "mp-resource";

export const apiUrl: ApiUrl<MyMiniprogram.HostKeys> = {
  get: {
    // GET http://localhost:8080/news-svc/details
    getNewsDetails: {
      hostKey: "news",
      path: "/details"
    }
  },
  post: {},
  put: {},
  delete: {},
};
// miniprogram/config/config.ts
import { ISite } from "mp-resource";
import { apiUrl } from "./api-config";

export const sysEnvConfig: ISite<
  MyMiniprogram.EnvKeys,
  MyMiniprogram.HostKeys
> = {
  runtimes: "DEV",
  systems: [
    {
      key: "DEV",
      data: {
        hostname: "http://localhost:8080",
      },
      hosts: [
        {
          key: "base",
          url: "{{data.hostname}}/base-svc",
        },
        {
          key: "news",
          url: "{{data.hostname}}/news-svc",
        },
        {
          key: "sport",
          url: "{{data.hostname}}/sport-svc",
        },
      ],
    },
  ],
  hosts: [
    { key: "base", header: { "product-code": "100001" } },
    { key: "news", header: { "product-code": "100001" } },
    { key: "sport", header: { "product-code": "100001" } },
  ],
  apiUrl,
  globalData: {},
};

Create a startup file

// miniprogram/resource.ts
import { sysEnvConfig } from "./config/config";
import { useResource } from "mp-resource";

export const resource = useResource(sysEnvConfig);

Using in app

// miniprogram/app.ts
import { resource } from "./resource";

App<TlDoctorMiniprogram.IAppOption>({
  globalData: {
    resource,
  },
  onLaunch() {
    this.globalData.resource.interceptors.request.use((option) => {
      if (this.globalData.accessToken) {
        option.header = {
          ...option.header,
          "access-token": this.globalData.accessToken,
        };
      } else {
        if (option.header) {
          delete option.header["access-token"];
        }
      }
      return option;
    });
    this.globalData.resource.interceptors.response.use(
      ({ result, urlKey }) =>
        new Promise((resolve, reject) => {
          if (result.data.status === 0) {
            // unpack
            result.data = result.data.data as any;
            resolve({ result, urlKey });
          } else {
            reject(result.data);
          }
        })
    );
  }
})

Use Resource send requests:

// miniprogram/service/mews-service.ts
import { resource } from "../resource";

export function useNewsService(){

  function getNewsDetails(id: string){
    // send GET request to "http://localhost:8080/news-svc/details/[id][0]"
    return resource.get("getNewsDetails", {}, [id]);
  }

  return { getNewsDetails };
}