@carforyou/api-client
v46.4.1
Published
Api client to connect with our APIs
Downloads
6,003
Readme
CAR FOR YOU API Client
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
fetchFrameNumberTypes
- it will get type Ids from vin search
fetchFrameNumberOptions
fetchProductionYearByFrameNumber
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:
typecasting is only needed in TypeScript projects(fetchListing as jest.Mock).mockReturnValue(`<your mocked data>`)
- 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 valuesListingFromType
- initializes an empty listing with values from a typeSearchMessageLeadFactory
- builds lead email listingSearchCallLeadFactory
- 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.