apeframework 0.0.0-dev.20 → 0.0.0-dev.21

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 (210) hide show
  1. package/LICENSE +1 -1
  2. package/cipher/Algorithm.js +2 -4
  3. package/cipher/Cipher.js +9 -15
  4. package/cipher/errors/DecryptError.js +3 -6
  5. package/cipher/errors/SecretLengthError.js +3 -6
  6. package/cipher/validateSecretLength.js +7 -10
  7. package/cli/Args.js +1 -2
  8. package/cli/Command.js +1 -2
  9. package/cli/getArgs.js +3 -6
  10. package/cli/parseArgs.js +3 -9
  11. package/cli/utils/exit.js +1 -4
  12. package/cli/utils/formatList.js +3 -6
  13. package/cli/utils/formatTable.js +3 -6
  14. package/cli/utils/formatText.js +3 -6
  15. package/cli/utils/print.js +1 -4
  16. package/cli/utils/printLn.js +3 -6
  17. package/config/Config.js +1 -2
  18. package/config/Properties.js +1 -2
  19. package/config/errors/FileReadError.js +3 -6
  20. package/config/errors/PropertyNameError.js +3 -6
  21. package/config/errors/PropertyParseError.js +3 -6
  22. package/config/getConfig.js +8 -11
  23. package/config/getPropertyEnvVar.js +1 -4
  24. package/config/parseProperty.js +3 -6
  25. package/config/readFile.js +8 -14
  26. package/config/validatePropertyName.js +3 -6
  27. package/env/Env.js +1 -2
  28. package/env/errors/FileReadError.js +3 -6
  29. package/env/getEnv.js +3 -6
  30. package/env/readFile.js +8 -14
  31. package/error/BaseError.js +1 -4
  32. package/jwt/Algorithm.js +2 -4
  33. package/jwt/Jwt.js +6 -9
  34. package/jwt/Payload.js +1 -2
  35. package/jwt/errors/SecretLengthError.js +3 -6
  36. package/jwt/validateSecretLength.js +7 -10
  37. package/logger/Level.js +2 -4
  38. package/logger/Logger.js +1 -4
  39. package/logger/Severity.js +2 -4
  40. package/logger/adapters/file/FileLogger.js +11 -17
  41. package/logger/adapters/file/errors/FileInitError.js +3 -6
  42. package/logger/adapters/file/initFile.js +6 -12
  43. package/logger/adapters/noop/NoopLogger.js +3 -6
  44. package/logger/adapters/stdio/StdioLogger.js +11 -17
  45. package/mailer/Address.js +1 -2
  46. package/mailer/Attachment.js +1 -2
  47. package/mailer/Event.js +1 -2
  48. package/mailer/List.js +1 -2
  49. package/mailer/Location.js +1 -2
  50. package/mailer/Mail.js +1 -2
  51. package/mailer/Mailer.js +16 -19
  52. package/mailer/Method.js +2 -4
  53. package/mailer/adapters/noop/NoopMailer.js +5 -8
  54. package/mailer/adapters/smtp/SmtpMailer.js +19 -25
  55. package/mailer/adapters/smtp/getAddress.js +1 -4
  56. package/mailer/adapters/smtp/getAddresses.js +3 -6
  57. package/mailer/adapters/smtp/getAttachments.js +1 -4
  58. package/mailer/errors/AddressEmailError.js +3 -6
  59. package/mailer/errors/EventAttachmentUrlError.js +3 -6
  60. package/mailer/errors/EventGeolocationError.js +3 -6
  61. package/mailer/errors/EventUrlError.js +3 -6
  62. package/mailer/errors/ListUrlError.js +3 -6
  63. package/mailer/getIcalendar.js +3 -6
  64. package/mailer/validateAddressEmail.js +5 -8
  65. package/mailer/validateEventAttachmentUrl.js +5 -8
  66. package/mailer/validateEventGeolocation.js +5 -8
  67. package/mailer/validateEventUrl.js +5 -8
  68. package/mailer/validateListUrl.js +5 -8
  69. package/package.json +2 -4
  70. package/parser/Parser.js +1 -2
  71. package/parser/errors/ParseError.js +3 -6
  72. package/parser/factories/enum/createEnumParser.js +3 -6
  73. package/parser/factories/unit/Unit.js +1 -2
  74. package/parser/factories/unit/createUnitParser.js +8 -11
  75. package/parser/parseBoolean.js +3 -6
  76. package/parser/parseBytes.js +3 -6
  77. package/parser/parseInteger.js +6 -9
  78. package/parser/parseMilliseconds.js +3 -6
  79. package/parser/parseNumber.js +4 -7
  80. package/parser/parseString.js +3 -6
  81. package/pwd/Pwd.js +6 -12
  82. package/pwd/errors/HashRoundsError.js +3 -6
  83. package/pwd/validateHashRounds.js +3 -6
  84. package/server/ErrorHandler.js +1 -2
  85. package/server/Format.js +1 -2
  86. package/server/Handler.js +1 -2
  87. package/server/Method.js +2 -4
  88. package/server/OpenApiFormat.js +2 -4
  89. package/server/Route.js +1 -2
  90. package/server/Schema.js +1 -2
  91. package/server/Server.js +15 -21
  92. package/server/getAjv.js +5 -11
  93. package/tls/Tls.js +1 -2
  94. package/tls/errors/FileReadError.js +3 -6
  95. package/tls/getTls.js +5 -8
  96. package/tls/readFile.js +7 -13
  97. package/utils/getRandomHexString.js +3 -6
  98. package/utils/getTimestamp.js +1 -4
  99. package/utils/isValidEmail.js +1 -4
  100. package/utils/isValidGeolocation.js +1 -4
  101. package/utils/isValidHexString.js +1 -4
  102. package/utils/isValidIsoDate.js +1 -4
  103. package/utils/isValidJwtToken.js +1 -4
  104. package/utils/isValidUrl.js +1 -4
  105. package/utils/isValidUuid.js +1 -4
  106. package/utils/wait.js +1 -4
  107. package/cipher/Algorithm.ts +0 -9
  108. package/cipher/Cipher.ts +0 -46
  109. package/cipher/errors/DecryptError.ts +0 -11
  110. package/cipher/errors/SecretLengthError.ts +0 -11
  111. package/cipher/validateSecretLength.ts +0 -18
  112. package/cli/Args.ts +0 -8
  113. package/cli/Command.ts +0 -7
  114. package/cli/getArgs.ts +0 -10
  115. package/cli/parseArgs.ts +0 -34
  116. package/cli/utils/exit.ts +0 -7
  117. package/cli/utils/formatList.ts +0 -11
  118. package/cli/utils/formatTable.ts +0 -25
  119. package/cli/utils/formatText.ts +0 -9
  120. package/cli/utils/print.ts +0 -7
  121. package/cli/utils/printLn.ts +0 -9
  122. package/config/Config.ts +0 -5
  123. package/config/Properties.ts +0 -12
  124. package/config/errors/FileReadError.ts +0 -11
  125. package/config/errors/PropertyNameError.ts +0 -11
  126. package/config/errors/PropertyParseError.ts +0 -11
  127. package/config/getConfig.ts +0 -47
  128. package/config/getPropertyEnvVar.ts +0 -15
  129. package/config/parseProperty.ts +0 -47
  130. package/config/readFile.ts +0 -34
  131. package/config/validatePropertyName.ts +0 -13
  132. package/env/Env.ts +0 -5
  133. package/env/errors/FileReadError.ts +0 -11
  134. package/env/getEnv.ts +0 -24
  135. package/env/readFile.ts +0 -24
  136. package/error/BaseError.ts +0 -12
  137. package/jwt/Algorithm.ts +0 -9
  138. package/jwt/Jwt.ts +0 -49
  139. package/jwt/Payload.ts +0 -14
  140. package/jwt/errors/SecretLengthError.ts +0 -11
  141. package/jwt/validateSecretLength.ts +0 -18
  142. package/logger/Level.ts +0 -13
  143. package/logger/Logger.ts +0 -19
  144. package/logger/Severity.ts +0 -12
  145. package/logger/adapters/file/FileLogger.ts +0 -87
  146. package/logger/adapters/file/errors/FileInitError.ts +0 -11
  147. package/logger/adapters/file/initFile.ts +0 -18
  148. package/logger/adapters/noop/NoopLogger.ts +0 -21
  149. package/logger/adapters/stdio/StdioLogger.ts +0 -89
  150. package/mailer/Address.ts +0 -8
  151. package/mailer/Attachment.ts +0 -11
  152. package/mailer/Event.ts +0 -20
  153. package/mailer/List.ts +0 -10
  154. package/mailer/Location.ts +0 -12
  155. package/mailer/Mail.ts +0 -23
  156. package/mailer/Mailer.ts +0 -69
  157. package/mailer/Method.ts +0 -8
  158. package/mailer/adapters/noop/NoopMailer.ts +0 -15
  159. package/mailer/adapters/smtp/SmtpMailer.ts +0 -74
  160. package/mailer/adapters/smtp/getAddress.ts +0 -15
  161. package/mailer/adapters/smtp/getAddresses.ts +0 -17
  162. package/mailer/adapters/smtp/getAttachments.ts +0 -20
  163. package/mailer/errors/AddressEmailError.ts +0 -11
  164. package/mailer/errors/EventAttachmentUrlError.ts +0 -11
  165. package/mailer/errors/EventGeolocationError.ts +0 -11
  166. package/mailer/errors/EventUrlError.ts +0 -11
  167. package/mailer/errors/ListUrlError.ts +0 -11
  168. package/mailer/getIcalendar.ts +0 -39
  169. package/mailer/validateAddressEmail.ts +0 -12
  170. package/mailer/validateEventAttachmentUrl.ts +0 -12
  171. package/mailer/validateEventGeolocation.ts +0 -15
  172. package/mailer/validateEventUrl.ts +0 -12
  173. package/mailer/validateListUrl.ts +0 -12
  174. package/parser/Parser.ts +0 -5
  175. package/parser/errors/ParseError.ts +0 -11
  176. package/parser/factories/enum/createEnumParser.ts +0 -21
  177. package/parser/factories/unit/Unit.ts +0 -8
  178. package/parser/factories/unit/createUnitParser.ts +0 -50
  179. package/parser/parseBoolean.ts +0 -33
  180. package/parser/parseBytes.ts +0 -55
  181. package/parser/parseInteger.ts +0 -23
  182. package/parser/parseMilliseconds.ts +0 -31
  183. package/parser/parseNumber.ts +0 -22
  184. package/parser/parseString.ts +0 -16
  185. package/pwd/Pwd.ts +0 -31
  186. package/pwd/errors/HashRoundsError.ts +0 -11
  187. package/pwd/validateHashRounds.ts +0 -11
  188. package/server/ErrorHandler.ts +0 -11
  189. package/server/Format.ts +0 -15
  190. package/server/Handler.ts +0 -10
  191. package/server/Method.ts +0 -11
  192. package/server/OpenApiFormat.ts +0 -8
  193. package/server/Route.ts +0 -16
  194. package/server/Schema.ts +0 -13
  195. package/server/Server.ts +0 -134
  196. package/server/getAjv.ts +0 -26
  197. package/tls/Tls.ts +0 -10
  198. package/tls/errors/FileReadError.ts +0 -11
  199. package/tls/getTls.ts +0 -24
  200. package/tls/readFile.ts +0 -18
  201. package/utils/getRandomHexString.ts +0 -9
  202. package/utils/getTimestamp.ts +0 -7
  203. package/utils/isValidEmail.ts +0 -9
  204. package/utils/isValidGeolocation.ts +0 -10
  205. package/utils/isValidHexString.ts +0 -9
  206. package/utils/isValidIsoDate.ts +0 -9
  207. package/utils/isValidJwtToken.ts +0 -9
  208. package/utils/isValidUrl.ts +0 -21
  209. package/utils/isValidUuid.ts +0 -10
  210. package/utils/wait.ts +0 -9
@@ -1,15 +0,0 @@
1
- import { isValidGeolocation } from 'utils/isValidGeolocation'
2
- import { EventGeolocationError } from './errors/EventGeolocationError'
3
-
4
- const validateEventGeolocation = (
5
- latitude: number,
6
- longitude: number,
7
- ): void => {
8
- if (!isValidGeolocation(latitude, longitude)) {
9
- throw new EventGeolocationError(latitude, longitude)
10
- }
11
- }
12
-
13
- export {
14
- validateEventGeolocation,
15
- }
@@ -1,12 +0,0 @@
1
- import { isValidUrl } from 'utils/isValidUrl'
2
- import { EventUrlError } from './errors/EventUrlError'
3
-
4
- const validateEventUrl = (url: string): void => {
5
- if (!isValidUrl(url)) {
6
- throw new EventUrlError(url)
7
- }
8
- }
9
-
10
- export {
11
- validateEventUrl,
12
- }
@@ -1,12 +0,0 @@
1
- import { isValidUrl } from 'utils/isValidUrl'
2
- import { ListUrlError } from './errors/ListUrlError'
3
-
4
- const validateListUrl = (property: string, url: string): void => {
5
- if (!isValidUrl(url)) {
6
- throw new ListUrlError(property, url)
7
- }
8
- }
9
-
10
- export {
11
- validateListUrl,
12
- }
package/parser/Parser.ts DELETED
@@ -1,5 +0,0 @@
1
- type Parser<Type> = (input: any) => Type
2
-
3
- export {
4
- type Parser,
5
- }
@@ -1,11 +0,0 @@
1
- import { BaseError } from '../../error/BaseError'
2
-
3
- class ParseError extends BaseError {
4
- public constructor(type: string) {
5
- super(`failed parsing ${type}`)
6
- }
7
- }
8
-
9
- export {
10
- ParseError,
11
- }
@@ -1,21 +0,0 @@
1
- import { ParseError } from '../../errors/ParseError'
2
- import type { Parser } from '../../Parser'
3
-
4
- const createEnumParser = <Type extends Record<string, string | number>>(
5
- params: {
6
- type: string,
7
- enum: Type,
8
- },
9
- ): Parser<Type[keyof Type]> => {
10
- return (input) => {
11
- if (!Object.values(params.enum).includes(input)) {
12
- throw new ParseError(params.type)
13
- }
14
-
15
- return input
16
- }
17
- }
18
-
19
- export {
20
- createEnumParser,
21
- }
@@ -1,8 +0,0 @@
1
- interface Unit {
2
- symbol: string,
3
- value: number,
4
- }
5
-
6
- export {
7
- type Unit,
8
- }
@@ -1,50 +0,0 @@
1
- import { ParseError } from '../../errors/ParseError'
2
- import { parseNumber } from '../../parseNumber'
3
- import { parseString } from '../../parseString'
4
- import type { Unit } from './Unit'
5
- import type { Parser } from '../../Parser'
6
-
7
- const createUnitParser = (params: {
8
- type: string,
9
- units: Unit[],
10
- }): Parser<number> => {
11
- return (input) => {
12
- let string: string
13
-
14
- try {
15
- string = parseString(input)
16
- } catch (error) {
17
- throw new ParseError(params.type)
18
- }
19
-
20
- const units: Unit[] = []
21
-
22
- params.units.forEach((u) => {
23
- units.push(u, { ...u, symbol: ` ${u.symbol}` })
24
- })
25
-
26
- units.sort((a, b) => {
27
- return b.symbol.length - a.symbol.length
28
- })
29
-
30
- const unit = units.find((u) => {
31
- return string.toUpperCase().endsWith(u.symbol.toUpperCase())
32
- })
33
-
34
- let number: number
35
-
36
- try {
37
- number = parseNumber(
38
- string.substring(0, string.length - (unit?.symbol.length ?? 0)),
39
- )
40
- } catch (error) {
41
- throw new ParseError(params.type)
42
- }
43
-
44
- return number * (unit?.value ?? 1)
45
- }
46
- }
47
-
48
- export {
49
- createUnitParser,
50
- }
@@ -1,33 +0,0 @@
1
- import { ParseError } from './errors/ParseError'
2
- import type { Parser } from './Parser'
3
-
4
- const parseBoolean: Parser<boolean> = (input) => {
5
- if ([
6
- true,
7
- 1,
8
- BigInt(1),
9
- '1',
10
- 'TRUE',
11
- 'true',
12
- ].includes(input)) {
13
- return true
14
- } else if ([
15
- undefined,
16
- null,
17
- false,
18
- 0,
19
- BigInt(0),
20
- '',
21
- '0',
22
- 'FALSE',
23
- 'false',
24
- ].includes(input)) {
25
- return false
26
- }
27
-
28
- throw new ParseError('boolean')
29
- }
30
-
31
- export {
32
- parseBoolean,
33
- }
@@ -1,55 +0,0 @@
1
- import { createUnitParser } from './factories/unit/createUnitParser'
2
-
3
- const parseBytes = createUnitParser({
4
- type: 'bytes',
5
- units: [
6
- {
7
- symbol: 'B',
8
- value: 1,
9
- },
10
- {
11
- symbol: 'kB',
12
- value: 1000,
13
- },
14
- {
15
- symbol: 'MB',
16
- value: 1000000,
17
- },
18
- {
19
- symbol: 'GB',
20
- value: 1000000000,
21
- },
22
- {
23
- symbol: 'TB',
24
- value: 1000000000000,
25
- },
26
- {
27
- symbol: 'PB',
28
- value: 1000000000000000,
29
- },
30
- {
31
- symbol: 'KiB',
32
- value: 1024,
33
- },
34
- {
35
- symbol: 'MiB',
36
- value: 131072,
37
- },
38
- {
39
- symbol: 'GiB',
40
- value: 134200000,
41
- },
42
- {
43
- symbol: 'TiB',
44
- value: 137400000000,
45
- },
46
- {
47
- symbol: 'PiB',
48
- value: 140700000000000,
49
- },
50
- ],
51
- })
52
-
53
- export {
54
- parseBytes,
55
- }
@@ -1,23 +0,0 @@
1
- import { ParseError } from './errors/ParseError'
2
- import { parseNumber } from './parseNumber'
3
- import type { Parser } from './Parser'
4
-
5
- const parseInteger: Parser<number> = (input) => {
6
- let number: number
7
-
8
- try {
9
- number = parseNumber(input)
10
- } catch (error) {
11
- throw new ParseError('integer')
12
- }
13
-
14
- if (!Number.isSafeInteger(number)) {
15
- throw new ParseError('integer')
16
- }
17
-
18
- return number
19
- }
20
-
21
- export {
22
- parseInteger,
23
- }
@@ -1,31 +0,0 @@
1
- import { createUnitParser } from './factories/unit/createUnitParser'
2
-
3
- const parseMilliseconds = createUnitParser({
4
- type: 'milliseconds',
5
- units: [
6
- {
7
- symbol: 'ms',
8
- value: 1,
9
- },
10
- {
11
- symbol: 's',
12
- value: 1000,
13
- },
14
- {
15
- symbol: 'm',
16
- value: 60000,
17
- },
18
- {
19
- symbol: 'h',
20
- value: 3600000,
21
- },
22
- {
23
- symbol: 'd',
24
- value: 86400000,
25
- },
26
- ],
27
- })
28
-
29
- export {
30
- parseMilliseconds,
31
- }
@@ -1,22 +0,0 @@
1
- import { ParseError } from './errors/ParseError'
2
- import type { Parser } from './Parser'
3
-
4
- const parseNumber: Parser<number> = (input) => {
5
- if ([undefined, null].includes(input)) {
6
- return 0
7
- } else if (['object', 'function', 'symbol'].includes(typeof input)) {
8
- throw new ParseError('number')
9
- }
10
-
11
- const number = Number(input)
12
-
13
- if (!Number.isFinite(number)) {
14
- throw new ParseError('number')
15
- }
16
-
17
- return number
18
- }
19
-
20
- export {
21
- parseNumber,
22
- }
@@ -1,16 +0,0 @@
1
- import { ParseError } from './errors/ParseError'
2
- import type { Parser } from './Parser'
3
-
4
- const parseString: Parser<string> = (input) => {
5
- if ([undefined, null].includes(input)) {
6
- return ''
7
- } else if (['object', 'function', 'symbol'].includes(typeof input)) {
8
- throw new ParseError('string')
9
- }
10
-
11
- return String(input)
12
- }
13
-
14
- export {
15
- parseString,
16
- }
package/pwd/Pwd.ts DELETED
@@ -1,31 +0,0 @@
1
- import bcrypt from 'bcryptjs'
2
- import { validateHashRounds } from './validateHashRounds'
3
-
4
- class Pwd {
5
- private readonly hashRounds: number
6
-
7
- public constructor(params: {
8
- hashRounds: number,
9
- }) {
10
- validateHashRounds(params.hashRounds)
11
-
12
- this.hashRounds = params.hashRounds
13
- }
14
-
15
- public async hashPassword(
16
- password: string,
17
- ): Promise<string> {
18
- return bcrypt.hashSync(password, this.hashRounds)
19
- }
20
-
21
- public async verifyPassword(
22
- password: string,
23
- hash: string,
24
- ): Promise<boolean> {
25
- return bcrypt.compareSync(password, hash)
26
- }
27
- }
28
-
29
- export {
30
- Pwd,
31
- }
@@ -1,11 +0,0 @@
1
- import { BaseError } from '../../error/BaseError'
2
-
3
- class HashRoundsError extends BaseError {
4
- public constructor(hashRounds: number) {
5
- super(`invalid hash rounds "${hashRounds}"`)
6
- }
7
- }
8
-
9
- export {
10
- HashRoundsError,
11
- }
@@ -1,11 +0,0 @@
1
- import { HashRoundsError } from './errors/HashRoundsError'
2
-
3
- const validateHashRounds = (hashRounds: number): void => {
4
- if (hashRounds < 1) {
5
- throw new HashRoundsError(hashRounds)
6
- }
7
- }
8
-
9
- export {
10
- validateHashRounds,
11
- }
@@ -1,11 +0,0 @@
1
- import type { FastifyError, FastifyReply, FastifyRequest } from 'fastify'
2
-
3
- type ErrorHandler = (
4
- error: FastifyError,
5
- request: FastifyRequest,
6
- response: FastifyReply,
7
- ) => Promise<void>
8
-
9
- export {
10
- type ErrorHandler,
11
- }
package/server/Format.ts DELETED
@@ -1,15 +0,0 @@
1
- import type { FormatDefinition } from 'ajv'
2
-
3
- type Format = NumberFormat | StringFormat
4
-
5
- interface NumberFormat extends FormatDefinition<number> {
6
- name: string,
7
- }
8
-
9
- interface StringFormat extends FormatDefinition<string> {
10
- name: string,
11
- }
12
-
13
- export {
14
- type Format,
15
- }
package/server/Handler.ts DELETED
@@ -1,10 +0,0 @@
1
- import type { FastifyReply, FastifyRequest } from 'fastify'
2
-
3
- type Handler = (
4
- request: FastifyRequest,
5
- response: FastifyReply,
6
- ) => Promise<void>
7
-
8
- export {
9
- type Handler,
10
- }
package/server/Method.ts DELETED
@@ -1,11 +0,0 @@
1
- enum Method {
2
- DELETE = 'DELETE',
3
- GET = 'GET',
4
- PATCH = 'PATCH',
5
- POST = 'POST',
6
- PUT = 'PUT',
7
- }
8
-
9
- export {
10
- Method,
11
- }
@@ -1,8 +0,0 @@
1
- enum OpenApiFormat {
2
- JSON = 'json',
3
- YAML = 'yaml',
4
- }
5
-
6
- export {
7
- OpenApiFormat,
8
- }
package/server/Route.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { Handler } from './Handler'
2
- import type { Method } from './Method'
3
- import type { Schema } from './Schema'
4
-
5
- interface Route {
6
- path: string,
7
- method: Method,
8
- name?: string,
9
- description?: string,
10
- schema: Schema,
11
- handler: Handler,
12
- }
13
-
14
- export {
15
- type Route,
16
- }
package/server/Schema.ts DELETED
@@ -1,13 +0,0 @@
1
- import type { OpenAPIV3 } from 'openapi-types'
2
-
3
- interface Schema {
4
- params?: OpenAPIV3.SchemaObject,
5
- query?: OpenAPIV3.SchemaObject,
6
- headers?: OpenAPIV3.SchemaObject,
7
- body?: OpenAPIV3.SchemaObject,
8
- response?: Record<number, OpenAPIV3.SchemaObject>,
9
- }
10
-
11
- export {
12
- type Schema,
13
- }
package/server/Server.ts DELETED
@@ -1,134 +0,0 @@
1
- import compress from '@fastify/compress'
2
- import cors from '@fastify/cors'
3
- import responseValidation from '@fastify/response-validation'
4
- import swagger from '@fastify/swagger'
5
- import fastify from 'fastify'
6
- import { OpenApiFormat } from './OpenApiFormat'
7
- import { getAjv } from './getAjv'
8
- import type { ErrorHandler } from './ErrorHandler'
9
- import type { Format } from './Format'
10
- import type { Handler } from './Handler'
11
- import type { Route } from './Route'
12
- import type { FastifyInstance } from 'fastify'
13
- import type { OpenAPIV3 } from 'openapi-types'
14
-
15
- class Server {
16
- private readonly server: FastifyInstance
17
-
18
- private readonly host: string
19
-
20
- private readonly port: number | undefined
21
-
22
- public constructor(params: {
23
- host: string,
24
- port?: number,
25
- routes: Route[],
26
- formats?: Format[],
27
- openapi?: {
28
- name?: string,
29
- version?: string,
30
- },
31
- responseValidation?: boolean,
32
- compression?: {
33
- enabled?: boolean,
34
- threshold?: number,
35
- },
36
- cors?: {
37
- enabled?: boolean,
38
- origins?: string[],
39
- },
40
- onRequest?: Handler,
41
- onResponse?: Handler,
42
- onNotFound?: Handler,
43
- onError?: ErrorHandler,
44
- }) {
45
- this.host = params.host
46
- this.port = params.port
47
-
48
- this.server = fastify()
49
-
50
- this.server.setValidatorCompiler(({ schema }) => {
51
- return getAjv(params.formats).compile(schema)
52
- })
53
-
54
- this.server.register(swagger, {
55
- openapi: {
56
- openapi: '3.1.0',
57
- info: {
58
- title: params.openapi?.name ?? '',
59
- version: params.openapi?.version ?? '',
60
- },
61
- },
62
- })
63
-
64
- if (params.responseValidation) {
65
- this.server.register(responseValidation)
66
- }
67
-
68
- if (params.compression?.enabled) {
69
- this.server.register(compress, {
70
- global: true,
71
- threshold: params.compression.threshold ?? 1024,
72
- })
73
- }
74
-
75
- if (params.cors?.enabled) {
76
- this.server.register(cors, {
77
- origin: params.cors.origins,
78
- })
79
- }
80
-
81
- this.server.register((server, options, done) => {
82
- params.routes.forEach((route) => {
83
- server.route({
84
- url: route.path,
85
- method: route.method,
86
- schema: {
87
- summary: route.name ?? route.path,
88
- description: route.description,
89
- ...route.schema.params ? { params: route.schema.params } : {},
90
- ...route.schema.query ? { query: route.schema.query } : {},
91
- ...route.schema.headers ? { headers: route.schema.headers } : {},
92
- ...route.schema.body ? { body: route.schema.body } : {},
93
- ...route.schema.response ? { response: route.schema.response } : {},
94
- },
95
- handler: route.handler,
96
- })
97
- })
98
- done()
99
- })
100
-
101
- if (params.onRequest) {
102
- this.server.addHook('onRequest', params.onRequest)
103
- }
104
-
105
- if (params.onResponse) {
106
- this.server.addHook('onResponse', params.onResponse)
107
- }
108
-
109
- if (params.onNotFound) {
110
- this.server.setNotFoundHandler(params.onNotFound)
111
- }
112
-
113
- if (params.onError) {
114
- this.server.setErrorHandler(params.onError)
115
- }
116
- }
117
-
118
- public async start(): Promise<string> {
119
- return this.server.listen({
120
- host: this.host,
121
- port: this.port,
122
- })
123
- }
124
-
125
- public openapi(format: OpenApiFormat): OpenAPIV3.Document {
126
- return this.server.swagger({
127
- yaml: format === OpenApiFormat.YAML,
128
- }) as OpenAPIV3.Document
129
- }
130
- }
131
-
132
- export {
133
- Server,
134
- }
package/server/getAjv.ts DELETED
@@ -1,26 +0,0 @@
1
- import { Ajv } from 'ajv'
2
- import fastUri from 'fast-uri'
3
- import type { Format } from './Format'
4
-
5
- const getAjv = (formats?: Format[]): Ajv => {
6
- const ajv = new Ajv({
7
- addUsedSchema: false,
8
- allErrors: false,
9
- coerceTypes: 'array',
10
- removeAdditional: true,
11
- uriResolver: fastUri,
12
- useDefaults: true,
13
- })
14
-
15
- if (formats) {
16
- formats.forEach((format) => {
17
- ajv.addFormat(format.name, format)
18
- })
19
- }
20
-
21
- return ajv
22
- }
23
-
24
- export {
25
- getAjv,
26
- }
package/tls/Tls.ts DELETED
@@ -1,10 +0,0 @@
1
- interface Tls {
2
- key?: string,
3
- cert?: string,
4
- ca?: string,
5
- verify?: boolean,
6
- }
7
-
8
- export {
9
- type Tls,
10
- }
@@ -1,11 +0,0 @@
1
- import { BaseError } from '../../error/BaseError'
2
-
3
- class FileReadError extends BaseError {
4
- public constructor(path: string, message: string) {
5
- super(`failed reading file "${path}": ${message}`)
6
- }
7
- }
8
-
9
- export {
10
- FileReadError,
11
- }
package/tls/getTls.ts DELETED
@@ -1,24 +0,0 @@
1
- import { readFile } from './readFile'
2
- import type { Tls } from './Tls'
3
-
4
- const getTls = (tls?: Tls): {
5
- key?: string,
6
- cert?: string,
7
- ca?: string,
8
- rejectUnauthorized?: boolean,
9
- } | undefined => {
10
- return tls
11
- ? {
12
- ...tls.key ? { key: readFile(tls.key) } : {},
13
- ...tls.cert ? { cert: readFile(tls.cert) } : {},
14
- ...tls.ca ? { ca: readFile(tls.ca) } : {},
15
- ...tls.verify === undefined
16
- ? {}
17
- : { rejectUnauthorized: tls.verify },
18
- }
19
- : undefined
20
- }
21
-
22
- export {
23
- getTls,
24
- }