@unireq/xml 0.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Olivier Orabona
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.
@@ -0,0 +1,57 @@
1
+ import { Policy, BodyDescriptor } from '@unireq/core';
2
+
3
+ /**
4
+ * @unireq/xml - XML parser policy using fast-xml-parser
5
+ */
6
+
7
+ /** XML parser options (fast-xml-parser X2jOptions) */
8
+ interface XMLParserOptions {
9
+ readonly ignoreAttributes?: boolean;
10
+ readonly attributeNamePrefix?: string;
11
+ readonly textNodeName?: string;
12
+ readonly [key: string]: unknown;
13
+ }
14
+ /** XML builder options (fast-xml-parser XmlBuilderOptions) */
15
+ interface XMLBuilderOptions {
16
+ readonly ignoreAttributes?: boolean;
17
+ readonly attributeNamePrefix?: string;
18
+ readonly textNodeName?: string;
19
+ readonly format?: boolean;
20
+ readonly indentBy?: string;
21
+ readonly [key: string]: unknown;
22
+ }
23
+ /**
24
+ * Creates an XML parser policy
25
+ * @param options - fast-xml-parser options
26
+ * @returns Policy that parses XML responses
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const xmlPolicy = xml({
31
+ * ignoreAttributes: false,
32
+ * attributeNamePrefix: '@_'
33
+ * });
34
+ * ```
35
+ */
36
+ declare function xml(options?: XMLParserOptions): Policy;
37
+ /**
38
+ * Create an XML body descriptor
39
+ * Serializes an object to XML string using fast-xml-parser
40
+ *
41
+ * @param data - Data to serialize as XML
42
+ * @param options - fast-xml-parser builder options
43
+ * @returns BodyDescriptor for XML content
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * import { xmlBody } from '@unireq/xml';
48
+ * import { client } from '@unireq/core';
49
+ * import { http } from '@unireq/http';
50
+ *
51
+ * const api = client(http('https://api.example.com'));
52
+ * const response = await api.post('/users', xmlBody({ user: { name: 'John', email: 'john@example.com' } }));
53
+ * ```
54
+ */
55
+ declare function xmlBody<T>(data: T, options?: XMLBuilderOptions): BodyDescriptor;
56
+
57
+ export { type XMLBuilderOptions, type XMLParserOptions, xml, xmlBody };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import {XMLParser,XMLBuilder}from'fast-xml-parser';function p(r={}){return async(t,o)=>{let e=await o(t);if(!(e.headers["content-type"]||e.headers["Content-Type"]||"").includes("xml"))return e;let n;if(typeof e.data=="string")n=e.data;else if(e.data instanceof ArrayBuffer)n=new TextDecoder().decode(e.data);else return e;let i=new XMLParser(r).parse(n);return {...e,data:i}}}function c(r,t={}){return {__brand:"BodyDescriptor",data:r,contentType:"application/xml",serialize:()=>new XMLBuilder(t).build(r)}}export{p as xml,c as xmlBody};//# sourceMappingURL=index.js.map
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["xml","options","ctx","next","response","xmlText","parsed","XMLParser","xmlBody","data","XMLBuilder"],"mappings":"mDAsCO,SAASA,CAAAA,CAAIC,CAAAA,CAA4B,EAAC,CAAW,CAC1D,OAAO,MAAOC,CAAAA,CAAKC,IAAS,CAC1B,IAAMC,EAAW,MAAMD,CAAAA,CAAKD,CAAG,CAAA,CAK/B,GAAI,CAAA,CAFgBE,EAAS,OAAA,CAAQ,cAAc,GAAKA,CAAAA,CAAS,OAAA,CAAQ,cAAc,CAAA,EAAK,EAAA,EAE3E,QAAA,CAAS,KAAK,CAAA,CAC7B,OAAOA,EAIT,IAAIC,CAAAA,CACJ,GAAI,OAAOD,CAAAA,CAAS,IAAA,EAAS,SAC3BC,CAAAA,CAAUD,CAAAA,CAAS,IAAA,CAAA,KAAA,GACVA,CAAAA,CAAS,IAAA,YAAgB,WAAA,CAClCC,EAAU,IAAI,WAAA,GAAc,MAAA,CAAOD,CAAAA,CAAS,IAAI,CAAA,CAAA,KAEhD,OAAOA,CAAAA,CAKT,IAAME,CAAAA,CADS,IAAIC,UAAUN,CAAO,CAAA,CACd,KAAA,CAAMI,CAAO,CAAA,CAEnC,OAAO,CACL,GAAGD,CAAAA,CACH,IAAA,CAAME,CACR,CACF,CACF,CAoBO,SAASE,CAAAA,CAAWC,EAASR,CAAAA,CAA6B,GAAoB,CACnF,OAAO,CACL,OAAA,CAAS,gBAAA,CACT,IAAA,CAAAQ,EACA,WAAA,CAAa,iBAAA,CACb,SAAA,CAAW,IACO,IAAIC,UAAAA,CAAWT,CAAO,CAAA,CACvB,KAAA,CAAMQ,CAAI,CAE7B,CACF","file":"index.js","sourcesContent":["/**\n * @unireq/xml - XML parser policy using fast-xml-parser\n */\n\nimport type { BodyDescriptor, Policy } from '@unireq/core';\nimport { XMLBuilder, XMLParser } from 'fast-xml-parser';\n\n/** XML parser options (fast-xml-parser X2jOptions) */\nexport interface XMLParserOptions {\n readonly ignoreAttributes?: boolean;\n readonly attributeNamePrefix?: string;\n readonly textNodeName?: string;\n readonly [key: string]: unknown;\n}\n\n/** XML builder options (fast-xml-parser XmlBuilderOptions) */\nexport interface XMLBuilderOptions {\n readonly ignoreAttributes?: boolean;\n readonly attributeNamePrefix?: string;\n readonly textNodeName?: string;\n readonly format?: boolean;\n readonly indentBy?: string;\n readonly [key: string]: unknown;\n}\n\n/**\n * Creates an XML parser policy\n * @param options - fast-xml-parser options\n * @returns Policy that parses XML responses\n *\n * @example\n * ```ts\n * const xmlPolicy = xml({\n * ignoreAttributes: false,\n * attributeNamePrefix: '@_'\n * });\n * ```\n */\nexport function xml(options: XMLParserOptions = {}): Policy {\n return async (ctx, next) => {\n const response = await next(ctx);\n\n // Only parse if content-type is XML\n const contentType = response.headers['content-type'] || response.headers['Content-Type'] || '';\n\n if (!contentType.includes('xml')) {\n return response;\n }\n\n // Get XML text\n let xmlText: string;\n if (typeof response.data === 'string') {\n xmlText = response.data;\n } else if (response.data instanceof ArrayBuffer) {\n xmlText = new TextDecoder().decode(response.data);\n } else {\n return response;\n }\n\n // Parse XML\n const parser = new XMLParser(options);\n const parsed = parser.parse(xmlText);\n\n return {\n ...response,\n data: parsed,\n };\n };\n}\n\n/**\n * Create an XML body descriptor\n * Serializes an object to XML string using fast-xml-parser\n *\n * @param data - Data to serialize as XML\n * @param options - fast-xml-parser builder options\n * @returns BodyDescriptor for XML content\n *\n * @example\n * ```ts\n * import { xmlBody } from '@unireq/xml';\n * import { client } from '@unireq/core';\n * import { http } from '@unireq/http';\n *\n * const api = client(http('https://api.example.com'));\n * const response = await api.post('/users', xmlBody({ user: { name: 'John', email: 'john@example.com' } }));\n * ```\n */\nexport function xmlBody<T>(data: T, options: XMLBuilderOptions = {}): BodyDescriptor {\n return {\n __brand: 'BodyDescriptor',\n data,\n contentType: 'application/xml',\n serialize: () => {\n const builder = new XMLBuilder(options);\n return builder.build(data);\n },\n };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@unireq/xml",
3
+ "version": "0.0.1",
4
+ "description": "XML parser policy for unireq",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "author": "Olivier Orabona",
18
+ "license": "MIT",
19
+ "dependencies": {
20
+ "@unireq/core": "0.0.1"
21
+ },
22
+ "peerDependencies": {
23
+ "fast-xml-parser": "^4.5.0"
24
+ },
25
+ "peerDependenciesMeta": {
26
+ "fast-xml-parser": {
27
+ "optional": true
28
+ }
29
+ },
30
+ "devDependencies": {
31
+ "typescript": "^5.9.3",
32
+ "tsup": "^8.5.1"
33
+ },
34
+ "engines": {
35
+ "node": ">=18.0.0"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "https://github.com/oorabona/unireq",
40
+ "directory": "packages/xml"
41
+ },
42
+ "bugs": {
43
+ "url": "https://github.com/oorabona/unireq/issues"
44
+ },
45
+ "homepage": "https://github.com/oorabona/unireq/tree/main/packages/xml",
46
+ "scripts": {
47
+ "build": "tsup",
48
+ "type-check": "tsc --noEmit",
49
+ "test": "vitest run",
50
+ "clean": "rm -rf dist *.tsbuildinfo"
51
+ }
52
+ }