@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.
@@ -1,80 +1,56 @@
1
- import type { EndpointFunctionArgs, HttpMethod, Util_FlatObject } from '@navios/builder'
2
- import type { ZodObject, ZodType } from 'zod/v4'
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 '../types/config.mjs'
10
+ import type { BaseXmlStreamConfig } from '../index.mjs'
7
11
 
8
12
  /**
9
- * Type helper that extracts the parameter types for an XML Stream endpoint handler.
13
+ * Extracts the typed parameters for an XML stream endpoint handler function.
10
14
  *
11
- * This type automatically infers the correct parameter types based on the endpoint
12
- * configuration, including URL parameters, query parameters, and request body.
15
+ * Similar to `EndpointParams`, but specifically for XML streaming endpoints.
13
16
  *
14
- * @template EndpointDeclaration - The endpoint declaration type from `declareXmlStream`.
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
- config: BaseXmlStreamConfig<any, any, any, any>
33
- },
34
- Url extends string = EndpointDeclaration['config']['url'],
35
- QuerySchema = EndpointDeclaration['config']['querySchema'],
36
- > = QuerySchema extends ZodObject
37
- ? EndpointDeclaration['config']['requestSchema'] extends ZodType
38
- ? Util_FlatObject<EndpointFunctionArgs<Url, QuerySchema, EndpointDeclaration['config']['requestSchema'], true>>
39
- : Util_FlatObject<EndpointFunctionArgs<Url, QuerySchema, undefined, true>>
40
- : EndpointDeclaration['config']['requestSchema'] extends ZodType
41
- ? Util_FlatObject<EndpointFunctionArgs<Url, undefined, EndpointDeclaration['config']['requestSchema'], true>>
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 for XML Stream endpoints that return JSX-based XML responses.
33
+ * Decorator that marks a method as an XML streaming endpoint.
46
34
  *
47
- * This decorator marks controller methods that return JSX elements, which will be
48
- * automatically rendered to XML and sent with the appropriate Content-Type header.
49
- * The method can be async and can contain async components, class components, and
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
- * @template Method - The HTTP method (GET, POST, etc.).
53
- * @template Url - The URL path pattern (supports parameters like `/posts/:id`).
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
- * import { XmlStream, declareXmlStream } from '@navios/adapter-xml'
66
- * import { Controller } from '@navios/core'
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('/api')
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
- Method extends HttpMethod = HttpMethod,
110
- Url extends string = string,
111
- QuerySchema = undefined,
112
- RequestSchema = ZodType,
113
- >(endpoint: { config: BaseXmlStreamConfig<Method, Url, QuerySchema, RequestSchema> }) {
114
- return (
115
- target: (
116
- params: QuerySchema extends ZodObject
117
- ? RequestSchema extends ZodType
118
- ? Util_FlatObject<EndpointFunctionArgs<Url, QuerySchema, RequestSchema, true>>
119
- : Util_FlatObject<EndpointFunctionArgs<Url, QuerySchema, undefined, true>>
120
- : RequestSchema extends ZodType
121
- ? Util_FlatObject<EndpointFunctionArgs<Url, undefined, RequestSchema, true>>
122
- : Util_FlatObject<EndpointFunctionArgs<Url, undefined, undefined, true>>,
123
- ) => Promise<any>, // Returns XmlNode
124
- context: ClassMethodDecoratorContext,
125
- ) => {
126
- if (typeof target !== 'function') {
127
- throw new Error('[Navios] XmlStream decorator can only be used on functions.')
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('[Navios] XmlStream decorator can only be used on methods.')
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
- const endpointMetadata = getEndpointMetadata<BaseXmlStreamConfig>(target, context)
122
+ let endpointMetadata = getEndpointMetadata<BaseEndpointOptions>(
123
+ target,
124
+ context,
125
+ )
136
126
  if (endpointMetadata.config && endpointMetadata.config.url) {
137
- throw new Error(`[Navios] Endpoint ${config.method} ${config.url} already exists.`)
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
- Method extends HttpMethod,
46
- Url extends string,
47
- QuerySchema = undefined,
48
- RequestSchema = undefined,
49
- >(
50
- config: BaseXmlStreamConfig<Method, Url, QuerySchema, RequestSchema>,
51
- ): { config: BaseXmlStreamConfig<Method, Url, QuerySchema, RequestSchema> } {
52
- return { config }
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
  }
@@ -1,4 +1,5 @@
1
- import type { BaseStreamConfig, HttpMethod } from '@navios/builder'
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 BaseStreamConfig<Method, Url, QuerySchema, RequestSchema> {
31
- /** Content-Type header, defaults to 'application/xml' */
32
- contentType?: 'application/xml' | 'text/xml' | 'application/rss+xml' | 'application/atom+xml'
33
- /** Include XML declaration (<?xml version="1.0"?>) - defaults to true */
34
- xmlDeclaration?: boolean
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
  }