6.10.0 • Published 16 days ago

@serato/sws-sdk v6.10.0

Weekly downloads
246
License
ISC
Repository
github
Last release
16 days ago

Serato Web Services SDK for JavaScript

A Javascript library for interacting with Serato Web Services.

Supports endpoints that use either JWT-based Bearer authorisation or Basic authorisation.

Consists of an Sws configuration class and mutiple service clients. A service client interacts with a specific SWS service. Service clients are exposed as properties of an Sws instance.

Service client have methods for interacting with the corresponding web service. Service client methods return a Promise which resolve to a data object containing the response from the web service.

SDK v6 has some major breaking changes from v5. Read the changelist for details.

Basic usage

Basic usage consists of creating an Sws instance then calling a method on a service client:

import { Sws } from '@serato/sws-sdk'

// `secret` is optional and only required when interacting with API endpoints that require Basic authentication
const sws = new Sws({ appId: 'myAppId', secret: 'myAppSecret' })

// Provided an access token for the current user (required for endpoints that use `Bearer` authorisation)
sws.accessToken = 'myAccessToken'

// Use the License service client to get the current user's licenses (all service methods return a Promise)
sws.license.getLicenses().then(
  (data) => {
    // `data` is a plain object of the API response
    console.log('Success')
  },
  (err) => {
    // `err` contains request and response details
    console.log('Failure')
  }
)

Configuration

The Sws object is used to configure the behavior of the service clients. A configuration object can be provided to the constructor:

import { Sws } from '@serato/sws-sdk'

const sws = new Sws({
  appId: 'myAppId',
  secret: 'myAppSecret', // Only required for SWS endpoints that require `Basic` authentication
  timeout: 3000, // Defaults to 3000 (ms)
  serviceUri: { // Base URIs for SWS services (defaults to production endpoints)
    id: 'my.id-service.uri',
    license: 'my.license-service.uri',
    ecom: 'my.ecom-service.uri',
    profile: 'my.profile-service.uri',
    da: 'my.da-service.uri',
    notifications: 'my.notifications-service.uri',
    rewards: : 'my.rewards-service.uri'
  }
})

Service clients

All service clients are exposed as properties of a Sws instance.

import { Sws } from '@serato/sws-sdk'

const sws = new Sws()

sws.id              // IdentityService instance
sws.license         // LicenseService instance
sws.ecom            // EcomService instance
sws.notifications   // NotificationsService instance
sws.notificationsV1 // NotificationsV1Service instance
sws.profile         // ProfileService instance
sws.da              // DigitalAssetsService instance
sws.rewards         // RewardsService instance
sws.cloudlib        // CloudLibraryService instance

Custom Callbacks

The default behavior for all service clients is raise an error for any API call that does not return a HTTP 2xx response. This can be handled by a client application's Promise reject callback.

But clients allow for the providing of custom callbacks to handle common error scenarios that may occur across all web services.

When a custom callback is provided an error is not raised and instead the Promise is resolved with the result of the custom callback.

Invalid Access Token callback

This callback is called when an SWS service indicates that an Access token has either expired or is otherwise invalid.

Invalid Refresh Token callback

This callback is called when the SWS Identity service indicates that a Refresh token has either expired or is otherwise invalid.

Password Re-entry Required callback

This callback is called when the SWS Identity service indicates that a client application must direct the user back to Identity service to re-enter their password.

Access Denied callback

This callback is called when an SWS service indicates that a user has insufficient permissions to access the requested resource.

Service Error callback

This callback is called when an SWS service returns a HTTP 500 Application Error response.

Service Unavailable callback

This callback is called when an SWS service returns a HTTP 503 Service Unavailable response.

Request Timeout Exceeded callback

This callback is called when an SWS service does not respond within the specified timeout period.

Setting callbacks

Callbacks can be attached to all service clients via methods of the Sws instance, or to individual service clients:

const sws = new Sws({ appId: 'myAppId' })

const invalidAccessTokenCallback = (request, err) => {
  // `request` is the request object that returned the error
  // `err` is the error response that was returned from the API call
  return 'Access token is invalid'
}
const invalidRefreshTokenCallback = (request, err) => {
  return 'Refresh token is invalid'
}
const reEnterPasswordCallback = (request, err) => {
  return 'Password must be re-entered'
}
const accessDeniedCallback = (request, err) => {
  return 'Access is denied'
}
const serviceErrorCallback = (request, err) => {
  return 'Service error'
}
const serviceUnavailableCallback = (request, err) => {
  return 'Service is unavailable'
}
const requestTimeoutExceededCallback = (request, err) => {
  return 'Request timeout exceeded'
}

// Attach the callback to all service clients
sws.setInvalidAccessTokenHandler(invalidAccessTokenCallback)
sws.setInvalidRefreshTokenHandler(invalidRefreshTokenCallback)
sws.setPasswordReEntryRequiredHandler(reEnterPasswordCallback)
sws.setAccessDeniedHandler(accessDeniedCallback)
sws.setServiceErrorHandler(serviceErrorCallback)
sws.setServiceUnavailableHandler(serviceUnavailableCallback)
sws.setTimesoutExceededHandler(requestTimeoutExceededCallback)

// Attach the callback to an individual service client
sws.license.invalidAccessTokenHandler = invalidAccessTokenCallback
sws.license.invalidRefreshTokenHandler = invalidRefreshTokenCallback
sws.license.passwordReEntryRequiredHandler = reEnterPasswordCallback
sws.license.accessDeniedHandler = accessDeniedCallback
sws.license.serviceErrorHandler = serviceErrorCallback
sws.license.serviceUnavailableHandler = serviceUnavailableCallback
sws.license.timeoutExceededHandler = requestTimeoutExceededCallback

SwsClient class

The SDK also includes a SwsClient class.

The class extends Sws and provides some useful functionality when using the SDK in web browser-based applications.

Create authorization request

The SwsClient.createAuthorizationRequest method returns an object that an application can use to start an authorization workflow using the SWS Identity Service.

The workflow is an implementation of Authorization Code Flow with Proof Key for Code Exchange (PKCE).

// SwsClient is the default export
import SwsClient from '@serato/sws-sdk'

const sws = new SwsClient({ appId: 'myAppId' })

// After authorization redirect back to this URL
const redirectUrl = window.location.href
const { state, codeVerifier, url } = sws.createAuthorizationRequest(redirectUrl)

// Store `state` and `codeVerifier` in a storage mechanism (eg localstorage, sessionstorage)
// for use after redirecting back into the application after the authorization process.

// Redirect the browser to the authorization URL
window.location.href = url

After the authorization process the SWS Identity Service will return the browser to redirectUrl with state and code URL parameters appended.

Compare the state URL parameter with the stored state value to confirm the authenticity of authorization request.

Use the IdentityService.tokenExchange method to exchange code, redirectUrl and codeVerifier for JWT access and refresh tokens.

Invalid Access Token handler

SwsClient sets a Invalid Access Token handler that:

  • Retrieves a new Access Token from the Identity Service and updates the Sws instance with it.
  • Provides a Access Token Updated callback that is called whenever a new Access Token is successfully received from the Identity Service.

Access Token Updated callback

The Access Token Updated token callback accepts two arguments: the value of the Access token, and a date object representing the expiry time of the access token.

// SwsClient is the default export
import SwsClient from '@serato/sws-sdk'

const sws = new SwsClient({ appId: 'myAppId' })

const accessTokenUpdatedCallback = (accessToken, accessTokenExp, refreshToken, refreshTokenExp) => {
  console.log('Access token value is ' + accessToken)
  console.log('Access token expires on ' + accessTokenExp.toISOString())
  console.log('Refresh token value is ' + refreshToken)
  console.log('Refresh token expires on ' + refreshTokenExp.toISOString())
}

// Attach the callback
sws.accessTokenUpdatedHandler = accessTokenUpdatedCallback
6.10.0

16 days ago

6.9.0

28 days ago

6.8.0-alpha-2

2 months ago

6.8.0-alpha-3

1 month ago

6.8.0-alpha-1

2 months ago

6.8.0

4 months ago

6.7.0

4 months ago

6.6.3

8 months ago

6.5.3

9 months ago

6.5.0

1 year ago

6.5.2

1 year ago

6.4.2

1 year ago

6.3.0

1 year ago

6.4.1

1 year ago

6.4.0

1 year ago

6.2.3

2 years ago

6.2.6

1 year ago

6.2.1

2 years ago

6.1.1

2 years ago

6.2.2

2 years ago

5.0.2

2 years ago

6.0.0

2 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.0.5

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.4

3 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.3

4 years ago

2.1.1

4 years ago

2.0.0

4 years ago

1.10.2

4 years ago

1.10.1

4 years ago

1.10.0

4 years ago

1.9.1

4 years ago

1.8.1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.1

6 years ago