From 443a41110a695b01b5f5c31226c269fdad12f878 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sat, 7 Sep 2024 03:43:42 +0800 Subject: [PATCH] init --- package.json | 12 ++++++++++++ readme.md | 2 ++ src/adapter.ts | 34 ++++++++++++++++++++++++++++++++++ src/index.ts | 37 +++++++++++++++++++++++++++++++++++++ test/query.test.ts | 9 +++++++++ tsconfig.json | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 package.json create mode 100644 readme.md create mode 100644 src/adapter.ts create mode 100644 src/index.ts create mode 100644 test/query.test.ts create mode 100644 tsconfig.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..0772284 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "@abearxiong/query", + "version": "0.0.1", + "main": "index.js", + "scripts": { + "build": "" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "" +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..33f6f26 --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +# query + diff --git a/src/adapter.ts b/src/adapter.ts new file mode 100644 index 0000000..21b153f --- /dev/null +++ b/src/adapter.ts @@ -0,0 +1,34 @@ +type AdapterOpts = { + url: string; + headers?: Record; + body: Record; +}; +export const adapter = async (opts: AdapterOpts) => { + return fetch(opts.url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + localStorage.getItem('token'), + ...opts.headers, + }, + body: JSON.stringify(opts.body), + }) + .then((response) => { + // 获取 Content-Type 头部信息 + const contentType = response.headers.get('Content-Type'); + // 判断返回的数据类型 + if (contentType && contentType.includes('application/json')) { + return response.json(); // 解析为 JSON + } else { + return response.text(); // 解析为文本 + } + }) + .catch((err) => { + if (err.name === 'AbortError') { + console.log('Request timed out and was aborted'); + } + return { + code: 500, + }; + }); +}; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..6287e78 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,37 @@ +import { adapter } from './adapter'; + +type Fn = (opts: { url?: string; headers?: Record; body?: Record; [key: string]: any }) => Promise>; +type QueryOpts = { + url?: string; + adapter?: typeof adapter; +}; +export class Query { + adapter: typeof adapter; + url: string; + beforeRequest?: Fn; + headers = { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + localStorage.getItem('token'), + }; + constructor(opts: QueryOpts) { + this.adapter = opts.adapter || adapter; + this.url = opts.url || '/api/router'; + } + async get(params: Record) { + return this.post(params); + } + async post(body: Record) { + const req = { + url: this.url, + headers: this.headers, + body, + }; + if (this.beforeRequest) { + await this.beforeRequest(req); + } + return this.adapter(req); + } + before(fn: Fn) { + this.beforeRequest = fn; + } +} diff --git a/test/query.test.ts b/test/query.test.ts new file mode 100644 index 0000000..af4bed7 --- /dev/null +++ b/test/query.test.ts @@ -0,0 +1,9 @@ +import { Query } from './../src/index'; + +const query = new Query({ url: '/api/router' }); + +// test('query', async () => { +// query.get({ id: 1 }).then((res) => { +// expect(res).toEqual({ id: 1 }); +// }); +// } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..6f8bce7 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,38 @@ +{ + "compilerOptions": { + "target": "ESNext", + "useDefineForClassFields": true, + "lib": [ + "DOM", + "DOM.Iterable", + "ESNext" + ], + "allowJs": false, + "skipLibCheck": true, + "esModuleInterop": false, + "allowSyntheticDefaultImports": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "module": "ESNext", + "moduleResolution": "Node", + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react-jsx", + "baseUrl": ".", + "typeRoots": [ + "node_modules/@types" + ], + "paths": { + "@/*": [ + "src/*" + ], + "*": [ + "types/*" + ] + }, + }, + "include": [ + "src" + ], +} \ No newline at end of file