@shencom/utils-resource
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

@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();

Package Sidebar

Install

npm i @shencom/utils-resource

Weekly Downloads

1

Version

1.3.1

License

ISC

Unpacked Size

71.8 kB

Total Files

19

Last publish

Collaborators

  • shencom