@xapi-js/core 1.3.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.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Robert Soriano <https://github.com/wobsoriano>
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Robert Soriano <https://github.com/wobsoriano>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,201 +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
25
-
26
- `@xapi-ts/core` provides the fundamental classes and functions for working with X-API data.
27
-
28
- ### Parsing X-API XML
29
-
30
- You can parse an X-API XML string into an `XapiRoot` object:
31
-
32
- ```typescript
33
- import { parse, XapiRoot } from '@xapi-ts/core';
34
-
35
- const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
36
- <Root xmlns="http://www.tobesoft.com/platform/Dataset" ver="4000">
37
- <Parameters>
38
- <Parameter id="service">stock</Parameter>
39
- <Parameter id="method">search</Parameter>
40
- </Parameters>
41
- </Root>`;
42
-
43
- const xapi = parse(xmlString);
44
- console.log('Service:', xapi.getParameter('service')?.value);
45
- console.log('Method:', xapi.getParameter('method')?.value);
11
+ ## Typed schemas
12
+
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`.
16
+
17
+ ```ts
18
+ import { InferRoot, RequestOf, ResponseOf, xapi } from '@xapi-js/core';
19
+
20
+ const request = xapi.root({
21
+ parameters: {
22
+ service: xapi.string(),
23
+ },
24
+ datasets: {
25
+ input: xapi.dataset({
26
+ id: xapi.int(),
27
+ amount: xapi.bigdecimal(),
28
+ ratio: xapi.decimal(),
29
+ score: xapi.float(),
30
+ note: xapi.string({ optional: true }),
31
+ }),
32
+ },
33
+ });
34
+
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
+ });
49
+
50
+ type OperationRequest = RequestOf<typeof operation>;
51
+ type OperationResponse = ResponseOf<typeof operation>;
46
52
  ```
47
53
 
48
- ### Creating and Manipulating XapiRoot
49
-
50
- You can create an `XapiRoot` object and add parameters and datasets programmatically:
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.
51
57
 
52
- ```typescript
53
- import { XapiRoot, Dataset } from '@xapi-ts/core';
58
+ ## Low-level API
54
59
 
55
- const xapi = new XapiRoot();
60
+ The original Dataset API remains available when dynamic column access is needed.
56
61
 
57
- // Add parameters
58
- xapi.addParameter({ id: 'resultCode', value: '0' });
59
- xapi.addParameter({ id: 'resultMsg', value: 'SUCCESS' });
62
+ ```ts
63
+ import { Dataset, parse, write, XapiRoot } from '@xapi-js/core';
60
64
 
61
- // Create and add a dataset
62
- const usersDataset = new Dataset('users');
63
- usersDataset.addColumn({ id: 'id', type: 'INT' });
64
- usersDataset.addColumn({ id: 'name', type: 'STRING' });
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 });
65
69
 
66
- usersDataset.newRow();
67
- usersDataset.setColumn(0, 'id', 1);
68
- usersDataset.setColumn(0, 'name', 'Alice');
70
+ const row = users.newRow();
71
+ users.setColumn(row, 'id', 1);
72
+ users.setColumn(row, 'name', 'Alice');
73
+ root.addDataset(users);
69
74
 
70
- usersDataset.newRow();
71
- usersDataset.setColumn(1, 'id', 2);
72
- usersDataset.setColumn(1, 'name', 'Bob');
73
-
74
- xapi.addDataset(usersDataset);
75
-
76
- console.log('XapiRoot created:', xapi);
77
- ```
78
-
79
- ### Serializing XapiRoot to XML
80
-
81
- You can serialize an `XapiRoot` object back into an X-API XML string:
82
-
83
- ```typescript
84
- import { write, XapiRoot, Dataset } from '@xapi-ts/core';
85
-
86
- const xapi = new XapiRoot();
87
- xapi.addParameter({ id: 'status', value: 'OK' });
88
-
89
- const productsDataset = new Dataset('products');
90
- productsDataset.addColumn({ id: 'productId', type: 'STRING' });
91
- productsDataset.addColumn({ id: 'price', type: 'INT' });
92
- productsDataset.newRow();
93
- productsDataset.setColumn(0, 'productId', 'P001');
94
- productsDataset.setColumn(0, 'price', 1000);
95
- xapi.addDataset(productsDataset);
96
-
97
- const xmlOutput = write(xapi);
98
- console.log('Generated XML:\n', xmlOutput);
75
+ const xml = write(root);
76
+ const parsed = parse(xml);
99
77
  ```
100
78
 
101
79
  ---
102
80
 
103
- # @xapi-ts/core
104
-
105
- 패키지는 X-API 데이터를 다루기 위한 핵심 유틸리티 및 타입을 제공합니다.
106
-
107
- ## 설치
108
-
109
- ```bash
110
- # npm
111
- npm install @xapi-ts/core
112
-
113
- # yarn
114
- yarn add @xapi-ts/core
115
-
116
- # pnpm
117
- pnpm add @xapi-ts/core
118
-
119
- # bun
120
- bun add @xapi-ts/core
121
-
122
- # deno
123
- deno add @xapi-ts/core
124
- ```
125
-
126
- ## 사용법
127
-
128
- `@xapi-ts/core`는 X-API 데이터를 다루기 위한 기본적인 클래스와 함수를 제공합니다.
129
-
130
- ### X-API XML 파싱
131
-
132
- X-API XML 문자열을 `XapiRoot` 객체로 파싱할 수 있습니다:
133
-
134
- ```typescript
135
- import { parse, XapiRoot } from '@xapi-ts/core';
136
-
137
- const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
138
- <Root xmlns="http://www.tobesoft.com/platform/Dataset" ver="4000">
139
- <Parameters>
140
- <Parameter id="service">stock</Parameter>
141
- <Parameter id="method">search</Parameter>
142
- </Parameters>
143
- </Root>`;
144
-
145
- const xapi = parse(xmlString);
146
- console.log('서비스:', xapi.getParameter('service')?.value);
147
- console.log('메서드:', xapi.getParameter('method')?.value);
148
- ```
149
-
150
- ### XapiRoot 생성 및 조작
151
-
152
- `XapiRoot` 객체를 생성하고 매개변수 및 데이터셋을 프로그래밍 방식으로 추가할 수 있습니다:
153
-
154
- ```typescript
155
- import { XapiRoot, Dataset } from '@xapi-ts/core';
156
-
157
- const xapi = new XapiRoot();
158
-
159
- // 매개변수 추가
160
- xapi.addParameter({ id: 'resultCode', value: '0' });
161
- xapi.addParameter({ id: 'resultMsg', value: 'SUCCESS' });
162
-
163
- // 데이터셋 생성 및 추가
164
- const usersDataset = new Dataset('users');
165
- usersDataset.addColumn({ id: 'id', type: 'INT' });
166
- usersDataset.addColumn({ id: 'name', type: 'STRING' });
167
- let rowIdx: number;
168
- rowIdx = usersDataset.newRow();
169
- usersDataset.setColumn(rowIdx, 'id', 1);
170
- usersDataset.setColumn(rowIdx, 'name', 'Alice');
171
-
172
- rowIdx = usersDataset.newRow();
173
- usersDataset.setColumn(rowIdx, 'id', 2);
174
- usersDataset.setColumn(rowIdx, 'name', 'Bob');
175
-
176
- xapi.addDataset(usersDataset);
177
-
178
- console.log('XapiRoot 생성됨:', xapi);
179
- ```
180
-
181
- ### XapiRoot를 XML로 직렬화
182
-
183
- `XapiRoot` 객체를 X-API XML 문자열로 다시 직렬화할 수 있습니다:
184
-
185
- ```typescript
186
- import { write, XapiRoot, Dataset } from '@xapi-ts/core';
187
-
188
- const xapi = new XapiRoot();
189
- xapi.addParameter({ id: 'status', value: 'OK' });
190
-
191
- const productsDataset = new Dataset('products');
192
- productsDataset.addColumn({ id: 'productId', type: 'STRING' });
193
- productsDataset.addColumn({ id: 'price', type: 'INT' });
194
- productsDataset.newRow();
195
- productsDataset.setColumn(0, 'productId', 'P001');
196
- productsDataset.setColumn(0, 'price', 1000);
197
- xapi.addDataset(productsDataset);
198
-
199
- const xmlOutput = write(xapi);
200
- console.log('생성된 XML:\n', xmlOutput);
201
- ```
81
+ Dataset schema는 `T[]`로, Root schema는 `parameters`와 여러 `datasets`를
82
+ 가진 plain object로 추론됩니다. `INT`, `FLOAT`, `DECIMAL`,
83
+ `BIGDECIMAL`처럼 JavaScript 타입은 같지만 X-API 전송 타입이 다른 컬럼도
84
+ schema에서 명시적으로 구분됩니다.