loopback4 休息连接器 多次呼叫

问题描述 投票:0回答:1

我在Loopback4中使用了Rest Connector,但是你如何扩展这个datasource,让它有选项来调用不止一个url,并且有不止一个函数?

这是我的ic.datasource.config.json文件。

{
  "name": "ic",
  "connector": "rest",
  "debug": "false",
  "options": {
    "headers": {
      "accept": "application/json",
      "content-type": "application/json",
      "authorization": "Basic 64 Encoded credentials"
    }
  },
  "operations": [{
    "template": {
      "method": "GET",
      "url": "https://webapi.ic.com/customers/71/jobs/{id}    
    },
    "functions": {
      "getDetails": ["id"]
    }
  }]
}

这是我的ic.service.ts文件。

import { getService, juggler } from '@loopback/service-proxy';
import { inject, Provider } from '@loopback/core';
import { icDataSource } from '../datasources/ic.datasource';

export interface icResponseData {
  id: string;
  title: string;
  manager: string;
  overview: string;
}

export interface icService {
  getDetails(id?: number): Promise<icResponseData>;
}

export class icProvider implements Provider<icService> {
  constructor(
  // ic must match the name property in the datasource json file
  @inject('datasources.ic')
  protected dataSource: juggler.DataSource = new icDataSource(),
 ) {}

 value(): Promise<icService> {
   return getService(this.dataSource);
 }
}
loopback4
1个回答
0
投票

我想明白了,我的ic.datasource....

这是我的ic.datasource.config.json文件。

{
  "name": "ic",
  "connector": "rest",
  "debug": "false",
  "options": {
    "headers": {
      "accept": "application/json",
      "content-type": "application/json",
      "authorization": "Basic 64 Encoded credentials"
    }
  },
  "operations": [{
    "template": {
      "method": "GET",
      "url": "https://webapi.ic.com/customers/71/search/{id}    
    },
    "functions": {
      "getAll": ["id"]
    }
  },
    {
      "template": {
        "method": "GET",
        "url": "https://webapi.ics.com/customers/71/jobs/{id}"
      },
      "functions": {
        "getJobDetails": ["id"]
      }
    }]
}

这是我的ic.service.ts文件。

import { getService, juggler } from '@loopback/service-proxy';
import { inject, Provider } from '@loopback/core';
import { icDataSource } from '../datasources/ic.datasource';

export interface icResponseData {
  id: string;
  title: string;
  manager: string;
  overview: string;
}

export interface icimsJobIDsResponseData {
  portalUrl: string;
  id: number;
  updateDate: string;
}

export interface icimsService {
  getAll(portalid?: number): Promise<icAllResponseData>;
}

export interface icService {
  getDetails(id?: number): Promise<icResponseData>;
}

export class icProvider implements Provider<icService> {
  constructor(
  // ic must match the name property in the datasource json file
  @inject('datasources.ic')
  protected dataSource: juggler.DataSource = new icDataSource(),
 ) {}

 value(): Promise<icService> {
   return getService(this.dataSource);
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.