@platformatic/service 1.12.0 → 1.13.0
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 +3 -0
- package/index.js +13 -13
- package/lib/compile.js +2 -1
- package/lib/generator/service-generator.d.ts +11 -0
- package/lib/generator/service-generator.js +106 -0
- package/lib/plugins/plugins.js +1 -1
- package/lib/start.js +4 -1
- package/package.json +7 -6
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
|
-
|
|
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(
|
|
37
|
-
for (const plugin of
|
|
38
|
-
|
|
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
|
-
|
|
48
|
+
app.register(setupOpenAPI, serviceConfig.openapi)
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
if (isKeyEnabled('graphql', serviceConfig)) {
|
|
49
|
-
|
|
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
|
-
|
|
70
|
+
app.register(setupTsCompiler)
|
|
68
71
|
}
|
|
69
|
-
|
|
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
|
|
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,106 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { BaseGenerator } = require('@platformatic/generators')
|
|
4
|
+
|
|
5
|
+
class ServiceGenerator extends BaseGenerator {
|
|
6
|
+
constructor (opts = {}) {
|
|
7
|
+
super(opts)
|
|
8
|
+
this.type = 'service'
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async _beforePrepare () {
|
|
12
|
+
this.config.env = {
|
|
13
|
+
PLT_SERVER_HOSTNAME: this.config.hostname,
|
|
14
|
+
PLT_SERVER_LOGGER_LEVEL: 'info',
|
|
15
|
+
PORT: 3042,
|
|
16
|
+
...this.config.env
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
this.config.dependencies = {
|
|
20
|
+
'@platformatic/service': `^${this.platformaticVersion}`
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getConfigFieldsDefinitions () {
|
|
25
|
+
return [
|
|
26
|
+
{
|
|
27
|
+
var: 'PLT_SERVER_HOSTNAME',
|
|
28
|
+
label: 'What is the hostname?',
|
|
29
|
+
default: '0.0.0.0',
|
|
30
|
+
type: 'string',
|
|
31
|
+
configValue: 'hostname'
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
var: 'PLT_SERVER_LOGGER_LEVEL',
|
|
35
|
+
label: 'What is the logger level?',
|
|
36
|
+
default: 'info',
|
|
37
|
+
type: 'string',
|
|
38
|
+
configValue: ''
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
label: 'Which port do you want to use?',
|
|
42
|
+
var: 'PORT',
|
|
43
|
+
default: 3042,
|
|
44
|
+
tyoe: 'number',
|
|
45
|
+
configValue: 'port'
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async _afterPrepare () {
|
|
51
|
+
const GLOBAL_TYPES_TEMPLATE = `
|
|
52
|
+
import { FastifyInstance } from 'fastify'
|
|
53
|
+
import { PlatformaticApp, PlatformaticServiceConfig } from '@platformatic/service'
|
|
54
|
+
|
|
55
|
+
declare module 'fastify' {
|
|
56
|
+
interface FastifyInstance {
|
|
57
|
+
platformatic: PlatformaticApp<PlatformaticServiceConfig>
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
`
|
|
61
|
+
this.addFile({ path: '', file: 'global.d.ts', contents: GLOBAL_TYPES_TEMPLATE })
|
|
62
|
+
if (this.config.isRuntimeContext) {
|
|
63
|
+
// remove env variables since they are all for the config.server property
|
|
64
|
+
this.config.env = {}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async _getConfigFileContents () {
|
|
69
|
+
const { typescript, isRuntimeContext } = this.config
|
|
70
|
+
const version = this.platformaticVersion
|
|
71
|
+
const config = {
|
|
72
|
+
$schema: `https://platformatic.dev/schemas/v${version}/service`,
|
|
73
|
+
service: {
|
|
74
|
+
openapi: true
|
|
75
|
+
},
|
|
76
|
+
watch: true
|
|
77
|
+
}
|
|
78
|
+
if (this.config.plugin) {
|
|
79
|
+
config.plugins = {
|
|
80
|
+
paths: [
|
|
81
|
+
{ path: './plugins', encapsulate: false },
|
|
82
|
+
'./routes'
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typescript === true) {
|
|
87
|
+
config.plugins.typescript = true
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!isRuntimeContext) {
|
|
92
|
+
config.server = {
|
|
93
|
+
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
94
|
+
port: '{PORT}',
|
|
95
|
+
logger: {
|
|
96
|
+
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return config
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = ServiceGenerator
|
|
105
|
+
module.exports.ServiceGenerator = ServiceGenerator
|
|
106
|
+
module.exports.Generator = ServiceGenerator
|
package/lib/plugins/plugins.js
CHANGED
|
@@ -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.
|
|
3
|
+
"version": "1.13.0",
|
|
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.
|
|
73
|
-
"@platformatic/config": "1.
|
|
74
|
-
"@platformatic/swagger-ui-theme": "1.
|
|
75
|
-
"@platformatic/
|
|
76
|
-
"@platformatic/
|
|
72
|
+
"@platformatic/client": "1.13.0",
|
|
73
|
+
"@platformatic/config": "1.13.0",
|
|
74
|
+
"@platformatic/swagger-ui-theme": "1.13.0",
|
|
75
|
+
"@platformatic/utils": "1.13.0",
|
|
76
|
+
"@platformatic/telemetry": "1.13.0",
|
|
77
|
+
"@platformatic/generators": "1.13.0"
|
|
77
78
|
},
|
|
78
79
|
"standard": {
|
|
79
80
|
"ignore": [
|