@platformatic/service 2.0.0-alpha.2 → 2.0.0-alpha.3

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/config.d.ts CHANGED
@@ -151,7 +151,6 @@ export interface PlatformaticService {
151
151
  defaultMetrics?: {
152
152
  enabled: boolean;
153
153
  };
154
- prefix?: string;
155
154
  auth?: {
156
155
  username: string;
157
156
  password: string;
@@ -173,6 +172,7 @@ export interface PlatformaticService {
173
172
  | boolean
174
173
  | string;
175
174
  $schema?: string;
175
+ module?: string;
176
176
  service?: {
177
177
  openapi?:
178
178
  | {
@@ -0,0 +1,14 @@
1
+ 'use strict'
2
+
3
+ const neostandard = require('neostandard')
4
+
5
+ module.exports = neostandard({
6
+ ts: true,
7
+ ignores: [
8
+ ...neostandard.resolveIgnoresFromGitignore(),
9
+ 'test/tmp/**/*',
10
+ 'test/fixtures/*/dist/**/*',
11
+ '**/dist/*',
12
+ 'fixtures/**/*',
13
+ ],
14
+ })
package/index.d.ts CHANGED
@@ -1,15 +1,14 @@
1
- /* eslint-disable @typescript-eslint/triple-slash-reference */
2
1
  /// <reference types="mercurius" />
3
2
  /// <reference types="@fastify/swagger" />
4
3
  import { FastifyInstance, FastifyBaseLogger } from 'fastify'
5
4
  import ConfigManager from '@platformatic/config'
6
- import type { IConfigManagerOptions } from '@platformatic/config'
5
+ import type { ConfigManagerConfig } from '@platformatic/config'
6
+ import type { Stackable as _Stackable, StackableInterface } from '@platformatic/config'
7
7
  import { BaseGenerator } from '@platformatic/generators'
8
8
  import { PlatformaticService } from './config'
9
9
  import type { JSONSchemaType } from 'ajv'
10
10
  import { ServiceGenerator } from './lib/generator/service-generator'
11
11
 
12
- /* eslint-disable @typescript-eslint/no-unused-vars */
13
12
  export import Generator = ServiceGenerator.ServiceGenerator
14
13
 
15
14
  export interface PlatformaticApp<T> {
@@ -28,19 +27,11 @@ declare module 'fastify' {
28
27
  }
29
28
  }
30
29
 
31
- export interface ConfigManagerConfig<T> extends Omit<IConfigManagerOptions, 'source' | 'watch' | 'schema' | 'configVersion'> {
32
- transformConfig: (this: ConfigManager<T>) => Promise<void>
33
- schema: object
34
- }
35
-
36
- export interface Stackable<ConfigType> {
37
- (app: FastifyInstance, opts: object): Promise<void>
38
-
39
- configType: string
40
- configManagerConfig: ConfigManagerConfig<ConfigType>
41
- schema: object
42
- Generator?: new () => BaseGenerator.BaseGenerator
30
+ type DefaultGenerator = new () => BaseGenerator.BaseGenerator
43
31
 
32
+ export interface Stackable<ConfigType, Generator = DefaultGenerator> extends _Stackable<ConfigType> {
33
+ app: (app: FastifyInstance, opts: object) => Promise<void>
34
+ Generator?: Generator
44
35
  version?: string
45
36
  upgrade?: (config: any, version: string) => Promise<any>
46
37
  transformConfig?: (config: any) => Promise<any>
@@ -54,9 +45,21 @@ interface TSCompiler {
54
45
  }
55
46
 
56
47
  export const schema: JSONSchemaType<PlatformaticServiceConfig>
48
+ export const configManagerConfig: ConfigManagerConfig<PlatformaticServiceConfig>
57
49
 
58
50
  export declare const platformaticService: Stackable<PlatformaticServiceConfig>
59
51
 
60
- export default platformaticService
52
+ export declare const app: (app: FastifyInstance, opts: object) => Promise<void>
61
53
 
62
54
  export const tsCompiler: TSCompiler
55
+
56
+ type defaultExport = Stackable<PlatformaticServiceConfig> & {
57
+ buildServer: (opts: object, app?: object, ConfigManagerConstructor?: object) => Promise<FastifyInstance>,
58
+ start: <ConfigType>(app: Stackable<ConfigType>, args: string[]) => Promise<void>,
59
+ tsCompiler: TSCompiler,
60
+ schema: JSONSchemaType<PlatformaticServiceConfig>,
61
+ }
62
+
63
+ export function buildStackable (opts: { config: string }, app?: object): Promise<StackableInterface>
64
+
65
+ export default defaultExport
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { isKeyEnabled } = require('@platformatic/utils')
4
+ const { loadConfig, ConfigManager } = require('@platformatic/config')
4
5
  const { readFile } = require('fs/promises')
5
6
  const { join } = require('path')
6
7
 
@@ -20,6 +21,7 @@ const { schema } = require('./lib/schema')
20
21
  const { addLoggerToTheConfig } = require('./lib/utils')
21
22
  const { start, buildServer } = require('./lib/start')
22
23
  const ServiceGenerator = require('./lib/generator/service-generator.js')
24
+ const { ServiceStackable } = require('./lib/stackable')
23
25
 
24
26
  const { version } = require('./package.json')
25
27
 
@@ -39,7 +41,7 @@ async function platformaticService (app, opts) {
39
41
  }
40
42
 
41
43
  // This must be done before loading the plugins, so they can inspect if the
42
- // openTelemetry decoretor exists and then configure accordingly.
44
+ // openTelemetry decorator exists and then configure accordingly.
43
45
  if (isKeyEnabled('telemetry', config)) {
44
46
  await app.register(telemetry, config.telemetry)
45
47
  }
@@ -93,8 +95,8 @@ async function platformaticService (app, opts) {
93
95
  }
94
96
 
95
97
  platformaticService[Symbol.for('skip-override')] = true
96
- platformaticService.configType = 'service'
97
- platformaticService.configManagerConfig = {
98
+
99
+ module.exports.configManagerConfig = {
98
100
  version,
99
101
  schema,
100
102
  allowToWatch: ['.env'],
@@ -102,7 +104,7 @@ platformaticService.configManagerConfig = {
102
104
  useDefaults: true,
103
105
  coerceTypes: true,
104
106
  allErrors: true,
105
- strict: false
107
+ strict: false,
106
108
  },
107
109
  async transformConfig () {
108
110
  // Set watch to true by default. This is not possible
@@ -132,19 +134,59 @@ platformaticService.configManagerConfig = {
132
134
  this.current.watch.ignore.push(outDir + '/**/*')
133
135
  }
134
136
  },
135
- upgrade
137
+ upgrade,
136
138
  }
137
139
 
140
+ platformaticService.configType = 'service'
141
+ platformaticService.schema = schema
142
+ platformaticService.configManagerConfig = module.exports.configManagerConfig
143
+
138
144
  function _buildServer (options, app) {
139
- return buildServer(options, app || platformaticService)
145
+ return buildServer(options, app || module.exports)
140
146
  }
141
- module.exports = platformaticService
147
+
148
+ async function buildStackable (
149
+ options,
150
+ app = platformaticService,
151
+ Stackable = ServiceStackable
152
+ ) {
153
+ let configManager = options.configManager
154
+
155
+ if (configManager === undefined) {
156
+ if (typeof options.config === 'string') {
157
+ ({ configManager } = await loadConfig({}, ['-c', options.config], app, {
158
+ onMissingEnv: options.onMissingEnv,
159
+ context: options.context,
160
+ }, true))
161
+ } else {
162
+ configManager = new ConfigManager({
163
+ ...app.configManagerConfig,
164
+ source: options.config,
165
+ })
166
+ await configManager.parseAndValidate()
167
+ }
168
+ }
169
+
170
+ const stackable = new Stackable({
171
+ init: () => buildServer(configManager.current, app),
172
+ stackable: app,
173
+ configManager,
174
+ context: options.context,
175
+ })
176
+
177
+ return stackable
178
+ }
179
+
180
+ module.exports.configType = 'service'
181
+ module.exports.app = platformaticService
142
182
  module.exports.schema = schema
143
183
  module.exports.buildServer = _buildServer
184
+ module.exports.buildStackable = buildStackable
144
185
  module.exports.schemas = require('./lib/schema')
145
186
  module.exports.platformaticService = platformaticService
146
187
  module.exports.addLoggerToTheConfig = addLoggerToTheConfig
147
188
  module.exports.start = start
148
189
  module.exports.Generator = ServiceGenerator
190
+ module.exports.ServiceStackable = ServiceStackable
149
191
  module.exports.buildCompileCmd = buildCompileCmd
150
192
  module.exports.extractTypeScriptCompileOptionsFromConfig = extractTypeScriptCompileOptionsFromConfig
package/index.test-d.ts CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  platformaticService,
13
13
  Stackable,
14
14
  Generator,
15
- PlatformaticServiceConfig
15
+ PlatformaticServiceConfig,
16
16
  } from '.'
17
17
 
18
18
  declare module 'fastify' {
@@ -32,21 +32,23 @@ expectType<MercuriusPlugin>(server.graphql)
32
32
  expectType<Promise<void>>(server.restart())
33
33
 
34
34
  function buildStackable (): Stackable<PlatformaticServiceConfig> {
35
- async function myApp (app: FastifyInstance, opts: object): Promise<void> {
36
- await platformaticService(app, opts)
37
- }
35
+ const myApp: Stackable<PlatformaticServiceConfig> = {
36
+ async app (app: FastifyInstance, opts: object): Promise<void> {
37
+ await platformaticService.app(app, opts)
38
+ },
38
39
 
39
- myApp.schema = platformaticService.schema
40
- myApp.configType = 'myApp'
41
- myApp.configManagerConfig = {
42
- version: platformaticService.configManagerConfig.version,
43
- ...platformaticService.configManagerConfig,
44
- async transformConfig (this: ConfigManager<PlatformaticServiceConfig>) {
45
- this.current.plugins = {
46
- paths: [{
47
- path: 'my-plugin'
48
- }]
49
- }
40
+ schema: platformaticService.schema,
41
+ configType: 'myApp',
42
+ configManagerConfig: {
43
+ version: platformaticService.configManagerConfig.version,
44
+ ...platformaticService.configManagerConfig,
45
+ async transformConfig (this: ConfigManager<PlatformaticServiceConfig>) {
46
+ this.current.plugins = {
47
+ paths: [{
48
+ path: 'my-plugin',
49
+ }],
50
+ }
51
+ },
50
52
  },
51
53
  async upgrade (config: PlatformaticServiceConfig, version: string) {
52
54
  const upgrade = platformaticService.configManagerConfig.upgrade
@@ -54,7 +56,7 @@ function buildStackable (): Stackable<PlatformaticServiceConfig> {
54
56
  return upgrade.call(this, config, version)
55
57
  }
56
58
  return config
57
- }
59
+ },
58
60
  }
59
61
 
60
62
  // configVersion is not part of ConfigManagerConfig
@@ -77,21 +79,22 @@ expectType<MyGenerator>(myGenerator)
77
79
  expectType<BaseGenerator.BaseGeneratorConfig>(myGenerator.config)
78
80
 
79
81
  function buildStackable2 (): Stackable<PlatformaticServiceConfig> {
80
- async function myApp (app: FastifyInstance, opts: object): Promise<void> {
81
- await platformaticService(app, opts)
82
- }
83
-
84
- myApp.schema = platformaticService.schema
85
- myApp.configType = 'myApp'
86
- myApp.configManagerConfig = {
87
- ...platformaticService.configManagerConfig,
88
- async transformConfig (this: ConfigManager<PlatformaticServiceConfig>) {
89
- this.current.plugins = {
90
- paths: [{
91
- path: 'my-plugin'
92
- }]
93
- }
94
- }
82
+ const myApp: Stackable<PlatformaticServiceConfig> = {
83
+ async app (app: FastifyInstance, opts: object): Promise<void> {
84
+ await platformaticService.app(app, opts)
85
+ },
86
+ schema: platformaticService.schema,
87
+ configType: 'myApp',
88
+ configManagerConfig: {
89
+ ...platformaticService.configManagerConfig,
90
+ async transformConfig (this: ConfigManager<PlatformaticServiceConfig>) {
91
+ this.current.plugins = {
92
+ paths: [{
93
+ path: 'my-plugin',
94
+ }],
95
+ }
96
+ },
97
+ },
95
98
  }
96
99
 
97
100
  await start(myApp, ['--help'])
package/lib/compile.js CHANGED
@@ -13,7 +13,7 @@ function buildCompileCmd (app) {
13
13
 
14
14
  try {
15
15
  const { configManager } = await loadConfig({}, _args, app, {
16
- watch: false
16
+ watch: false,
17
17
  })
18
18
  await configManager.parseAndValidate()
19
19
  config = configManager.current
@@ -27,7 +27,7 @@ function buildCompileCmd (app) {
27
27
  const logger = pino(
28
28
  pretty({
29
29
  translateTime: 'SYS:HH:MM:ss',
30
- ignore: 'hostname,pid'
30
+ ignore: 'hostname,pid',
31
31
  })
32
32
  )
33
33
 
@@ -35,7 +35,7 @@ function buildCompileCmd (app) {
35
35
  ...extractTypeScriptCompileOptionsFromConfig(config),
36
36
  cwd: fullPath,
37
37
  logger,
38
- clean: _args.includes('--clean')
38
+ clean: _args.includes('--clean'),
39
39
  }
40
40
 
41
41
  if (!await compile(compileOptions)) {
@@ -49,7 +49,7 @@ module.exports.buildCompileCmd = buildCompileCmd
49
49
  function extractTypeScriptCompileOptionsFromConfig (config) {
50
50
  return {
51
51
  tsConfig: config.plugins?.typescript?.tsConfig,
52
- flags: config.plugins?.typescript?.flags
52
+ flags: config.plugins?.typescript?.flags,
53
53
  }
54
54
  }
55
55
 
package/lib/create.mjs CHANGED
@@ -16,14 +16,14 @@ function printAppSummary (args, logger) {
16
16
  { config: 'Language', value: args.typescript ? 'Typescript' : 'Javascript' },
17
17
  { config: 'Init Git Repository', value: args.git },
18
18
  { config: 'Install Dependencies', value: args.install },
19
- { config: 'Sample Plugin and Tests', value: args.plugin }
19
+ { config: 'Sample Plugin and Tests', value: args.plugin },
20
20
  ]
21
21
 
22
22
  const p = new Table({
23
23
  columns: [
24
24
  { name: 'config', alignment: 'right' },
25
- { name: 'value', alignment: 'left' }
26
- ]
25
+ { name: 'value', alignment: 'left' },
26
+ ],
27
27
  })
28
28
 
29
29
  p.addRows(table)
@@ -34,7 +34,7 @@ async function createService (_args) {
34
34
  translateTime: 'SYS:HH:MM:ss',
35
35
  ignore: 'hostname,pid',
36
36
  minimumLevel: 'debug',
37
- sync: true
37
+ sync: true,
38
38
  })
39
39
 
40
40
  const logger = pino(stream)
@@ -49,8 +49,8 @@ async function createService (_args) {
49
49
  plugin: true,
50
50
  typescript: false,
51
51
  git: false,
52
- install: true
53
- }
52
+ install: true,
53
+ },
54
54
 
55
55
  })
56
56
 
@@ -64,7 +64,7 @@ async function createService (_args) {
64
64
  tests: args.plugin,
65
65
  typescript: args.typescript,
66
66
  initGitRepository: args.git,
67
- targetDirectory: args.dir
67
+ targetDirectory: args.dir,
68
68
  })
69
69
 
70
70
  try {
package/lib/gen-schema.js CHANGED
@@ -11,5 +11,5 @@ async function generateJsonSchemaConfig () {
11
11
 
12
12
  module.exports = {
13
13
  generateJsonSchemaConfig,
14
- filenameConfigJsonSchema
14
+ filenameConfigJsonSchema,
15
15
  }
package/lib/gen-types.mjs CHANGED
@@ -4,7 +4,7 @@ import { writeFile } from 'fs/promises'
4
4
  import pino from 'pino'
5
5
  import pretty from 'pino-pretty'
6
6
  import { loadConfig } from '@platformatic/config'
7
- import { platformaticService } from '../index.js'
7
+ import platformaticService from '../index.js'
8
8
  import { checkForDependencies } from '@platformatic/utils'
9
9
 
10
10
  const GLOBAL_TYPES_TEMPLATE = `
@@ -25,7 +25,7 @@ async function execute ({ logger, configManager }) {
25
25
  async function generateTypes (_args) {
26
26
  const logger = pino(pretty({
27
27
  translateTime: 'SYS:HH:MM:ss',
28
- ignore: 'hostname,pid'
28
+ ignore: 'hostname,pid',
29
29
  }))
30
30
  const { configManager, args } = await loadConfig({}, _args, platformaticService)
31
31
  await configManager.parseAndValidate()
@@ -8,7 +8,7 @@ class ServiceGenerator extends BaseGenerator {
8
8
  constructor (opts = {}) {
9
9
  super({
10
10
  ...opts,
11
- module: '@platformatic/service'
11
+ module: '@platformatic/service',
12
12
  })
13
13
  }
14
14
 
@@ -26,16 +26,16 @@ class ServiceGenerator extends BaseGenerator {
26
26
  this.addEnvVars({
27
27
  PLT_SERVER_HOSTNAME: this.config.hostname,
28
28
  PLT_SERVER_LOGGER_LEVEL: 'info',
29
- PORT: 3042
29
+ PORT: 3042,
30
30
  }, { overwrite: false })
31
31
  }
32
32
 
33
33
  this.addEnvVars({
34
- PLT_TYPESCRIPT: this.config.typescript
34
+ PLT_TYPESCRIPT: this.config.typescript,
35
35
  }, { overwrite: false, default: true })
36
36
 
37
37
  this.config.dependencies = {
38
- '@platformatic/service': `^${this.platformaticVersion}`
38
+ '@platformatic/service': `^${this.platformaticVersion}`,
39
39
  }
40
40
  }
41
41
  }
@@ -50,22 +50,22 @@ class ServiceGenerator extends BaseGenerator {
50
50
  label: 'What is the hostname?',
51
51
  default: '0.0.0.0',
52
52
  type: 'string',
53
- configValue: 'hostname'
53
+ configValue: 'hostname',
54
54
  },
55
55
  {
56
56
  var: 'PLT_SERVER_LOGGER_LEVEL',
57
57
  label: 'What is the logger level?',
58
58
  default: 'info',
59
59
  type: 'string',
60
- configValue: ''
60
+ configValue: '',
61
61
  },
62
62
  {
63
63
  label: 'Which port do you want to use?',
64
64
  var: 'PORT',
65
65
  default: 3042,
66
66
  type: 'number',
67
- configValue: 'port'
68
- }
67
+ configValue: 'port',
68
+ },
69
69
  ]
70
70
  }
71
71
 
@@ -91,19 +91,19 @@ declare module 'fastify' {
91
91
  const { isRuntimeContext } = this.config
92
92
  const version = this.platformaticVersion
93
93
  const config = {
94
- $schema: `https://platformatic.dev/schemas/v${version}/service`,
94
+ $schema: `https://schemas.platformatic.dev/@platformatic/service/${version}.json`,
95
95
  service: {
96
- openapi: true
96
+ openapi: true,
97
97
  },
98
- watch: true
98
+ watch: true,
99
99
  }
100
100
  if (this.config.plugin) {
101
101
  config.plugins = {
102
102
  paths: [
103
103
  { path: './plugins', encapsulate: false },
104
- './routes'
104
+ './routes',
105
105
  ],
106
- typescript: `{${this.getEnvVarName('PLT_TYPESCRIPT')}}`
106
+ typescript: `{${this.getEnvVarName('PLT_TYPESCRIPT')}}`,
107
107
  }
108
108
  }
109
109
 
@@ -112,8 +112,8 @@ declare module 'fastify' {
112
112
  hostname: '{PLT_SERVER_HOSTNAME}',
113
113
  port: '{PORT}',
114
114
  logger: {
115
- level: '{PLT_SERVER_LOGGER_LEVEL}'
116
- }
115
+ level: '{PLT_SERVER_LOGGER_LEVEL}',
116
+ },
117
117
  }
118
118
  }
119
119