Skip to content

Commit

Permalink
Merge pull request #91 from svrooij/feature/tests
Browse files Browse the repository at this point in the history
Correct response parsing and tests
  • Loading branch information
svrooij authored Dec 24, 2020
2 parents 8ec7e7e + b69f196 commit b5a55ae
Show file tree
Hide file tree
Showing 52 changed files with 2,041 additions and 364 deletions.
39 changes: 13 additions & 26 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,38 @@ module.exports = {
},
rules: {
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
"class-methods-use-this":"off",
"import/first": "warn",
"import/no-cycle": "off",
"import/no-duplicates": "warn",
"max-classes-per-file":"off",
"max-len":"off",
"no-console": "off",
"no-return-await": "off",
"linebreak-style": "off"
// e.g. '@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'class-methods-use-this':'off',
'max-classes-per-file':'off',
'max-len':'off',
'no-return-await': 'off',
'linebreak-style': 'off'
},
overrides: [
{
files: ['src/services/virtual-line-in.service.ts'],
files: ['src/helpers/metadata-helper.ts'],
rules: {
"@typescript-eslint/no-unused-vars": "off"
}
},
{
files: ['src/helpers/*.ts'],
rules: {
'no-bitwise': 'warn',
'no-underscore-dangle': 'warn',
'no-useless-escape': 'warn',
'prefer-destructuring': 'warn'
'no-underscore-dangle': 'off',
}
},
{
files: ['src/models/*.ts'],
rules: {
'import/prefer-default-export': 'warn'
'import/prefer-default-export': 'off'
}
},
{
files: ['src/services/*.ts'],
rules: {
'import/order': 'warn',
'no-multiple-empty-lines': 'warn'
'no-multiple-empty-lines': 'off'
}
},
{
files: ['src/sonos-device.ts'],
files: ['src/sonos-event-listener.ts', 'src/services/base-service.ts'],
rules: {
'@typescript-eslint/ban-types': 'warn'
'import/no-cycle': 'off',
}
}
]
Expand Down
12 changes: 11 additions & 1 deletion docs/sonos-device/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,17 @@ Check out [this sample](https://github.com/svrooij/node-sonos-ts/blob/master/exa

## Event listener status

The event listener got 2 extra endpoints, a `/health` endpoint which will just respond with status code 200 if the listener is still running. There also is a `/status` endpoint, this will give some information about the event listener.
The event listener got 2 extra http endpoints, a `/health` endpoint which will just respond with status code 200 if the listener is still running. There also is a `/status` endpoint, this will give some information about the event listener.

The status endpoint is very handy if it looks you aren't receiving any events. It will show you the generated callback url. If that url isn't reachable from the network where the sonos speaker is in, you need to tweak your listener configuration.

### Event listener status in your app

You don't need to do an http call to the status endpoint, you can also directly get it from the `SonosEventListener`. Just call `SonosEventListener.DefaultInstance.GetStatus()` or `SonosEventListener.DefaultInstance.GetSubscriptions()`.

## Starting and stopping the listener

You probably won't need this, but if you want you can start and stop the event listener manually.

- `SonosEventListener.DefaultInstance.StartListener()` Start the listener (this will also enable the status and health endpoints)
- `SonosEventListener.DefaultInstance.StopListener()` Stop the listener, this will also unregister all subscriptions, but the services won't no you stopped the listener. It's better to unsubscribe first, see [event sample](https://github.com/svrooij/node-sonos-ts/blob/master/examples/events.js)
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
moduleFileExtensions: ['ts', 'tsx', 'jsx', 'js', 'json'],
testEnvironment: 'node',
collectCoverage: true,
coverageReporters: ['text', 'text-summary', 'lcov'],
coverageReporters: ['text', 'text-summary', 'lcov', 'html'],
collectCoverageFrom: [
'src/*.ts',
'src/helpers/**/*.ts',
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"types": "lib/index.d.js",
"scripts": {
"build": "tsc",
"test": "npm run lint && jest",
"lint-fix": "eslint ./src/*.ts ./src/**/*.ts --fix",
"lint": "eslint ./src/*.ts ./src/**/*.ts",
"jest": "jest",
"prepack": "npm run build",
"serve-docs": "docker run --rm --volume=\"$PWD/docs/vendor/bundle:/usr/local/bundle\" --volume=\"$PWD/docs:/srv/jekyll\" -p 4000:4000 -p 35729:35729 -it jekyll/jekyll jekyll serve --livereload",
"prepack": "npm run build"
"test": "npm run lint && jest --forceExit"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -51,6 +52,7 @@
"debug": "4.2.0",
"fast-xml-parser": "^3.17.4",
"guid-typescript": "^1.0.9",
"html-entities": "^1.4.0",
"node-fetch": "^2.6.1",
"strict-event-emitter-types": "^2.0.0"
},
Expand Down
21 changes: 4 additions & 17 deletions src/helpers/async-helper.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import { EventEmitter } from 'events';

/**
* Error that is thrown by AsyncEvent, if event didn't trigger before timeout
*
* @export
* @class EventTimeoutError
* @extends {Error}
*/
export class EventTimeoutError extends Error {
constructor(eventName: string) {
super(`Event ${eventName} didn't fire before timeout`);
this.name = 'EventTimeoutError';
}
}
import EventTimeoutError from './event-timeout-error';

/**
* Async helper exposes a function to wait for an event or timeout.
Expand All @@ -21,7 +8,7 @@ export class EventTimeoutError extends Error {
* @hidden
* @class AsyncHelper
*/
export class AsyncHelper {
export default class AsyncHelper {
/**
* Awaitable event handler with timeout
*
Expand Down Expand Up @@ -49,14 +36,14 @@ export class AsyncHelper {
}

/**
* Awaitable timeout
* Awaitable delay
*
* @static
* @param {number} ms Time to wait in milliseconds
* @returns {Promise<void>}
* @memberof AsyncHelper
*/
static async Delay(ms: number): Promise<void> {
await new Promise((resolve) => setTimeout(() => resolve(), ms));
await new Promise((resolve) => setTimeout(() => resolve(undefined), ms));
}
}
13 changes: 13 additions & 0 deletions src/helpers/event-timeout-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Error that is thrown by AsyncEvent, if event didn't trigger before timeout
*
* @export
* @class EventTimeoutError
* @extends {Error}
*/
export default class EventTimeoutError extends Error {
constructor(eventName: string) {
super(`Event ${eventName} didn't fire before timeout`);
this.name = 'EventTimeoutError';
}
}
4 changes: 2 additions & 2 deletions src/helpers/json-helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default class JsonHelper {
static TryParse(input: string): string | any {
static TryParse(input: string): string | unknown {
try {
return JSON.parse(input) as any;
return JSON.parse(input) as unknown;
} catch {
return input;
}
Expand Down
Loading

0 comments on commit b5a55ae

Please sign in to comment.