在节点上启动集成。js快递

本指南将帮助您从头开始在JavaScript中设置最简单的测试集成,以处理通知。 您可以检查您的服务器是否正在处理通知。

您将需要一个节点。js和Java至少是版本11,以及用于检查服务器操作的Curl

提前设置它们。 如何安装:

下载 OpenAPI-规格

  1. 打开要保存规范的文件夹。 在本说明书中,将其称为 <project_directory>.

  2. 在其中运行命令提示符。

  3. 编写命令:

    git clone https://github.com/yandex-market/yandex-market-notification-api.git
    
  1. 下载 Yandex的规格。市场通知服务 GitHub.
  2. 将存档解压缩到要保存规范的文件夹。 在本说明书中,将其称为 <project_directory>.

生成服务器

  1. 打开文件夹 yandex-market-notification-api,这出现在上一步。

  2. 在其中运行命令提示符。

  3. 编写命令:

    npx @openapitools/openapi-generator-cli generate -i <path_to_openapi.yaml> -g nodejs-express-server -o <output_path>
    

作为一个 输出路径 指定项目所在的文件夹。

如果 openapi工具尚未安装,请接受安装。 在文件夹中 输出路径 客户端的文件将出现。

例子:

假设您下载了一个存档并解压缩。 规范在文件夹中 <project_directory>/yandex-market-notification-api.

要将项目放在文件夹中吗 <project_directory>/market-integration.

1. 打开文件夹 <project_directory>/yandex-market-notification-api.

2. 在其中运行命令提示符。

3. 在打开的控制台中,写:

   ```
   npx@openapitools/openapi-generator-cligenerate-iopenapi/openapi.yaml-g javascript-o:no-translate[。./市场整合]
   ```

4. 如果系统提示您安装生成器,请输入 Y 并点击 进入.

实施通知处理

  1. 在IDE中使用生成的代码打开该目录。

  2. service/NotificationService.js 通知处理逻辑所在。 添加一个简单的代码来处理与类型的通知 PING:

    const Service = require('./Service');
    const logger = require('../logger');
    
    const sendNotification = (sendNotificationRequest) => new Promise(
      async (resolve, reject) => {
        try {
          if (sendNotificationRequest.body.notificationType == 'PING') {
            logger.info("PING notification processed")
          }
          // Возвращаем обязательное тело ответа
          resolve(Service.successResponse({
            name: "shop",
            time: new Date().toISOString(),
            version: "1.0.0"
          }));
        } catch (e) {
          reject(Service.rejectResponse(
            e.message || 'Invalid input',
            e.status || 405,
          ));
        }
      },
    );
    
    module.exports = {
      sendNotification,
    };
    
  3. 写入控制台 node index.js. 服务器将启动。

  4. 在单独的终端窗口中,向服务器发出测试请求:

    curl -X POST -L 'http://localhost:8080/notification' -H 'Content-Type: application/json' -d '{"notificationType": "PING", "time": "2025-01-01T00:00:00.000Z"}'
    
  5. 在回应中你会看到 {"name":"shop","time":"2025-02-28t12:32:01.918Z","version":"1.0.0"},并在服务器记录消息 PING notification processed.