@data-fair/lib-node 2.3.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "2.3.1",
3
+ "version": "2.4.0",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -15,10 +15,9 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@data-fair/lib-common-types": "^1.5.0",
18
+ "@data-fair/lib-validation": "^1.0.0",
18
19
  "@data-fair/lib-utils": "^1.1.0",
19
20
  "agentkeepalive": "^4.5.0",
20
- "ajv-formats": "^3.0.1",
21
- "ajv-i18n": "^4.2.0",
22
21
  "axios": "^1.7.7",
23
22
  "cacheable-lookup": "^7.0.0",
24
23
  "semver": "^7.6.3"
@@ -1,6 +1,6 @@
1
1
  import semver from 'semver'
2
2
  import path from 'path'
3
- import { readdirSync, readFileSync } from 'fs'
3
+ import { readdirSync, readFileSync, statSync } from 'fs'
4
4
  import Debug from 'debug'
5
5
  const debug = Debug('upgrade')
6
6
  // chose the proper scripts to execute, then run them
@@ -34,19 +34,19 @@ async function runScripts (db, basePath, isFresh) {
34
34
  debug('list scripts from ' + scriptsRoot)
35
35
  let scripts = await listScripts(scriptsRoot)
36
36
  if (previousPersion) {
37
- debug(`Current service version from database : ${previousPersion}`)
37
+ debug(`current service version from database : ${previousPersion}`)
38
38
  } else {
39
- debug('No service version found in database, this is the first time the upgrade system is run')
39
+ debug('no service version found in database, this is the first time the upgrade system is run')
40
40
  scripts = scripts.filter(scriptDef => scriptDef.version === 'init')
41
41
  if (isFresh) {
42
42
  if (await isFresh()) {
43
- debug('isFresh function returned true, this is fresh install, do not run any script')
43
+ debug('isFresh function returned true, this is a fresh install, do not run any script')
44
44
  } else {
45
- debug('isFresh function returned false, this is not fresh install, run all init scripts')
45
+ debug('isFresh function returned false, this is not a fresh install, run all init scripts')
46
46
  previousPersion = '0.0.0'
47
47
  }
48
48
  } else {
49
- debug('No isFresh function, it could be a fresh install, do not run any script')
49
+ debug('no isFresh function, it could be a fresh install, do not run any script')
50
50
  }
51
51
  }
52
52
  if (previousPersion) {
@@ -54,7 +54,7 @@ async function runScripts (db, basePath, isFresh) {
54
54
  if (semver.gte(scriptDef.version, previousPersion)) {
55
55
  for (const scriptName of scriptDef.names) {
56
56
  const script = (await import(path.join(scriptsRoot, scriptDef.version, scriptName))).default
57
- debug('Apply script %s/%s : %s', scriptDef.version, scriptName, script.description)
57
+ debug('apply script %s/%s : %s', scriptDef.version, scriptName, script.description)
58
58
  await script.exec(db, Debug(`upgrade:${scriptDef.version}:${scriptName}`))
59
59
  }
60
60
  }
@@ -63,12 +63,14 @@ async function runScripts (db, basePath, isFresh) {
63
63
  const coercedVersion = semver.coerce(pjson.version)
64
64
  if (!coercedVersion) { throw new Error(`Invalid version number in package.json ${pjson.version}`) }
65
65
  const newService = { id: pjson.name, version: coercedVersion.version }
66
- debug(`Upgrade scripts are over, save current version number ${newService.version}`)
66
+ debug(`upgrade scripts are over, save current version number ${newService.version}`)
67
67
  await services.updateOne({ id: pjson.name }, { $set: newService }, { upsert: true })
68
68
  }
69
69
  // Walk the scripts directories
70
70
  async function listScripts (scriptsRoot) {
71
- const dirs = readdirSync(scriptsRoot).sort()
71
+ const dirs = readdirSync(scriptsRoot)
72
+ .filter(dir => statSync(path.join(scriptsRoot, dir)).isDirectory())
73
+ .sort()
72
74
  return dirs.map(dir => {
73
75
  return {
74
76
  version: dir,
package/validation.d.ts CHANGED
@@ -1,8 +1 @@
1
- import type { ErrorObject, ValidateFunction } from 'ajv';
2
- export declare const errorsText: (errors: ErrorObject[] | null | undefined, varName?: string) => string;
3
- export type AssertValidOptions = {
4
- lang?: string;
5
- name?: string;
6
- internal?: boolean;
7
- };
8
- export declare function assertValid<T>(validate: ValidateFunction, data: any, options?: AssertValidOptions): asserts data is T;
1
+ export * from '@data-fair/lib-validation';
package/validation.js CHANGED
@@ -1,37 +1 @@
1
- import ajvFrModule from 'ajv-i18n/localize/fr/index.js'
2
- import ajvEnModule from 'ajv-i18n/localize/en/index.js'
3
- import { httpError } from '@data-fair/lib-utils/http-errors.js'
4
- const ajvFr = ajvFrModule
5
- const ajvEn = ajvEnModule
6
- const localize = {
7
- fr: ajvFr,
8
- en: ajvEn
9
- }
10
- // cf https://github.com/ajv-validator/ajv/blob/b3e0cb17d0e095b5c883042b2306571be5ec86b7/lib/core.ts#L650
11
- export const errorsText = (errors, varName = 'data') => {
12
- if (!errors || errors.length === 0) { return 'No errors' }
13
- return errors
14
- .map((e) => {
15
- let msg = `${varName}${e.instancePath} ${e.message}`.trim()
16
- const paramKeys = Object.keys(e.params || {}).filter(key => {
17
- if (key === 'error') { return false }
18
- if (e.keyword === 'type' && key === 'type') { return false }
19
- return true
20
- })
21
- const params = paramKeys
22
- .map(key => paramKeys.length > 1 ? `${key}=${e.params[key]}` : e.params[key])
23
- .join(', ')
24
- if (params) { msg += ` (${params})` }
25
- return msg
26
- })
27
- .reduce((text, msg) => text + ', ' + msg)
28
- }
29
- export function assertValid (validate, data, options = {}) {
30
- const lang = options.lang ?? 'fr'
31
- const name = options.name ?? 'data'
32
- if (!validate(data)) {
33
- (localize[lang] || localize.fr)(validate.errors)
34
- const message = errorsText(validate.errors, name)
35
- if (options.internal) { throw new Error(message) } else { throw httpError(400, message) }
36
- }
37
- }
1
+ export * from '@data-fair/lib-validation'