// AUTO-GENERATED — do not edit by hand
// Upstream: https://github.com/n8n-io/n8n/blob/master/packages/nodes-base/nodes/Npm/Npm.node.ts
// To implement: copy to ../impl/npm.rs, fill in execute(), add to ../impl/mod.rs
// r8r: rust port of n8n.io
// License: Unlicense

use async_trait::async_trait;
use crate::core::error::NodeError;
use crate::core::types::*;
use crate::engine::{ExecuteCtx, Node, NodeFactory};
use std::sync::Arc;

pub struct NpmNode;

static META: NodeMetadata = NodeMetadata {
    name: "npm",
    display_name: "Npm",
    version: &[1],
    group: &["transform"],
    description: "Consume NPM registry API",
    inputs: 1,
    outputs: 1,
    properties: &[
    PropertyDef { name: "resource", display_name: "Resource", kind: PropertyKind::Options, required: false, default: Some("package") }
],
    credentials: &[
    CredentialRef { name: "npmApi", required: false }
],
};

#[async_trait]
impl Node for NpmNode {
    fn metadata(&self) -> &'static NodeMetadata { &META }

    async fn execute(&self, _ctx: &mut ExecuteCtx<'_>) -> Result<NodeOutput, NodeError> {
    static ROUTING: crate::engine::routing::RoutingDescriptor =
        crate::engine::routing::RoutingDescriptor {
            base_url: "={{ $credentials.registryUrl }}",
            credential: Some("npmApi"),
            auth_headers: &[("Authorization", "Bearer {{accessToken}}")],
            auth_query: &[],
            dispatch_param: "operation",
            operations: &[
                crate::engine::routing::OperationDescriptor {
                    value: "getMany", method: "GET", url_template: "/-/package/{{ encodeURIComponent($parameter.packageName) }}/dist-tags",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "update", method: "PUT", url_template: "/-/package/{{ encodeURIComponent($parameter.packageName) }}/dist-tags/{{ encodeURIComponent($parameter.distTagName) }}",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "getMetadata", method: "GET", url_template: "/{{ encodeURIComponent($parameter.packageName) }}/{{ $parameter.packageVersion }}",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "getVersions", method: "GET", url_template: "/{{ encodeURIComponent($parameter.packageName) }}",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "search", method: "GET", url_template: "/-/v1/search",
                    body_template: "", query: &[],
                }
            ],
            sends: &[],
        };
    crate::engine::routing::execute_routing(&ROUTING, _ctx).await
    }
}

inventory::submit! { NodeFactory::new("npm", || Arc::new(NpmNode)) }
