strict-qs 8.0.3 → 9.0.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
@@ -25,7 +25,7 @@
25
25
  }
26
26
  },
27
27
  "name": "strict-qs",
28
- "version": "8.0.3",
28
+ "version": "9.0.0",
29
29
  "description": "A stricter Query String parser",
30
30
  "type": "module",
31
31
  "main": "dist/index.js",
@@ -34,20 +34,19 @@
34
34
  "build": "rimraf 'dist' && tsc --outDir dist",
35
35
  "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md",
36
36
  "cli": "env NODE_ENV=${NODE_ENV:-cli}",
37
- "cover": "npm run jest -- --coverage",
38
- "cz": "env NODE_ENV=${NODE_ENV:-cli} git cz",
37
+ "commitlint": "commitlint",
38
+ "cover": "node --run jest -- --coverage",
39
39
  "doc": "echo \"# API\" > API.md; jsdoc2md dist/*.js >> API.md && git add API.md",
40
- "format": "npm run prettier",
40
+ "format": "node --run prettier",
41
41
  "jest": "NODE_OPTIONS=--experimental-vm-modules NODE_ENV=test jest",
42
42
  "lint": "eslint src/*.ts",
43
43
  "metapak": "metapak",
44
- "precz": "npm t && npm run lint && npm run build && npm run doc && npm run metapak -- -s",
44
+ "precommit": "node --run test && node --run lint && node --run metapak -- -s && node --run build && node --run doc",
45
45
  "prettier": "prettier --write src/*.ts",
46
- "preversion": "npm t && npm run lint && npm run build && npm run doc && npm run metapak -- -s",
46
+ "preversion": "node --run test && node --run lint && node --run build && node --run doc && node --run metapak -- -s",
47
47
  "rebuild": "swc ./src -s -d dist -C jsc.target=es2022",
48
- "test": "npm run jest",
49
- "type-check": "tsc --pretty --noEmit",
50
- "version": "npm run changelog"
48
+ "test": "node --run jest",
49
+ "version": "node --run changelog"
51
50
  },
52
51
  "repository": {
53
52
  "type": "git",
@@ -70,59 +69,35 @@
70
69
  },
71
70
  "homepage": "https://github.com/nfroidure/strict-qs#readme",
72
71
  "devDependencies": {
73
- "@eslint/js": "^9.7.0",
74
- "@swc/cli": "^0.4.0",
75
- "@swc/core": "^1.6.13",
76
- "@swc/helpers": "^0.5.12",
77
- "@swc/jest": "^0.2.36",
78
- "commitizen": "^4.3.0",
79
- "conventional-changelog-cli": "^5.0.0",
80
- "cz-conventional-changelog": "^3.3.0",
81
- "eslint": "^9.7.0",
82
- "eslint-config-prettier": "^9.1.0",
83
- "eslint-plugin-jest": "^28.6.0",
84
- "eslint-plugin-prettier": "^5.1.3",
85
- "jest": "^29.7.0",
86
- "jsdoc-to-markdown": "^8.0.0",
87
- "metapak": "^6.0.1",
88
- "metapak-nfroidure": "18.2.0",
89
- "prettier": "^3.3.3",
90
- "rimraf": "^6.0.1",
91
- "typescript": "^5.5.3",
92
- "typescript-eslint": "^7.16.0"
72
+ "@commitlint/cli": "^20.5.0",
73
+ "@commitlint/config-conventional": "^20.5.0",
74
+ "@eslint/js": "^10.0.1",
75
+ "@swc/cli": "^0.8.0",
76
+ "@swc/core": "^1.15.21",
77
+ "@swc/jest": "^0.2.39",
78
+ "@types/debug": "^4.1.13",
79
+ "conventional-changelog": "^7.2.0",
80
+ "eslint": "^10.1.0",
81
+ "eslint-config-prettier": "^10.1.8",
82
+ "eslint-plugin-jest": "^29.15.1",
83
+ "eslint-plugin-prettier": "^5.5.5",
84
+ "jest": "^30.3.0",
85
+ "jsdoc-to-markdown": "^9.1.3",
86
+ "metapak": "^7.0.0",
87
+ "metapak-nfroidure": "22.0.1",
88
+ "prettier": "^3.8.1",
89
+ "rimraf": "^6.1.3",
90
+ "typescript": "^6.0.2",
91
+ "typescript-eslint": "^8.57.2"
93
92
  },
94
93
  "dependencies": {
95
- "debug": "^4.3.5",
96
- "yerror": "^8.0.0"
94
+ "debug": "^4.4.0",
95
+ "yerror": "^9.0.0"
97
96
  },
98
97
  "engines": {
99
- "node": ">=20.11.1"
100
- },
101
- "config": {
102
- "commitizen": {
103
- "path": "./node_modules/cz-conventional-changelog"
104
- }
105
- },
106
- "greenkeeper": {
107
- "ignore": [
108
- "commitizen",
109
- "cz-conventional-changelog",
110
- "conventional-changelog-cli",
111
- "typescript",
112
- "rimraf",
113
- "@swc/cli",
114
- "@swc/core",
115
- "@swc/helpers",
116
- "eslint",
117
- "prettier",
118
- "eslint-config-prettier",
119
- "eslint-plugin-prettier",
120
- "typescript-eslint",
121
- "jsdoc-to-markdown",
122
- "jest",
123
- "@swc/jest"
124
- ]
98
+ "node": ">=24.14.0"
125
99
  },
100
+ "config": {},
126
101
  "contributors": [],
127
102
  "files": [
128
103
  "dist",
@@ -160,10 +135,21 @@
160
135
  },
161
136
  "extensionsToTreatAsEsm": [
162
137
  ".ts"
163
- ],
164
- "prettierPath": null
138
+ ]
139
+ },
140
+ "funding": {
141
+ "type": "individual",
142
+ "url": "https://github.com/sponsors/nfroidure"
165
143
  },
166
144
  "overrides": {
167
- "eslint": "^9.7.0"
145
+ "typescript-eslint": {
146
+ "typescript": "^6"
147
+ },
148
+ "@typescript-eslint/eslint-plugin": {
149
+ "typescript": "^6"
150
+ },
151
+ "@typescript-eslint/parser": {
152
+ "typescript": "^6"
153
+ }
168
154
  }
169
155
  }
package/src/index.test.ts CHANGED
@@ -1,37 +1,45 @@
1
- import { describe, test } from '@jest/globals';
2
- import assert from 'assert';
3
- import { qsStrict } from './index.js';
4
- import type { QSParameter } from './index.js';
1
+ import { describe, test, expect } from '@jest/globals';
2
+ import { qsStrict, type QSParameter } from './index.js';
3
+ import { YError } from 'yerror';
5
4
 
6
5
  describe('strict-qs', () => {
7
6
  describe('with no search', () => {
8
- const qsDefinition = [];
7
+ const qsDefinition: QSParameter[] = [];
9
8
 
10
9
  test('should work', () => {
11
- assert.deepEqual(qsStrict({}, qsDefinition, ''), {});
10
+ expect(qsStrict({}, qsDefinition, '')).toEqual({});
12
11
  });
13
12
  });
14
13
 
15
14
  describe('with bad search', () => {
16
- const qsDefinition = [];
15
+ const qsDefinition: QSParameter[] = [];
17
16
 
18
17
  test('should fail', () => {
19
- assert.throws(() => {
18
+ try {
20
19
  qsStrict({}, qsDefinition, '?');
21
- }, /E_EMPTY_SEARCH/);
20
+ throw new YError('E_UNEXPECTED_SUCCESS');
21
+ } catch (err) {
22
+ expect(err).toMatchInlineSnapshot(
23
+ `[YError: E_EMPTY_SEARCH (["?"]): E_EMPTY_SEARCH]`,
24
+ );
25
+ }
22
26
  });
23
27
 
24
28
  test('should work when allowed', () => {
25
- assert.deepEqual(
26
- qsStrict({ allowEmptySearch: true }, qsDefinition, '?'),
29
+ expect(qsStrict({ allowEmptySearch: true }, qsDefinition, '?')).toEqual(
27
30
  {},
28
31
  );
29
32
  });
30
33
 
31
34
  test('should fail', () => {
32
- assert.throws(() => {
35
+ try {
33
36
  qsStrict({}, qsDefinition, 'lol');
34
- }, /E_MALFORMED_SEARCH/);
37
+ throw new YError('E_UNEXPECTED_SUCCESS');
38
+ } catch (err) {
39
+ expect(err).toMatchInlineSnapshot(
40
+ `[YError: E_MALFORMED_SEARCH (["lol"]): E_MALFORMED_SEARCH]`,
41
+ );
42
+ }
35
43
  });
36
44
  });
37
45
 
@@ -47,9 +55,14 @@ describe('strict-qs', () => {
47
55
  ];
48
56
 
49
57
  test('should fail', () => {
50
- assert.throws(() => {
58
+ try {
51
59
  qsStrict({}, qsDefinition, '?user=lol');
52
- }, /E_UNSUPPORTED_TYPE/);
60
+ throw new YError('E_UNEXPECTED_SUCCESS');
61
+ } catch (err) {
62
+ expect(err).toMatchInlineSnapshot(
63
+ `[YError: E_UNSUPPORTED_TYPE (["user","object"]): E_UNSUPPORTED_TYPE]`,
64
+ );
65
+ }
53
66
  });
54
67
  });
55
68
 
@@ -63,20 +76,24 @@ describe('strict-qs', () => {
63
76
  type: 'number',
64
77
  },
65
78
  ordered: true,
66
- description: 'The pages to print',
67
79
  },
68
80
  ];
69
81
 
70
82
  test('should work when params are ordered', () => {
71
- assert.deepEqual(qsStrict({}, qsDefinition, '?pages=0&pages=1&pages=2'), {
72
- pages: [0, 1, 2], // eslint-disable-line
83
+ expect(qsStrict({}, qsDefinition, '?pages=0&pages=1&pages=2')).toEqual({
84
+ pages: [0, 1, 2],
73
85
  });
74
86
  });
75
87
 
76
88
  test('should fail when params are not ordered', () => {
77
- assert.throws(() => {
89
+ try {
78
90
  qsStrict({}, qsDefinition, '?pages=0&pages=2&pages=1');
79
- }, /E_UNORDERED_QUERY_PARAMS/);
91
+ throw new YError('E_UNEXPECTED_SUCCESS');
92
+ } catch (err) {
93
+ expect(err).toMatchInlineSnapshot(
94
+ `[YError: E_UNORDERED_QUERY_PARAMS ([1,2]): E_UNORDERED_QUERY_PARAMS]`,
95
+ );
96
+ }
80
97
  });
81
98
  });
82
99
 
@@ -90,16 +107,15 @@ describe('strict-qs', () => {
90
107
  ];
91
108
 
92
109
  test('should work', () => {
93
- assert.deepEqual(
110
+ expect(
94
111
  qsStrict(
95
112
  {},
96
113
  qsDefinition,
97
114
  '?redirectURL=' + encodeURIComponent('http://localhost/plop'),
98
115
  ),
99
- {
100
- redirectURL: 'http://localhost/plop',
101
- },
102
- );
116
+ ).toEqual({
117
+ redirectURL: 'http://localhost/plop',
118
+ });
103
119
  });
104
120
  });
105
121
 
@@ -113,7 +129,7 @@ describe('strict-qs', () => {
113
129
  ];
114
130
 
115
131
  test('should work', () => {
116
- assert.deepEqual(qsStrict({}, qsDefinition, '?query=a+b+c'), {
132
+ expect(qsStrict({}, qsDefinition, '?query=a+b+c')).toEqual({
117
133
  query: 'a b c',
118
134
  });
119
135
  });
@@ -128,7 +144,6 @@ describe('strict-qs', () => {
128
144
  required: true,
129
145
  default: 'en',
130
146
  enum: ['fr', 'en', 'de'],
131
- description: 'The language for the search',
132
147
  },
133
148
  {
134
149
  name: 'types',
@@ -138,20 +153,17 @@ describe('strict-qs', () => {
138
153
  type: 'string',
139
154
  enum: ['open', 'closed', 'pending', 'idle', 'invalid'],
140
155
  },
141
- description: 'The types of the search',
142
156
  },
143
157
  {
144
158
  name: 'code',
145
159
  in: 'query',
146
160
  type: 'number',
147
161
  pattern: '^[0-9]+$',
148
- description: 'The code id',
149
162
  },
150
163
  {
151
164
  name: 'full',
152
165
  in: 'query',
153
166
  type: 'boolean',
154
- description: 'Wether it is a full search or not',
155
167
  },
156
168
  {
157
169
  name: 'nums',
@@ -159,204 +171,243 @@ describe('strict-qs', () => {
159
171
  type: 'array',
160
172
  items: {
161
173
  type: 'number',
162
- enum: [1, 2, 3, 4], // eslint-disable-line
174
+ enum: [1, 2, 3, 4],
163
175
  },
164
- description: 'The types of the search',
165
176
  },
166
177
  ];
167
178
 
168
179
  test('should work with good params', () => {
169
- assert.deepEqual(
180
+ expect(
170
181
  qsStrict(
171
182
  {},
172
183
  qsDefinition,
173
184
  '?lang=fr&types=open&types=closed&types=pending&code=3&full=true',
174
185
  ),
175
- {
176
- lang: 'fr',
177
- types: ['open', 'closed', 'pending'],
178
- code: 3,
179
- full: true,
180
- },
181
- );
182
- assert.throws(
183
- qsStrict.bind(
184
- null,
186
+ ).toEqual({
187
+ lang: 'fr',
188
+ types: ['open', 'closed', 'pending'],
189
+ code: 3,
190
+ full: true,
191
+ });
192
+ try {
193
+ qsStrict(
185
194
  {},
186
195
  qsDefinition,
187
196
  '?lang=cn&types=open&types=closed&types=pending&code=3&full=true',
188
- ),
189
- /E_NOT_IN_ENUM/,
190
- );
191
- assert.throws(
192
- qsStrict.bind(
193
- null,
197
+ );
198
+ throw new YError('E_UNEXPECTED_SUCCESS');
199
+ } catch (err) {
200
+ expect(err).toMatchInlineSnapshot(
201
+ `[YError: E_NOT_IN_ENUM (["lang","cn"]): E_NOT_IN_ENUM]`,
202
+ );
203
+ }
204
+ try {
205
+ qsStrict(
194
206
  {},
195
207
  qsDefinition,
196
208
  '?lang=fr&types=open&types=closed&types=pending&code=3.4&full=true',
197
- ),
198
- /E_PATTERN_DOES_NOT_MATCH/,
199
- );
209
+ );
210
+ throw new YError('E_UNEXPECTED_SUCCESS');
211
+ } catch (err) {
212
+ expect(err).toMatchInlineSnapshot(
213
+ `[YError: E_PATTERN_DOES_NOT_MATCH (["code",3.4]): E_PATTERN_DOES_NOT_MATCH]`,
214
+ );
215
+ }
200
216
  });
201
217
 
202
218
  test('should work with all params', () => {
203
- assert.deepEqual(
219
+ expect(
204
220
  qsStrict(
205
221
  {},
206
222
  qsDefinition,
207
223
  '?lang=fr&types=open&types=closed&types=pending&code=3&full=false&nums=4',
208
224
  ),
209
- {
210
- lang: 'fr',
211
- types: ['open', 'closed', 'pending'],
212
- code: 3,
213
- full: false,
214
- nums: [4], // eslint-disable-line
215
- },
216
- );
225
+ ).toEqual({
226
+ lang: 'fr',
227
+ types: ['open', 'closed', 'pending'],
228
+ code: 3,
229
+ full: false,
230
+ nums: [4],
231
+ });
217
232
  });
218
233
 
219
234
  test('should fail when required params are not in order', () => {
220
- assert.throws(() => {
235
+ try {
221
236
  qsStrict(
222
237
  {},
223
238
  qsDefinition,
224
239
  '?lang=fr&types=open&lang=hop&types=closed&types=pending&code=3&full=true',
225
240
  );
226
- }, /E_BAD_QUERY_PARAM_POSITION/);
241
+ throw new YError('E_UNEXPECTED_SUCCESS');
242
+ } catch (err) {
243
+ expect(err).toMatchInlineSnapshot(
244
+ `[YError: E_BAD_QUERY_PARAM_POSITION (["lang",0,2]): E_BAD_QUERY_PARAM_POSITION]`,
245
+ );
246
+ }
227
247
  });
228
248
 
229
249
  test('should work when required params are not in order with option allowUnorderedParams', () => {
230
- assert.deepEqual(
250
+ expect(
231
251
  qsStrict(
232
252
  { allowUnorderedParams: true },
233
253
  qsDefinition,
234
254
  '?lang=fr&types=open&lang=fr&types=closed&types=pending&code=3&full=true',
235
255
  ),
236
- {
237
- lang: 'fr',
238
- types: ['open', 'closed', 'pending'],
239
- code: 3,
240
- full: true,
241
- },
242
- );
256
+ ).toEqual({
257
+ lang: 'fr',
258
+ types: ['open', 'closed', 'pending'],
259
+ code: 3,
260
+ full: true,
261
+ });
243
262
  });
244
263
 
245
264
  test('should work when required params are not in order with option allowUnorderedParams and allowUnknownParams', () => {
246
- assert.deepEqual(
265
+ expect(
247
266
  qsStrict(
248
267
  { allowUnorderedParams: true, allowUnknownParams: true },
249
268
  qsDefinition,
250
269
  '?lang=fr&types=open&pippip=yeah&lang=fr&types=closed&types=pending&code=3&full=true',
251
270
  ),
252
- {
253
- lang: 'fr',
254
- types: ['open', 'closed', 'pending'],
255
- code: 3,
256
- full: true,
257
- },
258
- );
271
+ ).toEqual({
272
+ lang: 'fr',
273
+ types: ['open', 'closed', 'pending'],
274
+ code: 3,
275
+ full: true,
276
+ });
259
277
  });
260
278
 
261
279
  test('should fail when required params are not provided', () => {
262
- assert.throws(() => {
280
+ try {
263
281
  qsStrict(
264
282
  {},
265
283
  qsDefinition,
266
284
  '?types=open&types=closed&types=pending&code=3&full=true',
267
285
  );
268
- }, /E_REQUIRED_QUERY_PARAM/);
286
+ throw new YError('E_UNEXPECTED_SUCCESS');
287
+ } catch (err) {
288
+ expect(err).toMatchInlineSnapshot(
289
+ `[YError: E_REQUIRED_QUERY_PARAM (["lang"]): E_REQUIRED_QUERY_PARAM]`,
290
+ );
291
+ }
269
292
  });
270
293
 
271
294
  test('should fail when a bad boolean is provided', () => {
272
- assert.throws(() => {
295
+ try {
273
296
  qsStrict(
274
297
  {},
275
298
  qsDefinition,
276
299
  '?lang=fr&types=open&types=closed&types=pending&code=3&full=1',
277
300
  );
278
- }, /E_BAD_BOOLEAN/);
301
+ throw new YError('E_UNEXPECTED_SUCCESS');
302
+ } catch (err) {
303
+ expect(err).toMatchInlineSnapshot(
304
+ `[YError: E_BAD_BOOLEAN (["1"]): E_BAD_BOOLEAN]`,
305
+ );
306
+ }
279
307
  });
280
308
 
281
309
  test('should fail when a bad array item is given', () => {
282
- assert.throws(() => {
310
+ try {
283
311
  qsStrict(
284
312
  {},
285
313
  qsDefinition,
286
314
  '?lang=fr&types=open&types=closed&types=pending&code=3&full=true&nums=1&nums=0.0',
287
315
  );
288
- }, /E_NON_REENTRANT_NUMBER/);
316
+ throw new YError('E_UNEXPECTED_SUCCESS');
317
+ } catch (err) {
318
+ expect(err).toMatchInlineSnapshot(
319
+ `[YError: E_NON_REENTRANT_NUMBER (["0.0","0"]): E_NON_REENTRANT_NUMBER]`,
320
+ );
321
+ }
289
322
  });
290
323
 
291
324
  test('should work whith an unexisting param and the allowUnknownParams option', () => {
292
- assert.deepEqual(
325
+ expect(
293
326
  qsStrict(
294
327
  { allowUnknownParams: true },
295
328
  qsDefinition,
296
329
  '?lol=9&lang=fr&types=open&types=closed&types=pending&code=3&full=true',
297
330
  ),
298
- {
299
- code: 3,
300
- full: true,
301
- lang: 'fr',
302
- types: ['open', 'closed', 'pending'],
303
- },
304
- );
331
+ ).toEqual({
332
+ code: 3,
333
+ full: true,
334
+ lang: 'fr',
335
+ types: ['open', 'closed', 'pending'],
336
+ });
305
337
  });
306
338
 
307
339
  test('should fail when an unexisting param is set at the begin', () => {
308
- assert.throws(() => {
340
+ try {
309
341
  qsStrict(
310
342
  {},
311
343
  qsDefinition,
312
344
  '?lol=9&lang=fr&types=open&types=closed&types=pending&code=3&full=true',
313
345
  );
314
- }, /E_UNAUTHORIZED_QUERY_PARAM/);
346
+ throw new YError('E_UNEXPECTED_SUCCESS');
347
+ } catch (err) {
348
+ expect(err).toMatchInlineSnapshot(
349
+ `[YError: E_UNAUTHORIZED_QUERY_PARAM (["lol"]): E_UNAUTHORIZED_QUERY_PARAM]`,
350
+ );
351
+ }
315
352
  });
316
353
 
317
354
  test('should fail when an unexisting param is set in the middle', () => {
318
- assert.throws(() => {
355
+ try {
319
356
  qsStrict(
320
357
  {},
321
358
  qsDefinition,
322
359
  '?lang=fr&types=open&types=closed&types=pendinglol=9&&code=3&full=true',
323
360
  );
324
- }, /E_UNAUTHORIZED_QUERY_PARAM/);
361
+ throw new YError('E_UNEXPECTED_SUCCESS');
362
+ } catch (err) {
363
+ expect(err).toMatchInlineSnapshot(
364
+ `[YError: E_UNAUTHORIZED_QUERY_PARAM ([""]): E_UNAUTHORIZED_QUERY_PARAM]`,
365
+ );
366
+ }
325
367
  });
326
368
 
327
369
  test('should fail when an unexisting param is set at the end', () => {
328
- assert.throws(() => {
370
+ try {
329
371
  qsStrict(
330
372
  {},
331
373
  qsDefinition,
332
374
  '?lang=fr&types=open&types=closed&types=pending&code=3&full=true&lol=9',
333
375
  );
334
- }, /E_UNAUTHORIZED_QUERY_PARAM/);
376
+ throw new YError('E_UNEXPECTED_SUCCESS');
377
+ } catch (err) {
378
+ expect(err).toMatchInlineSnapshot(
379
+ `[YError: E_UNAUTHORIZED_QUERY_PARAM (["lol"]): E_UNAUTHORIZED_QUERY_PARAM]`,
380
+ );
381
+ }
335
382
  });
336
383
 
337
384
  test('should fail when setting a query param to the default value', () => {
338
- assert.throws(() => {
385
+ try {
339
386
  qsStrict(
340
387
  {},
341
388
  qsDefinition,
342
389
  '?lang=en&types=open&types=closed&types=pending&code=3&full=true',
343
390
  );
344
- }, /E_CANNOT_SET_TO_DEFAULT/);
391
+ throw new YError('E_UNEXPECTED_SUCCESS');
392
+ } catch (err) {
393
+ expect(err).toMatchInlineSnapshot(
394
+ `[YError: E_CANNOT_SET_TO_DEFAULT (["lang","en"]): E_CANNOT_SET_TO_DEFAULT]`,
395
+ );
396
+ }
345
397
  });
346
398
  test('should work when setting a query param to the default value and allowDefault option', () => {
347
- assert.deepEqual(
399
+ expect(
348
400
  qsStrict(
349
401
  { allowDefault: true },
350
402
  qsDefinition,
351
403
  '?lang=en&types=open&types=closed&types=pending&code=3&full=true',
352
404
  ),
353
- {
354
- code: 3,
355
- full: true,
356
- lang: 'en',
357
- types: ['open', 'closed', 'pending'],
358
- },
359
- );
405
+ ).toEqual({
406
+ code: 3,
407
+ full: true,
408
+ lang: 'en',
409
+ types: ['open', 'closed', 'pending'],
410
+ });
360
411
  });
361
412
  });
362
413
  });