# strapi-client

> An http client to easily access Strapi.io API.

Latest version **1.0.0** (published 2019-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install strapi-client
pnpm add strapi-client
yarn add strapi-client
bun add strapi-client
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-11-14 |
| First published | 2019-11-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| Author | Jhon Andrew Q. Baes |
| Maintainers | jhon-andrew |

## Links

- npm: https://www.npmjs.com/package/strapi-client
- npm.io page: https://npm.io/package/strapi-client

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.19.0

## Recent versions

- 1.0.0 (latest) — 2019-11-14

## README

strapi-client
===

An http client to easily access Strapi.io API.

---
---

## Usage

---

### Create an instance

```js
const StrapiClient = require('strapi-client')
const strapi = new StrapiClient('http://localhost:1337')
```

---

### Login to the API

`login(identifier, password)`

```js
const { jwt, user } = await strapi.login('uname', 'pword')
console.log('Token:', jwt)
console.log('User:', user)
```

If the login succeeds, the token will automatically be applied to the `Authorization` header.

---

### Get currently authenticated user

`currentUser(token)`

```js
const user = await strapi.currentUser()
```

`token` is optional. If a user has recently logged in, the token is automatically applied to the `Authorization` header.

Persistence of the `token` should be managed by the developer. To reauthenticate a user, just pass the token to the `currentUser()` method.

```js
const user = await strapi.currentUser('efghijk1234567...')
```

---

### Register an account

`register(data)`

```js
let newUser = {
  email: 'email@email.com',
  username: 'uname',
  password: 'pword'
}

const { jwt, user } = strapi.register(newUser)

console.log('Token:', jwt)
console.log('User:', user)
```

---

### Count the entries of a content-type

`count(contentType, query)`

```js
const numberOfUsers = await strapi.count('users')
```

`query` is optional. It accepts an object that is passed as HTTP params along with the `GET` request. See Strapi's documentation about [Parameters](https://strapi.io/documentation/3.0.0-beta.x/content-api/parameters.html).

---

### Create an entry

`create(contentType, data)`

```js
const article = await strapi.create('articles', {
  title: 'Welcome to Strapi Client!',
  content: 'Strapi-client helps us do CRUD easily.'
})
```

Returns the created entry.

---

### Get a list or a specific entry

`get(contentType, id | query)`

```js
// Get a specific user
const user = await strapi.get('users', 2)

// Get a list of users
const users = await strapi.get('users')
```

`query` is optional. See the definition of this argument above. If a `number` is passed instead of an `object`, the method will return an object containing the entry of the given `ID`. Otherwise, will return an `array` of entries of the content-type.

---

### Update an entry

`update(contentType, id, updatedData)`

```js
const updatedArticle = await strapi.update('articles', 12, {
  title: 'Strapi Client - CRUD made simple!'
})
```

Returns the updated entry.

---

### Delete an entry

`delete(contentType, id)`

```js
const deletedArticle = await strapi.delete('articles', 12)
```

Returns the deleted entry.

---
---

## Note:

* All method returns a `Promise.reject` if a server or connectivity issue occurs.
* If the api fails to perform a request, it will return an object containing the error information.

---
---

## TODO:

* Improve the documentation.
* Add other necessary methods.
* Find support and contributions. :P

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