Allthings Node/Javascript SDK

Contents
Installation & Usage
yarn add @allthings/sdk
const allthings = require('@allthings/sdk')
const client = allthings.restClient({
accessToken: '043dab7447450772example1214b552838003522',
})
client
.getCurrentUser()
.then((viewer) => console.log(`Welcome back ${viewer.username}!`))
Configuration
Request logs
If you want to see in your logs which url has been called during it's runtime,
you can enable them by providing LOG_REQUEST
as an environment variable where the
value can be anything which is truthy.
Configuration Options
The available configuration options are outlined here:
accessToken | | API Access Token |
clientId | | OAuth 2.0 clientId |
clientSecret | | OAuth 2.0 client secret |
username | | Username to use with OAuth 2.0 Password Grant authentication flow |
password | | Password to use with OAuth 2.0 Password Grant authentication flow |
concurrency | | Number of concurrent requests to perform in parallel. Default behavior is burst of 30/s, 1/s thereafter |
apiUrl | | Base API url to use. Defaults to https://api.allthings.me/, respects value of the ALLTHINGS_REST_API_URL environment variable |
Authentication
@TODO
process.env.ALLTHINGS_OAUTH_CLIENT_ID
process.env.ALLTHINGS_OAUTH_CLIENT_SECRET,
process.env.ALLTHINGS_OAUTH_PASSWORD,
process.env.ALLTHINGS_OAUTH_USERNAME,
OAuth Implicit Grant Example
@TODO
const allthings = require('@allthings/sdk')
const client = allthings.restClient({
accessToken: '043dab7447450772example1214b552838003522',
})
client
.getCurrentUser()
.then((viewer) => console.log(`Welcome back ${viewer.username}!`))
OAuth Authorization Code Grant Example
- Initialize instance of
client
:
const allthings = require('@allthings/sdk')
const client = allthings.restClient({
clientId: '5d038ef2441f4de574005c54_example',
clientSecret: '40f63f981ff082dbc8d273983ac3852c2e51e90856123156',
redirectUri: 'https://example-app.com/callback',
})
- Construct a URI to send authorization request to using a
state
which should be unique per request and hard to guess. It can be generated with client.oauth.generateState()
method:
const state = client.oauth.generateState()
const authorizationUri = client.oauth.authorizationCode.getUri(state)
-
Direct user's browser to the constructed URI.
-
When user completes authentication process, he is redirected to the redirectUri
having code
and state
query string arguments, e.g.:
https://example-app.com/callback?code=ebc110bee11b2829&state=k1bt3c1d0vnfu7qk
At this point state
must be validated - if it doesn't match the one generated on step 2, such request is probably malicious and should be aborted.
- Use the code extracted from query parameters on the previous step to obtain an access token:
await client.oauth.authorizationCode.requestToken(code)
- Client is ready to make API requests:
const user = await client.getCurrentUser()
API
Allthings SDK module
restClient(configurationOptions?): Client
Create an client instance of the SDK.
const allthings = require('@allthings/sdk')
const client = allthings.restClient(configurationOptions)
client.createAgent()
Create a new agent. This is a convenience function around creating a user and adding that user to a property-manager's team.
const appId = '575027e58178f56a008b4568'
const propertyManagerId = '5a818c07ef5f2f00441146a2'
const username = 'mr.example@allthings.test'
const agent = await client.createAgent(appId, propertyManagerId, username, {
email: 'mr.example@allthings.test',
locale: 'en_US',
})
export type MethodCreateAgent = (
appId: string,
propertyManagerId: string,
username: string,
data: PartialUser & {
readonly email: string
readonly locale: EnumLocale
},
) => UserResult
export interface IAllthingsRestClient {
readonly delete: MethodHttpDelete
readonly get: MethodHttpGet
readonly post: MethodHttpPost
readonly patch: MethodHttpPatch
readonly agentCreate: MethodAgentCreate
readonly agentCreatePermissions: MethodAgentCreatePermissions
readonly appCreate: MethodAppCreate
readonly bookingGetById: MethodBookingGetById
readonly bookingUpdateById: MethodBookingUpdateById
readonly groupCreate: MethodGroupCreate
readonly groupGetById: MethodGroupGetById
readonly groupUpdateById: MethodGroupUpdateById
readonly lookupIds: MethodLookupIds
readonly notificationsGetByUser: MethodNotificationsGetByUser
readonly notificationsUpdateReadByUser: MethodNotificationsUpdateReadByUser
readonly notificationUpdateRead: MethodNotificationUpdateRead
readonly notificationSettingsResetByUser: MethodNotificationSettingsResetByUser
readonly notificationSettingsUpdateByUser: MethodNotificationSettingsUpdateByUser
readonly propertyCreate: MethodPropertyCreate
readonly propertyGetById: MethodPropertyGetById
readonly propertyUpdateById: MethodPropertyUpdateById
readonly registrationCodeCreate: MethodRegistrationCodeCreate
readonly unitCreate: MethodUnitCreate
readonly unitGetById: MethodUnitGetById
readonly unitUpdateById: MethodUnitUpdateById
readonly userCreate: MethodUserCreate
readonly userGetById: MethodUserGetById
readonly userUpdateById: MethodUserUpdateById
readonly getUsers: MethodGetUsers
readonly getCurrentUser: MethodGetCurrentUser
readonly userChangePassword: MethodUserChangePassword
readonly userCreatePermission: MethodUserCreatePermission
readonly userGetPermissions: MethodUserGetPermissions
readonly userDeletePermission: MethodUserDeletePermission
readonly userGetUtilisationPeriods: MethodUserGetUtilisationPeriods
readonly userCheckInToUtilisationPeriod: MethodUserCheckInToUtilisationPeriod
readonly userRelationCreate: MethodUserRelationCreate
readonly userRelationDelete: MethodUserRelationDelete
readonly utilisationPeriodCreate: MethodUtilisationPeriodCreate
readonly utilisationPeriodDelete: MethodUtilisationPeriodDelete
readonly utilisationPeriodGetById: MethodUtilisationPeriodGetById
readonly utilisationPeriodUpdateById: MethodUtilisationPeriodUpdateById
readonly utilisationPeriodCheckInUser: MethodUtilisationPeriodCheckInUser
}
Release management & versioning
!! DO NOT npm version
!!
The Allthings SDK makes use of semantic-release which automates the whole package release workflow including:
- determining the next version number
- generating the release notes and publishing the package.
This repository is also configured to squash-merge
(see here).
When you squash merge, GitHub takes the title of the PR for the squash-merge's commit subject.
By choosing a proper PR title e.g. feat: my new feature
your merged PR will trigger a new release.
See semantic-releases docs for available prefixes.
Local tests
To run local tests, init the shell from devenv and use this command:
ALLTHINGS_OAUTH_CLIENT_ID='<oauth_client_id>' \
ALLTHINGS_OAUTH_CLIENT_SECRET='<oauth_client_secret>' \
ALLTHINGS_OAUTH_USERNAME='<oauth_username' \
ALLTHINGS_OAUTH_PASSWORD="<oauth_password>" \
./bin/test-ci.sh --update --log accounts,events,php
if you don't want to init the shell, please also provide next env variables to command as well:
- ALLTHINGS_ACCOUNTS_RECAPTCHA_SECRET_KEY
- SSL_PASS
Credentials could be found in dev.secrets
repo.