UNPKG

@open-data-v/data

Version:

OpenDataV 数据处理基础组件包

1 lines 3.44 kB
{"version":3,"file":"handler.umd.cjs","sources":["../../src/rest/handler.ts"],"sourcesContent":["import type { DataAcceptor, DataInstance, Response } from '@open-data-v/base'\nimport { cloneDeep } from 'lodash-es'\n\nimport type { RequestInstance } from '../hooks'\nimport { useRequest } from '../hooks'\nimport type { StoreRestOption } from './type'\n\nclass RestRequestData implements DataInstance {\n public options?: StoreRestOption\n public requestInstance: RequestInstance\n public timer: any = 0\n\n constructor(options?: StoreRestOption, connector?: RequestInstance) {\n this.options = options\n this.requestInstance = connector || useRequest()\n }\n public close() {\n clearInterval(this.timer)\n }\n\n public async connect(acceptor: DataAcceptor) {\n const { otherConfig = { isRepeat: false, interval: 3000 } } = this.options || {}\n if (otherConfig.isRepeat) {\n const handler = async () => {\n const resp = await this.getRespData()\n acceptor(resp)\n }\n const interval = otherConfig.interval | 3000\n this.timer = setInterval(handler, interval) as unknown as IntervalHandle\n } else {\n const resp = await this.getRespData()\n acceptor(resp)\n }\n }\n\n public async getRespData(): Promise<Response> {\n const response: Response = {\n status: 'FAILED',\n data: ''\n }\n if (!this.options) {\n return response\n }\n\n try {\n const config = {\n url: this.options.url,\n method: this.options.method,\n headers: this.options.headers,\n params: this.options.params,\n data: this.options.data\n }\n const resp = await this.requestInstance.request(config)\n response.status = 'SUCCESS'\n response.data = resp.data\n } catch (err: any) {\n err.response || (err.toJSON ? err.toJSON() : {})\n response.status = 'FAILED'\n response.data = err.stack || err.message\n }\n return response\n }\n\n public async debug(acceptor: DataAcceptor) {\n this.connect(acceptor)\n }\n\n public toJSON() {\n return {\n options: cloneDeep(this.options),\n type: 'REST'\n }\n }\n}\n\nexport default RestRequestData\n"],"names":["RestRequestData","options","connector","__publicField","useRequest","acceptor","otherConfig","handler","resp","interval","response","config","err","cloneDeep"],"mappings":"gVAOA,MAAMA,CAAwC,CAK5C,YAAYC,EAA2BC,EAA6B,CAJ7DC,EAAA,gBACAA,EAAA,w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