Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ULMS-3167 Refactored api routes, removed deprecated code #142

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 0 additions & 135 deletions __tests__/basic-client.test.js

This file was deleted.

10 changes: 5 additions & 5 deletions __tests__/error.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
MQTTClientError,
MQTTRPCServiceError,
PresenceError,
PresenceWsError,
TimeoutError,
} from '../src/error'

const clientError = new MQTTClientError('Message')
const presenceError = new PresenceError('Message')
const presenceWsError = new PresenceWsError('Message')
const serviceError = new MQTTRPCServiceError('Message')
const timeoutError = new TimeoutError('Message')

Expand All @@ -26,13 +26,13 @@ describe('Custom errors have right titles', () => {

// PresenceError
it('PresenceError message is valid (`fromType` method)', () => {
expect(PresenceError.fromType('test').message).toEqual('UNKNOWN_ERROR')
expect(PresenceWsError.fromType('test').message).toEqual('UNKNOWN_ERROR')
})
it('PresenceError name is valid', () => {
expect(presenceError.name).toEqual('PresenceError')
expect(presenceWsError.name).toEqual('PresenceWsError')
})
it('PresenceError message is valid (default)', () => {
expect(presenceError.message).toEqual('Message')
expect(presenceWsError.message).toEqual('Message')
})

// MQTTRPCServiceError
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ulms/api-clients",
"version": "7.9.10",
"version": "7.10.0",
"description": "JavaScript API clients for ULMS platform",
"keywords": [],
"homepage": "https://github.com/foxford/ulms-api-clients-js#readme",
Expand Down
52 changes: 19 additions & 33 deletions src/basic-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ const parseParameter = (key, value) => {
return `${key}=${value}`
}

const responseTransformer = (_) => _.data
async function handleResponse() {
throw new Error('Method `handleResponse` not implemented.')
}

class BasicClient {
constructor(baseUrl, httpClient, tokenProvider) {
this.baseUrl = baseUrl
this.customHeaders = {}
this.httpClient = httpClient
this.tokenProvider = tokenProvider
this.labels = {}
this.trackEvent = undefined
this.handleResponse = handleResponse
}

url(endpoint, parameters) {
Expand All @@ -44,7 +46,7 @@ class BasicClient {

// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(parameters)) {
if (value !== undefined) {
if (value !== undefined && value !== null) {
urlParameters += urlParameters
? `&${parseParameter(key, value)}`
: parseParameter(key, value)
Expand All @@ -59,38 +61,22 @@ class BasicClient {
return `${this.baseUrl}${endpoint}`
}

static headers(token, labels = {}, headers = {}) {
static headers(token, headers = {}) {
return {
...headers,
authorization: `Bearer ${token}`,
'content-type': 'application/json',
...labels,
}
}

setHeaders(headers) {
this.customHeaders = headers
}

setLabels(labels) {
const { app_audience, app_label, app_version, scope } = labels // eslint-disable-line camelcase

this.labels = {
...(app_audience !== undefined && { 'ulms-app-audience': app_audience }), // eslint-disable-line camelcase
...(app_label !== undefined && { 'ulms-app-label': app_label }), // eslint-disable-line camelcase
...(app_version !== undefined && { 'ulms-app-version': app_version }), // eslint-disable-line camelcase
...(scope !== undefined && { 'ulms-scope': scope }),
}
}

setTrackEventFunction(trackEvent) {
this.trackEvent = trackEvent
}

clearLabels() {
this.labels = {}
}

async get(url, options = {}) {
const { retry: retryEnabled } = options

Expand All @@ -99,13 +85,13 @@ class BasicClient {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient
.get(url, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)
}

return retry(task, onRetry)
Expand All @@ -114,31 +100,31 @@ class BasicClient {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient.get(url, requestOptions).then(responseTransformer)
return this.httpClient.get(url, requestOptions).then(this.handleResponse)
}

async put(url, data, options = {}) {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient
.put(url, data, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)
}

async post(url, data, options = {}) {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

Expand All @@ -151,7 +137,7 @@ class BasicClient {
: undefined
const result = this.httpClient
.post(url, data, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)

result.catch((error) => {
const responseEnd = Date.now()
Expand All @@ -174,31 +160,31 @@ class BasicClient {

return this.httpClient
.post(url, data, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)
}

async patch(url, data, options = {}) {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient
.patch(url, data, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)
}

async delete(url, options = {}) {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient.delete(url, requestOptions).then(responseTransformer)
return this.httpClient.delete(url, requestOptions).then(this.handleResponse)
}
}

Expand Down
Loading