Skip to content

Commit

Permalink
feat: Introduce config object to wrap env var lookups (#1279)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer committed Mar 18, 2024
1 parent b50fe83 commit d1ae2dd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/network-of-terms-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions packages/network-of-terms-query/src/config.ts
Original file line number Diff line number Diff line change
@@ -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<Env> = envSchema({
schema,
});
5 changes: 3 additions & 2 deletions packages/network-of-terms-query/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d1ae2dd

Please sign in to comment.