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

Add dynamic TOML support via [DynamicToml] Service #9517

Merged
merged 4 commits into from
Aug 30, 2023
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
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',
cptpiepmatz marked this conversation as resolved.
Show resolved Hide resolved
)
.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',
})
Loading