# Membership SDK

A software development kit that centralises Membership API calls.

[![CircleCI](https://circleci.com/gh/Financial-Times/n-membership-sdk.svg?style=svg&circle-token=bc2b68593d23ef4700c6932fe6c00289578c7922)](https://circleci.com/gh/Financial-Times/n-membership-sdk)

```javascript
const { ProductionConfiguration, Session } = require("n-membership-sdk");

// Use ProductionConfiguration defaults and set a key
const config = new ProductionConfiguration({
  sessionApiKey: "secret",
});

// Setup and use a service
const sessionSdk = new Session(config);
const sessionInfo = sessionSdk.fetchSessionInfo(sessionId);
```

## Table of Contents

* [Usage](#usage)
	* [Configuration](#configuration)
	* [Service classes](#service-classes)
	* [Models](#models)
* [Testing](#testing)
* [Services](#services)
* [Migration Guides](docs/migrations)

## Usage

### Configuration

The Configuration class provides environment information for services, a Configuration instance needs to be provided when instantiating a service class.

```javascript
const config = new Configuration({
    // Provide environment information
})
```

There are two helper classes extend `Configuration` and come with pre-populated environment information `TestConfiguration` and `ProductionConfiguration`. Non application specific configuration is defaulted (i.e. hosts) leaving only keys to be provided by the developer.

```javascript
const config = new ProductionConfiguration({
    invoiceApiKey: 'secret'
})
```

#### Methods

*   `set` Set the config properties using either a property string or config object
*   `get` Return the value of the property given

### Service classes

- [Authorisation](docs/services/AUTHORISATION.md)
- [B2cPartnershipService](docs/services/B2C-PARTNERSHIP-SERVICE.md)
- [CustomerSSOService](docs/services/CUSTOMER-SSO.md)
- [DeliveryAddress](docs/services/DELIVERY-ADDRESS.md)
- [Invoice](docs/services/INVOICE.md)
- [Licence](docs/services/LICENCE.md)
- [PrintFulfilment](docs/services/PRINT-FULFILMENT.md)
- [PrintInfo](docs/services/PRINT-INFO.md)
- [RedeemableTokenService](docs/services/REDEEMABLE-TOKEN.md)
- [Session](docs/services/SESSION.md)
- [StateManager](docs/services/STATE-MANAGER.md)
- [Subscription](docs/services/SUBSCRIPTION.md)
- [Suspension](docs/services/SUSPENSION-SERVICE.md)
- [Transition](docs/services/TRANSITION.md)
- [User](docs/services/USER.md)

### Models

- [Offer](docs/models/OFFER.md)
- [User](docs/models/USER.md)
- [RedeemableToken](docs/models/REDEEMABLE-TOKEN.md)
- [PersonalisedToken](docs/models/PERSONALISED-TOKEN.md)
- [Licence](docs/models/LICENCE.md)
- [AcquisitionContext](docs/models/ACQUISITION-CONTEXT.md)

### Errors

Exposes all the error types the SDK could throw allowing the developer to test the error and handle things differently.

#### Usage

```javascript
const { Errors } = require('@financial-times/n-membership-sdk');

try {
    const gateway = await subscription.fetchGatewayName(type, country);
} catch (error) {
    if (error instanceof Errors.NotFoundError) {
        // Handle Gateway not being found
    } else {
        // Handle all other errors
    }
}
```

#### Types

```
BadRequestError - HTTP response code 400 received from API
UnauthorizedError - HTTP response code 401 received from API
ForbiddenError - HTTP response code 403 received from API
NotFoundError - HTTP response code 404 received from API
ServerError - HTTP response code in 500 range or unhandled 400 range received from API
InvalidResponseError - Response from the API was invalid
EmptyResultError - No result to return after processing
ValidationError - Incorrect parameter supplied
```

## Services

These are the Membership endpoints that are currently being used by the SDK.

### User

```
HEAD https://api.ft.com/users?email=${email}
POST https://api.ft.com/users/profile
PUT  https://api.ft.com/users/${user.id}/profile
POST https://api.ft.com/login
POST https://api.ft.com/users/${userId}/credentials/change-password
```

### Subscription

```
GET  https://api.ft.com/subscriptions/${subscriptionId}
POST https://api.ft.com/subscriptions/actions/subscribe
GET  https://api.ft.com/paymentpage2/config/${paymentType}/${appId}/${countryCode}
GET  https://api.ft.com/payment-gateway/${paymentType}/${countryCode}
POST https://api.ft.com/paymentpage2/validation/signature
```

### Authorisation

```
POST https://api.ft.com/authorize?client_id=${clientId}&response_type=token
```

### Invoice

```
GET https://api-t.ft.com/invoice/membership/invoices/v1?accountId=${accountId}
```
