@xapi-js/core 1.4.0 → 1.4.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.
Files changed (2) hide show
  1. package/README.md +55 -192
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,221 +1,84 @@
1
- # @xapi-ts/core
1
+ # @xapi-js/core
2
2
 
3
- This package provides core utilities and types for working with X-API data.
3
+ Core XML, Dataset, and typed-schema support for Tobesoft X-API.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- # npm
9
- npm install @xapi-ts/core
10
-
11
- # yarn
12
- yarn add @xapi-ts/core
13
-
14
- # pnpm
15
- pnpm add @xapi-ts/core
16
-
17
- # bun
18
- bun add @xapi-ts/core
19
-
20
- # deno
21
- deno add @xapi-ts/core
8
+ pnpm add @xapi-js/core
22
9
  ```
23
10
 
24
- ## Usage
11
+ ## Typed schemas
25
12
 
26
- `@xapi-ts/core` provides the fundamental classes and functions for working with X-API data.
13
+ A Dataset schema maps to `T[]`; a Root schema maps to an object containing
14
+ `parameters` and named `datasets`. Column methods preserve X-API wire types,
15
+ even when multiple wire types map to JavaScript `number`.
27
16
 
28
- ### Typed schemas
17
+ ```ts
18
+ import { InferRoot, RequestOf, ResponseOf, xapi } from '@xapi-js/core';
29
19
 
30
- ```typescript
31
- import { InferRoot, xapi } from '@xapi-js/core';
32
-
33
- const schema = xapi.root({
34
- parameters: { ErrorCode: xapi.int() },
20
+ const request = xapi.root({
21
+ parameters: {
22
+ service: xapi.string(),
23
+ },
35
24
  datasets: {
36
- users: xapi.dataset({
25
+ input: xapi.dataset({
37
26
  id: xapi.int(),
38
- balance: xapi.bigdecimal(),
39
- name: xapi.string({ size: 100 }),
40
- photo: xapi.blob({ optional: true }),
27
+ amount: xapi.bigdecimal(),
28
+ ratio: xapi.decimal(),
29
+ score: xapi.float(),
30
+ note: xapi.string({ optional: true }),
41
31
  }),
42
32
  },
43
33
  });
44
34
 
45
- type Payload = InferRoot<typeof schema>;
46
- ```
47
-
48
- ### Parsing X-API XML
49
-
50
- You can parse an X-API XML string into an `XapiRoot` object:
51
-
52
- ```typescript
53
- import { parse, XapiRoot } from '@xapi-ts/core';
54
-
55
- const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
56
- <Root xmlns="http://www.tobesoft.com/platform/Dataset" ver="4000">
57
- <Parameters>
58
- <Parameter id="service">stock</Parameter>
59
- <Parameter id="method">search</Parameter>
60
- </Parameters>
61
- </Root>`;
35
+ type RequestPayload = InferRoot<typeof request>;
36
+
37
+ const operation = xapi.operation({
38
+ request,
39
+ response: xapi.root({
40
+ parameters: { ErrorCode: xapi.int(), ErrorMsg: xapi.string() },
41
+ datasets: {
42
+ output: xapi.dataset({
43
+ id: xapi.int(),
44
+ amount: xapi.bigdecimal(),
45
+ }),
46
+ },
47
+ }),
48
+ });
62
49
 
63
- const xapi = parse(xmlString);
64
- console.log('Service:', xapi.getParameter('service')?.value);
65
- console.log('Method:', xapi.getParameter('method')?.value);
50
+ type OperationRequest = RequestOf<typeof operation>;
51
+ type OperationResponse = ResponseOf<typeof operation>;
66
52
  ```
67
53
 
68
- ### Creating and Manipulating XapiRoot
69
-
70
- You can create an `XapiRoot` object and add parameters and datasets programmatically:
71
-
72
- ```typescript
73
- import { XapiRoot, Dataset } from '@xapi-ts/core';
54
+ `encodeRoot(schema, value)` converts the inferred plain object to `XapiRoot`.
55
+ `decodeRoot(schema, root)` converts it back. Adapters call these functions
56
+ automatically when supplied with a schema or operation.
74
57
 
75
- const xapi = new XapiRoot();
58
+ ## Low-level API
76
59
 
77
- // Add parameters
78
- xapi.addParameter({ id: 'resultCode', value: '0' });
79
- xapi.addParameter({ id: 'resultMsg', value: 'SUCCESS' });
60
+ The original Dataset API remains available when dynamic column access is needed.
80
61
 
81
- // Create and add a dataset
82
- const usersDataset = new Dataset('users');
83
- usersDataset.addColumn({ id: 'id', type: 'INT' });
84
- usersDataset.addColumn({ id: 'name', type: 'STRING' });
62
+ ```ts
63
+ import { Dataset, parse, write, XapiRoot } from '@xapi-js/core';
85
64
 
86
- usersDataset.newRow();
87
- usersDataset.setColumn(0, 'id', 1);
88
- usersDataset.setColumn(0, 'name', 'Alice');
65
+ const root = new XapiRoot();
66
+ const users = new Dataset('users');
67
+ users.addColumn({ id: 'id', type: 'INT', size: 10 });
68
+ users.addColumn({ id: 'name', type: 'STRING', size: 100 });
89
69
 
90
- usersDataset.newRow();
91
- usersDataset.setColumn(1, 'id', 2);
92
- usersDataset.setColumn(1, 'name', 'Bob');
70
+ const row = users.newRow();
71
+ users.setColumn(row, 'id', 1);
72
+ users.setColumn(row, 'name', 'Alice');
73
+ root.addDataset(users);
93
74
 
94
- xapi.addDataset(usersDataset);
95
-
96
- console.log('XapiRoot created:', xapi);
97
- ```
98
-
99
- ### Serializing XapiRoot to XML
100
-
101
- You can serialize an `XapiRoot` object back into an X-API XML string:
102
-
103
- ```typescript
104
- import { write, XapiRoot, Dataset } from '@xapi-ts/core';
105
-
106
- const xapi = new XapiRoot();
107
- xapi.addParameter({ id: 'status', value: 'OK' });
108
-
109
- const productsDataset = new Dataset('products');
110
- productsDataset.addColumn({ id: 'productId', type: 'STRING' });
111
- productsDataset.addColumn({ id: 'price', type: 'INT' });
112
- productsDataset.newRow();
113
- productsDataset.setColumn(0, 'productId', 'P001');
114
- productsDataset.setColumn(0, 'price', 1000);
115
- xapi.addDataset(productsDataset);
116
-
117
- const xmlOutput = write(xapi);
118
- console.log('Generated XML:\n', xmlOutput);
75
+ const xml = write(root);
76
+ const parsed = parse(xml);
119
77
  ```
120
78
 
121
79
  ---
122
80
 
123
- # @xapi-ts/core
124
-
125
- 패키지는 X-API 데이터를 다루기 위한 핵심 유틸리티 및 타입을 제공합니다.
126
-
127
- ## 설치
128
-
129
- ```bash
130
- # npm
131
- npm install @xapi-ts/core
132
-
133
- # yarn
134
- yarn add @xapi-ts/core
135
-
136
- # pnpm
137
- pnpm add @xapi-ts/core
138
-
139
- # bun
140
- bun add @xapi-ts/core
141
-
142
- # deno
143
- deno add @xapi-ts/core
144
- ```
145
-
146
- ## 사용법
147
-
148
- `@xapi-ts/core`는 X-API 데이터를 다루기 위한 기본적인 클래스와 함수를 제공합니다.
149
-
150
- ### X-API XML 파싱
151
-
152
- X-API XML 문자열을 `XapiRoot` 객체로 파싱할 수 있습니다:
153
-
154
- ```typescript
155
- import { parse, XapiRoot } from '@xapi-ts/core';
156
-
157
- const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
158
- <Root xmlns="http://www.tobesoft.com/platform/Dataset" ver="4000">
159
- <Parameters>
160
- <Parameter id="service">stock</Parameter>
161
- <Parameter id="method">search</Parameter>
162
- </Parameters>
163
- </Root>`;
164
-
165
- const xapi = parse(xmlString);
166
- console.log('서비스:', xapi.getParameter('service')?.value);
167
- console.log('메서드:', xapi.getParameter('method')?.value);
168
- ```
169
-
170
- ### XapiRoot 생성 및 조작
171
-
172
- `XapiRoot` 객체를 생성하고 매개변수 및 데이터셋을 프로그래밍 방식으로 추가할 수 있습니다:
173
-
174
- ```typescript
175
- import { XapiRoot, Dataset } from '@xapi-ts/core';
176
-
177
- const xapi = new XapiRoot();
178
-
179
- // 매개변수 추가
180
- xapi.addParameter({ id: 'resultCode', value: '0' });
181
- xapi.addParameter({ id: 'resultMsg', value: 'SUCCESS' });
182
-
183
- // 데이터셋 생성 및 추가
184
- const usersDataset = new Dataset('users');
185
- usersDataset.addColumn({ id: 'id', type: 'INT' });
186
- usersDataset.addColumn({ id: 'name', type: 'STRING' });
187
- let rowIdx: number;
188
- rowIdx = usersDataset.newRow();
189
- usersDataset.setColumn(rowIdx, 'id', 1);
190
- usersDataset.setColumn(rowIdx, 'name', 'Alice');
191
-
192
- rowIdx = usersDataset.newRow();
193
- usersDataset.setColumn(rowIdx, 'id', 2);
194
- usersDataset.setColumn(rowIdx, 'name', 'Bob');
195
-
196
- xapi.addDataset(usersDataset);
197
-
198
- console.log('XapiRoot 생성됨:', xapi);
199
- ```
200
-
201
- ### XapiRoot를 XML로 직렬화
202
-
203
- `XapiRoot` 객체를 X-API XML 문자열로 다시 직렬화할 수 있습니다:
204
-
205
- ```typescript
206
- import { write, XapiRoot, Dataset } from '@xapi-ts/core';
207
-
208
- const xapi = new XapiRoot();
209
- xapi.addParameter({ id: 'status', value: 'OK' });
210
-
211
- const productsDataset = new Dataset('products');
212
- productsDataset.addColumn({ id: 'productId', type: 'STRING' });
213
- productsDataset.addColumn({ id: 'price', type: 'INT' });
214
- productsDataset.newRow();
215
- productsDataset.setColumn(0, 'productId', 'P001');
216
- productsDataset.setColumn(0, 'price', 1000);
217
- xapi.addDataset(productsDataset);
218
-
219
- const xmlOutput = write(xapi);
220
- console.log('생성된 XML:\n', xmlOutput);
221
- ```
81
+ Dataset schema는 `T[]`로, Root schema는 `parameters`와 여러 `datasets`를
82
+ 가진 plain object로 추론됩니다. `INT`, `FLOAT`, `DECIMAL`,
83
+ `BIGDECIMAL`처럼 JavaScript 타입은 같지만 X-API 전송 타입이 다른 컬럼도
84
+ schema에서 명시적으로 구분됩니다.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xapi-js/core",
3
3
  "type": "module",
4
- "version": "1.4.0",
4
+ "version": "1.4.1",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",