@xapi-js/adaptor-express 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,115 @@
1
- # @xapi-ts/adaptor-express
2
-
3
- This package provides an Express.js adaptor for X-API data.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- pnpm add @xapi-ts/adaptor-express
9
- ```
10
-
11
- ## Usage
12
-
13
- (Add usage examples here)
14
-
15
- ---
16
-
17
- # @xapi-ts/adaptor-express
18
-
19
- 이 패키지는 X-API 데이터를 위한 Express.js 어댑터를 제공합니다.
20
-
21
- ## 설치
22
-
23
- ```bash
24
- pnpm add @xapi-ts/adaptor-express
25
- ```
26
-
27
- ## 사용법
28
-
29
- (여기에 사용 예시를 추가하세요)
1
+ # @xapi-ts/adaptor-express
2
+
3
+ This package provides an Express.js adaptor for X-API data.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # npm
9
+ npm install @xapi-ts/adaptor-express
10
+
11
+ # yarn
12
+ yarn add @xapi-ts/adaptor-express
13
+
14
+ # pnpm
15
+ pnpm add @xapi-ts/adaptor-express
16
+
17
+ # bun
18
+ bun add @xapi-ts/adaptor-express
19
+
20
+ # deno
21
+ deno add @xapi-ts/adaptor-express
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Here's how to use `@xapi-ts/adaptor-express` with an Express application:
27
+
28
+ ```typescript
29
+ import express from 'express';
30
+ import { xapiExpress } from '@xapi-ts/adaptor-express';
31
+ import { XapiRoot } from '@xapi-ts/core';
32
+
33
+ const app = express();
34
+
35
+ // Define your X-API handler function
36
+ const xapiHandler = async (xapi: XapiRoot): Promise<XapiRoot> => {
37
+ // Process the incoming X-API request (xapi)
38
+ // For example, you can access parameters or datasets:
39
+ const service = xapi.parameters.get('service')?.value;
40
+ console.log(`Service requested: ${service}`);
41
+
42
+ // Create a response X-API object
43
+ const responseXapi = new XapiRoot();
44
+ responseXapi.addParameter({ id: 'result', value: 'success' });
45
+ return responseXapi;
46
+ };
47
+
48
+ // Use the xapiExpress middleware
49
+ app.use('/xapi', xapiExpress(xapiHandler));
50
+
51
+ // Start the server
52
+ const PORT = 3000;
53
+ app.listen(PORT, () => {
54
+ console.log(`Express server listening on port ${PORT}`);
55
+ });
56
+ ```
57
+
58
+ ---
59
+
60
+ # @xapi-ts/adaptor-express
61
+
62
+ 이 패키지는 X-API 데이터를 위한 Express.js 어댑터를 제공합니다.
63
+
64
+ ## 설치
65
+
66
+ ```bash
67
+ # npm
68
+ npm install @xapi-ts/adaptor-express
69
+
70
+ # yarn
71
+ yarn add @xapi-ts/adaptor-express
72
+
73
+ # pnpm
74
+ pnpm add @xapi-ts/adaptor-express
75
+
76
+ # bun
77
+ bun add @xapi-ts/adaptor-express
78
+
79
+ # deno
80
+ deno add @xapi-ts/adaptor-express
81
+ ```
82
+
83
+ ## 사용법
84
+
85
+ 다음은 Express 애플리케이션에서 `@xapi-ts/adaptor-express`를 사용하는 방법입니다:
86
+
87
+ ```typescript
88
+ import express from 'express';
89
+ import { xapiExpress } from '@xapi-ts/adaptor-express';
90
+ import { XapiRoot } from '@xapi-ts/core';
91
+
92
+ const app = express();
93
+
94
+ // X-API 핸들러 함수 정의
95
+ const xapiHandler = async (xapi: XapiRoot): Promise<XapiRoot> => {
96
+ // 들어오는 X-API 요청(xapi) 처리
97
+ // 예를 들어, 파라미터 또는 데이터셋에 접근할 수 있습니다:
98
+ const service = xapi.parameters.get('service')?.value;
99
+ console.log(`Service requested: ${service}`);
100
+
101
+ // 응답 X-API 객체 생성
102
+ const responseXapi = new XapiRoot();
103
+ responseXapi.addParameter({ id: 'result', value: 'success' });
104
+ return responseXapi;
105
+ };
106
+
107
+ // xapiExpress 미들웨어 사용
108
+ app.use('/xapi', xapiExpress(xapiHandler));
109
+
110
+ // 서버 시작
111
+ const PORT = 3000;
112
+ app.listen(PORT, () => {
113
+ console.log(`Express server listening on port ${PORT}`);
114
+ });
115
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,53 @@
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
+ * Express middleware to handle X-API XML requests and responses.
30
+ * It parses incoming XML into an XapiRoot object, passes it to the provided handler function,
31
+ * and then serializes the handler's XapiRoot response back into XML.
32
+ *
33
+ * @param handler - An asynchronous function that takes an XapiRoot object and returns a Promise of an XapiRoot object.
34
+ * @returns An Express middleware function.
35
+ */
36
+ const xapiExpress = (handler) => {
37
+ return async (req, res, next) => {
38
+ if (req.headers["content-type"] !== "application/xml") return next();
39
+ try {
40
+ const xapiReq = (0, __xapi_js_core.parse)(req.body);
41
+ const xapiRes = await handler(xapiReq);
42
+ const xml = (0, __xapi_js_core.write)(xapiRes);
43
+ res.setHeader("Content-Type", "application/xml");
44
+ res.write(xml);
45
+ res.end();
46
+ } catch (error) {
47
+ next(error);
48
+ }
49
+ };
50
+ };
51
+
52
+ //#endregion
53
+ exports.xapiExpress = xapiExpress;
@@ -0,0 +1,21 @@
1
+ import { XapiRoot } from "@xapi-js/core";
2
+ import { NextFunction, Request, Response } from "express";
3
+
4
+ //#region src/index.d.ts
5
+
6
+ /**
7
+ * Type definition for an X-API Express handler function.
8
+ * This function receives an XapiRoot object (parsed from the request body) and should return a Promise resolving to an XapiRoot object (for the response).
9
+ */
10
+ type XapiExpressHandler = (xapi: XapiRoot) => Promise<XapiRoot>;
11
+ /**
12
+ * Express middleware to handle X-API XML requests and responses.
13
+ * It parses incoming XML into an XapiRoot object, passes it to the provided handler function,
14
+ * and then serializes the handler's XapiRoot response back into XML.
15
+ *
16
+ * @param handler - An asynchronous function that takes an XapiRoot object and returns a Promise of an XapiRoot object.
17
+ * @returns An Express middleware function.
18
+ */
19
+ declare const xapiExpress: (handler: XapiExpressHandler) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
20
+ //#endregion
21
+ export { xapiExpress };
@@ -0,0 +1,21 @@
1
+ import { XapiRoot } from "@xapi-js/core";
2
+ import { NextFunction, Request, Response } from "express";
3
+
4
+ //#region src/index.d.ts
5
+
6
+ /**
7
+ * Type definition for an X-API Express handler function.
8
+ * This function receives an XapiRoot object (parsed from the request body) and should return a Promise resolving to an XapiRoot object (for the response).
9
+ */
10
+ type XapiExpressHandler = (xapi: XapiRoot) => Promise<XapiRoot>;
11
+ /**
12
+ * Express middleware to handle X-API XML requests and responses.
13
+ * It parses incoming XML into an XapiRoot object, passes it to the provided handler function,
14
+ * and then serializes the handler's XapiRoot response back into XML.
15
+ *
16
+ * @param handler - An asynchronous function that takes an XapiRoot object and returns a Promise of an XapiRoot object.
17
+ * @returns An Express middleware function.
18
+ */
19
+ declare const xapiExpress: (handler: XapiExpressHandler) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
20
+ //#endregion
21
+ export { xapiExpress };
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ import { parse, write } from "@xapi-js/core";
2
+
3
+ //#region src/index.ts
4
+ /**
5
+ * Express middleware to handle X-API XML requests and responses.
6
+ * It parses incoming XML into an XapiRoot object, passes it to the provided handler function,
7
+ * and then serializes the handler's XapiRoot response back into XML.
8
+ *
9
+ * @param handler - An asynchronous function that takes an XapiRoot object and returns a Promise of an XapiRoot object.
10
+ * @returns An Express middleware function.
11
+ */
12
+ const xapiExpress = (handler) => {
13
+ return async (req, res, next) => {
14
+ if (req.headers["content-type"] !== "application/xml") return next();
15
+ try {
16
+ const xapiReq = parse(req.body);
17
+ const xapiRes = await handler(xapiReq);
18
+ const xml = write(xapiRes);
19
+ res.setHeader("Content-Type", "application/xml");
20
+ res.write(xml);
21
+ res.end();
22
+ } catch (error) {
23
+ next(error);
24
+ }
25
+ };
26
+ };
27
+
28
+ //#endregion
29
+ export { xapiExpress };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xapi-js/adaptor-express",
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
  "license": "MIT",
32
32
  "dependencies": {
33
33
  "express": "^5.1.0",
34
- "@xapi-js/core": "1.0.0"
34
+ "@xapi-js/core": "1.2.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/express": "^5.0.3",
38
38
  "node-mocks-http": "^1.17.2"
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"