Skip to content

Commit a2325d2

Browse files
committed
chore: Updated build system.
1 parent 0c92692 commit a2325d2

File tree

9 files changed

+61
-58
lines changed

9 files changed

+61
-58
lines changed

package.json

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,37 @@
2828
"typings": "./types/index.d.ts",
2929
"types": "./types/index.d.ts",
3030
"scripts": {
31-
"prebuild": "rm -rf dist types && npm run lint",
32-
"build": "tsc -p . && esm-pkg-add-imports-extensions dist",
31+
"dev": "swc -w -d dist src",
32+
"prebuild": "rm -rf dist types && npm run typecheck && npm run lint",
33+
"build": "swc --delete-dir-on-start -d dist src",
3334
"format": "prettier -w src test",
35+
"typecheck": "tsc -p . --emitDeclarationOnly",
3436
"lint": "eslint src test",
35-
"test": "c8 --reporter=text --reporter=html esm-ts-tap --reporter=spec --no-coverage test/*.test.ts",
36-
"test:ci": "c8 --reporter=text --reporter=json --check-coverage --branches 90 --functions 90 --lines 90 --statements 90 esm-ts-tap --no-color --no-coverage test/*.test.ts",
37-
"ci": "npm run lint && npm run test:ci",
37+
"test": "c8 -c test/config/c8-local.json tap --rcfile=test/config/tap.yml test/*.test.ts",
38+
"test:ci": "c8 -c test/config/c8-ci.json tap --rcfile=test/config/tap.yml --no-color test/*.test.ts",
39+
"ci": "npm run build && npm run test:ci",
3840
"prepublishOnly": "npm run ci",
3941
"postpublish": "git push origin && git push origin -f --tags"
4042
},
4143
"dependencies": {
42-
"ajv": "^8.6.2",
43-
"fastify-plugin": "^3.0.0",
44-
"http-errors-enhanced": "^1.0.3"
44+
"ajv": "^8.10.0",
45+
"fastify-plugin": "^3.0.1",
46+
"http-errors-enhanced": "^1.0.4"
4547
},
4648
"devDependencies": {
47-
"@cowtech/eslint-config": "^8.0.1",
48-
"@cowtech/esm-package-utils": "^0.9.3",
49-
"@types/node": "^17.0.2",
50-
"@types/tap": "^15.0.5",
51-
"ajv-formats": "^2.1.0",
52-
"c8": "^7.8.0",
53-
"fastify": "^3.20.2",
54-
"prettier": "^2.3.2",
55-
"tap": "^15.0.9",
56-
"ts-node": "^10.2.0",
57-
"typescript": "^4.2.4"
49+
"@cowtech/eslint-config": "^8.4.0",
50+
"@swc/cli": "^0.1.55",
51+
"@swc/core": "^1.2.150",
52+
"@types/node": "^17.0.21",
53+
"@types/tap": "^15.0.6",
54+
"ajv-formats": "^2.1.1",
55+
"c8": "^7.11.0",
56+
"chokidar": "^3.5.3",
57+
"fastify": "^3.27.2",
58+
"prettier": "^2.5.1",
59+
"tap": "^15.2.3",
60+
"ts-node": "^10.7.0",
61+
"typescript": "^4.6.2"
5862
},
5963
"engines": {
6064
"node": ">=14.15.0"

src/handlers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
serializeError,
1111
UnsupportedMediaTypeError
1212
} from 'http-errors-enhanced'
13-
import { GenericObject, kHttpErrorsEnhancedConfiguration, NodeError, RequestSection } from './interfaces'
14-
import { upperFirst } from './utils'
15-
import { convertValidationErrors, validationMessagesFormatters, ValidationResult } from './validation'
13+
import { GenericObject, kHttpErrorsEnhancedConfiguration, NodeError, RequestSection } from './interfaces.js'
14+
import { upperFirst } from './utils.js'
15+
import { convertValidationErrors, validationMessagesFormatters, ValidationResult } from './validation.js'
1616

1717
export function handleNotFoundError(request: FastifyRequest, reply: FastifyReply): void {
1818
handleErrors(new NotFoundError('Not found.'), request, reply)

src/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify'
22
import fastifyPlugin from 'fastify-plugin'
3-
import { handleErrors, handleNotFoundError } from './handlers'
4-
import { Configuration, kHttpErrorsEnhancedConfiguration, kHttpErrorsEnhancedResponseValidations } from './interfaces'
5-
import { addResponseValidation, compileResponseValidationSchema } from './validation'
6-
7-
export * from './handlers'
8-
export * from './interfaces'
9-
export { convertValidationErrors, niceJoin, validationMessagesFormatters } from './validation'
3+
import { handleErrors, handleNotFoundError } from './handlers.js'
4+
import {
5+
Configuration,
6+
kHttpErrorsEnhancedConfiguration,
7+
kHttpErrorsEnhancedResponseValidations
8+
} from './interfaces.js'
9+
import { addResponseValidation, compileResponseValidationSchema } from './validation.js'
10+
11+
export * from './handlers.js'
12+
export * from './interfaces.js'
13+
export { convertValidationErrors, niceJoin, validationMessagesFormatters } from './validation.js'
1014

1115
export const plugin = fastifyPlugin(
1216
function (instance: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void {

src/utils.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export function upperFirst(source: any): string {
33
return source
44
}
55

6-
return source[0].toUpperCase() + source.substring(1)
6+
return source[0].toUpperCase() + source.slice(1)
77
}
88

99
export function get<T>(target: any, path: string): T {
@@ -16,12 +16,8 @@ export function get<T>(target: any, path: string): T {
1616
break
1717
}
1818

19-
const index = token.match(/^(\d+)|(?:\[(\d+)\])$/)
20-
if (index) {
21-
target = target[parseInt(index[1] ?? index[2], 10)]
22-
} else {
23-
target = target[token]
24-
}
19+
const index = token.match(/^(\d+)|\[(\d+)]$/)
20+
target = index ? target[Number.parseInt(index[1] ?? index[2], 10)] : target[token]
2521
}
2622

2723
return target

src/validation.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
ResponseSchemas,
1616
ValidationFormatter,
1717
Validations
18-
} from './interfaces'
19-
import { get } from './utils'
18+
} from './interfaces.js'
19+
import { get } from './utils.js'
2020

2121
export interface ValidationResult extends FastifyValidationResult {
2222
dataPath: any
@@ -32,7 +32,7 @@ export function niceJoin(array: Array<string>, lastSeparator: string = ' and ',
3232
case 2:
3333
return array.join(lastSeparator)
3434
default:
35-
return array.slice(0, array.length - 1).join(separator) + lastSeparator + array[array.length - 1]
35+
return array.slice(0, -1).join(separator) + lastSeparator + array.at(-1)!
3636
}
3737
}
3838

@@ -87,7 +87,7 @@ export const validationMessagesFormatters: { [key: string]: ValidationFormatter
8787
values.map((f: string) => `"${f}"`),
8888
' or '
8989
)}`,
90-
pattern: pattern => `must match pattern "${pattern.replace(/\(\?:/g, '(')}"`,
90+
pattern: pattern => `must match pattern "${pattern.replaceAll('(?:', '(')}"`,
9191
invalidResponseCode: code => `This endpoint cannot respond with HTTP status ${code}.`,
9292
invalidResponse: code =>
9393
`The response returned from the endpoint violates its specification for the HTTP status ${code}.`,
@@ -116,13 +116,13 @@ export function convertValidationErrors(
116116
let key = e.dataPath ?? e.instancePath /* c8 ignore next */ ?? ''
117117

118118
if (key.startsWith('.')) {
119-
key = key.substring(1)
119+
key = key.slice(1)
120120
}
121121

122122
// Remove useless quotes
123123
/* c8 ignore next 3 */
124124
if (key.startsWith('[') && key.endsWith(']')) {
125-
key = key.substring(1, key.length - 1)
125+
key = key.slice(1, -1)
126126
}
127127

128128
// Depending on the type
@@ -165,11 +165,10 @@ export function convertValidationErrors(
165165
pattern = e.params.pattern as string
166166
value = get<string>(data, key)
167167

168-
if (pattern === '.+' && !value) {
169-
message = validationMessagesFormatters.presentString()
170-
} else {
171-
message = validationMessagesFormatters.pattern(e.params.pattern)
172-
}
168+
message =
169+
pattern === '.+' && !value
170+
? validationMessagesFormatters.presentString()
171+
: validationMessagesFormatters.pattern(e.params.pattern)
173172

174173
break
175174
case 'format':
@@ -192,8 +191,8 @@ export function convertValidationErrors(
192191

193192
// Remove useless quotes
194193
/* c8 ignore next 3 */
195-
if (key.match(/(?:^['"])(?:[^.]+)(?:['"]$)/)) {
196-
key = key.substring(1, key.length - 1)
194+
if (/^["'][^.]+["']$/.test(key)) {
195+
key = key.slice(1, -1)
197196
}
198197

199198
// Fix empty properties

test/index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
UNSUPPORTED_MEDIA_TYPE
1212
} from 'http-errors-enhanced'
1313
import t from 'tap'
14-
import { handleErrors, plugin as fastifyHttpErrorsEnhanced } from '../src'
14+
import { handleErrors, plugin as fastifyHttpErrorsEnhanced } from '../src/index.js'
1515

1616
type Callback = () => void
1717

@@ -314,7 +314,7 @@ t.test('Plugin', t => {
314314
t.equal(response.headers['x-custom-header'], 'Custom-Value')
315315

316316
const payload = JSON.parse(response.payload)
317-
t.match(payload.stack[0], /^(?:Object\.handler \((?:file:\/\/)?\$ROOT\/test\/index\.test\.ts:\d+:\d+\))$/)
317+
t.match(payload.stack[0], /^Object\.handler \((?:file:\/\/)?\$ROOT\/test\/index\.test\.ts:\d+:\d+\)$/)
318318
delete payload.stack
319319

320320
t.same(payload, {
@@ -401,7 +401,7 @@ t.test('Plugin', t => {
401401
t.equal(response.headers['x-custom-header'], 'Custom-Value')
402402

403403
const payload = JSON.parse(response.payload)
404-
t.match(payload.stack[0], /^(?:Object\.handler \((?:file:\/\/)?\$ROOT\/test\/index\.test\.ts:\d+:\d+\))$/)
404+
t.match(payload.stack[0], /^Object\.handler \((?:file:\/\/)?\$ROOT\/test\/index\.test\.ts:\d+:\d+\)$/)
405405
delete payload.stack
406406

407407
t.same(payload, {
@@ -529,7 +529,7 @@ t.test('Plugin', t => {
529529
t.equal(response.statusCode, INTERNAL_SERVER_ERROR)
530530

531531
const payload = JSON.parse(response.payload)
532-
t.match(payload.stack[0], /^(?:Object\.handler \((?:file:\/\/)?\$ROOT\/test\/index\.test\.ts:\d+:\d+\))$/)
532+
t.match(payload.stack[0], /^Object\.handler \((?:file:\/\/)?\$ROOT\/test\/index\.test\.ts:\d+:\d+\)$/)
533533
delete payload.stack
534534

535535
t.same(payload, {
@@ -547,7 +547,7 @@ t.test('Plugin', t => {
547547
t.equal(response.statusCode, INTERNAL_SERVER_ERROR)
548548

549549
const payload = JSON.parse(response.payload)
550-
t.match(payload.stack[0], /^(?:Object\.handler \((?:file:\/\/)?\$ROOT\/test\/index\.test\.ts:\d+:\d+\))$/)
550+
t.match(payload.stack[0], /^Object\.handler \((?:file:\/\/)?\$ROOT\/test\/index\.test\.ts:\d+:\d+\)$/)
551551
delete payload.stack
552552

553553
t.same(payload, {

test/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

33
import t from 'tap'
4-
import { get, upperFirst } from '../src/utils'
4+
import { get, upperFirst } from '../src/utils.js'
55

66
t.test('Utils', t => {
77
t.test('.upperFirst should correctly convert strings', t => {

test/validation.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import addFormats from 'ajv-formats'
55
import fastify, { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify'
66
import { ACCEPTED, INTERNAL_SERVER_ERROR, OK } from 'http-errors-enhanced'
77
import t from 'tap'
8-
import { convertValidationErrors, niceJoin, plugin as fastifyErrorProperties } from '../src'
9-
import { ValidationResult } from '../src/validation'
8+
import { convertValidationErrors, niceJoin, plugin as fastifyErrorProperties } from '../src/index.js'
9+
import { ValidationResult } from '../src/validation.js'
1010

1111
function buildServer(options: FastifyPluginOptions = {}): FastifyInstance {
1212
const server = fastify()

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2020",
3+
"target": "ES2021",
44
"module": "ESNext",
55
"moduleResolution": "node",
66
"jsx": "preserve",

0 commit comments

Comments
 (0)