@carforyou/api-client

中文文档
46.4.1发布于 2022/03/21MITTypeScriptESM + CJS零依赖近 30 天下载 1.2万

Api client to connect with our APIs

此软件包已被标记为弃用

Platform was closed down and no longer maintained. Check @smg-automotive/api-client-pkg as replacement

原始使用文档
文档目录19 节
  1. Usage
  2. Configuration
  3. Mocking API calls during development
  4. Authenticated Requests
  5. Following API calls are handled
  6. CarForYou service
  7. Options service
  8. Inventory search service
  9. Catalogue service
  10. Dealer service
  11. Email delivery service
  12. User notification service
  13. Reporting service
  14. Car sale tracking service
  15. Buyer service
  16. Mocking in tests
  17. Factories
  18. Development
  19. Release a new version

CAR FOR YOU API Client

semantic-release

Usage

npm install @carforyou/api-client

Initialize the API client:

import { ApiClient } from "@carforyou/api-client"

ApiClient.configure(<your_configuration_object>)

Import the fetchXYZ call you need and use in your code:

import { fetchReferenceData } from "@carforyou/api-client"

fetchReferenceData()

Configuration

Option NameMeaning
hostURL to the API gateway
debugSet to true to console.log requests and API responses.

Mocking API calls during development

You can pass a custom host with the API calls options object.

Backend offers stub APIs for each service, which you can use:

import { fetchListings } from "@carforyou/api-client"

fetchListings({ host: "https://inventory-search-service-stub.dev.carforyou.ch" })

If you want to provide your own mock data, serve a mock json file locally with json-server.

import { fetchListings } from "@carforyou/api-client"

fetchListings({ host: "http://localhost:3001" })

Authenticated Requests

Requests that need authorization, need to pass a valid JWT to authenticate the user on the requested resource.

Implementing authenticated requests

To authenticate a request, simply pass the isAuthorizedRequest option to the request helper (fetch/post/put/delete-Data) and the api client will add the Authorization header. For server side requests, don't forget to pass the options down to the helper function so the consumer can pass the accessToken when invoking the request.

Note: If one forgets to add the isAuthorizedRequest option, the access token will not be set as a header. On the consumer side, if the access token is not passed, the api client will throw an error.

Performing authenticated requests

Pass the access token as a request option to the api call dummyFetchCall(data, {accessToken: JWT})

Refreshing Access Tokens

The consumer is responsible to ensure a valid token is passed to the request. The api client will pass the provided token as an Authorization header to the api call.

Following API calls are handled

Also accompanying modes and param types, as well as default values, are exported.

CarForYou service

Options service

Inventory search service

Catalogue service

Dealer service

Email delivery service

User notification service

Reporting service

Car sale tracking service

Buyer service

Mocking in tests

To be able to mock api calls in tests you need to:

  • require the API call to be mocked in your test file:
    import { fetchListing } from "@carforyou/api-client"
    
  • use jest mocking to mock the module:
    jest.mock("@carforyou/api-client", () => ({
      // Add this if you want to have access to other exported methods
      ...jest.requireActual("@carforyou/api-client"),
      fetchListing: jest.fn(),
    }))
    
  • you can now set up the mock per test basis:
      (fetchListing as jest.Mock).mockReturnValue(`<your mocked data>`)
    
    typecasting is only needed in TypeScript projects
  • and expect on the mocked function:
    expect(fetchListing).toHaveBeenCalled()
    

Be aware that this creates a global mock in your tests. You'd need to clear mock state in beforeEach:

  (fetchListing as jest.Mock).mockClear()

Factories

Following factories are exported:

  • TypeFactory
  • SearchTypeFactory
  • OptionsFactory
  • ListingFactory
  • SearchListingFactory
  • EmptyListing - builds a listing without any values
  • ListingFromType - initializes an empty listing with values from a type
  • SearchMessageLeadFactory - builds lead email listing
  • SearchCallLeadFactory - builds lead call listing

Development

npm run build

You can link your local npm package to integrate it with any local project:

cd carforyou-api-client-pkg
npm run build

cd carforyou-listings-web
npm link ../carforyou-api-client-pkg

Release a new version

New versions are released on the ci using semantic-release as soon as you merge into master. Please make sure your merge commit message adheres to the corresponding conventions.

版本与发布记录

@carforyou/api-client 共发布 917 个版本,首次发布于 2019/09/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
46.4.1latest2022/03/214 年前
46.4.1-vstx-11-db166a8a042082ba4178f672df4c0643d77e1f9f.22022/03/214 年前
46.4.1-vstx-11-db166a8a042082ba4178f672df4c0643d77e1f9f.12022/03/184 年前
46.4.1-vstx-11-43364af886b78df8a13eb82d9fd81b4753265566.12022/03/184 年前
46.4.02022/03/034 年前
46.4.0-car9676-04228354774a9983d6c653ee8fec6bb84f9c00d7.12022/03/034 年前

其他发布通道:next → 9.1.0-1,car3592 → 9.2.1-car3592.3,car3278 → 9.2.2-car3278.1,CAR-3715-add-profile-endpoint → 10.1.0-CAR-3715-add-profile-endpoint.2,form-typings → 10.1.1-form-typings-a63df13464373cc51e017cdefaa88c57ab82ec8b.1,add-helper-functions → 10.2.0-add-helper-functions-ce3575c70e2008faeef70fb0041518e8a0d73773.1。

关于 @carforyou/api-client 的常见问题

@carforyou/api-client 是什么?

Api client to connect with our APIs注意:该包已被作者标记为弃用——Platform was closed down and no longer maintained. Check @smg-automotive/api-client-pkg as replacement

如何安装 @carforyou/api-client?

在项目目录运行 npm install @carforyou/api-client,或使用 pnpm add / yarn add / bun add。

@carforyou/api-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@carforyou/api-client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@carforyou/api-client 可以商用吗?

@carforyou/api-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@carforyou/api-client 的下载量有多少?

根据 npm 官方下载统计,@carforyou/api-client 在 2026/10/09 – 2026/11/07 这 30 天内被下载 12,259 次,日均约 409 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@carforyou/api-client 还在维护吗?

最新版本 46.4.1 发布于 2022/03/21(4 年前);至今共发布 917 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/09/20。作者已将其标记为弃用,新项目建议寻找替代方案。