@kevisual/query 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ type AdapterOpts = {
2
+ url: string;
3
+ headers?: Record<string, string>;
4
+ body?: Record<string, any>;
5
+ };
6
+ export declare const adapter: (opts: AdapterOpts) => Promise<any>;
7
+ export {};
@@ -0,0 +1,23 @@
1
+ import { adapter } from './adapter.ts';
2
+ type Fn = (opts: {
3
+ url?: string;
4
+ headers?: Record<string, string>;
5
+ body?: Record<string, any>;
6
+ [key: string]: any;
7
+ }) => Promise<Record<string, any>>;
8
+ type QueryOpts = {
9
+ url?: string;
10
+ adapter?: typeof adapter;
11
+ headers?: Record<string, string>;
12
+ };
13
+ export declare class Query {
14
+ adapter: typeof adapter;
15
+ url: string;
16
+ beforeRequest?: Fn;
17
+ headers?: Record<string, string>;
18
+ constructor(opts: QueryOpts);
19
+ get(params: Record<string, any>): Promise<any>;
20
+ post(body: Record<string, any>): Promise<any>;
21
+ before(fn: Fn): void;
22
+ }
23
+ export { adapter };
package/dist/index.js ADDED
@@ -0,0 +1,63 @@
1
+ const adapter = async (opts) => {
2
+ return fetch(opts.url, {
3
+ method: 'POST',
4
+ headers: {
5
+ 'Content-Type': 'application/json',
6
+ ...opts.headers,
7
+ },
8
+ body: JSON.stringify(opts.body),
9
+ })
10
+ .then((response) => {
11
+ // 获取 Content-Type 头部信息
12
+ const contentType = response.headers.get('Content-Type');
13
+ // 判断返回的数据类型
14
+ if (contentType && contentType.includes('application/json')) {
15
+ return response.json(); // 解析为 JSON
16
+ }
17
+ else {
18
+ return response.text(); // 解析为文本
19
+ }
20
+ })
21
+ .catch((err) => {
22
+ if (err.name === 'AbortError') {
23
+ console.log('Request timed out and was aborted');
24
+ }
25
+ console.error(err);
26
+ return {
27
+ code: 500,
28
+ };
29
+ });
30
+ };
31
+
32
+ class Query {
33
+ adapter;
34
+ url;
35
+ beforeRequest;
36
+ headers;
37
+ constructor(opts) {
38
+ this.adapter = opts.adapter || adapter;
39
+ this.url = opts.url || '/api/router';
40
+ this.headers = opts.headers || {
41
+ 'Content-Type': 'application/json',
42
+ };
43
+ }
44
+ async get(params) {
45
+ return this.post(params);
46
+ }
47
+ async post(body) {
48
+ const req = {
49
+ url: this.url,
50
+ headers: this.headers,
51
+ body,
52
+ };
53
+ if (this.beforeRequest) {
54
+ await this.beforeRequest(req);
55
+ }
56
+ return this.adapter(req);
57
+ }
58
+ before(fn) {
59
+ this.beforeRequest = fn;
60
+ }
61
+ }
62
+
63
+ export { Query, adapter };
@@ -0,0 +1,8 @@
1
+ type AdapterOpts = {
2
+ url: string;
3
+ headers?: Record<string, string>;
4
+ body?: Record<string, any>;
5
+ };
6
+ export declare const nodeAdapter: (opts: AdapterOpts) => Promise<any>;
7
+ export declare const adapter: (opts: AdapterOpts) => Promise<any>;
8
+ export {};
@@ -0,0 +1,50 @@
1
+ import http from 'http';
2
+
3
+ const nodeAdapter = async (opts) => {
4
+ return new Promise((resolve, reject) => {
5
+ const postData = JSON.stringify(opts.body || '');
6
+ const _url = new URL(opts.url);
7
+ const { hostname, port, pathname } = _url;
8
+ console.log('hostname:', hostname, port, pathname);
9
+ const options = {
10
+ hostname: hostname,
11
+ port: port,
12
+ path: pathname || '/api/router',
13
+ method: 'POST', // Assuming it's a POST request
14
+ headers: {
15
+ 'Content-Type': 'application/json',
16
+ 'Content-Length': Buffer.byteLength(postData),
17
+ ...opts.headers,
18
+ },
19
+ };
20
+ const req = http.request(options, (res) => {
21
+ let data = '';
22
+ // Collect data chunks
23
+ res.on('data', (chunk) => {
24
+ data += chunk;
25
+ });
26
+ // Resolve when the response is complete
27
+ res.on('end', () => {
28
+ try {
29
+ const parsedData = JSON.parse(data);
30
+ resolve(parsedData);
31
+ }
32
+ catch (error) {
33
+ reject(error);
34
+ }
35
+ });
36
+ });
37
+ // Handle request errors
38
+ req.on('error', (error) => {
39
+ reject(error);
40
+ });
41
+ // Write the request body and end the request
42
+ if (opts.body) {
43
+ req.write(postData);
44
+ }
45
+ req.end();
46
+ });
47
+ };
48
+ const adapter = nodeAdapter;
49
+
50
+ export { adapter, nodeAdapter };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@kevisual/query",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "private": false,
8
+ "type": "module",
9
+ "scripts": {
10
+ "build": "npm run clean && rollup -c",
11
+ "test": "NODE_ENV=development node --experimental-vm-modules node_modules/jest/bin/jest.js --detectOpenHandles",
12
+ "clean": "rm -rf dist"
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "keywords": [],
18
+ "author": "",
19
+ "license": "ISC",
20
+ "description": "",
21
+ "devDependencies": {
22
+ "@rollup/plugin-node-resolve": "^15.2.3",
23
+ "@rollup/plugin-typescript": "^11.1.6",
24
+ "@types/jest": "^29.5.12",
25
+ "jest": "^29.7.0",
26
+ "jest-config": "^29.7.0",
27
+ "rollup": "^4.21.2",
28
+ "ts-jest": "^29.2.5",
29
+ "ts-node": "^10.9.2",
30
+ "tslib": "^2.7.0",
31
+ "typescript": "^5.5.4"
32
+ },
33
+ "packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447",
34
+ "publishConfig": {
35
+ "access": "public"
36
+ }
37
+ }
package/readme.md ADDED
@@ -0,0 +1,2 @@
1
+ # query
2
+