@navios/adapter-xml 0.9.0 → 1.0.0-alpha.2
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/CHANGELOG.md +23 -0
- package/bunPlugin.cache +1 -1
- package/dist/src/decorators/xml-stream.decorator.d.mts +26 -70
- package/dist/src/decorators/xml-stream.decorator.d.mts.map +1 -1
- package/dist/src/handlers/xml-stream.d.mts +3 -5
- package/dist/src/handlers/xml-stream.d.mts.map +1 -1
- package/dist/src/types/config.d.mts +9 -5
- package/dist/src/types/config.d.mts.map +1 -1
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/tsconfig.spec.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/index.cjs +9 -69
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +34 -77
- package/lib/index.d.cts.map +1 -1
- package/lib/index.d.mts +33 -76
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +9 -69
- package/lib/index.mjs.map +1 -1
- package/package.json +9 -9
- package/src/decorators/xml-stream.decorator.mts +92 -101
- package/src/handlers/xml-stream.mts +19 -11
- package/src/types/config.mts +19 -10
|
@@ -1,80 +1,56 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
BaseEndpointOptions,
|
|
3
|
+
RequestArgs,
|
|
4
|
+
Simplify,
|
|
5
|
+
StreamHandler,
|
|
6
|
+
} from '@navios/builder'
|
|
3
7
|
|
|
4
8
|
import { getEndpointMetadata, XmlStreamAdapterToken } from '@navios/core'
|
|
5
9
|
|
|
6
|
-
import type { BaseXmlStreamConfig } from '../
|
|
10
|
+
import type { BaseXmlStreamConfig } from '../index.mjs'
|
|
7
11
|
|
|
8
12
|
/**
|
|
9
|
-
*
|
|
13
|
+
* Extracts the typed parameters for an XML stream endpoint handler function.
|
|
10
14
|
*
|
|
11
|
-
*
|
|
12
|
-
* configuration, including URL parameters, query parameters, and request body.
|
|
15
|
+
* Similar to `EndpointParams`, but specifically for XML streaming endpoints.
|
|
13
16
|
*
|
|
14
|
-
* @
|
|
15
|
-
* @template Url - The URL path pattern.
|
|
16
|
-
* @template QuerySchema - The query parameter schema type.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const getFeed = declareXmlStream({
|
|
21
|
-
* method: 'GET',
|
|
22
|
-
* url: '/feed/:category',
|
|
23
|
-
* querySchema: z.object({ page: z.string() }),
|
|
24
|
-
* })
|
|
25
|
-
*
|
|
26
|
-
* // XmlStreamParams<typeof getFeed> resolves to:
|
|
27
|
-
* // { urlParams: { category: string }, query: { page: string } }
|
|
28
|
-
* ```
|
|
17
|
+
* @typeParam EndpointDeclaration - The XML stream endpoint declaration from @navios/builder
|
|
29
18
|
*/
|
|
30
19
|
export type XmlStreamParams<
|
|
31
|
-
EndpointDeclaration extends
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
: Util_FlatObject<EndpointFunctionArgs<Url, undefined, undefined, true>>
|
|
20
|
+
EndpointDeclaration extends StreamHandler<Config, false>,
|
|
21
|
+
Config extends BaseXmlStreamConfig = EndpointDeclaration['config'],
|
|
22
|
+
> = Simplify<
|
|
23
|
+
RequestArgs<
|
|
24
|
+
Config['url'],
|
|
25
|
+
Config['querySchema'],
|
|
26
|
+
Config['requestSchema'],
|
|
27
|
+
Config['urlParamsSchema'],
|
|
28
|
+
true
|
|
29
|
+
>
|
|
30
|
+
>
|
|
43
31
|
|
|
44
32
|
/**
|
|
45
|
-
* Decorator
|
|
33
|
+
* Decorator that marks a method as an XML streaming endpoint.
|
|
46
34
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* The method
|
|
50
|
-
* regular JSX elements.
|
|
35
|
+
* Use this decorator for endpoints that stream XML data (e.g., RSS feeds, sitemaps).
|
|
36
|
+
* The endpoint must be defined using @navios/builder's `declareXmlStream` method.
|
|
37
|
+
* The method returns JSX elements, which will be automatically rendered to XML.
|
|
51
38
|
*
|
|
52
|
-
* @
|
|
53
|
-
* @
|
|
54
|
-
* @template QuerySchema - Optional Zod schema for query parameter validation.
|
|
55
|
-
* @template RequestSchema - Optional Zod schema for request body validation.
|
|
56
|
-
*
|
|
57
|
-
* @param endpoint - The endpoint declaration created with `declareXmlStream`.
|
|
58
|
-
* @returns A method decorator function.
|
|
59
|
-
*
|
|
60
|
-
* @throws {Error} If used on a non-function or non-method.
|
|
61
|
-
* @throws {Error} If the endpoint URL already exists.
|
|
39
|
+
* @param endpoint - The XML stream endpoint declaration from @navios/builder
|
|
40
|
+
* @returns A method decorator
|
|
62
41
|
*
|
|
63
42
|
* @example
|
|
64
43
|
* ```typescript
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* const getRssFeed = declareXmlStream({
|
|
69
|
-
* method: 'GET',
|
|
44
|
+
* const getRssFeed = api.declareXmlStream({
|
|
45
|
+
* method: 'get',
|
|
70
46
|
* url: '/feed.xml',
|
|
71
47
|
* contentType: 'application/rss+xml',
|
|
72
48
|
* })
|
|
73
49
|
*
|
|
74
|
-
* @Controller(
|
|
75
|
-
* class FeedController {
|
|
50
|
+
* @Controller()
|
|
51
|
+
* export class FeedController {
|
|
76
52
|
* @XmlStream(getRssFeed)
|
|
77
|
-
* async getFeed() {
|
|
53
|
+
* async getFeed(request: XmlStreamParams<typeof getRssFeed>) {
|
|
78
54
|
* return (
|
|
79
55
|
* <rss version="2.0">
|
|
80
56
|
* <channel>
|
|
@@ -85,58 +61,73 @@ export type XmlStreamParams<
|
|
|
85
61
|
* }
|
|
86
62
|
* }
|
|
87
63
|
* ```
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* ```typescript
|
|
91
|
-
* // With query parameters
|
|
92
|
-
* const getSitemap = declareXmlStream({
|
|
93
|
-
* method: 'GET',
|
|
94
|
-
* url: '/sitemap.xml',
|
|
95
|
-
* querySchema: z.object({ page: z.string().optional() }),
|
|
96
|
-
* })
|
|
97
|
-
*
|
|
98
|
-
* @Controller()
|
|
99
|
-
* class SitemapController {
|
|
100
|
-
* @XmlStream(getSitemap)
|
|
101
|
-
* async getSitemap(params: { query?: { page?: string } }) {
|
|
102
|
-
* const page = params.query?.page
|
|
103
|
-
* return <urlset>...</urlset>
|
|
104
|
-
* }
|
|
105
|
-
* }
|
|
106
|
-
* ```
|
|
107
64
|
*/
|
|
108
|
-
export function XmlStream<
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
65
|
+
export function XmlStream<Config extends BaseEndpointOptions>(endpoint: {
|
|
66
|
+
config: Config
|
|
67
|
+
}): (
|
|
68
|
+
target: (
|
|
69
|
+
params: RequestArgs<
|
|
70
|
+
Config['url'],
|
|
71
|
+
Config['querySchema'],
|
|
72
|
+
Config['requestSchema'],
|
|
73
|
+
Config['urlParamsSchema'],
|
|
74
|
+
true
|
|
75
|
+
>,
|
|
76
|
+
reply: any,
|
|
77
|
+
) => any,
|
|
78
|
+
context: ClassMethodDecoratorContext,
|
|
79
|
+
) => void
|
|
80
|
+
// Bun doesn't support reply parameter
|
|
81
|
+
export function XmlStream<Config extends BaseEndpointOptions>(endpoint: {
|
|
82
|
+
config: Config
|
|
83
|
+
}): (
|
|
84
|
+
target: (
|
|
85
|
+
params: RequestArgs<
|
|
86
|
+
Config['url'],
|
|
87
|
+
Config['querySchema'],
|
|
88
|
+
Config['requestSchema'],
|
|
89
|
+
Config['urlParamsSchema'],
|
|
90
|
+
true
|
|
91
|
+
>,
|
|
92
|
+
) => any,
|
|
93
|
+
context: ClassMethodDecoratorContext,
|
|
94
|
+
) => void
|
|
95
|
+
export function XmlStream<Config extends BaseEndpointOptions>(endpoint: {
|
|
96
|
+
config: Config
|
|
97
|
+
}): (target: () => any, context: ClassMethodDecoratorContext) => void
|
|
98
|
+
export function XmlStream<Config extends BaseEndpointOptions>(endpoint: {
|
|
99
|
+
config: Config
|
|
100
|
+
}) {
|
|
101
|
+
type Params = RequestArgs<
|
|
102
|
+
Config['url'],
|
|
103
|
+
Config['querySchema'],
|
|
104
|
+
Config['requestSchema'],
|
|
105
|
+
Config['urlParamsSchema'],
|
|
106
|
+
true
|
|
107
|
+
>
|
|
108
|
+
|
|
109
|
+
type Handler =
|
|
110
|
+
| ((params: Params, reply: any) => any)
|
|
111
|
+
| ((params: Params) => any)
|
|
112
|
+
| (() => any)
|
|
113
|
+
|
|
114
|
+
return (target: Handler, context: ClassMethodDecoratorContext) => {
|
|
129
115
|
if (context.kind !== 'method') {
|
|
130
|
-
throw new Error(
|
|
116
|
+
throw new Error(
|
|
117
|
+
'[Navios] Endpoint decorator can only be used on methods.',
|
|
118
|
+
)
|
|
131
119
|
}
|
|
132
|
-
|
|
133
120
|
const config = endpoint.config
|
|
134
121
|
if (context.metadata) {
|
|
135
|
-
|
|
122
|
+
let endpointMetadata = getEndpointMetadata<BaseEndpointOptions>(
|
|
123
|
+
target,
|
|
124
|
+
context,
|
|
125
|
+
)
|
|
136
126
|
if (endpointMetadata.config && endpointMetadata.config.url) {
|
|
137
|
-
throw new Error(
|
|
127
|
+
throw new Error(
|
|
128
|
+
`[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`,
|
|
129
|
+
)
|
|
138
130
|
}
|
|
139
|
-
// @ts-expect-error We don't need to set correctly in the metadata
|
|
140
131
|
endpointMetadata.config = config
|
|
141
132
|
endpointMetadata.adapterToken = XmlStreamAdapterToken
|
|
142
133
|
endpointMetadata.classMethod = target.name
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { HttpMethod } from '@navios/builder'
|
|
1
|
+
import type { HttpMethod, RequestArgs, StreamHandler } from '@navios/builder'
|
|
2
|
+
import type { ZodObject, ZodType } from 'zod/v4'
|
|
2
3
|
|
|
3
|
-
import type { BaseXmlStreamConfig } from '../types/config.mjs'
|
|
4
|
+
import type { BaseXmlStreamConfig, XmlStreamConfig } from '../types/config.mjs'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Declares an XML Stream endpoint configuration for use with `@XmlStream` decorator.
|
|
@@ -41,13 +42,20 @@ import type { BaseXmlStreamConfig } from '../types/config.mjs'
|
|
|
41
42
|
* })
|
|
42
43
|
* ```
|
|
43
44
|
*/
|
|
44
|
-
export function declareXmlStream<
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
export function declareXmlStream<const Config extends XmlStreamConfig>(
|
|
46
|
+
config: Config,
|
|
47
|
+
): StreamHandler<Config, false> {
|
|
48
|
+
const handler = async (
|
|
49
|
+
_params: RequestArgs<
|
|
50
|
+
Config['url'],
|
|
51
|
+
Config['querySchema'],
|
|
52
|
+
Config['requestSchema'],
|
|
53
|
+
Config['urlParamsSchema'],
|
|
54
|
+
true
|
|
55
|
+
>,
|
|
56
|
+
) => {
|
|
57
|
+
throw new Error('Not implemented')
|
|
58
|
+
}
|
|
59
|
+
handler.config = config
|
|
60
|
+
return handler as unknown as StreamHandler<Config, false>
|
|
53
61
|
}
|
package/src/types/config.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BaseEndpointOptions, HttpMethod } from '@navios/builder'
|
|
2
|
+
import type { ZodObject, ZodType } from 'zod/v4'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Configuration interface for XML Stream endpoints.
|
|
@@ -22,16 +23,24 @@ import type { BaseStreamConfig, HttpMethod } from '@navios/builder'
|
|
|
22
23
|
* }
|
|
23
24
|
* ```
|
|
24
25
|
*/
|
|
26
|
+
export interface XmlStreamConfig extends BaseEndpointOptions {
|
|
27
|
+
contentType?:
|
|
28
|
+
| 'application/xml'
|
|
29
|
+
| 'text/xml'
|
|
30
|
+
| 'application/rss+xml'
|
|
31
|
+
| 'application/atom+xml'
|
|
32
|
+
xmlDeclaration?: boolean
|
|
33
|
+
encoding?: string
|
|
34
|
+
}
|
|
35
|
+
|
|
25
36
|
export interface BaseXmlStreamConfig<
|
|
26
37
|
Method extends HttpMethod = HttpMethod,
|
|
27
38
|
Url extends string = string,
|
|
28
|
-
QuerySchema = undefined,
|
|
29
|
-
RequestSchema = undefined,
|
|
30
|
-
> extends
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
/** XML encoding, defaults to 'UTF-8' */
|
|
36
|
-
encoding?: string
|
|
39
|
+
QuerySchema extends ZodObject | undefined = undefined,
|
|
40
|
+
RequestSchema extends ZodType | undefined = undefined,
|
|
41
|
+
> extends XmlStreamConfig {
|
|
42
|
+
method: Method
|
|
43
|
+
url: Url
|
|
44
|
+
querySchema?: QuerySchema
|
|
45
|
+
requestSchema?: RequestSchema
|
|
37
46
|
}
|