@xapi-js/adaptor-fetch 1.0.0 → 1.2.0

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.
package/README.md CHANGED
@@ -1,29 +1,119 @@
1
- # @xapi-ts/adaptor-fetch
2
-
3
- This package provides a Fetch API adaptor for X-API data.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- pnpm add @xapi-ts/adaptor-fetch
9
- ```
10
-
11
- ## Usage
12
-
13
- (Add usage examples here)
14
-
15
- ---
16
-
17
- # @xapi-ts/adaptor-fetch
18
-
19
- 이 패키지는 X-API 데이터를 위한 Fetch API 어댑터를 제공합니다.
20
-
21
- ## 설치
22
-
23
- ```bash
24
- pnpm add @xapi-ts/adaptor-fetch
25
- ```
26
-
27
- ## 사용법
28
-
29
- (여기에 사용 예시를 추가하세요)
1
+ # @xapi-ts/adaptor-fetch
2
+
3
+ This package provides a Fetch API adaptor for X-API data.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # npm
9
+ npm install @xapi-ts/adaptor-fetch
10
+
11
+ # yarn
12
+ yarn add @xapi-ts/adaptor-fetch
13
+
14
+ # pnpm
15
+ pnpm add @xapi-ts/adaptor-fetch
16
+
17
+ # bun
18
+ bun add @xapi-ts/adaptor-fetch
19
+
20
+ # deno
21
+ deno add @xapi-ts/adaptor-fetch
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Here's how to use `@xapi-ts/adaptor-fetch` to send and receive X-API data:
27
+
28
+ ```typescript
29
+ import { xapiFetch } from '@xapi-ts/adaptor-fetch';
30
+ import { XapiRoot } from '@xapi-ts/core';
31
+
32
+ async function sendXapiRequest() {
33
+ const requestXapi = new XapiRoot();
34
+ requestXapi.addParameter({ id: 'service', value: 'stock' });
35
+ requestXapi.addParameter({ id: 'method', value: 'search' });
36
+
37
+ try {
38
+ const responseXapi = await xapiFetch('http://localhost:3000/api/xapi', requestXapi);
39
+
40
+ // Access parameters from the response
41
+ const result = responseXapi.parameters.get('result')?.value;
42
+ console.log(`Result: ${result}`);
43
+
44
+ // Access datasets from the response
45
+ const stockDataset = responseXapi.getDataset('stockList');
46
+ if (stockDataset) {
47
+ console.log('Stock List:');
48
+ stockDataset.rows.forEach(row => {
49
+ console.log(` Code: ${row.get('stockCode')}, Price: ${row.get('currentPrice')}`);
50
+ });
51
+ }
52
+ } catch (error) {
53
+ console.error('Error sending X-API request:', error);
54
+ }
55
+ }
56
+
57
+ sendXapiRequest();
58
+ ```
59
+
60
+ ---
61
+
62
+ # @xapi-ts/adaptor-fetch
63
+
64
+ 이 패키지는 X-API 데이터를 위한 Fetch API 어댑터를 제공합니다.
65
+
66
+ ## 설치
67
+
68
+ ```bash
69
+ # npm
70
+ npm install @xapi-ts/adaptor-fetch
71
+
72
+ # yarn
73
+ yarn add @xapi-ts/adaptor-fetch
74
+
75
+ # pnpm
76
+ pnpm add @xapi-ts/adaptor-fetch
77
+
78
+ # bun
79
+ bun add @xapi-ts/adaptor-fetch
80
+
81
+ # deno
82
+ deno add @xapi-ts/adaptor-fetch
83
+ ```
84
+
85
+ ## 사용법
86
+
87
+ 다음은 `@xapi-ts/adaptor-fetch`를 사용하여 X-API 데이터를 송수신하는 방법입니다:
88
+
89
+ ```typescript
90
+ import { xapiFetch } from '@xapi-ts/adaptor-fetch';
91
+ import { XapiRoot } from '@xapi-ts/core';
92
+
93
+ async function sendXapiRequest() {
94
+ const requestXapi = new XapiRoot();
95
+ requestXapi.addParameter({ id: 'service', value: 'stock' });
96
+ requestXapi.addParameter({ id: 'method', value: 'search' });
97
+
98
+ try {
99
+ const responseXapi = await xapiFetch('http://localhost:3000/api/xapi', requestXapi);
100
+
101
+ // 응답에서 파라미터 접근
102
+ const result = responseXapi.parameters.get('result')?.value;
103
+ console.log(`결과: ${result}`);
104
+
105
+ // 응답에서 데이터셋 접근
106
+ const stockDataset = responseXapi.getDataset('stockList');
107
+ if (stockDataset) {
108
+ console.log('주식 목록:');
109
+ stockDataset.rows.forEach(row => {
110
+ console.log(` 코드: ${row.get('stockCode')}, 가격: ${row.get('currentPrice')}`);
111
+ });
112
+ }
113
+ } catch (error) {
114
+ console.error('X-API 요청 전송 오류:', error);
115
+ }
116
+ }
117
+
118
+ sendXapiRequest();
119
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,54 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+ let __xapi_js_core = require("@xapi-js/core");
25
+ __xapi_js_core = __toESM(__xapi_js_core);
26
+
27
+ //#region src/index.ts
28
+ /**
29
+ * Sends an X-API request using the Fetch API and parses the XML response.
30
+ *
31
+ * @param url - The URL to send the request to.
32
+ * @param xapi - The XapiRoot object to be serialized into the request body.
33
+ * @param options - Optional Fetch API request initialization options.
34
+ * @returns A Promise that resolves to an XapiRoot object parsed from the response.
35
+ * @throws {Error} if the response body is empty.
36
+ */
37
+ async function xapiFetch(url, xapi, options) {
38
+ const xml = (0, __xapi_js_core.write)(xapi);
39
+ const response = await fetch(url, {
40
+ method: "POST",
41
+ ...options,
42
+ headers: {
43
+ "Content-Type": "application/xml",
44
+ ...options?.headers
45
+ },
46
+ body: xml
47
+ });
48
+ if (!response.body) throw new Error("Response body is empty");
49
+ const text = await response.text();
50
+ return (0, __xapi_js_core.parse)(text);
51
+ }
52
+
53
+ //#endregion
54
+ exports.xapiFetch = xapiFetch;
@@ -0,0 +1,16 @@
1
+ import { XapiRoot } from "@xapi-js/core";
2
+
3
+ //#region src/index.d.ts
4
+
5
+ /**
6
+ * Sends an X-API request using the Fetch API and parses the XML response.
7
+ *
8
+ * @param url - The URL to send the request to.
9
+ * @param xapi - The XapiRoot object to be serialized into the request body.
10
+ * @param options - Optional Fetch API request initialization options.
11
+ * @returns A Promise that resolves to an XapiRoot object parsed from the response.
12
+ * @throws {Error} if the response body is empty.
13
+ */
14
+ declare function xapiFetch(url: string, xapi: XapiRoot, options?: RequestInit): Promise<XapiRoot>;
15
+ //#endregion
16
+ export { xapiFetch };
@@ -0,0 +1,16 @@
1
+ import { XapiRoot } from "@xapi-js/core";
2
+
3
+ //#region src/index.d.ts
4
+
5
+ /**
6
+ * Sends an X-API request using the Fetch API and parses the XML response.
7
+ *
8
+ * @param url - The URL to send the request to.
9
+ * @param xapi - The XapiRoot object to be serialized into the request body.
10
+ * @param options - Optional Fetch API request initialization options.
11
+ * @returns A Promise that resolves to an XapiRoot object parsed from the response.
12
+ * @throws {Error} if the response body is empty.
13
+ */
14
+ declare function xapiFetch(url: string, xapi: XapiRoot, options?: RequestInit): Promise<XapiRoot>;
15
+ //#endregion
16
+ export { xapiFetch };
package/dist/index.js ADDED
@@ -0,0 +1,30 @@
1
+ import { parse, write } from "@xapi-js/core";
2
+
3
+ //#region src/index.ts
4
+ /**
5
+ * Sends an X-API request using the Fetch API and parses the XML response.
6
+ *
7
+ * @param url - The URL to send the request to.
8
+ * @param xapi - The XapiRoot object to be serialized into the request body.
9
+ * @param options - Optional Fetch API request initialization options.
10
+ * @returns A Promise that resolves to an XapiRoot object parsed from the response.
11
+ * @throws {Error} if the response body is empty.
12
+ */
13
+ async function xapiFetch(url, xapi, options) {
14
+ const xml = write(xapi);
15
+ const response = await fetch(url, {
16
+ method: "POST",
17
+ ...options,
18
+ headers: {
19
+ "Content-Type": "application/xml",
20
+ ...options?.headers
21
+ },
22
+ body: xml
23
+ });
24
+ if (!response.body) throw new Error("Response body is empty");
25
+ const text = await response.text();
26
+ return parse(text);
27
+ }
28
+
29
+ //#endregion
30
+ export { xapiFetch };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xapi-js/adaptor-fetch",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.2.0",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -31,14 +31,14 @@
31
31
  "author": "Clickin <josh87786@gmail.com>",
32
32
  "dependencies": {
33
33
  "vitest": "^3.2.4",
34
- "@xapi-js/core": "1.0.0"
34
+ "@xapi-js/core": "1.2.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "typescript": "^5.0.0",
38
38
  "vitest-fetch-mock": "^0.4.5"
39
39
  },
40
40
  "scripts": {
41
- "build": "tsup src/index.ts --format cjs,esm --dts",
41
+ "build": "tsdown",
42
42
  "test": "vitest run",
43
43
  "test:watch": "vitest",
44
44
  "coverage": "vitest run --coverage"