types.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import type { Auth } from './auth'
  2. import type { Logger } from './log'
  3. import type { Models } from './db'
  4. import type cors from 'cors'
  5. import type { Db, MongoClient } from 'mongodb'
  6. /** Application configuration context. */
  7. export interface Config {
  8. api: {
  9. /** URL prefix for all APIs (default: "/api") */
  10. prefix: string
  11. }
  12. auth: {
  13. jwt: {
  14. /** Expiration time for JWTs (default: 86400 (seconds, or one day)) */
  15. expiresIn: number
  16. /**
  17. * JWT secret for signing and verification.
  18. * If a value is not set, this is auto generated when the app starts
  19. */
  20. secret: string
  21. }
  22. }
  23. /**
  24. * CORS options.
  25. * At the moment, this is always undefined for default usage; a future update should add support for configuration
  26. * via environment variables.
  27. *
  28. * @see https://www.npmjs.com/package/cors#usage
  29. */
  30. cors: Parameters<typeof cors>[0]
  31. fs: {
  32. /**
  33. * Filesystem root directory (AKA location of package.json).
  34. * This is detected at startup and not configurable.
  35. */
  36. root: string
  37. }
  38. http: {
  39. /** HTTP bind host (default: empty) */
  40. host: string
  41. /** HTTP bind port (default: 5001) */
  42. port: number
  43. }
  44. log: {
  45. /** Log level (default: "info") */
  46. level: string
  47. }
  48. mongo: {
  49. /** Database to use in MongoDB */
  50. db: string
  51. /**
  52. * MongoDB connection URI.
  53. *
  54. * @see https://www.mongodb.com/docs/drivers/node/current/quick-start/create-a-connection-string/
  55. */
  56. uri: string
  57. /**
  58. * Whether to use transactions to encapsulate multiple document writes in MongoDB clusters or replica sets
  59. * (default: false)
  60. *
  61. * @see https://www.mongodb.com/docs/v7.0/core/transactions/#feature-compatibility-version--fcv-
  62. */
  63. useTransactions: boolean
  64. }
  65. /**
  66. * If the application cannot shut down because a process has stalled, it will force shutdown with `process.exit(1)`
  67. * after this period has elapsed (default: 60000 (milliseconds, or one minute))
  68. */
  69. shutdownTimeout: number
  70. }
  71. /**
  72. * Application context.
  73. * This is shared throughout the entire codebase, providing access to almost all functionality wherever it's needed.
  74. */
  75. export interface Context {
  76. auth: Auth
  77. config: Config
  78. /**
  79. * Get the reference to the application context.
  80. * This can be useful in cases where the context object passed through scope is not the same as the application
  81. * context, such as inside of models.
  82. */
  83. ctx(): Context
  84. db: Db
  85. log: Logger
  86. model: Models
  87. mongo: MongoClient
  88. }