npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@carforyou/api-client

v46.4.1

Published

Api client to connect with our APIs

Downloads

6,003

Readme

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.