@xapi-js/adaptor-express 1.2.0 → 1.4.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/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,115 +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
- # 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
- });
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
115
  ```
package/dist/index.cjs CHANGED
@@ -1,53 +1,28 @@
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
-
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _xapi_js_core = require("@xapi-js/core");
27
3
  //#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();
4
+ function xapiExpress(operationOrHandler, operationHandler) {
5
+ const operation = typeof operationOrHandler === "function" ? void 0 : operationOrHandler;
6
+ const handler = typeof operationOrHandler === "function" ? operationOrHandler : operationHandler;
7
+ return createMiddleware(async (req, res, next) => {
8
+ if (!req.headers["content-type"]?.startsWith("application/xml")) return next();
39
9
  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);
10
+ const requestRoot = (0, _xapi_js_core.parse)(req.body);
11
+ const request = operation ? (0, _xapi_js_core.decodeRoot)(operation.request, requestRoot) : requestRoot;
12
+ const response = await handler(request);
13
+ const xml = (0, _xapi_js_core.write)(operation ? (0, _xapi_js_core.encodeRoot)(operation.response, response) : response);
43
14
  res.setHeader("Content-Type", "application/xml");
44
15
  res.write(xml);
45
16
  res.end();
46
17
  } catch (error) {
47
18
  next(error);
48
19
  }
20
+ });
21
+ }
22
+ function createMiddleware(middleware) {
23
+ return async (req, res, next) => {
24
+ return middleware(req, res, next);
49
25
  };
50
- };
51
-
26
+ }
52
27
  //#endregion
53
- exports.xapiExpress = xapiExpress;
28
+ exports.xapiExpress = xapiExpress;
package/dist/index.d.cts CHANGED
@@ -1,13 +1,12 @@
1
- import { XapiRoot } from "@xapi-js/core";
1
+ import { RequestOf, ResponseOf, XapiOperation, XapiRoot } from "@xapi-js/core";
2
2
  import { NextFunction, Request, Response } from "express";
3
-
4
3
  //#region src/index.d.ts
5
-
6
4
  /**
7
5
  * Type definition for an X-API Express handler function.
8
6
  * 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
7
  */
10
8
  type XapiExpressHandler = (xapi: XapiRoot) => Promise<XapiRoot>;
9
+ type XapiOperationHandler<Operation extends XapiOperation> = (request: RequestOf<Operation>) => ResponseOf<Operation> | Promise<ResponseOf<Operation>>;
11
10
  /**
12
11
  * Express middleware to handle X-API XML requests and responses.
13
12
  * It parses incoming XML into an XapiRoot object, passes it to the provided handler function,
@@ -16,6 +15,8 @@ type XapiExpressHandler = (xapi: XapiRoot) => Promise<XapiRoot>;
16
15
  * @param handler - An asynchronous function that takes an XapiRoot object and returns a Promise of an XapiRoot object.
17
16
  * @returns An Express middleware function.
18
17
  */
19
- declare const xapiExpress: (handler: XapiExpressHandler) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
18
+ declare function xapiExpress(handler: XapiExpressHandler): ReturnType<typeof createMiddleware>;
19
+ declare function xapiExpress<Operation extends XapiOperation>(operation: Operation, handler: XapiOperationHandler<Operation>): ReturnType<typeof createMiddleware>;
20
+ declare function createMiddleware(middleware: (req: Request, res: Response, next: NextFunction) => Promise<unknown>): (req: Request, res: Response, next: NextFunction) => Promise<unknown>;
20
21
  //#endregion
21
22
  export { xapiExpress };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,12 @@
1
- import { XapiRoot } from "@xapi-js/core";
1
+ import { RequestOf, ResponseOf, XapiOperation, XapiRoot } from "@xapi-js/core";
2
2
  import { NextFunction, Request, Response } from "express";
3
-
4
3
  //#region src/index.d.ts
5
-
6
4
  /**
7
5
  * Type definition for an X-API Express handler function.
8
6
  * 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
7
  */
10
8
  type XapiExpressHandler = (xapi: XapiRoot) => Promise<XapiRoot>;
9
+ type XapiOperationHandler<Operation extends XapiOperation> = (request: RequestOf<Operation>) => ResponseOf<Operation> | Promise<ResponseOf<Operation>>;
11
10
  /**
12
11
  * Express middleware to handle X-API XML requests and responses.
13
12
  * It parses incoming XML into an XapiRoot object, passes it to the provided handler function,
@@ -16,6 +15,8 @@ type XapiExpressHandler = (xapi: XapiRoot) => Promise<XapiRoot>;
16
15
  * @param handler - An asynchronous function that takes an XapiRoot object and returns a Promise of an XapiRoot object.
17
16
  * @returns An Express middleware function.
18
17
  */
19
- declare const xapiExpress: (handler: XapiExpressHandler) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
18
+ declare function xapiExpress(handler: XapiExpressHandler): ReturnType<typeof createMiddleware>;
19
+ declare function xapiExpress<Operation extends XapiOperation>(operation: Operation, handler: XapiOperationHandler<Operation>): ReturnType<typeof createMiddleware>;
20
+ declare function createMiddleware(middleware: (req: Request, res: Response, next: NextFunction) => Promise<unknown>): (req: Request, res: Response, next: NextFunction) => Promise<unknown>;
20
21
  //#endregion
21
22
  export { xapiExpress };
package/dist/index.js CHANGED
@@ -1,29 +1,27 @@
1
- import { parse, write } from "@xapi-js/core";
2
-
1
+ import { decodeRoot, encodeRoot, parse, write } from "@xapi-js/core";
3
2
  //#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();
3
+ function xapiExpress(operationOrHandler, operationHandler) {
4
+ const operation = typeof operationOrHandler === "function" ? void 0 : operationOrHandler;
5
+ const handler = typeof operationOrHandler === "function" ? operationOrHandler : operationHandler;
6
+ return createMiddleware(async (req, res, next) => {
7
+ if (!req.headers["content-type"]?.startsWith("application/xml")) return next();
15
8
  try {
16
- const xapiReq = parse(req.body);
17
- const xapiRes = await handler(xapiReq);
18
- const xml = write(xapiRes);
9
+ const requestRoot = parse(req.body);
10
+ const request = operation ? decodeRoot(operation.request, requestRoot) : requestRoot;
11
+ const response = await handler(request);
12
+ const xml = write(operation ? encodeRoot(operation.response, response) : response);
19
13
  res.setHeader("Content-Type", "application/xml");
20
14
  res.write(xml);
21
15
  res.end();
22
16
  } catch (error) {
23
17
  next(error);
24
18
  }
19
+ });
20
+ }
21
+ function createMiddleware(middleware) {
22
+ return async (req, res, next) => {
23
+ return middleware(req, res, next);
25
24
  };
26
- };
27
-
25
+ }
28
26
  //#endregion
29
- export { xapiExpress };
27
+ 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.2.0",
4
+ "version": "1.4.0",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "license": "MIT",
32
32
  "dependencies": {
33
33
  "express": "^5.1.0",
34
- "@xapi-js/core": "1.2.0"
34
+ "@xapi-js/core": "1.4.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/express": "^5.0.3",