Files
shields/services/wheelmap/wheelmap.service.js
chris48s 2c0737592b migrate examples to openApi part 8; affects [ubuntu uptimerobot vaadin vcpkg visualstudiomarketplace wheelmap] (#9463)
* migrate some services from examples to openApi

* improve and de-dupe service titles
2023-08-31 20:31:11 +00:00

72 lines
1.6 KiB
JavaScript

import Joi from 'joi'
import { BaseJsonService, pathParams } from '../index.js'
const schema = Joi.object({
node: Joi.object({
wheelchair: Joi.string().required(),
}).required(),
}).required()
export default class Wheelmap extends BaseJsonService {
static category = 'other'
static route = {
base: 'wheelmap/a',
pattern: ':nodeId(-?[0-9]+)',
}
static auth = {
passKey: 'wheelmap_token',
authorizedOrigins: ['https://wheelmap.org'],
isRequired: true,
}
static openApi = {
'/wheelmap/a/{nodeId}': {
get: {
summary: 'Wheelmap',
parameters: pathParams({
name: 'nodeId',
example: '26699541',
}),
},
},
}
static defaultBadgeData = { label: 'accessibility' }
static render({ accessibility }) {
let color
if (accessibility === 'yes') {
color = 'brightgreen'
} else if (accessibility === 'limited') {
color = 'yellow'
} else if (accessibility === 'no') {
color = 'red'
}
return { message: accessibility, color }
}
async fetch({ nodeId }) {
return this._requestJson(
this.authHelper.withQueryStringAuth(
{ passKey: 'api_key' },
{
schema,
url: `https://wheelmap.org/api/nodes/${nodeId}`,
httpErrors: {
401: 'invalid token',
404: 'node not found',
},
},
),
)
}
async handle({ nodeId }) {
const json = await this.fetch({ nodeId })
const accessibility = json.node.wheelchair
return this.constructor.render({ accessibility })
}
}