# @cocart/cocart-rest-api

> CoCart - JavaScript Library

Latest version **1.1.0** (published 2021-06-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @cocart/cocart-rest-api
pnpm add @cocart/cocart-rest-api
yarn add @cocart/cocart-rest-api
bun add @cocart/cocart-rest-api
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2021-06-03 |
| First published | 2020-11-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=8.0.0 |
| Dependencies | 4 |
| Unpacked size | 32.5 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | Sebastien Dumont |
| Maintainers | seb86 |
| Keywords | wordpress, woocommerce, rest, cocart, promise, node |

## Links

- npm: https://www.npmjs.com/package/@cocart/cocart-rest-api
- Repository: https://github.com/co-cart/cocart-js-lib
- Issues: https://github.com/co-cart/cocart-js-lib/issues
- npm.io page: https://npm.io/package/@cocart/cocart-rest-api

## Dependencies (4)

- [axios](https://npm.io/package/axios.md) ^0.21.0
- [url-parse](https://npm.io/package/url-parse.md) ^1.4.7
- [oauth-1.0a](https://npm.io/package/oauth-1.0a.md) ^2.2.6
- [create-hmac](https://npm.io/package/create-hmac.md) ^1.1.7

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2021-06-03
- 1.0.1 — 2021-03-15
- 1.0.0 — 2020-11-24

## README

<h1 align="center">CoCart - JavaScript Library <a href="https://github.com/co-cart/cocart-js-lib/releases/latest/"><img src="https://img.shields.io/static/v1?goVersion=&message=v1.1.0&label=&color=9a6fc4&style=flat-square"></a></h1>

<p align="center"><img src="https://raw.githubusercontent.com/co-cart/co-cart/master/.github/Logo-1024x534.png.webp" alt="CoCart" /></p>

> 🍴 Forked from WooCommerce JavaScript Library and modified to support CoCart REST API instead. Authentication is optional.

JavaScript library for CoCart, supports CommonJS (CJS) and ECMAScript Modules (ESM).

Requests are made with [Axios library](https://github.com/axios/axios) with [support to promises](https://github.com/axios/axios#promises).

[![build status](https://travis-ci.org/co-cart/cocart-js-lib.svg?branch=master)](https://travis-ci.org/github/co-cart/cocart-js-lib)
[![npm version](https://img.shields.io/npm/v/@cocart/cocart-rest-api.svg)](https://www.npmjs.com/package/@cocart/cocart-rest-api)

> ⚠️This library will **NOT** support the **LEGACY API** of CoCart.

## Installation

```
npm install --save @cocart/cocart-rest-api
```

## Getting started

Check out the CoCart API endpoints and data that can be manipulated in <https://docs.cocart.xyz/>.

## Setup

### ESM example

```js
import CoCartAPI from "@cocart/cocart-rest-api";

const CoCart = new CoCartAPI({
  url: "https://example.com",
});
```

### CJS example

```js
const CoCartAPI = require("@cocart/cocart-rest-api").default;

const CoCart = new CoCartAPI({
  url: "https://example.com",
});
```

### Options

| Option            | Type      | Required | Description                                                                                                                                                                         |
|-------------------|-----------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `url`             | `String`  | yes      | Your Store URL, example: https://example.com/                                                                                                                                       |
| `consumerKey`     | `String`  | no       | Your API consumer key, username or email address.                                                                                                                                   |
| `consumerSecret`  | `String`  | no       | Your API consumer secret or password.                                                                                                                                               |
| `wpAPIPrefix`     | `String`  | no       | Custom WP REST API URL prefix, used to support custom prefixes created with the [rest_url_prefix](https://developer.wordpress.org/reference/functions/rest_get_url_prefix/) filter. |
| `version`         | `String`  | no       | API version, default is `cocart/v2`                                                                                                                                                 |
| `encoding`        | `String`  | no       | Encoding, default is 'utf-8'                                                                                                                                                        |
| `queryStringAuth` | `Bool`    | no       | When `true` and using under HTTPS force Basic Authentication as query string, default is `false`                                                                                    |
| `port`            | `string`  | no       | Provide support for URLs with ports, eg: `8080`                                                                                                                                     |
| `oauth`           | `Bool`    | no       | When `true` authorize via OAuth 1.0a, default is `false`                                                                                                                            |
| `timeout`         | `Integer` | no       | Define the request timeout                                                                                                                                                          |
| `axiosConfig`     | `Object`  | no       | Define the custom [Axios config](https://github.com/axios/axios#request-config), also override this library options.                                                                |

## Methods

### GET

- `.get(endpoint)`
- `.get(endpoint, params)`

| Params     | Type     | Description                                           |
|------------|----------|------------------------------------------------------ |
| `endpoint` | `String` | CoCart API endpoint, example: `cart` or `products/32` |
| `params`   | `Object` | Query strings params, example: `{ per_page: 20 }`     |

### POST

- `.post(endpoint, data)`
- `.post(endpoint, data, params)`

| Params     | Type     | Description                                                  |
|------------|----------|------------------------------------------------------------- |
| `endpoint` | `String` | CoCart API endpoint, example: `cart/add-item` or `cart/item` |
| `data`     | `Object` | JS object to be converted into JSON and sent in the request  |
| `params`   | `Object` | Query strings params                                         |

### PUT

- `.put(endpoint, data)`
- `.putt(endpoint, data, params)`

| Params     | Type     | Description                                                 |
|------------|----------|-------------------------------------------------------------|
| `endpoint` | `String` | CoCart API endpoint, example: `cart/item/<item_key>`        |
| `data`     | `Object` | JS object to be converted into JSON and sent in the request |
| `params`   | `Object` | Query strings params                                        |

### DELETE

- `.delete(endpoint)`
- `.delete(endpoint, params)`

| Params     | Type     | Description                                           |
|------------|----------|------------------------------------------------------ |
| `endpoint` | `String` | CoCart API endpoint, example: `cart/item` or `coupon` |
| `params`   | `Object` | Query strings params, example: `{ force: true }`      |

## Bug Reporting

If you think you have found a bug in the library, please [open a new issue](https://github.com/co-cart/cocart-js-lib/issues/new/choose) and I will do my best to help you out.

## CoCart Channels

We have different channels at your disposal where you can find information about the CoCart project, discuss it and get involved:

[![Twitter: cart_co](https://img.shields.io/twitter/follow/cart_co?style=social)](https://twitter.com/cart_co) [![CoCart JS Lib Github Stars](https://img.shields.io/github/stars/co-cart/cocart-js-lib?style=social)](https://github.com/co-cart/cocart-js-lib)

<ul>
  <li>📖 <strong>Docs</strong>: this is the place to learn how to build amazing sites with CoCart. <a href="https://docs.cocart.xyz/#getting-started">Get started!</a></li>
  <li>👪 <strong>Community</strong>: use our Slack chat room to share any doubts, feedback and meet great people. This is your place too to share <a href="https://cocart.xyz/community/">how are you planning to use CoCart!</a></li>
  <li>🐞 <strong>GitHub</strong>: we use GitHub for bugs and pull requests, doubts are solved with the community.</li>
  <li>🐦 <strong>Social media</strong>: a more informal place to interact with CoCart users, reach out to us on <a href="https://twitter.com/cart_co">Twitter.</a></li>
  <li>💌 <strong>Newsletter</strong>: do you want to receive the latest plugin updates and news? Subscribe <a href="https://twitter.com/cart_co">here.</a></li>
</ul>

## I'm not seeing `X-COCART-API` header returning

If you want this header to return then you need to enable CORS. Simply [install CoCart CORS plugin](https://wordpress.org/plugins/cocart-cors/) for quick activation.

## Changelog

[See changelog for details](https://github.com/co-cart/cocart-js-lib/blob/master/CHANGELOG.md)

## Credits

CoCart is developed and maintained by [Sébastien Dumont](https://github.com/seb86).

---

[sebastiendumont.com](https://sebastiendumont.com) &nbsp;&middot;&nbsp;
GitHub [@seb86](https://github.com/seb86) &nbsp;&middot;&nbsp;
Twitter [@sebd86](https://twitter.com/sebd86)

<p align="center">
    <img src="https://raw.githubusercontent.com/seb86/my-open-source-readme-template/master/a-sebastien-dumont-production.png" width="353">
</p>

---
_Source: https://npm.io/package/@cocart/cocart-rest-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
