npm

@carforyou/api-client
TypeScript icon, indicating that this package has built-in type declarations

46.4.1 • Public • Published

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 Name Meaning
host URL to the API gateway
debug Set 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.

Readme

Keywords

none

Package Sidebar

Install

npm i @carforyou/api-client

Weekly Downloads

925

Version

46.4.1

License

MIT

Unpacked Size

1.8 MB

Total Files

867

Last publish

Collaborators

  • carforyou-engineering
  • lkappeler