Skip to content

Commit

Permalink
Add dynamic TOML support via [DynamicToml] Service (#9517)
Browse files Browse the repository at this point in the history
* exported BaseTomlService

* added dynamic-toml service

* added dynamic-toml tester

* fixed dynamic-toml tests
  • Loading branch information
cptpiepmatz authored Aug 30, 2023
1 parent e815710 commit 170d29e
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/base-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BaseJsonService from './base-json.js'
import BaseGraphqlService from './base-graphql.js'
import BaseStaticService from './base-static.js'
import BaseSvgScrapingService from './base-svg-scraping.js'
import BaseTomlService from './base-toml.js'
import BaseXmlService from './base-xml.js'
import BaseYamlService from './base-yaml.js'
import deprecatedService from './deprecated-service.js'
Expand All @@ -23,6 +24,7 @@ export {
BaseGraphqlService,
BaseStaticService,
BaseSvgScrapingService,
BaseTomlService,
BaseXmlService,
BaseYamlService,
deprecatedService,
Expand Down
54 changes: 54 additions & 0 deletions services/dynamic/dynamic-toml.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { MetricNames } from '../../core/base-service/metric-helper.js'
import { BaseTomlService, queryParams } from '../index.js'
import { createRoute } from './dynamic-helpers.js'
import jsonPath from './json-path.js'

export default class DynamicToml extends jsonPath(BaseTomlService) {
static enabledMetrics = [MetricNames.SERVICE_RESPONSE_SIZE]
static route = createRoute('toml')
static openApi = {
'/badge/dynamic/toml': {
get: {
summary: 'Dynamic TOML Badge',
description: `<p>
The Dynamic TOML Badge allows you to extract an arbitrary value from any
TOML Document using a JSONPath selector and show it on a badge.
</p>`,
parameters: queryParams(
{
name: 'url',
description: 'The URL to a TOML document',
required: true,
example:
'https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/toml-spec-example.toml',
},
{
name: 'query',
description:
'A <a href="https://jsonpath.com/">JSONPath</a> expression that will be used to query the document',
required: true,
example: '$.title',
},
{
name: 'prefix',
description: 'Optional prefix to append to the value',
example: '[',
},
{
name: 'suffix',
description: 'Optional suffix to append to the value',
example: ']',
},
),
},
},
}

async fetch({ schema, url, httpErrors }) {
return this._requestToml({
schema,
url,
httpErrors,
})
}
}
107 changes: 107 additions & 0 deletions services/dynamic/dynamic-toml.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('No URL specified')
.get('.json?query=$.name&label=Package Name')
.expectBadge({
label: 'Package Name',
message: 'invalid query parameter: url',
color: 'red',
})

t.create('No query specified')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/toml-spec-example.toml&label=Package Name',
)
.expectBadge({
label: 'Package Name',
message: 'invalid query parameter: query',
color: 'red',
})

t.create('TOML from url')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/toml-spec-example.toml&query=$.title',
)
.expectBadge({
label: 'custom badge',
message: 'TOML Example',
color: 'blue',
})

t.create('TOML from url | multiple results')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/toml-spec-example.toml&query=$.database.data[0][*]',
)
.expectBadge({ label: 'custom badge', message: 'delta, phi' })

t.create('TOML from url | caching with new query params')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/toml-spec-example.toml&query=$.owner.name',
)
.expectBadge({ label: 'custom badge', message: 'Tom Preston-Werner' })

t.create('TOML from url | with prefix & suffix & label')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/toml-spec-example.toml&query=$.database.temp_targets.cpu&prefix=%2B&suffix=°C&label=CPU Temp Target',
)
.expectBadge({ label: 'CPU Temp Target', message: '+79.5°C' })

t.create('TOML from url | object doesnt exist')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/toml-spec-example.toml&query=$.does_not_exist',
)
.expectBadge({
label: 'custom badge',
message: 'no result',
color: 'lightgrey',
})

t.create('TOML from url | invalid url')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/not-a-file.toml&query=$.version',
)
.expectBadge({
label: 'custom badge',
message: 'resource not found',
color: 'red',
})

t.create('TOML from url | user color overrides default')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/toml-spec-example.toml&query=$.title&color=10ADED',
)
.expectBadge({
label: 'custom badge',
message: 'TOML Example',
color: '#10aded',
})

t.create('TOML from url | error color overrides default')
.get(
'.json?url=https://raw.githubusercontent.com/squirrelchat/smol-toml/mistress/bench/testfiles/not-a-file.toml&query=$.version',
)
.expectBadge({
label: 'custom badge',
message: 'resource not found',
color: 'red',
})

t.create('TOML from url | error color overrides user specified')
.get('.json?query=$.version&color=10ADED')
.expectBadge({
label: 'custom badge',
message: 'invalid query parameter: url',
color: 'red',
})

t.create('TOML contains a string')
.get('.json?url=https://example.test/toml&query=$.foo,')
.intercept(nock =>
nock('https://example.test').get('/toml').reply(200, '"foo"'),
)
.expectBadge({
label: 'custom badge',
message: 'unparseable toml response',
color: 'lightgrey',
})

0 comments on commit 170d29e

Please sign in to comment.