dev-config.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* tslint:disable:no-console */
  2. import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
  3. import { AssetServerPlugin } from '@vendure/asset-server-plugin';
  4. import { ADMIN_API_PATH, API_PORT, SHOP_API_PATH } from '@vendure/common/lib/shared-constants';
  5. import { Order, OrderService, OrderState, TransactionalConnection } from '@vendure/core';
  6. import {
  7. CustomOrderProcess,
  8. DefaultJobQueuePlugin,
  9. DefaultLogger,
  10. DefaultSearchPlugin,
  11. examplePaymentHandler,
  12. FulfillmentHandler,
  13. LanguageCode,
  14. Logger,
  15. LogLevel,
  16. manualFulfillmentHandler,
  17. PermissionDefinition,
  18. Surcharge,
  19. VendureConfig,
  20. } from '@vendure/core';
  21. import { ElasticsearchPlugin } from '@vendure/elasticsearch-plugin';
  22. import { defaultEmailHandlers, EmailPlugin } from '@vendure/email-plugin';
  23. import path from 'path';
  24. import { ConnectionOptions } from 'typeorm';
  25. let connection: TransactionalConnection;
  26. let orderService: OrderService;
  27. const customerValidationProcess: CustomOrderProcess<OrderState> = {
  28. // The init method allows us to inject services
  29. // and other providers
  30. init(injector) {
  31. connection = injector.get(TransactionalConnection);
  32. orderService = injector.get(OrderService);
  33. },
  34. // The logic for enforcing our validation goes here
  35. async onTransitionStart(fromState, toState, data) {
  36. if (fromState === 'AddingItems' && toState === 'ArrangingPayment') {
  37. const { ctx, order } = data;
  38. await orderService.addSurchargeToOrder(ctx, order.id, {
  39. description: '3% payment surcharge',
  40. sku: 'PAYMENT_SURCHARGE',
  41. listPrice: Math.round(order.subTotal * 0.03),
  42. listPriceIncludesTax: ctx.channel.pricesIncludeTax,
  43. });
  44. }
  45. },
  46. };
  47. /**
  48. * Config settings used during development
  49. */
  50. export const devConfig: VendureConfig = {
  51. apiOptions: {
  52. port: API_PORT,
  53. adminApiPath: ADMIN_API_PATH,
  54. adminApiPlayground: {
  55. settings: {
  56. 'request.credentials': 'include',
  57. } as any,
  58. },
  59. adminApiDebug: true,
  60. shopApiPath: SHOP_API_PATH,
  61. shopApiPlayground: {
  62. settings: {
  63. 'request.credentials': 'include',
  64. } as any,
  65. },
  66. shopApiDebug: true,
  67. },
  68. authOptions: {
  69. disableAuth: false,
  70. tokenMethod: 'cookie',
  71. sessionSecret: 'some-secret',
  72. requireVerification: true,
  73. customPermissions: [],
  74. },
  75. orderOptions: {
  76. process: [customerValidationProcess],
  77. },
  78. dbConnectionOptions: {
  79. synchronize: false,
  80. logging: false,
  81. migrations: [path.join(__dirname, 'migrations/*.ts')],
  82. ...getDbConfig(),
  83. },
  84. paymentOptions: {
  85. paymentMethodHandlers: [examplePaymentHandler],
  86. },
  87. customFields: {
  88. OrderLine: [
  89. { name: 'test', type: 'string', nullable: true },
  90. { name: 'test2', type: 'string', nullable: true },
  91. ],
  92. },
  93. logger: new DefaultLogger({ level: LogLevel.Info }),
  94. importExportOptions: {
  95. importAssetsDir: path.join(__dirname, 'import-assets'),
  96. },
  97. shippingOptions: {
  98. fulfillmentHandlers: [manualFulfillmentHandler],
  99. },
  100. plugins: [
  101. AssetServerPlugin.init({
  102. route: 'assets',
  103. assetUploadDir: path.join(__dirname, 'assets'),
  104. port: 5002,
  105. }),
  106. DefaultSearchPlugin,
  107. DefaultJobQueuePlugin,
  108. // ElasticsearchPlugin.init({
  109. // host: 'http://localhost',
  110. // port: 9200,
  111. // }),
  112. EmailPlugin.init({
  113. devMode: true,
  114. handlers: defaultEmailHandlers,
  115. templatePath: path.join(__dirname, '../email-plugin/templates'),
  116. outputPath: path.join(__dirname, 'test-emails'),
  117. mailboxPort: 5003,
  118. globalTemplateVars: {
  119. verifyEmailAddressUrl: 'http://localhost:4201/verify',
  120. passwordResetUrl: 'http://localhost:4201/reset-password',
  121. changeEmailAddressUrl: 'http://localhost:4201/change-email-address',
  122. },
  123. }),
  124. AdminUiPlugin.init({
  125. port: 5001,
  126. }),
  127. ],
  128. };
  129. function getDbConfig(): ConnectionOptions {
  130. const dbType = process.env.DB || 'mysql';
  131. switch (dbType) {
  132. case 'postgres':
  133. console.log('Using postgres connection');
  134. return {
  135. synchronize: true,
  136. type: 'postgres',
  137. host: '127.0.0.1',
  138. port: 5432,
  139. username: 'admin',
  140. password: 'secret',
  141. database: 'vendure-dev',
  142. };
  143. case 'sqlite':
  144. console.log('Using sqlite connection');
  145. return {
  146. synchronize: false,
  147. type: 'better-sqlite3',
  148. database: path.join(__dirname, 'vendure.sqlite'),
  149. };
  150. case 'sqljs':
  151. console.log('Using sql.js connection');
  152. return {
  153. type: 'sqljs',
  154. autoSave: true,
  155. database: new Uint8Array([]),
  156. location: path.join(__dirname, 'vendure.sqlite'),
  157. };
  158. case 'mysql':
  159. default:
  160. console.log('Using mysql connection');
  161. return {
  162. synchronize: true,
  163. type: 'mariadb',
  164. host: '127.0.0.1',
  165. port: 3306,
  166. username: 'root',
  167. password: '',
  168. database: 'vendure-dev',
  169. };
  170. }
  171. }