init
This commit is contained in:
		
							
								
								
									
										12
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					  "name": "@abearxiong/query",
 | 
				
			||||||
 | 
					  "version": "0.0.1",
 | 
				
			||||||
 | 
					  "main": "index.js",
 | 
				
			||||||
 | 
					  "scripts": {
 | 
				
			||||||
 | 
					    "build": ""
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "keywords": [],
 | 
				
			||||||
 | 
					  "author": "",
 | 
				
			||||||
 | 
					  "license": "ISC",
 | 
				
			||||||
 | 
					  "description": ""
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										34
									
								
								src/adapter.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/adapter.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					type AdapterOpts = {
 | 
				
			||||||
 | 
					  url: string;
 | 
				
			||||||
 | 
					  headers?: Record<string, string>;
 | 
				
			||||||
 | 
					  body: Record<string, any>;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					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,
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
							
								
								
									
										37
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					import { adapter } from './adapter';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Fn = (opts: { url?: string; headers?: Record<string, string>; body?: Record<string, any>; [key: string]: any }) => Promise<Record<string, any>>;
 | 
				
			||||||
 | 
					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<string, any>) {
 | 
				
			||||||
 | 
					    return this.post(params);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  async post(body: Record<string, any>) {
 | 
				
			||||||
 | 
					    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;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										9
									
								
								test/query.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								test/query.test.ts
									
									
									
									
									
										Normal file
									
								
							@@ -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 });
 | 
				
			||||||
 | 
					//   });
 | 
				
			||||||
 | 
					// }
 | 
				
			||||||
							
								
								
									
										38
									
								
								tsconfig.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								tsconfig.json
									
									
									
									
									
										Normal file
									
								
							@@ -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"
 | 
				
			||||||
 | 
					  ],
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user