@platformatic/service 1.12.1 → 1.13.1

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.
package/index.d.ts CHANGED
@@ -6,6 +6,7 @@ import ConfigManager from '@platformatic/config'
6
6
  import type { IConfigManagerOptions } from '@platformatic/config'
7
7
  import { PlatformaticService } from './config'
8
8
  import type { JSONSchemaType } from 'ajv'
9
+ import { ServiceGenerator } from './lib/generator/service-generator'
9
10
  export interface PlatformaticApp<T> {
10
11
  configManager: ConfigManager<T>
11
12
  config: T
@@ -51,3 +52,5 @@ export declare const platformaticService: Stackable<PlatformaticServiceConfig>
51
52
  export default platformaticService
52
53
 
53
54
  export const tsCompiler: TSCompiler
55
+
56
+ export const Generator: ServiceGenerator.ServiceGenerator
package/index.js CHANGED
@@ -18,10 +18,13 @@ const { telemetry } = require('@platformatic/telemetry')
18
18
  const { schema } = require('./lib/schema')
19
19
  const { addLoggerToTheConfig } = require('./lib/utils')
20
20
  const { start, buildServer } = require('./lib/start')
21
+ const ServiceGenerator = require('./lib/generator/service-generator.js')
21
22
 
22
- async function platformaticService (app, opts, toLoad = []) {
23
+ // TODO(mcollina): toLoad is deprecated, remove it in the next major version.
24
+ async function platformaticService (app, opts, toLoad) {
23
25
  const configManager = app.platformatic.configManager
24
26
  const config = configManager.current
27
+ const beforePlugins = opts.beforePlugins || toLoad || []
25
28
 
26
29
  if (isKeyEnabled('metrics', config)) {
27
30
  app.register(setupMetrics, config.metrics)
@@ -33,20 +36,20 @@ async function platformaticService (app, opts, toLoad = []) {
33
36
  await app.register(telemetry, config.telemetry)
34
37
  }
35
38
 
36
- if (Array.isArray(toLoad)) {
37
- for (const plugin of toLoad) {
38
- await app.register(plugin)
39
+ if (Array.isArray(beforePlugins)) {
40
+ for (const plugin of beforePlugins) {
41
+ app.register(plugin)
39
42
  }
40
43
  }
41
44
 
42
45
  const serviceConfig = config.service || {}
43
46
 
44
47
  if (isKeyEnabled('openapi', serviceConfig)) {
45
- await app.register(setupOpenAPI, serviceConfig.openapi)
48
+ app.register(setupOpenAPI, serviceConfig.openapi)
46
49
  }
47
50
 
48
51
  if (isKeyEnabled('graphql', serviceConfig)) {
49
- await app.register(setupGraphQL, serviceConfig.graphql)
52
+ app.register(setupGraphQL, serviceConfig.graphql)
50
53
  }
51
54
 
52
55
  if (isKeyEnabled('clients', config)) {
@@ -55,7 +58,7 @@ async function platformaticService (app, opts, toLoad = []) {
55
58
 
56
59
  if (config.plugins) {
57
60
  let registerTsCompiler = false
58
- const typescript = config.plugins.typescript
61
+ const typescript = config.plugins.paths && config.plugins.typescript
59
62
  /* c8 ignore next 6 */
60
63
  if (typescript === true) {
61
64
  registerTsCompiler = true
@@ -64,9 +67,9 @@ async function platformaticService (app, opts, toLoad = []) {
64
67
  }
65
68
 
66
69
  if (registerTsCompiler) {
67
- await app.register(setupTsCompiler)
70
+ app.register(setupTsCompiler)
68
71
  }
69
- await app.register(loadPlugins)
72
+ app.register(loadPlugins)
70
73
  }
71
74
 
72
75
  if (isKeyEnabled('cors', config.server)) {
@@ -76,10 +79,6 @@ async function platformaticService (app, opts, toLoad = []) {
76
79
  if (isKeyEnabled('healthCheck', config.server)) {
77
80
  app.register(setupHealthCheck, config.server.healthCheck)
78
81
  }
79
-
80
- if (!app.hasRoute({ url: '/', method: 'GET' }) && !Array.isArray(toLoad)) {
81
- await app.register(require('./lib/root-endpoint'))
82
- }
83
82
  }
84
83
 
85
84
  platformaticService[Symbol.for('skip-override')] = true
@@ -135,3 +134,4 @@ module.exports.platformaticService = platformaticService
135
134
  module.exports.addLoggerToTheConfig = addLoggerToTheConfig
136
135
  module.exports.tsCompiler = compiler
137
136
  module.exports.start = start
137
+ module.exports.Generator = ServiceGenerator
package/lib/compile.js CHANGED
@@ -77,7 +77,7 @@ async function compile (cwd, config, originalLogger, options) {
77
77
  }
78
78
  delete env.NODE_V8_COVERAGE
79
79
  // somehow c8 does not pick up these lines even if there is a specific test
80
- /* c8 ignore 10 */
80
+ /* c8 ignore start */
81
81
  if (options.clean) {
82
82
  // delete outdir directory
83
83
  const tsConfigContents = JSON.parse(await readFile(tsConfigPath, 'utf8'))
@@ -88,6 +88,7 @@ async function compile (cwd, config, originalLogger, options) {
88
88
  await rm(outDirFullPath, { recursive: true })
89
89
  }
90
90
  }
91
+ /* c8 ignore stop */
91
92
  await execa(tscExecutablePath, tsFlags, { cwd, env })
92
93
  logger.info('Typescript compilation completed successfully.')
93
94
  return true
@@ -0,0 +1,11 @@
1
+ import { BaseGenerator } from '@platformatic/generators'
2
+
3
+ interface KeyValue {
4
+ [key: string]: string
5
+ }
6
+
7
+ export namespace ServiceGenerator {
8
+ export class ServiceGenerator extends BaseGenerator.BaseGenerator {
9
+ constructor (opts?: BaseGenerator.BaseGeneratorOptions)
10
+ }
11
+ }
@@ -0,0 +1,119 @@
1
+ 'use strict'
2
+
3
+ const { BaseGenerator } = require('@platformatic/generators')
4
+ const { getPackageConfigurationObject } = require('@platformatic/generators/lib/utils')
5
+
6
+ class ServiceGenerator extends BaseGenerator {
7
+ constructor (opts = {}) {
8
+ super(opts)
9
+ this.type = 'service'
10
+ }
11
+
12
+ async _beforePrepare () {
13
+ this.config.env = {
14
+ PLT_SERVER_HOSTNAME: this.config.hostname,
15
+ PLT_SERVER_LOGGER_LEVEL: 'info',
16
+ PORT: 3042,
17
+ ...this.config.env
18
+
19
+ }
20
+ this.config.dependencies = {
21
+ '@platformatic/service': `^${this.platformaticVersion}`
22
+ }
23
+ }
24
+
25
+ getConfigFieldsDefinitions () {
26
+ return [
27
+ {
28
+ var: 'PLT_SERVER_HOSTNAME',
29
+ label: 'What is the hostname?',
30
+ default: '0.0.0.0',
31
+ type: 'string',
32
+ configValue: 'hostname'
33
+ },
34
+ {
35
+ var: 'PLT_SERVER_LOGGER_LEVEL',
36
+ label: 'What is the logger level?',
37
+ default: 'info',
38
+ type: 'string',
39
+ configValue: ''
40
+ },
41
+ {
42
+ label: 'Which port do you want to use?',
43
+ var: 'PORT',
44
+ default: 3042,
45
+ tyoe: 'number',
46
+ configValue: 'port'
47
+ }
48
+ ]
49
+ }
50
+
51
+ async _afterPrepare () {
52
+ const GLOBAL_TYPES_TEMPLATE = `
53
+ import { FastifyInstance } from 'fastify'
54
+ import { PlatformaticApp, PlatformaticServiceConfig } from '@platformatic/service'
55
+
56
+ declare module 'fastify' {
57
+ interface FastifyInstance {
58
+ platformatic: PlatformaticApp<PlatformaticServiceConfig>
59
+ }
60
+ }
61
+ `
62
+ this.addFile({ path: '', file: 'global.d.ts', contents: GLOBAL_TYPES_TEMPLATE })
63
+ if (this.config.isRuntimeContext) {
64
+ // remove env variables since they are all for the config.server property
65
+ this.config.env = {}
66
+ }
67
+ }
68
+
69
+ async _getConfigFileContents () {
70
+ const { typescript, isRuntimeContext } = this.config
71
+ const version = this.platformaticVersion
72
+ const config = {
73
+ $schema: `https://platformatic.dev/schemas/v${version}/service`,
74
+ service: {
75
+ openapi: true
76
+ },
77
+ watch: true
78
+ }
79
+ if (this.config.plugin) {
80
+ config.plugins = {
81
+ paths: [
82
+ { path: './plugins', encapsulate: false },
83
+ './routes'
84
+ ]
85
+ }
86
+
87
+ if (typescript === true) {
88
+ config.plugins.typescript = true
89
+ }
90
+ }
91
+
92
+ if (!isRuntimeContext) {
93
+ config.server = {
94
+ hostname: '{PLT_SERVER_HOSTNAME}',
95
+ port: '{PORT}',
96
+ logger: {
97
+ level: '{PLT_SERVER_LOGGER_LEVEL}'
98
+ }
99
+ }
100
+ }
101
+
102
+ if (this.packages.length > 0) {
103
+ if (!config.plugins) {
104
+ config.plugins = {}
105
+ }
106
+ config.plugins.packages = this.packages.map((packageDefinition) => {
107
+ return {
108
+ name: packageDefinition.name,
109
+ options: getPackageConfigurationObject(packageDefinition.options)
110
+ }
111
+ })
112
+ }
113
+ return config
114
+ }
115
+ }
116
+
117
+ module.exports = ServiceGenerator
118
+ module.exports.ServiceGenerator = ServiceGenerator
119
+ module.exports.Generator = ServiceGenerator
@@ -34,7 +34,7 @@ async function loadPlugins (app) {
34
34
  isOutDirAccessible = await isFileAccessible(outDir)
35
35
  }
36
36
 
37
- if (isOutDirAccessible) {
37
+ if (config.plugins.paths && isOutDirAccessible) {
38
38
  config.plugins.paths = config.plugins.paths.map((plugin) => {
39
39
  /* c8 ignore next 3 */
40
40
  const tmp = typeof plugin === 'string'
package/lib/start.js CHANGED
@@ -56,7 +56,10 @@ async function buildServer (options, app) {
56
56
 
57
57
  const root = fastify(fastifyOptions)
58
58
  root.decorate('platformatic', { configManager, config })
59
- root.register(app)
59
+ await root.register(app)
60
+ if (!root.hasRoute({ url: '/', method: 'GET' })) {
61
+ await root.register(require('./root-endpoint'))
62
+ }
60
63
 
61
64
  root.decorate('url', {
62
65
  getter () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/service",
3
- "version": "1.12.1",
3
+ "version": "1.13.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -69,11 +69,12 @@
69
69
  "pino-pretty": "^10.2.0",
70
70
  "rfdc": "^1.3.0",
71
71
  "ua-parser-js": "^1.0.36",
72
- "@platformatic/client": "1.12.1",
73
- "@platformatic/config": "1.12.1",
74
- "@platformatic/telemetry": "1.12.1",
75
- "@platformatic/utils": "1.12.1",
76
- "@platformatic/swagger-ui-theme": "1.12.1"
72
+ "@platformatic/client": "1.13.1",
73
+ "@platformatic/config": "1.13.1",
74
+ "@platformatic/generators": "1.13.1",
75
+ "@platformatic/telemetry": "1.13.1",
76
+ "@platformatic/swagger-ui-theme": "1.13.1",
77
+ "@platformatic/utils": "1.13.1"
77
78
  },
78
79
  "standard": {
79
80
  "ignore": [