@natlibfi/melinda-rest-api-commons 6.1.0-alpha.0 → 6.2.0-alpha.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.
Files changed (92) hide show
  1. package/.github/workflows/melinda-node-tests-and-publish.yml +4 -2
  2. package/README.md +53 -5
  3. package/dist/amqp.d.ts +41 -0
  4. package/dist/amqp.d.ts.map +1 -0
  5. package/dist/amqp.js +237 -235
  6. package/dist/amqp.js.map +1 -7
  7. package/dist/constants.d.ts +49 -0
  8. package/dist/constants.d.ts.map +1 -0
  9. package/dist/constants.js +38 -38
  10. package/dist/constants.js.map +1 -7
  11. package/dist/helpers/conversion.d.ts +8 -0
  12. package/dist/helpers/conversion.d.ts.map +1 -0
  13. package/dist/helpers/conversion.js +70 -64
  14. package/dist/helpers/conversion.js.map +1 -7
  15. package/dist/helpers/fix-constants.d.ts +76 -0
  16. package/dist/helpers/fix-constants.d.ts.map +1 -0
  17. package/dist/helpers/fix-constants.js +41 -41
  18. package/dist/helpers/fix-constants.js.map +1 -7
  19. package/dist/helpers/fix-handle-tempurns.d.ts +3 -0
  20. package/dist/helpers/fix-handle-tempurns.d.ts.map +1 -0
  21. package/dist/helpers/fix-handle-tempurns.js +73 -71
  22. package/dist/helpers/fix-handle-tempurns.js.map +1 -7
  23. package/dist/helpers/fix-strip-f884s.d.ts +10 -0
  24. package/dist/helpers/fix-strip-f884s.d.ts.map +1 -0
  25. package/dist/helpers/fix-strip-f884s.js +161 -163
  26. package/dist/helpers/fix-strip-f884s.js.map +1 -7
  27. package/dist/helpers/fix.d.ts +6 -0
  28. package/dist/helpers/fix.d.ts.map +1 -0
  29. package/dist/helpers/fix.js +133 -121
  30. package/dist/helpers/fix.js.map +1 -7
  31. package/dist/helpers/validation.d.ts +9 -0
  32. package/dist/helpers/validation.d.ts.map +1 -0
  33. package/dist/helpers/validation.js +14 -16
  34. package/dist/helpers/validation.js.map +1 -7
  35. package/dist/index.d.ts +13 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +1 -1
  38. package/dist/index.js.map +1 -7
  39. package/dist/mongo.d.ts +165 -0
  40. package/dist/mongo.d.ts.map +1 -0
  41. package/dist/mongo.js +595 -547
  42. package/dist/mongo.js.map +1 -7
  43. package/dist/mongoError.d.ts +2 -0
  44. package/dist/mongoError.d.ts.map +1 -0
  45. package/dist/mongoError.js +19 -0
  46. package/dist/mongoError.js.map +1 -0
  47. package/dist/mongoLog.d.ts +50 -0
  48. package/dist/mongoLog.d.ts.map +1 -0
  49. package/dist/mongoLog.js +274 -242
  50. package/dist/mongoLog.js.map +1 -7
  51. package/dist/utils.d.ts +36 -0
  52. package/dist/utils.d.ts.map +1 -0
  53. package/dist/utils.js +112 -110
  54. package/dist/utils.js.map +1 -7
  55. package/eslint.config.ts +66 -0
  56. package/package.json +15 -11
  57. package/{tests/amqp-error-branches.test.js → test/amqp-error-branches.test.ts} +68 -52
  58. package/{tests/amqp.healthtest.js → test/amqp.healthtest.ts} +7 -3
  59. package/{tests/amqp.tests.localtest.js → test/amqp.tests.localtest.ts} +54 -35
  60. package/{tests/helpers/conversion.test.js → test/helpers/conversion.test.ts} +26 -14
  61. package/{tests/helpers/fix-edge.test.js → test/helpers/fix-edge.test.ts} +16 -12
  62. package/{tests/helpers/fix.test.js → test/helpers/fix.test.ts} +14 -8
  63. package/{tests/helpers/validation.test.js → test/helpers/validation.test.ts} +7 -6
  64. package/{tests/mongo-add-blob-size.test.js → test/mongo-add-blob-size.test.ts} +14 -15
  65. package/{tests/mongo-check-and-check-state.test.js → test/mongo-check-and-check-state.test.ts} +6 -8
  66. package/{tests/mongo-check-and-set-import-job-state.test.js → test/mongo-check-and-set-import-job-state.test.ts} +6 -9
  67. package/{tests/mongo-check-timeout.test.js → test/mongo-check-timeout.test.ts} +6 -9
  68. package/{tests/mongo-create-bulk.test.js → test/mongo-create-bulk.test.ts} +6 -7
  69. package/{tests/mongo-create-prio.test.js → test/mongo-create-prio.test.ts} +6 -6
  70. package/{tests/mongo-error-branches.test.js → test/mongo-error-branches.test.ts} +147 -51
  71. package/{tests/mongo-get-one.test.js → test/mongo-get-one.test.ts} +6 -6
  72. package/{tests/mongo-get-stream.test.js → test/mongo-get-stream.test.ts} +6 -8
  73. package/{tests/mongo-push-ids.test.js → test/mongo-push-ids.test.ts} +6 -6
  74. package/{tests/mongo-push-messages.test.js → test/mongo-push-messages.test.ts} +6 -6
  75. package/{tests/mongo-query-by-id.test.js → test/mongo-query-by-id.test.ts} +6 -9
  76. package/{tests/mongo-query.test.js → test/mongo-query.test.ts} +6 -7
  77. package/{tests/mongo-read-content.test.js → test/mongo-read-content.test.ts} +6 -9
  78. package/{tests/mongo-remove.test.js → test/mongo-remove.test.ts} +6 -8
  79. package/{tests/mongo-set-blob-size.test.js → test/mongo-set-blob-size.test.ts} +6 -6
  80. package/{tests/mongo-set-import-job-state.test.js → test/mongo-set-import-job-state.test.ts} +6 -6
  81. package/{tests/mongo-set-operation.test.js → test/mongo-set-operation.test.ts} +6 -6
  82. package/{tests/mongo-set-operations.test.js → test/mongo-set-operations.test.ts} +6 -6
  83. package/{tests/mongo-set-state.test.js → test/mongo-set-state.test.ts} +6 -6
  84. package/{tests/mongoLog-protect.test.js → test/mongoLog-protect.test.ts} +18 -17
  85. package/{tests/mongoLog.test.js → test/mongoLog.test.ts} +8 -8
  86. package/{tests/testUtils.js → test/testUtils.ts} +49 -15
  87. package/{tests/utils.test.js → test/utils.test.ts} +5 -5
  88. package/tsconfig.json +62 -0
  89. package/tsconfig.test.json +17 -0
  90. package/dist/helpers/fix-utils.js +0 -39
  91. package/dist/helpers/fix-utils.js.map +0 -7
  92. package/eslint.config.js +0 -53
@@ -1,23 +1,39 @@
1
-
2
1
  import assert from 'node:assert';
3
2
  import createDebugLogger from 'debug';
4
- import createMongoOperator from '../src/mongo.js';
3
+ import type {Readable} from 'node:stream';
4
+ import createMongoOperator from '../src/mongo.ts';
5
5
 
6
6
 
7
7
  const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons/mongo:test');
8
8
 
9
- export async function getMongoOperator(mongoFixtures) {
9
+ // the fixura-mongo fixtures object is intentionally untyped in tests
10
+ // (fixture-driven scaffolding, see the eslint house rules for test/)
11
+ export async function getMongoOperator(mongoFixtures: any) {
10
12
  const mongoUri = await mongoFixtures.getUri();
11
13
  const mongoOperator = await createMongoOperator(mongoUri, 'foobar', '');
12
14
  return mongoOperator;
13
15
  }
14
16
 
15
- export async function compareToFirstDbEntry({mongoFixtures, expectedResult, expectModificationTime = false, formatDates = true, expectedFileCount = undefined}) {
17
+ interface DbEntryCompareParams {
18
+ mongoFixtures: any;
19
+ expectedResult: unknown;
20
+ expectModificationTime?: boolean;
21
+ formatDates?: boolean;
22
+ expectedFileCount?: number;
23
+ }
24
+
25
+ export async function compareToFirstDbEntry(params: DbEntryCompareParams) {
26
+ const {mongoFixtures, expectedResult, expectModificationTime = false, formatDates = true, expectedFileCount = undefined} = params;
16
27
  const result = await compareToDbEntry({mongoFixtures, expectedResult, resultIndex: 0, expectModificationTime, formatDates, expectedFileCount});
17
28
  return result;
18
29
  }
19
30
 
20
- export async function compareToDbEntry({mongoFixtures, expectedResult, resultIndex = 0, expectModificationTime = false, formatDates = true, expectedFileCount = undefined}) {
31
+ interface DbEntryParams extends DbEntryCompareParams {
32
+ resultIndex?: number;
33
+ }
34
+
35
+ export async function compareToDbEntry(params: DbEntryParams) {
36
+ const {mongoFixtures, expectedResult, resultIndex = 0, expectModificationTime = false, formatDates = true, expectedFileCount = undefined} = params;
21
37
  const dump = await mongoFixtures.dump();
22
38
  debug(`--- We have ${dump.foobar.length} documents in db`);
23
39
  debug(dump.foobar);
@@ -39,7 +55,13 @@ export async function compareToDbEntry({mongoFixtures, expectedResult, resultInd
39
55
  return;
40
56
  }
41
57
 
42
- function checkFileCount({dump, expectedFileCount}) {
58
+ interface CheckFileCountParams {
59
+ dump: any;
60
+ expectedFileCount: number | undefined;
61
+ }
62
+
63
+ function checkFileCount(params: CheckFileCountParams) {
64
+ const {dump, expectedFileCount} = params;
43
65
  if (expectedFileCount !== undefined) {
44
66
  const fileCount = dump['foobar.files']?.length;
45
67
  debug(`--- We have ${fileCount} files in db`);
@@ -50,7 +72,13 @@ function checkFileCount({dump, expectedFileCount}) {
50
72
 
51
73
  }
52
74
 
53
- function checkModificationTime({result, expectModificationTime}) {
75
+ interface CheckModificationTimeParams {
76
+ result: any;
77
+ expectModificationTime: boolean;
78
+ }
79
+
80
+ function checkModificationTime(params: CheckModificationTimeParams) {
81
+ const {result, expectModificationTime} = params;
54
82
  if (expectModificationTime) {
55
83
  debug('Check if modificationTime was edited');
56
84
  const isNotEmpty = result.modificationTime !== '';
@@ -62,7 +90,7 @@ function checkModificationTime({result, expectModificationTime}) {
62
90
  }
63
91
 
64
92
 
65
- export function formatQueueItem(queueItem) {
93
+ export function formatQueueItem(queueItem: any): any {
66
94
  const filteredQueueItem = {
67
95
  ...queueItem,
68
96
  creationTime: '',
@@ -72,12 +100,19 @@ export function formatQueueItem(queueItem) {
72
100
  return filteredQueueItem;
73
101
  }
74
102
 
75
- export function handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus}) {
103
+ interface HandleErrorParams {
104
+ error: any;
105
+ expectedToThrow: boolean;
106
+ expectedErrorMessage: string;
107
+ expectedErrorStatus: string;
108
+ }
109
+
110
+ export function handleError(params: HandleErrorParams) {
111
+ const {error, expectedToThrow, expectedErrorMessage, expectedErrorStatus} = params;
76
112
  debug(error);
77
113
  if (expectedToThrow) {
78
114
  debug('OK. Expected to throw');
79
115
  debug(JSON.stringify(error));
80
- // eslint-disable-next-line max-lines
81
116
  if (expectedErrorMessage !== '' || expectedErrorStatus !== '') {
82
117
  const errorMessage = error.message || error.payload.message || error.payload || '';
83
118
  debug(errorMessage);
@@ -92,12 +127,11 @@ export function handleError({error, expectedToThrow, expectedErrorMessage, expec
92
127
  throw error;
93
128
  }
94
129
 
95
- export function streamToString(stream) {
96
- const chunks = [];
130
+ export function streamToString(stream: Readable): Promise<string> {
131
+ const chunks: Buffer[] = [];
97
132
  return new Promise((resolve, reject) => {
98
- // eslint-disable-next-line functional/immutable-data
99
- stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
100
- stream.on('error', (err) => reject(err));
133
+ stream.on('data', (chunk: Buffer) => chunks.push(Buffer.from(chunk)));
134
+ stream.on('error', (err: unknown) => reject(err));
101
135
  stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
102
136
  });
103
137
  }
@@ -9,7 +9,7 @@ import {
9
9
  createRecordResponseItem,
10
10
  addRecordResponseItem,
11
11
  addRecordResponseItems
12
- } from '../src/utils.js';
12
+ } from '../src/utils.ts';
13
13
 
14
14
  test('createImportJobState should return state object for valid operation and state', () => {
15
15
  const result = createImportJobState('CREATE', 'EMPTY');
@@ -267,9 +267,9 @@ test('createRecordResponseItem should map UNKNOWN status to ERROR with ids', ()
267
267
  });
268
268
 
269
269
  test('addRecordResponseItem should push the recordResponseItem via mongoOperator', async () => {
270
- let captured;
270
+ let captured: unknown;
271
271
  const mongoOperator = {
272
- pushMessages: async (args) => {
272
+ pushMessages: async (args: unknown) => {
273
273
  captured = args;
274
274
  }
275
275
  };
@@ -304,10 +304,10 @@ test('addRecordResponseItem should throw for non-object input', async () => {
304
304
  });
305
305
 
306
306
  test('addRecordResponseItems should push all items in one call', async () => {
307
- let captured;
307
+ let captured: unknown;
308
308
  let callCount = 0;
309
309
  const mongoOperator = {
310
- pushMessages: async (args) => {
310
+ pushMessages: async (args: unknown) => {
311
311
  callCount += 1;
312
312
  captured = args;
313
313
  }
package/tsconfig.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "include": [
3
+ "src"
4
+ ],
5
+ "exclude": [
6
+ "node_modules",
7
+ "dist"
8
+ ],
9
+ "compilerOptions": {
10
+ // File Layout
11
+ "rootDir": "./src",
12
+ "outDir": "./dist",
13
+ // Environment Settings
14
+ "module": "nodenext",
15
+ "moduleResolution": "nodenext",
16
+ "target": "esnext",
17
+ "typeRoots": [
18
+ "./node_modules/@types"
19
+ ],
20
+ "types": [
21
+ "node"
22
+ ],
23
+ "lib": [
24
+ "esnext"
25
+ ],
26
+ // Other Outputs
27
+ "sourceMap": true,
28
+ "declaration": true,
29
+ "declarationMap": true,
30
+ // Stricter Typechecking Options
31
+ "noUncheckedIndexedAccess": true,
32
+ // Style Options
33
+ "noImplicitReturns": true,
34
+ "noImplicitOverride": true,
35
+ "noUnusedLocals": true,
36
+ "noUnusedParameters": true,
37
+ "noFallthroughCasesInSwitch": true,
38
+ "noPropertyAccessFromIndexSignature": true,
39
+ // Recommended Options
40
+ "strict": true,
41
+ "isolatedModules": true,
42
+ "noUncheckedSideEffectImports": true,
43
+ "moduleDetection": "force",
44
+ "skipLibCheck": true,
45
+ "allowImportingTsExtensions": true,
46
+ "noEmit": false,
47
+ // Customized options
48
+ "allowJs": true,
49
+ "checkJs": true,
50
+ "esModuleInterop": true,
51
+ "allowUnreachableCode": false,
52
+ "strictFunctionTypes": true,
53
+ "strictNullChecks": true,
54
+ "removeComments": true,
55
+ // Only allow syntax accepted by Node >= 23.6
56
+ "erasableSyntaxOnly": true,
57
+ // Proposed options from Node docs: https://nodejs.org/api/typescript.html#type-stripping
58
+ "rewriteRelativeImportExtensions": true,
59
+ "verbatimModuleSyntax": true,
60
+ "forceConsistentCasingInFileNames": true
61
+ }
62
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "include": [
4
+ "src",
5
+ "test"
6
+ ],
7
+ "exclude": [
8
+ "node_modules",
9
+ "dist"
10
+ ],
11
+ "compilerOptions": {
12
+ // Typechecking only — never emits. The build config (tsconfig.json)
13
+ // keeps rootDir ./src so `npm run build` still emits only src/ -> dist/.
14
+ "noEmit": true,
15
+ "rootDir": "."
16
+ }
17
+ }
@@ -1,39 +0,0 @@
1
- export function removeSubfield(field, code) {
2
- if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {
3
- const filteredSubfields = field.subfields.filter((sf) => sf.code !== code);
4
- if (filteredSubfields.length < 1) {
5
- return false;
6
- }
7
- return {
8
- tag: field.tag,
9
- ind1: field.ind1,
10
- ind2: field.ind2,
11
- subfields: filteredSubfields
12
- };
13
- }
14
- return field;
15
- }
16
- export function sortFieldsBySubfieldValue(fields, subfieldCode) {
17
- return [...fields].sort((a, b) => {
18
- const a1value = getFirstSubfieldValue(a, subfieldCode);
19
- const b1value = getFirstSubfieldValue(b, subfieldCode);
20
- if (a1value && !b1value) {
21
- return -1;
22
- }
23
- if (!a1value && b1value) {
24
- return 1;
25
- }
26
- if (a1value > b1value) {
27
- return 1;
28
- }
29
- if (b1value > a1value) {
30
- return -1;
31
- }
32
- return 0;
33
- });
34
- function getFirstSubfieldValue(field, subfieldCode2) {
35
- const subs = field.subfields ? field.subfields.filter((subf) => subf.code === subfieldCode2) : [];
36
- return subs.length > 0 ? subs[0].value : "";
37
- }
38
- }
39
- //# sourceMappingURL=fix-utils.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/helpers/fix-utils.js"],
4
- "sourcesContent": ["export function removeSubfield(field, code) {\n\n // Handle non-numeric fields, and fields with a numeric tag of 010 and greater\n // Aleph's FMT as a controlfield might be a problem\n if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {\n\n const filteredSubfields = field.subfields.filter(sf => sf.code !== code);\n\n // Remove whole field if there are no subfields left\n if (filteredSubfields.length < 1) {\n return false;\n }\n\n return {\n tag: field.tag,\n ind1: field.ind1,\n ind2: field.ind2,\n subfields: filteredSubfields\n };\n }\n // return controlFields as is\n return field;\n}\n\n\n// sort fields by value of each fields first subfield with subfielCode\nexport function sortFieldsBySubfieldValue(fields, subfieldCode) {\n return [...fields].sort((a, b) => {\n const a1value = getFirstSubfieldValue(a, subfieldCode);\n const b1value = getFirstSubfieldValue(b, subfieldCode);\n if (a1value && !b1value) {\n return -1;\n }\n if (!a1value && b1value) {\n return 1;\n }\n if (a1value > b1value) {\n return 1;\n }\n if (b1value > a1value) {\n return -1;\n }\n return 0;\n });\n // get value for the for instance of subfield with subfieldCode\n function getFirstSubfieldValue(field, subfieldCode) {\n const subs = field.subfields ? field.subfields.filter(subf => subf.code === subfieldCode) : [];\n return subs.length > 0 ? subs[0].value : '';\n }\n}\n"],
5
- "mappings": "AAAO,gBAAS,eAAe,OAAO,MAAM;AAI1C,MAAI,CAAC,MAAM,MAAM,GAAG,KAAK,SAAS,MAAM,KAAK,EAAE,KAAK,IAAI;AAEtD,UAAM,oBAAoB,MAAM,UAAU,OAAO,QAAM,GAAG,SAAS,IAAI;AAGvE,QAAI,kBAAkB,SAAS,GAAG;AAChC,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,MACL,KAAK,MAAM;AAAA,MACX,MAAM,MAAM;AAAA,MACZ,MAAM,MAAM;AAAA,MACZ,WAAW;AAAA,IACb;AAAA,EACF;AAEA,SAAO;AACT;AAIO,gBAAS,0BAA0B,QAAQ,cAAc;AAC9D,SAAO,CAAC,GAAG,MAAM,EAAE,KAAK,CAAC,GAAG,MAAM;AAChC,UAAM,UAAU,sBAAsB,GAAG,YAAY;AACrD,UAAM,UAAU,sBAAsB,GAAG,YAAY;AACrD,QAAI,WAAW,CAAC,SAAS;AACvB,aAAO;AAAA,IACT;AACA,QAAI,CAAC,WAAW,SAAS;AACvB,aAAO;AAAA,IACT;AACA,QAAI,UAAU,SAAS;AACrB,aAAO;AAAA,IACT;AACA,QAAI,UAAU,SAAS;AACrB,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT,CAAC;AAED,WAAS,sBAAsB,OAAOA,eAAc;AAClD,UAAM,OAAO,MAAM,YAAY,MAAM,UAAU,OAAO,UAAQ,KAAK,SAASA,aAAY,IAAI,CAAC;AAC7F,WAAO,KAAK,SAAS,IAAI,KAAK,CAAC,EAAE,QAAQ;AAAA,EAC3C;AACF;",
6
- "names": ["subfieldCode"]
7
- }
package/eslint.config.js DELETED
@@ -1,53 +0,0 @@
1
- // Eslint configuration object for src
2
- const configSrc = {
3
- files: [
4
- "src/*",
5
- "tests/*"
6
- ],
7
- linterOptions: {
8
- reportUnusedDisableDirectives: true,
9
- },
10
- rules: {
11
- "no-console": "warn",
12
- "eqeqeq": ["error", "always"],
13
- "no-const-assign": "error",
14
- "max-depth": ["warn", 4],
15
- "max-lines": ["warn", 500],
16
- "max-lines-per-function": ["warn", {"max": 100}],
17
- "no-else-return": ["error", {allowElseIf: false}],
18
- "no-plusplus": [
19
- "error",
20
- {
21
- "allowForLoopAfterthoughts": true
22
- }
23
- ],
24
- "array-callback-return": [
25
- "error",
26
- {
27
- "checkForEach": false
28
- }
29
- ],
30
- "no-unused-vars": [
31
- "error",
32
- {
33
- "argsIgnorePattern": "next"
34
- }
35
- ],
36
- "no-warning-comments": "off"
37
- }
38
- };
39
-
40
- // Eslint configuration object for globally ignoring .js files
41
- // - ignore all files that start with a dot
42
- // - ignore all files inside directories named 'dist'
43
- const configIgnores = {
44
- ignores: [
45
- "**/.*",
46
- "**/dist/"
47
- ]
48
- };
49
-
50
- export default [
51
- configSrc,
52
- configIgnores
53
- ];