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

@shencom/utils-resource

v1.3.1

Published

资源统一管理工具包

Downloads

40

Readme

@shencom/utils-resource

获取文件和点位信息

Install

pnpm add @shencom/utils

# or

pnpm add @shencom/utils-resource

Basic Usage

import { ScResourceFileBase, ScResourceGisBase } from '@shencom/utils';
// import { ScResourceFileBase, ScResourceGisBase } from '@shencom/utils-resource';

Option

interface CacheController {
  set: (key: string, data: any) => void;
  get: <T = any>(key: string) => T | null;
  remove: (key: string) => void;
}

interface ResourceOptions {
  /** 存储控制器 */
  Cache: CacheController;
  /** 数据缓存时间,单位毫秒;默认: 7天 */
  CacheTime?: number;
  /** 缓存数据量,单位 KB;默认: 2M */
  CacheSize?: number;
  /** 通过ID请求数据方法 */
  Fetch: (ids: string[]) => Promise<D[]>;
}

interface ResourceFileOptions extends ResourceOptions<FileItem> {
  /** 保留字段, 默认: ['id', 'fileName', 'remoteUrl', 'name']  */
  Fields?: (keyof FileItem)[];
}

interface ResourceGisOptions extends ResourceOptions<GisItem> {
  /** 保留字段, 默认: ['id', 'addr', 'lat', 'lng']  */
  Fields?: (keyof GisItem)[];
}

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | --------- | ---------------------- | --------------- | ------ | ------ | | Cache | 存储控制器 | CacheController | - | 必填 | | CacheTime | 数据缓存时间,单位毫秒 | number | - | 7天 | | CacheSize | 缓存数据量,单位 KB | number | - | 2M | | Fetch | 通过 ID 请求数据方法 | Function | - | 必填 |

Methods

ScResourceFileBase

  • 说明: 获取文件信息
  • 类型: (options?: ResourceFileOptions): Object
  • 参数:
    • options - 初始化配置

init

// 本地存储对象
const Storage = new ScStorageBase({
  /* ... */
});

const Cache = {
  set: Storage.setLasting.bind(Storage),
  get: Storage.getLasting.bind(Storage),
  remove: Storage.removeLasting.bind(Storage),
};

// 调接口获取文件详情的方法
const getFileInfo = (ids: string[]) => Promise<FileItem[]>;
// 初始化文件资源对象
const ResourceFile = ScResourceFileBase({
  Cache,
  Fetch: getFileInfo,
});

ResourceFile.getData

获取文件信息的方法

ResourceFile.getData('1449561090540367872');
// Promise<[{id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...'}]>

ResourceFile.getData('url 字符串');
// Promise<[{id: '随机 id', fileName: 'xx', remoteUrl: 'url 字符串'}]>

ResourceFile.getData(['1449561090540367872', '1449122610622427136']);
/*
  Promise<[
    {id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...'},
    {id: '1449122610622427136', fileName: 'xx', remoteUrl: 'https://...'}
  ]>
*/

ResourceFile.getData({ id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...' });
// Promise<[{id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...'}]>

ResourceFile.getData([
  '1449561090540367872',
  'url 字符串',
  { id: '1449122610622427136', fileName: 'xx', remoteUrl: 'https://...' },
]);
/*
  Promise<[
    {id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...'},
    {id: '随机 id', fileName: 'xx', remoteUrl: 'url 字符串'},
    {id: '1449122610622427136', fileName: 'xx', remoteUrl: 'https://...'}
  ]>
*/

ResourceFile.all

获取全部的文件信息

const CacheData = ResourceFile.all();

ResourceFile.show

通过 id 获取缓存内对应的文件信息

const files = ResourceFile.show(['id1', 'id2']);

ResourceFile.keys

获取存储的 id

const CacheIds = ResourceFile.keys();

删除指定 id 的文件信息

ResourceFile.remove('id1');

清空所有的文件信息

ResourceFile.clear();

ScResourceGisBase

  • 说明: 获取点位信息
  • 类型: (options: ResourceGisOptions): Object
  • 参数:
    • options - 初始化配置

init

// 本地存储对象
const Storage = new ScStorageBase({
  /* ... */
});

const Cache = {
  set: Storage.setLasting.bind(Storage),
  get: Storage.getLasting.bind(Storage),
  remove: Storage.removeLasting.bind(Storage),
};

// 调接口获取点位详情的方法
const getGisInfo = (ids: string[]) => Promise<FileItem[]>;
// 初始化点位资源对象
const ResourceGis = ScResourceGisBase({
  Cache,
  Fetch: getGisInfo,
});

ResourceGis.getData

获取文件信息的方法

ResourceGis.getData('1449561090540367222');
// Promise<[{ id: '1449561090540367222', addr: '地址地址', lng: '114.06667', lat: '22.61667' }]>

ResourceGis.getData('地址地址');
// Promise<[{ id: '随机 id', addr: '地址地址' }]>

ResourceGis.getData(['1449561090540367222', '1449122610622427316']);
/* 
  Promise<[
    { id: '1449561090540367222', addr: '地址地址', lng: '114.06667', lat: '22.61667' },
    { id: '1449122610622427316', addr: '地址地址', lng: '114.06667', lat: '22.61667' },
  ]>
*/

ResourceGis.getData({ id: '1449561090540367222', lng: '114.06667', lat: '22.61667' });
// Promise<[{ id: '1449561090540367222', lng: '114.06667', lat: '22.61667' }]>

ResourceGis.getData([
  '1449561090540367222',
  '地址地址',
  { id: '1449561090540367222', lng: '114.06667', lat: '22.61667' },
]);
/* 
  Promise<[
    { id: '1449561090540367222', addr: '地址地址', lng: '114.06667', lat: '22.61667' },
    { id: '随机 id', addr: '地址地址' },
    { id: '1449561090540367222', lng: '114.06667', lat: '22.61667' }
  ]>
*/

ResourceGis.all

获取全部的 GIS 信息

const CacheData = ResourceGis.all();

ResourceGis.show

通过 id 获取缓存内对应的 GIS 信息

const files = ResourceGis.show(['id1', 'id2']);

ResourceGis.keys

获取存储的 id

const CacheIds = ResourceGis.keys();

删除指定 id 的 GIS 信息

ResourceGis.remove('id1');

清空所有的 GIS 信息

ResourceGis.clear();