createClient(): throw if userAgent is one of the documented ones 💥📝

related: #286
fixes #286
This commit is contained in:
Jannis R 2023-03-14 21:00:20 +01:00
parent 5910d62535
commit e7602e6c84
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 12 additions and 1 deletions

View file

@ -8,6 +8,13 @@ import {INVALID_REQUEST} from './lib/errors.js'
import {sliceLeg} from './lib/slice-leg.js' import {sliceLeg} from './lib/slice-leg.js'
import {HafasError} from './lib/errors.js' import {HafasError} from './lib/errors.js'
// background info: https://github.com/public-transport/hafas-client/issues/286
const FORBIDDEN_USER_AGENTS = [
'my-awesome-program', // previously used in readme.md, p/*/readme.md & docs/*.md
'hafas-client-example', // previously used in p/*/example.js
'link-to-your-project-or-email', // now used throughout
]
const isNonEmptyString = str => 'string' === typeof str && str.length > 0 const isNonEmptyString = str => 'string' === typeof str && str.length > 0
const validateLocation = (loc, name = 'location') => { const validateLocation = (loc, name = 'location') => {
@ -35,6 +42,9 @@ const createClient = (profile, userAgent, opt = {}) => {
if ('string' !== typeof userAgent) { if ('string' !== typeof userAgent) {
throw new TypeError('userAgent must be a string'); throw new TypeError('userAgent must be a string');
} }
if (FORBIDDEN_USER_AGENTS.includes(userAgent.toLowerCase())) {
throw new TypeError(`userAgent should tell the HAFAS API operators how to contact you. If you have copied "${userAgent}" value from the documentation, please adapt it.`);
}
const _stationBoard = async (station, type, resultsField, parse, opt = {}) => { const _stationBoard = async (station, type, resultsField, parse, opt = {}) => {
if (isObj(station)) station = profile.formatStation(station.id) if (isObj(station)) station = profile.formatStation(station.id)

View file

@ -53,7 +53,8 @@ Because the operatores of the HAFAS endpoint should be able to contact you about
import {createClient} from 'hafas-client' import {createClient} from 'hafas-client'
import {profile as dbProfile} from 'hafas-client/p/db/index.js' import {profile as dbProfile} from 'hafas-client/p/db/index.js'
const userAgent = 'link-to-your-project-or-email' // adapt this to your project! // Adapt this to your project! createClient() won't work with this string.
const userAgent = 'link-to-your-project-or-email'
// create a client with the Deutsche Bahn profile // create a client with the Deutsche Bahn profile
const client = createClient(dbProfile, userAgent) const client = createClient(dbProfile, userAgent)