# @janiscommerce/api-session

> A session manager for APIs

Latest version **3.4.0** (published 2023-03-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install @janiscommerce/api-session
pnpm add @janiscommerce/api-session
yarn add @janiscommerce/api-session
bun add @janiscommerce/api-session
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.4.0 |
| Published | 2023-03-20 |
| First published | 2019-09-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 22.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Janis |
| Maintainers | janiscommerce |

## Links

- npm: https://www.npmjs.com/package/@janiscommerce/api-session
- Repository: https://github.com/janis-commerce/api-session
- Homepage: https://github.com/janis-commerce/api-session.git#readme
- Issues: https://github.com/janis-commerce/api-session/issues
- npm.io page: https://npm.io/package/@janiscommerce/api-session

## Dependencies (1)

- [lllog](https://npm.io/package/lllog.md) ^1.1.2

## Recent versions

- 3.4.0 (latest) — 2023-03-20
- 3.1.1-typed.0 (typed) — 2021-01-25
- 3.3.1 — 2022-03-15
- 3.3.0 — 2022-03-11
- 3.2.0 — 2021-11-18
- 3.1.1 — 2021-01-27
- 3.1.0 — 2020-12-17
- 3.0.0 — 2020-08-27
- 2.0.0 — 2020-06-11
- 1.4.0 — 2020-05-19
- 1.3.1 — 2020-03-25
- 1.3.0 — 2020-01-21
- 1.2.1 — 2019-10-16
- 1.2.0 — 2019-10-01
- 1.1.0 — 2019-10-01
- … 1 more at https://npm.io/package/@janiscommerce/api-session/versions

## README

# api-session

![Build Status](https://github.com/janis-commerce/api-session/workflows/Build%20Status/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/api-session/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/api-session?branch=master)
[![npm version](https://badge.fury.io/js/%40janiscommerce%2Fapi-session.svg)](https://www.npmjs.com/package/@janiscommerce/api-session)


A session manager for APIs

## 📦 Installation
```sh
npm install @janiscommerce/api-session
```

## :gear: API
The package exports two classes ApiSession and ApiSessionError.

### `constructor(authorizationData, client)`

Creates an APISession with the `authorizationData` provided or the `client` for direct injection.

#### Parameters

- `authorizationData` is an **optional** _object_ with the following (also optional) properties: { userId, clientId, clientCode, profileId, permissions, hasAccessToAllLocations, locations, warehousesIds }
- `client` is an **optional** _object_ for client injection without performing any DB gets

### `validateLocation(locationId)`

Validate if the location given is valid for the session. It validates against the `locations` and the `hasAccessToAllLocations` boolean.
Returns *Boolean*.

### `validateWarehouse(warehouseId)`. _Since 3.3.0_

Validate if the warehouse given is valid for the session. It validates against the `warehousesIds` and the `hasAccessToAllLocations` boolean.
Returns *Boolean*.

### APISession getters

ApiSession has the following getters:

* userId {string} The ID of the user or undefined in case there is no user
* userIsDev {boolean} If user is dev
* serviceName {string} The name of the service or undefined in case there is no service
* isService {boolean} If session is associated to a service
* clientId {string} The ID of the client or undefined in case there is no client
* clientCode {string} The code of the client or undefined in case there is no client
* currency {string|undefined} The currency of the client or undefined in case there is no client nor currency related. _Since 3.4.0_
* currencyDisplay {string} The currency display of the client or default value in case there is no client. Possible values: `code`, `symbol`. Default: `symbol`. _Since 3.4.0_
* profileId {string} The ID of the profile or undefined in case there is no profile
* hasAccessToAllLocations {boolean} If has access to all locations
* locations {array<string>} The List of Locations to which the user has permissions
* warehousesIds {array<string>} The List of Warehouses to which the user has permissions. _Since 3.3.0_
* permissions {array} The permission keys or undefined in case there are no permissions associated
* *async* client {object} Resolves to the client object with the `getInstance()` method injected. The properties depend on your client internal structure. The client is injected with a `getInstance()` method to propagate the session to other instances.

## Model Client
The package uses the Client Model in our service for getting the clients. For more information see [@janiscommerce/model](https://www.npmjs.com/package/@janiscommerce/model)

## Usage
```js
const { ApiSession, ApiSessionError } = require('@janiscommerce/api-session');
```

## Examples
```js
const { ApiSession } = require('@janiscommerce/api-session');

const SomeModel = require('../models/some-model');

const session = new ApiSession({
	userId: 1,
	userIsDev: false,
	clientId: 2,
	clientCode: 'janis',
	profileId: 5,
	permissions: [
		'catalog:product:read',
		'catalog:product:write'
	],
	locations: ['location-1'],
	hasAccessToAllLocations: false
});

console.log(`Session created for user ${session.userId} on client ${session.clientCode}.`);

const sessionInjectedModel = session.getSessionInstance(SomeModel, 'some-param', 'some-other-param');

console.log(`Session is propagated for user ${sessionInjectedModel.session.userId} on client ${sessionInjectedModel.session.clientCode}.`);

const client = await sessionInjectedModel.session.client;

console.log(client);
// Outputs your client object

const hasAccess = session.validateLocation('location-1');

console.log(`Session has access to location 1: ${hasAccess}`);
// Outputs 'Session has access to location 1: true'
```

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