1
0

types.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. http: {
  32. /** HTTP bind host (default: empty) */
  33. host: string
  34. /** HTTP bind port (default: 5001) */
  35. port: number
  36. }
  37. log: {
  38. /** Log level (default: "info") */
  39. level: string
  40. }
  41. mongo: {
  42. /** Database to use in MongoDB */
  43. db: string
  44. /**
  45. * MongoDB connection URI.
  46. *
  47. * @see https://www.mongodb.com/docs/drivers/node/current/quick-start/create-a-connection-string/
  48. */
  49. uri: string
  50. /**
  51. * Whether to use transactions to encapsulate multiple document writes in MongoDB clusters or replica sets
  52. * (default: false)
  53. *
  54. * @see https://www.mongodb.com/docs/v7.0/core/transactions/#feature-compatibility-version--fcv-
  55. */
  56. useTransactions: boolean
  57. }
  58. /**
  59. * If the application cannot shut down because a process has stalled, it will force shutdown with `process.exit(1)`
  60. * after this period has elapsed (default: 60000 (milliseconds, or one minute))
  61. */
  62. shutdownTimeout: number
  63. }
  64. /**
  65. * Application context.
  66. * This is shared throughout the entire codebase, providing access to almost all functionality wherever it's needed.
  67. */
  68. export interface Context {
  69. auth: Auth
  70. config: Config
  71. /**
  72. * Get the reference to the application context.
  73. * This can be useful in cases where the context object passed through scope is not the same as the application
  74. * context, such as inside of models.
  75. */
  76. ctx(): Context
  77. db: Db
  78. log: Logger
  79. model: Models
  80. mongo: MongoClient
  81. }