# @opensrp/server-service

> Wrapper around OpenSRP Server REST API

Latest version **0.0.17** (published 2021-03-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @opensrp/server-service
pnpm add @opensrp/server-service
yarn add @opensrp/server-service
bun add @opensrp/server-service
```

## Health

**Score 35/100 (D)** — status: abandoned.

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.0.17 |
| Published | 2021-03-01 |
| First published | 2020-03-12 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 36.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 33 |
| Author | Ona Engineering |
| Maintainers | opensrp-admin |

## Links

- npm: https://www.npmjs.com/package/@opensrp/server-service
- Repository: https://github.com/OpenSRP/web
- Homepage: https://github.com/OpenSRP/web#readme
- Issues: https://github.com/OpenSRP/web/issues
- npm.io page: https://npm.io/package/@opensrp/server-service

## Dependencies (1)

- [@onaio/utils](https://npm.io/package/@onaio/utils.md) ^0.0.1

## Recent versions

- 0.0.17 (latest) — 2021-03-01
- 0.0.16 — 2021-02-24
- 0.0.15 — 2021-02-23
- 0.0.14 — 2021-02-18
- 0.0.13 — 2021-01-15
- 0.0.12 — 2020-11-12
- 0.0.11 — 2020-11-05
- 0.0.10 — 2020-11-02
- 0.0.8 — 2020-10-29
- 0.0.7 — 2020-10-28
- 0.0.6 — 2020-10-27
- 0.0.4 — 2020-06-24
- 0.0.2 — 2020-06-08
- 0.0.1 — 2020-03-12

## README

# OpenSRP Service

Exposes an api that abstracts making CRUD requests to a server. This module was built for the openSRPService but is
generic enough to be used on most apis. It exposes the below methods.

- **create** - post a data object to an api.
- **read** - get a single data object using its unique id
- **update** - Modify an entity on the api
- **list** - get many entities from specified entry point
- **delete** - make a `DELETE` request to the api

## Installation

```node
yarn add @opensrp/server-service
```

## Usage

`OpenSRPService` makes use of the following options

- **accessTokenOrCallBack:**(string | function)
  - **required**
  - an async call back function that returns access token
- **baseURL:**(string)
  - **required**
  - uri of the api
- **endpoint:**(string)
  - **required**
  - the endpoint.
- **signal:**(AbortSignal)
  - optional
  - optionally used to cancel pending requests.
- **getOptions:**(Function)
  - optional
  - A function thats given the signal and method to use. Used to add custom options that you might want to pass on tho the fetch request

### Errors

OpenSRP exposes 2 additional error classes:

#### HTTPError

This is thrown when the statusCode is not within the allowed success range for any respective operation.

The fetch.response.text() value from the api is attached to the error as the value of `description`.

Also attaches the following fields from the response to the thrown error object: - statusCode - statusText - url

#### NetworkError

This is thrown when the fetch error cannot go through due to a failed internet connection.

### Code example

```typescript
import OpenSRPService from '@opensrp/server-service';

function getFetchOptions(
  signal: AbortSignal,
  method: HTTPMethod
): { headers: HeadersInit; method: HTTPMethod } {
  return {
    headers: {
      /**... */
    },
    method,
    signal,
  };
}

const loadClients = async () => {
  const clientService = new openSRPService(
    OPENSRP_API_BASE_URL,
    OPENSRP_CLIENT_ENDPOINT,
    getFetchOptions
  );
  return await clientService.list();
};
```

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