From d1ae2ddcbe732cd994572add1c2d2b04780142e3 Mon Sep 17 00:00:00 2001 From: David de Boer Date: Mon, 18 Mar 2024 14:15:21 +0100 Subject: [PATCH] feat: Introduce config object to wrap env var lookups (#1279) --- package-lock.json | 1 + packages/network-of-terms-query/package.json | 1 + packages/network-of-terms-query/src/config.ts | 24 +++++++++++++++++++ packages/network-of-terms-query/src/query.ts | 5 ++-- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 packages/network-of-terms-query/src/config.ts diff --git a/package-lock.json b/package-lock.json index 800eb9431..520706a7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19278,6 +19278,7 @@ "@opentelemetry/sdk-metrics": "1.22.0", "@opentelemetry/semantic-conventions": "1.22.0", "@rdfjs/data-model": "^2.0.2", + "env-schema": "^5.2.1", "joi": "^17.12.2", "pino": "^8.19.0", "pino-pretty": "^10.3.1", diff --git a/packages/network-of-terms-query/package.json b/packages/network-of-terms-query/package.json index 920f0a4d8..2864abba2 100644 --- a/packages/network-of-terms-query/package.json +++ b/packages/network-of-terms-query/package.json @@ -39,6 +39,7 @@ "@opentelemetry/sdk-metrics": "1.22.0", "@opentelemetry/semantic-conventions": "1.22.0", "@rdfjs/data-model": "^2.0.2", + "env-schema": "^5.2.1", "joi": "^17.12.2", "pino": "^8.19.0", "pino-pretty": "^10.3.1", diff --git a/packages/network-of-terms-query/src/config.ts b/packages/network-of-terms-query/src/config.ts new file mode 100644 index 000000000..92dc54e36 --- /dev/null +++ b/packages/network-of-terms-query/src/config.ts @@ -0,0 +1,24 @@ +import {envSchema, JSONSchemaType} from 'env-schema'; + +const schema = { + type: 'object', + properties: { + MAX_QUERY_TIMEOUT: { + type: 'number', + default: 60000, + }, + DEFAULT_QUERY_TIMEOUT: { + type: 'number', + default: 5000, + }, + }, +}; + +interface Env { + MAX_QUERY_TIMEOUT: number; + DEFAULT_QUERY_TIMEOUT: number; +} + +export const config: JSONSchemaType = envSchema({ + schema, +}); diff --git a/packages/network-of-terms-query/src/query.ts b/packages/network-of-terms-query/src/query.ts index ea9724d6a..22883ec3f 100644 --- a/packages/network-of-terms-query/src/query.ts +++ b/packages/network-of-terms-query/src/query.ts @@ -12,6 +12,7 @@ import {QueryEngine} from '@comunica/query-sparql'; import {BindingsFactory} from '@comunica/bindings-factory'; import {DataFactory} from 'rdf-data-factory'; import {sourceQueriesHistogram} from './instrumentation.js'; +import {config} from './config.js'; export type TermsResult = Terms | TimeoutError | ServerError; @@ -102,8 +103,8 @@ export class QueryTermsService { Joi.number() .integer() .min(1) - .max(parseInt(process.env.MAX_QUERY_TIMEOUT as string) || 60000) - .default(parseInt(process.env.DEFAULT_QUERY_TIMEOUT as string) || 5000) + .max(config.MAX_QUERY_TIMEOUT) + .default(config.DEFAULT_QUERY_TIMEOUT) ); const timer = new Hoek.Bench();