Skip to content

Commit

Permalink
feat(api): implement config::byKey
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumearm committed Nov 3, 2018
1 parent f64a80b commit 2e429df
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 14 deletions.
36 changes: 36 additions & 0 deletions src/__nock-fixtures__/jeedom-config::byKey.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"scope": "http://192.168.1.60:80",
"method": "POST",
"path": "/core/api/jeeApi.php",
"body": {
"jsonrpc": "2.0",
"id": null,
"method": "config::byKey",
"params": {
"key": "version",
"apikey": "42"
}
},
"status": 200,
"response": {
"jsonrpc": "2.0",
"id": null,
"result": "3.2.11"
},
"rawHeaders": [
"Date",
"Sat, 03 Nov 2018 16:16:20 GMT",
"Server",
"Apache",
"Access-Control-Allow-Origin",
"*",
"Content-Length",
"45",
"Connection",
"close",
"Content-Type",
"text/html; charset=UTF-8"
]
}
]
4 changes: 2 additions & 2 deletions src/__nock-fixtures__/jeedom-datetime.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"response": {
"jsonrpc": "2.0",
"id": null,
"result": 1541259462.0145
"result": 1541261780.4365
},
"rawHeaders": [
"Date",
"Sat, 03 Nov 2018 15:37:41 GMT",
"Sat, 03 Nov 2018 16:16:20 GMT",
"Server",
"Apache",
"Access-Control-Allow-Origin",
Expand Down
2 changes: 1 addition & 1 deletion src/__nock-fixtures__/jeedom-ping.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"rawHeaders": [
"Date",
"Sat, 03 Nov 2018 15:37:39 GMT",
"Sat, 03 Nov 2018 16:16:20 GMT",
"Server",
"Apache",
"Access-Control-Allow-Origin",
Expand Down
2 changes: 1 addition & 1 deletion src/__nock-fixtures__/jeedom-version.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"rawHeaders": [
"Date",
"Sat, 03 Nov 2018 15:37:39 GMT",
"Sat, 03 Nov 2018 16:16:20 GMT",
"Server",
"Apache",
"Access-Control-Allow-Origin",
Expand Down
8 changes: 5 additions & 3 deletions src/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jeedom-node jeedom api datetime 1`] = `1541259462.0145`;
exports[`jeedom-node api config config::byKey 1`] = `"3.2.11"`;

exports[`jeedom-node jeedom api ping 1`] = `"pong"`;
exports[`jeedom-node api datetime 1`] = `1541261780.4365`;

exports[`jeedom-node jeedom api version 1`] = `"3.2.11"`;
exports[`jeedom-node api ping 1`] = `"pong"`;

exports[`jeedom-node api version 1`] = `"3.2.11"`;
3 changes: 3 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const createJeedomApi = (jeedomRpc: RpcInterface): JeedomApi => ({
ping: () => jeedomRpc('ping'),
version: () => jeedomRpc('version'),
datetime: () => jeedomRpc('datetime'),
config: {
byKey: (params) => jeedomRpc('config::byKey', params),
},
})

export * from './types'
11 changes: 11 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ export type PingResponse = Promise<'pong'>
export type VersionResponse = Promise<string>
export type DatetimeResponse = Promise<number>

export type ConfigByKeyParams = {
key: string
plugin?: string
default?: string
}

export type ConfigByKeyResponse = any

export type JeedomApi = {
ping: () => PingResponse
version: () => VersionResponse
datetime: () => DatetimeResponse
config: {
byKey: (params: ConfigByKeyParams) => ConfigByKeyResponse
}
}
19 changes: 13 additions & 6 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import Jeedom from '.'

const HOST = 'http://192.168.1.60'

const record = setupRecorder({ mode: 'record' })

const recordAndMatchSnapshot = (
testName: string,
recorder: (fileName: string) => Promise<{ completeRecording: () => void }>,
Expand All @@ -25,12 +23,21 @@ describe('jeedom-node', () => {
})
})

describe('jeedom api', () => {
describe('api', () => {
const recorder = setupRecorder({ mode: 'record' })

const record = (testName: string, runEffect: () => Promise<any>) =>
recordAndMatchSnapshot(testName, recorder, runEffect)

const apikey = process.env.JEEDOM_API_KEY || ''
const api = Jeedom({ host: HOST, apikey })

recordAndMatchSnapshot('ping', record, () => api.ping())
recordAndMatchSnapshot('version', record, () => api.version())
recordAndMatchSnapshot('datetime', record, () => api.datetime())
record('ping', () => api.ping())
record('version', () => api.version())
record('datetime', () => api.datetime())

describe('config', () => {
record('config::byKey', () => api.config.byKey({ key: 'version' }))
})
})
})
2 changes: 1 addition & 1 deletion src/rpc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type JsonRpcResponse<T = any> = {
error?: { message: string; code: number }
}

export type Params = Record<string, string>
export type Params = Record<string, string | undefined>

export type RpcInterface = (method: string, params?: Params, id?: Nullable<number>) => Promise<any>

Expand Down

0 comments on commit 2e429df

Please sign in to comment.